You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2005/03/14 09:37:29 UTC

cvs commit: jakarta-turbine-jcs/src/java/org/apache/jcs/utils/struct SortedPreferentialArray.java

asmuts      2005/03/14 00:37:29

  Modified:    src/java/org/apache/jcs/utils/struct
                        SortedPreferentialArray.java
  Added:       src/test/org/apache/jcs/utils/struct
                        TestSortedPrefArray.java
               src/test/org/apache/jcs TestRemovalSimple.java
                        TestRemovalConcurrent.java RemovalTestUtil.java
  Log:
  Added some simple unit tests.  Concurrent and simple removal tests.  Sorted Preferential Array tests.
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/src/test/org/apache/jcs/utils/struct/TestSortedPrefArray.java
  
  Index: TestSortedPrefArray.java
  ===================================================================
  package org.apache.jcs.utils.struct;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import junit.framework.TestCase;
  
  /**
   * Tests the SortedPrefArray used by the recycle bin.
   *
   * @author aaronsm
   *
   */
  public class TestSortedPrefArray
    extends TestCase
  {
  
    /**
     * Constructor for the TestSimpleLoad object
     *
     * @param testName
     *            Description of the Parameter
     */
    public TestSortedPrefArray( String testName )
    {
      super( testName );
    }
  
    /**
     * Description of the Method
     *
     * @param args
     *            Description of the Parameter
     */
    public static void main( String args[] )
    {
      String[] testCaseName = {
        TestSortedPrefArray.class.getName()};
      junit.textui.TestRunner.main( testCaseName );
    }
  
    /**
     *
     *
     * @exception Exception
     */
    public void testLargePref() throws Exception
    {
  
      int maxSize = 25;
  
      SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
      //array.setPreferLarge( false );
      array.setPreferLarge( true );
      String[] elem = {
        "10", "11", "01", "02", "03", "04", "05", "08", "07",
        "06", "09", "12", "13", "15", "14", "20", "25", "29", "28", "16", "17",
        "96", "00", "72", "39", "55", "44", "26", "22", "59", "38", "16", "27"};
  
      // put more than the max in a random order
      for ( int i = 0; i < elem.length; i++ ) {
        array.add( elem[i] );
        System.out.println( array.dumpArray() );
      }
  
      assertEquals( "Size was not as expected.", maxSize, array.size() );
  
      // this is a fragile test, since it relies on a hardcoded array
      String smallest = ( String ) array.getSmallest();
      assertEquals( "smallest should be 08", "08", smallest );
  
      String largest = ( String ) array.getLargest();
      assertEquals( "Largest should be 96", "96", largest );
  
      // this should take 96;
      String taken = ( String ) array.takeNearestLargerOrEqual( "95" );
      assertEquals( "Taken should be 96", "96", taken );
      assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
  
      System.out.println( array.dumpArray() );
  
    }
  
    /**
     * Verify that we don't get an error when taking from an empty array.
     *
     * @throws Exception
     */
    public void testEmptyTake() throws Exception
    {
  
      int maxSize = 25;
      SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
      array.setPreferLarge( true );
      for ( int i = 0; i < maxSize; i++ ) {
        String taken = ( String ) array.takeNearestLargerOrEqual( String.valueOf(
          i ) );
        assertNull( "taken should be null, since nothing was in the array", taken );
      }
  
    }
  
    /**
     * Verify that we don't get a null pointer if we insert a null.
     *
     * @throws Exception
     */
    public void testNullInsertion() throws Exception
    {
  
      int maxSize = 25;
      SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
      array.setPreferLarge( true );
  
      String[] elem = {
        "10", "11", "01", "02", "03", "04", "05", "08", "07",
        "06", "09", "12", "13", "15", "14", "20", "25", "29", "28", "16", "17",
        "96", "00", "72", "39", "55", "44", "26", "22", "59", "38", "16", "27"};
  
      // put more than the max in a random order
      for ( int i = 0; i < elem.length; i++ ) {
        array.add( elem[i] );
      }
      System.out.println( array.dumpArray() );
  
      assertEquals( "Size was not as expected.", maxSize, array.size() );
  
      try {
        // should not get an error
        array.add( null );
      }
      catch ( NullPointerException e ) {
        fail( "Got a null pointer inserting a null" );
      }
  
    }
  
    /**
     * Verify that we don't get an npe when taking with a null
     *
     * @throws Exception
     */
    public void testNullTake() throws Exception
    {
  
      int maxSize = 25;
      SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
      array.setPreferLarge( true );
      try {
        String taken = ( String ) array.takeNearestLargerOrEqual( null );
        assertNull( "taken should be null, since nothing was in the array", taken );
      }
      catch ( NullPointerException e ) {
        fail( "Got a null pointer trying to take with a null" );
      }
  
    }
  
  }
  
  
  
  1.1                  jakarta-turbine-jcs/src/test/org/apache/jcs/TestRemovalSimple.java
  
  Index: TestRemovalSimple.java
  ===================================================================
  package org.apache.jcs;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import junit.framework.TestCase;
  
  /**
   * Verify that basic removal functionality works.
   */
  public class TestRemovalSimple
    extends TestCase
  {
    /**
     * Constructor for the TestDiskCache object.
     * @param testName
     */
    public TestRemovalSimple( String testName )
    {
      super( testName );
    }
  
    /**
     * Test setup
     */
    public void setUp() throws Exception
    {
      JCS.setConfigFilename( "/TestRemoval.ccf" );
      JCS.getInstance( "testCache1" );
    }
  
    /**
     * Main method passes this test to the text test runner.
     * @param args
     */
    public static void main( String args[] )
    {
      String[] testCaseName = {
        TestRemovalSimple.class.getName()};
      junit.textui.TestRunner.main( testCaseName );
    }
  
    /**
     * Verify that 2 level deep hierchical removal works.
     *
     * @throws Exception
     */
    public void testTwoDeepRemoval() throws Exception
    {
  
      System.out.println( "------------------------------------------" );
      System.out.println( "testTwoDeepRemoval" );
  
      int count = 500;
      JCS jcs = JCS.getInstance( "testCache1" );
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.put( "key:" + i + ":anotherpart", "data" + i );
      }
  
      for ( int i = count; i >= 0; i-- ) {
        String res = ( String ) jcs.get( "key:" + i + ":anotherpart" );
        if ( res == null ) {
          assertNotNull( "[key:" + i + ":anotherpart] should not be null, " +
                         jcs.getStats(), res );
        }
      }
      System.out.println( "Confirmed that " + count + " items could be found" );
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.remove( "key:" + i + ":" );
        assertNull( jcs.getStats(), jcs.get( "key:" + i + ":anotherpart" ) );
      }
      System.out.println( "Confirmed that " + count + " items were removed" );
  
      System.out.println( jcs.getStats() );
  
    }
  
    /**
     * Verify that 1 level deep hierchical removal works.
     *
     * @throws Exception
     */
    public void testSingleDepthRemoval() throws Exception
    {
  
      System.out.println( "------------------------------------------" );
      System.out.println( "testSingleDepthRemoval" );
  
      int count = 500;
      JCS jcs = JCS.getInstance( "testCache1" );
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.put( i + ":key", "data" + i );
      }
  
      for ( int i = count; i >= 0; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
      }
      System.out.println( "Confirmed that " + count + " items could be found" );
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.remove( i + ":" );
        assertNull( jcs.get( i + ":key" ) );
      }
      System.out.println( "Confirmed that " + count + " items were removed" );
  
      System.out.println( jcs.getStats() );
  
    }
  
    /**
     * Verify that clear removes everyting as it should.
     *
     * @throws Exception
     */
    public void testClear() throws Exception
    {
  
      System.out.println( "------------------------------------------" );
      System.out.println( "testRemoveAll" );
  
      int count = 500;
      JCS jcs = JCS.getInstance( "testCache1" );
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.put( i + ":key", "data" + i );
      }
  
      for ( int i = count; i >= 0; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
      }
      System.out.println( "Confirmed that " + count + " items could be found" );
  
      System.out.println( jcs.getStats() );
  
      jcs.clear();
  
      for ( int i = count; i >= 0; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res != null ) {
          assertNull( "[" + i + ":key] should be null after remvoeall" +
                      jcs.getStats(), res );
        }
      }
      System.out.println( "Confirmed that all items were removed" );
  
    }
  
    /**
     * Verify that we can clear repeatedly without error.
     *
     * @throws Exception
     */
    public void testClearRepeatedlyWithoutError() throws Exception
    {
  
      System.out.println( "------------------------------------------" );
      System.out.println( "testRemoveAll" );
  
      int count = 500;
      JCS jcs = JCS.getInstance( "testCache1" );
  
      jcs.clear();
  
      for ( int i = 0; i <= count; i++ ) {
        jcs.put( i + ":key", "data" + i );
      }
  
      for ( int i = count; i >= 0; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
      }
      System.out.println( "Confirmed that " + count + " items could be found" );
  
      System.out.println( jcs.getStats() );
  
      for ( int i = count; i >= 0; i-- ) {
        jcs.put( i + ":key", "data" + i );
        jcs.clear();
        String res = ( String ) jcs.get( i + ":key" );
        if ( res != null ) {
          assertNull( "[" + i + ":key] should be null after remvoeall" +
                      jcs.getStats(), res );
        }
      }
      System.out.println( "Confirmed that all items were removed" );
  
    }
  }
  
  
  
  1.1                  jakarta-turbine-jcs/src/test/org/apache/jcs/TestRemovalConcurrent.java
  
  Index: TestRemovalConcurrent.java
  ===================================================================
  package org.apache.jcs;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import junit.extensions.ActiveTestSuite;
  import junit.framework.Test;
  import junit.framework.TestCase;
  
  /**
   * Test which exercises the hierarchical removal when the cache is active.
   * */
  public class TestRemovalConcurrent
    extends TestCase
  {
    /**
     * Constructor for the TestDiskCache object.
     * @param testName
     */
    public TestRemovalConcurrent( String testName )
    {
      super( testName );
    }
  
    /**
     * Main method passes this test to the text test runner.
     * @param args
     */
    public static void main( String args[] )
    {
      String[] testCaseName = {
        RemovalTestUtil.class.getName()};
      junit.textui.TestRunner.main( testCaseName );
    }
  
    /**
     * A unit test suite for JUnit.  This verfies that we can remove hierarchically
     * while the region is active.
     *
     * @return    The test suite
     */
    public static Test suite()
    {
      ActiveTestSuite suite = new ActiveTestSuite();
  
      suite.addTest( new RemovalTestUtil( "testRemoveCache1" )
      {
        public void runTest() throws Exception
        {
          runTestPutThenRemoveCategorical( 0, 200 );
        }
      } );
  
      suite.addTest( new RemovalTestUtil( "testPutCache1" )
      {
        public void runTest() throws Exception
        {
          runPutInRange( 300, 400 );
        }
      } );
  
      suite.addTest( new RemovalTestUtil( "testPutCache2" )
      {
        public void runTest() throws Exception
        {
          runPutInRange( 401, 600 );
        }
      } );
  
      // stomp on previous put
      suite.addTest( new RemovalTestUtil( "testPutCache3" )
      {
        public void runTest() throws Exception
        {
          runPutInRange( 401, 600 );
        }
      } );
  
      suite.addTest( new RemovalTestUtil( "testRemoveCache1" )
      {
        public void runTest() throws Exception
        {
          runTestPutThenRemoveCategorical( 601, 700 );
        }
      } );
  
      suite.addTest( new RemovalTestUtil( "testRemoveCache1" )
      {
        public void runTest() throws Exception
        {
          runTestPutThenRemoveCategorical( 701, 800 );
        }
      } );
  
      suite.addTest( new RemovalTestUtil( "testPutCache2" )
      {
        // verify that there are no errors with concurrent gets.
        public void runTest() throws Exception
        {
          runGetInRange( 0, 1000, false );
        }
      } );
      return suite;
  
    }
  
    /**
     * Test setup
     */
    public void setUp() throws Exception
    {
      JCS.setConfigFilename( "/TestRemoval.ccf" );
      JCS.getInstance( "testCache1" );
    }
  
  }
  
  
  
  1.1                  jakarta-turbine-jcs/src/test/org/apache/jcs/RemovalTestUtil.java
  
  Index: RemovalTestUtil.java
  ===================================================================
  package org.apache.jcs;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import junit.framework.TestCase;
  
  /**
   *  Simple methods to be run by active test suites that test removal.
   *
   */
  public class RemovalTestUtil
    extends TestCase
  {
  
    /**
     *  Constructor for the TestSimpleLoad object
     *
     *@param  testName  Description of the Parameter
     */
    public RemovalTestUtil( String testName )
    {
      super( testName );
    }
  
    /**
     *  A unit test for JUnit
     *
     *@exception  Exception  Description of the Exception
     */
    public void runTestPutThenRemoveCategorical( int start, int end ) throws
      Exception
    {
      JCS jcs = JCS.getInstance( "testCache1" );
  
      for ( int i = start; i <= end; i++ ) {
        jcs.put( i + ":key", "data" + i );
      }
  
      for ( int i = end; i >= start; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
      }
      System.out.println( "Confirmed that " + ( end - start ) +
                          " items could be found" );
  
      for ( int i = start; i <= end; i++ ) {
        jcs.remove( i + ":" );
        assertNull( jcs.get( i + ":key" ) );
      }
      System.out.println( "Confirmed that " + ( end - start ) +
                          " items were removed" );
  
      System.out.println( jcs.getStats() );
  
    }
  
    /**
     * Put items in the cache in this key range.  Can be used to verify that
     * concurrent operations are not effected by things like hierchical removal.
     *
     * @param start int
     * @param end int
     * @throws Exception
     */
    public void runPutInRange( int start, int end ) throws Exception
    {
      JCS jcs = JCS.getInstance( "testCache1" );
  
      for ( int i = start; i <= end; i++ ) {
        jcs.put( i + ":key", "data" + i );
      }
  
      for ( int i = end; i >= start; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
      }
  
    }
  
    /**
     * Just get from start to end.
     *
     * @param start int
     * @param end int
     * @param check boolean -- check to see if the items are in the cache.
     * @throws Exception
     */
    public void runGetInRange( int start, int end, boolean check ) throws
      Exception
    {
      JCS jcs = JCS.getInstance( "testCache1" );
  
      // don't care if they are found
      for ( int i = end; i >= start; i-- ) {
        String res = ( String ) jcs.get( i + ":key" );
        if ( check && res == null ) {
          assertNotNull( "[" + i + ":key] should not be null", res );
        }
  
      }
  
    }
  
  }
  
  
  
  1.4       +32 -30    jakarta-turbine-jcs/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java
  
  Index: SortedPreferentialArray.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SortedPreferentialArray.java	1 Feb 2005 04:53:58 -0000	1.3
  +++ SortedPreferentialArray.java	14 Mar 2005 08:37:29 -0000	1.4
  @@ -64,6 +64,11 @@
      */
     public void add(Comparable obj)
     {
  +    if ( obj == null )
  +    {
  +      return;
  +    }
  +
       if (curSize < maxSize)
       {
         insert(obj);
  @@ -108,6 +113,11 @@
       }
     }
   
  +  /**
  +   * Returns the largest without removing it from the array.
  +   *
  +   * @return Comparable
  +   */
     public Comparable getLargest()
     {
       int num = curSize - 1;
  @@ -118,6 +128,11 @@
       return array[num];
     }
   
  +  /**
  +   * Returns the smallest element without removing it from the array.
  +   *
  +   * @return Comparable
  +   */
     public Comparable getSmallest()
     {
       return array[0];
  @@ -258,10 +273,15 @@
      * Returns and removes the nearer larger or equal object from the aray.
      *
      * @param obj Comparable
  -   * @return Comparable
  +   * @return Comparable, null if arg is null or none was found.
      */
     public Comparable takeNearestLargerOrEqual(Comparable obj)
     {
  +    if ( obj == null )
  +    {
  +      return null;
  +    }
  +
       Comparable retVal = null;
       try
       {
  @@ -294,6 +314,16 @@
     }
   
     /**
  +   * Returns the current size of the array.
  +   *
  +   * @return int
  +   */
  +  public int size()
  +  {
  +    return this.curSize;
  +  }
  +
  +  /**
      * This determines the position in the array that is occupied by an object
      * that is larger or equal to the argument.  If none exists, -1 is
      * returned.
  @@ -551,7 +581,7 @@
     /**
      * Debugging method to return a human readable display of array data.
      */
  -  private String dumpArray()
  +  protected String dumpArray()
     {
       StringBuffer buf = new StringBuffer();
       buf.append("\n ---------------------------");
  @@ -566,32 +596,4 @@
       return buf.toString();
     }
   
  -  /**
  -   * Simple testing program.
  -   *
  -   * @param args String[]
  -   */
  -  public static void main(String args[])
  -  {
  -
  -    SortedPreferentialArray array = new SortedPreferentialArray(25);
  -    //array.setPreferLarge( false );
  -    array.setPreferLarge( true );
  -    String[] elem =
  -        {
  -        "10", "11", "01", "02", "03", "04", "05", "08", "07",
  -        "06", "09", "12", "13", "15", "14", "20", "25", "29", "28", "16", "17",
  -        "96", "00", "72", "39", "55", "44", "26", "22", "59", "38", "16", "27"};
  -
  -    for (int i = 0; i < elem.length; i++)
  -    {
  -      array.add(elem[i]);
  -      System.out.println(array.dumpArray());
  -    }
  -
  -    //String t = "01";
  -    //System.out.print(t.compareTo("10"));
  -
  -  }
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-dev-help@jakarta.apache.org