View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  //
    19  // Log4j uses the JUnit framework for internal unit testing. JUnit
    20  // available from
    21  //
    22  //     http://www.junit.org
    23
    24
    25  package org.apache.log4j.helpers;
    26
    27  import org.apache.log4j.spi.LoggingEvent;
    28  import org.apache.log4j.Logger;
    29  import org.apache.log4j.Level;
    30  import org.apache.log4j.helpers.CyclicBuffer;
    31
    32  import junit.framework.TestCase;
    33  import junit.framework.TestSuite;
    34  import junit.framework.TestFailure;
    35  import junit.framework.Test;
    36
    37  /***
    38     Unit test the {@link CyclicBuffer}.
    39
    40     @author Ceki G&uuml;lc&uuml;
    41
    42  */
    43  public class CyclicBufferTestCase extends TestCase {
    44
    45    static Logger cat = Logger.getLogger("x");
    46
    47    static int MAX = 1000;
    48
    49    static LoggingEvent[] e = new LoggingEvent[MAX];
    50
    51    {
    52      for (int i = 0; i < MAX; i++) {
    53        e[i] =  new LoggingEvent("", cat, Level.DEBUG, "e"+i, null);
    54      }
    55    }
    56
    57
    58    public CyclicBufferTestCase(String name) {
    59      super(name);
    60    }
    61
    62
    63    public
    64    void setUp() {
    65
    66    }
    67
    68
    69    public
    70    void test0() {
    71      int size = 2;
    72
    73      CyclicBuffer cb = new CyclicBuffer(size);
    74      assertEquals(cb.getMaxSize(), size);
    75
    76      cb.add(e[0]);
    77      assertEquals(cb.length(), 1);
    78      assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 0);
    79      assertNull(cb.get()); assertEquals(cb.length(), 0);
    80
    81
    82      cb = new CyclicBuffer(size);
    83      cb.add(e[0]);
    84      cb.add(e[1]);
    85      assertEquals(cb.length(), 2);
    86      assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 1);
    87      assertEquals(cb.get(), e[1]); assertEquals(cb.length(), 0);
    88      assertNull(cb.get()); assertEquals(cb.length(), 0);
    89
    90
    91    }
    92
    93    /***
    94       Test a buffer of size 1,2,4,8,..,128
    95     */
    96    public
    97    void test1() {
    98      for(int bufSize = 1; bufSize <= 128; bufSize *=2)
    99        doTest1(bufSize);
    100   }
    101
    102   void doTest1(int size) {
    103     //System.out.println("Doing test with size = "+size);
    104     CyclicBuffer cb = new CyclicBuffer(size);
    105
    106     assertEquals(cb.getMaxSize(), size);
    107
    108     for(int i = -(size+10); i < (size+10); i++) {
    109       assertNull(cb.get(i));
    110     }
    111
    112     for(int i = 0; i < MAX; i++) {
    113       cb.add(e[i]);
    114       int limit = i < size-1 ? i : size-1;
    115
    116       //System.out.println("\nLimit is " + limit + ", i="+i);
    117
    118       for(int j = limit; j >= 0; j--) {
    119 	//System.out.println("i= "+i+", j="+j);
    120 	assertEquals(cb.get(j), e[i-(limit-j)]);
    121       }
    122       assertNull(cb.get(-1));
    123       assertNull(cb.get(limit+1));
    124     }
    125   }
    126
    127   public
    128   void testResize() {
    129     for(int isize = 1; isize <= 128; isize *=2) {
    130       doTestResize(isize, isize/2+1, isize/2+1);
    131       doTestResize(isize, isize/2+1, isize+10);
    132       doTestResize(isize, isize+10, isize/2+1);
    133       doTestResize(isize, isize+10, isize+10);
    134     }
    135   }
    136
    137   void doTestResize(int initialSize, int numberOfAdds, int newSize) {
    138     //System.out.println("initialSize = "+initialSize+", numberOfAdds="
    139     //	       +numberOfAdds+", newSize="+newSize);
    140     CyclicBuffer cb = new CyclicBuffer(initialSize);
    141     for(int i = 0; i < numberOfAdds; i++) {
    142       cb.add(e[i]);
    143     }
    144     cb.resize(newSize);
    145
    146     int offset = numberOfAdds - initialSize;
    147     if(offset< 0)
    148       offset = 0;
    149
    150     int len = newSize < numberOfAdds ? newSize : numberOfAdds;
    151     len = len < initialSize ? len : initialSize;
    152     //System.out.println("Len = "+len+", offset="+offset);
    153     for(int j = 0; j < len; j++) {
    154       assertEquals(cb.get(j), e[offset+j]);
    155     }
    156
    157   }
    158
    159
    160   public
    161   static
    162   Test suite() {
    163     TestSuite suite = new TestSuite();
    164     suite.addTest(new CyclicBufferTestCase("test0"));
    165     suite.addTest(new CyclicBufferTestCase("test1"));
    166     suite.addTest(new CyclicBufferTestCase("testResize"));
    167     return suite;
    168   }
    169 }