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
    31  import org.apache.log4j.helpers.BoundedFIFO;
    32
    33  import junit.framework.TestCase;
    34  import junit.framework.TestSuite;
    35  import junit.framework.TestFailure;
    36  import junit.framework.Test;
    37
    38
    39
    40
    41  /***
    42     Unit test the {@link BoundedFIFO}.
    43     @author Ceki G&uuml;lc&uuml;
    44     @since 0.9.1 */
    45  public class BoundedFIFOTestCase extends TestCase {
    46    static Logger cat = Logger.getLogger("x");
    47
    48    static int MAX = 1000;
    49
    50    static LoggingEvent[] e = new LoggingEvent[MAX];
    51
    52    {
    53      for (int i = 0; i < MAX; i++) {
    54        e[i] =  new LoggingEvent("", cat, Level.DEBUG, "e"+i, null);
    55      }
    56    }
    57
    58
    59    public BoundedFIFOTestCase(String name) {
    60      super(name);
    61    }
    62
    63
    64    public
    65    void setUp() {
    66
    67    }
    68
    69
    70    /***
    71       Pattern: +++++..-----..
    72     */
    73    public
    74    void test1() {
    75      for(int size = 1; size <= 128; size *=2) {
    76        BoundedFIFO bf = new BoundedFIFO(size);
    77
    78        assertEquals(bf.getMaxSize(), size);
    79        assertNull(bf.get());
    80
    81        int i;
    82        int j;
    83        int k;
    84
    85        for(i = 1; i < 2*size; i++) {
    86  	for(j = 0; j < i; j++) {
    87  	  //System.out.println("Putting "+e[j]);
    88  	  bf.put(e[j]); assertEquals(bf.length(), j < size ?  j+1 : size);
    89  	}
    90  	int max = size < j ? size : j;
    91  	j--;
    92  	for(k = 0; k <= j; k++) {
    93  	  //System.out.println("max="+max+", j="+j+", k="+k);
    94  	  assertEquals(bf.length(), max - k > 0 ? max - k : 0);
    95  	  Object r = bf.get();
    96  	  //System.out.println("Got "+r);
    97  	  if(k >= size)
    98  	    assertNull(r);
    99  	  else
    100 	    assertEquals(r, e[k]);
    101 	}
    102       }
    103       //System.out.println("Passed size="+size);
    104     }
    105   }
    106
    107
    108   /***
    109      Pattern: ++++--++--++
    110    */
    111   public
    112   void test2() {
    113     int size = 3;
    114     BoundedFIFO bf = new BoundedFIFO(size);
    115
    116     bf.put(e[0]);
    117     assertEquals(bf.get(), e[0]);
    118     assertNull(bf.get());
    119
    120     bf.put(e[1]); assertEquals(bf.length(), 1);
    121     bf.put(e[2]); assertEquals(bf.length(), 2);
    122     bf.put(e[3]); assertEquals(bf.length(), 3);
    123     assertEquals(bf.get(), e[1]); assertEquals(bf.length(), 2);
    124     assertEquals(bf.get(), e[2]); assertEquals(bf.length(), 1);
    125     assertEquals(bf.get(), e[3]); assertEquals(bf.length(), 0);
    126     assertNull(bf.get()); assertEquals(bf.length(), 0);
    127   }
    128
    129   int min(int a, int b) {
    130     return a < b ? a : b;
    131   }
    132
    133
    134   /***
    135      Pattern ++++++++++++++++++++ (insert only);
    136    */
    137   public
    138   void testResize1() {
    139     int size = 10;
    140
    141     for(int n = 1; n < size*2; n++) {
    142       for(int i = 0; i < size*2; i++) {
    143
    144         BoundedFIFO bf = new BoundedFIFO(size);
    145         for(int f = 0; f < i; f++) {
    146           bf.put(e[f]);
    147         }
    148
    149         bf.resize(n);
    150         int expectedSize = min(n, min(i, size));
    151         assertEquals(bf.length(), expectedSize);
    152         for(int c = 0; c < expectedSize; c++) {
    153           assertEquals(bf.get(), e[c]);
    154         }
    155       }
    156     }
    157   }
    158
    159
    160
    161   /***
    162      Pattern ++...+ --...-
    163    */
    164   public
    165   void testResize2() {
    166     int size = 10;
    167
    168     for(int n = 1; n < size*2; n++) {
    169       for(int i = 0; i < size*2; i++) {
    170 	for(int d = 0; d < min(i,size); d++) {
    171
    172 	  BoundedFIFO bf = new BoundedFIFO(size);
    173 	  for(int p = 0; p < i; p++) {
    174 	    bf.put(e[p]);
    175 	  }
    176
    177 	  for(int g = 0; g < d; g++) {
    178 	    bf.get();
    179 	  }
    180
    181 	  // x = the number of elems in
    182 	  int x = bf.length();
    183
    184 	  bf.resize(n);
    185
    186 	  int expectedSize = min(n, x);
    187 	  assertEquals(bf.length(), expectedSize);
    188
    189 	  for(int c = 0; c < expectedSize; c++) {
    190 	    assertEquals(bf.get(), e[c+d]);
    191 	  }
    192 	  assertNull(bf.get());
    193 	}
    194       }
    195     }
    196   }
    197
    198
    199   /***
    200      Pattern: i inserts, d deletes, r inserts
    201    */
    202   public
    203   void testResize3() {
    204     int size = 10;
    205
    206     for(int n = 1; n < size*2; n++) {
    207       for(int i = 0; i < size; i++) {
    208 	for(int d = 0; d < i; d++) {
    209 	  for(int r = 0; r < d; r++) {
    210
    211 	    BoundedFIFO bf = new BoundedFIFO(size);
    212 	    for(int p0 = 0; p0 < i; p0++)
    213 	      bf.put(e[p0]);
    214
    215 	    for(int g = 0; g < d; g++)
    216 	      bf.get();
    217 	    for(int p1 = 0; p1 < r; p1++)
    218 	      bf.put(e[i+p1]);
    219
    220
    221
    222 	    int x =  bf.length();
    223
    224 	    bf.resize(n);
    225
    226
    227 	    int expectedSize = min(n, x);
    228 	    assertEquals(bf.length(), expectedSize);
    229
    230 	    for(int c = 0; c < expectedSize; c++) {
    231 	      assertEquals(bf.get(), e[c+d]);
    232 	    }
    233 	    //assertNull(bf.get());
    234 	  }
    235 	}
    236       }
    237     }
    238   }
    239
    240
    241   public
    242   static
    243   Test suite() {
    244     TestSuite suite = new TestSuite();
    245     suite.addTest(new BoundedFIFOTestCase("test1"));
    246     suite.addTest(new BoundedFIFOTestCase("test2"));
    247     suite.addTest(new BoundedFIFOTestCase("testResize1"));
    248     suite.addTest(new BoundedFIFOTestCase("testResize2"));
    249     suite.addTest(new BoundedFIFOTestCase("testResize3"));
    250     return suite;
    251   }
    252 }