Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/AuxiliaryCacheMockImpl.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/AuxiliaryCacheMockImpl.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/AuxiliaryCacheMockImpl.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/AuxiliaryCacheMockImpl.java Thu May 10 09:03:42 2007
@@ -1,160 +1,179 @@
-package org.apache.jcs.auxiliary;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Set;
-
-import org.apache.jcs.engine.CacheConstants;
-import org.apache.jcs.engine.behavior.ICache;
-import org.apache.jcs.engine.behavior.ICacheElement;
-import org.apache.jcs.engine.stats.behavior.IStats;
-
-/**
- * Mock auxiliary for unit tests.
- * <p>
- * @author Aaron Smuts
- */
-public class AuxiliaryCacheMockImpl
- implements AuxiliaryCache
-{
- private static final long serialVersionUID = 1L;
-
- /** Can setup the cache type */
- public int cacheType = ICache.DISK_CACHE;
-
- /** Can setup status */
- public int status = CacheConstants.STATUS_ALIVE;
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#update(org.apache.jcs.engine.behavior.ICacheElement)
- */
- public void update( ICacheElement ce )
- throws IOException
- {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#get(java.io.Serializable)
- */
- public ICacheElement get( Serializable key )
- throws IOException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#remove(java.io.Serializable)
- */
- public boolean remove( Serializable key )
- throws IOException
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#removeAll()
- */
- public void removeAll()
- throws IOException
- {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#dispose()
- */
- public void dispose()
- throws IOException
- {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#getSize()
- */
- public int getSize()
- {
- // TODO Auto-generated method stub
- return 0;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatus()
- */
- public int getStatus()
- {
- // TODO Auto-generated method stub
- return status;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#getCacheName()
- */
- public String getCacheName()
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#getGroupKeys(java.lang.String)
- */
- public Set getGroupKeys( String group )
- throws IOException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
- */
- public IStats getStatistics()
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.engine.behavior.ICache#getStats()
- */
- public String getStats()
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.jcs.engine.behavior.ICacheType#getCacheType()
- */
- public int getCacheType()
- {
- return cacheType;
- }
-
- /**
- * @return Returns the AuxiliaryCacheAttributes.
- */
- public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
- {
- return null;
- }
-}
+package org.apache.jcs.auxiliary;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 java.io.IOException;
+import java.io.Serializable;
+import java.util.Set;
+
+import org.apache.jcs.engine.CacheConstants;
+import org.apache.jcs.engine.behavior.ICache;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.stats.behavior.IStats;
+
+/**
+ * Mock auxiliary for unit tests.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class AuxiliaryCacheMockImpl
+ implements AuxiliaryCache
+{
+ private static final long serialVersionUID = 1L;
+
+ /** Can setup the cache type */
+ public int cacheType = ICache.DISK_CACHE;
+
+ /** Can setup status */
+ public int status = CacheConstants.STATUS_ALIVE;
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#update(org.apache.jcs.engine.behavior.ICacheElement)
+ */
+ public void update( ICacheElement ce )
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#get(java.io.Serializable)
+ */
+ public ICacheElement get( Serializable key )
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#remove(java.io.Serializable)
+ */
+ public boolean remove( Serializable key )
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#removeAll()
+ */
+ public void removeAll()
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#dispose()
+ */
+ public void dispose()
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getSize()
+ */
+ public int getSize()
+ {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatus()
+ */
+ public int getStatus()
+ {
+ // TODO Auto-generated method stub
+ return status;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getCacheName()
+ */
+ public String getCacheName()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getGroupKeys(java.lang.String)
+ */
+ public Set getGroupKeys( String group )
+ throws IOException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
+ */
+ public IStats getStatistics()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.engine.behavior.ICache#getStats()
+ */
+ public String getStats()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.jcs.engine.behavior.ICacheType#getCacheType()
+ */
+ public int getCacheType()
+ {
+ return cacheType;
+ }
+
+ /**
+ * @return Returns the AuxiliaryCacheAttributes.
+ */
+ public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
+ {
+ return null;
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/DiskTestObject.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/DiskTestObject.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/DiskTestObject.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/DiskTestObject.java Thu May 10 09:03:42 2007
@@ -1,41 +1,51 @@
-package org.apache.jcs.auxiliary.disk;
-
-/*
- * 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 java.io.Serializable;
-
-/**
- * Resembles a cached image.
- */
-public class DiskTestObject
- implements Serializable
-{
- private static final long serialVersionUID = 1L;
-
- /**
- * Key
- */
- public Integer id;
-
- /**
- * Byte size
- */
- public byte[] imageBytes;
-
- /**
- * @param id
- * @param imageBytes
- */
- public DiskTestObject( Integer id, byte[] imageBytes )
- {
- this.id = id;
- this.imageBytes = imageBytes;
- }
-}
\ No newline at end of file
+package org.apache.jcs.auxiliary.disk;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 java.io.Serializable;
+
+/**
+ * Resembles a cached image.
+ */
+public class DiskTestObject
+ implements Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Key
+ */
+ public Integer id;
+
+ /**
+ * Byte size
+ */
+ public byte[] imageBytes;
+
+ /**
+ * @param id
+ * @param imageBytes
+ */
+ public DiskTestObject( Integer id, byte[] imageBytes )
+ {
+ this.id = id;
+ this.imageBytes = imageBytes;
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java Thu May 10 09:03:42 2007
@@ -1,213 +1,216 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-/*
- * 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;
-
-import org.apache.jcs.JCS;
-
-/**
- * Test which exercises the block disk cache. This one uses three different
- * regions for thre threads.
- */
-public class BlockDiskCacheConcurrentUnitTest
- extends TestCase
-{
- /**
- * Number of items to cache, twice the configured maxObjects for the memory
- * cache regions.
- */
- private static int items = 200;
-
- /**
- * Constructor for the TestDiskCache object.
- *
- * @param testName
- * @throws Exception
- */
- public BlockDiskCacheConcurrentUnitTest( String testName ) throws Exception
- {
- super( testName );
- }
-
- /**
- * Main method passes this test to the text test runner.
- *
- * @param args
- */
- public static void main( String args[] )
- {
- String[] testCaseName = { BlockDiskCacheConcurrentUnitTest.class.getName() };
- junit.textui.TestRunner.main( testCaseName );
- }
-
- /**
- * A unit test suite for JUnit
- *
- * @return The test suite
- * @throws Exception
- */
- public static Test suite() throws Exception
- {
- ActiveTestSuite suite = new ActiveTestSuite();
-
- JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
- JCS.getInstance( "indexedRegion1" ).clear();
- JCS.getInstance( "indexedRegion2" ).clear();
- JCS.getInstance( "indexedRegion3" ).clear();
-
- suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache1" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "indexedRegion1" );
- }
- } );
-
- suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache2" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "indexedRegion2" );
- }
- } );
-
- suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache3" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "indexedRegion3" );
- }
- } );
-
- suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache4" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
- }
- } );
-
- return suite;
- }
-
- /**
- * Test setup
- */
- public void setUp()
- {
- JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
- }
-
- /**
- * Adds items to cache, gets them, and removes them. The item count is more
- * than the size of the memory cache, so items should spool to disk.
- *
- * @param region
- * Name of the region to access
- *
- * @exception Exception
- * If an error occurs
- */
- public void runTestForRegion( String region )
- throws Exception
- {
- JCS jcs = JCS.getInstance( region );
-
- // Add items to cache
- for ( int i = 0; i <= items; i++ )
- {
- jcs.put( i + ":key", region + " data " + i );
- }
-
- // Test that all items are in cache
- for ( int i = 0; i <= items; i++ )
- {
- String value = (String) jcs.get( i + ":key" );
-
- assertEquals( region + " data " + i, value );
- }
-
- // Remove all the items
- for ( int i = 0; i <= items; i++ )
- {
- jcs.remove( i + ":key" );
- }
-
- // Verify removal
- // another thread may have inserted since
- for ( int i = 0; i <= items; i++ )
- {
- assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
- .get( i + ":key" ) );
- }
- }
-
- /**
- * Adds items to cache, gets them, and removes them. The item count is more
- * than the size of the memory cache, so items should spool to disk.
- *
- * @param region
- * Name of the region to access
- * @param start
- * @param end
- *
- * @exception Exception
- * If an error occurs
- */
- public void runTestForRegionInRange( String region, int start, int end )
- throws Exception
- {
- JCS jcs = JCS.getInstance( region );
-
- // Add items to cache
- for ( int i = start; i <= end; i++ )
- {
- jcs.put( i + ":key", region + " data " + i );
- }
-
- // Test that all items are in cache
- for ( int i = start; i <= end; i++ )
- {
- String value = (String) jcs.get( i + ":key" );
-
- assertEquals( region + " data " + i, value );
- }
-
- // Remove all the items
- for ( int i = start; i <= end; i++ )
- {
- jcs.remove( i + ":key" );
- }
-
- System.out.println( jcs.getStats() );
-
- // Verify removal
- // another thread may have inserted since
- for ( int i = start; i <= end; i++ )
- {
- assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
- + ":key" ) );
- }
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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;
+
+import org.apache.jcs.JCS;
+
+/**
+ * Test which exercises the block disk cache. This one uses three different
+ * regions for thre threads.
+ */
+public class BlockDiskCacheConcurrentUnitTest
+ extends TestCase
+{
+ /**
+ * Number of items to cache, twice the configured maxObjects for the memory
+ * cache regions.
+ */
+ private static int items = 200;
+
+ /**
+ * Constructor for the TestDiskCache object.
+ *
+ * @param testName
+ * @throws Exception
+ */
+ public BlockDiskCacheConcurrentUnitTest( String testName ) throws Exception
+ {
+ super( testName );
+ }
+
+ /**
+ * Main method passes this test to the text test runner.
+ *
+ * @param args
+ */
+ public static void main( String args[] )
+ {
+ String[] testCaseName = { BlockDiskCacheConcurrentUnitTest.class.getName() };
+ junit.textui.TestRunner.main( testCaseName );
+ }
+
+ /**
+ * A unit test suite for JUnit
+ *
+ * @return The test suite
+ * @throws Exception
+ */
+ public static Test suite() throws Exception
+ {
+ ActiveTestSuite suite = new ActiveTestSuite();
+
+ JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
+ JCS.getInstance( "indexedRegion1" ).clear();
+ JCS.getInstance( "indexedRegion2" ).clear();
+ JCS.getInstance( "indexedRegion3" ).clear();
+
+ suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache1" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "indexedRegion1" );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache2" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "indexedRegion2" );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache3" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "indexedRegion3" );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache4" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
+ }
+ } );
+
+ return suite;
+ }
+
+ /**
+ * Test setup
+ */
+ public void setUp()
+ {
+ JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
+ }
+
+ /**
+ * Adds items to cache, gets them, and removes them. The item count is more
+ * than the size of the memory cache, so items should spool to disk.
+ *
+ * @param region
+ * Name of the region to access
+ *
+ * @exception Exception
+ * If an error occurs
+ */
+ public void runTestForRegion( String region )
+ throws Exception
+ {
+ JCS jcs = JCS.getInstance( region );
+
+ // Add items to cache
+ for ( int i = 0; i <= items; i++ )
+ {
+ jcs.put( i + ":key", region + " data " + i );
+ }
+
+ // Test that all items are in cache
+ for ( int i = 0; i <= items; i++ )
+ {
+ String value = (String) jcs.get( i + ":key" );
+
+ assertEquals( region + " data " + i, value );
+ }
+
+ // Remove all the items
+ for ( int i = 0; i <= items; i++ )
+ {
+ jcs.remove( i + ":key" );
+ }
+
+ // Verify removal
+ // another thread may have inserted since
+ for ( int i = 0; i <= items; i++ )
+ {
+ assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
+ .get( i + ":key" ) );
+ }
+ }
+
+ /**
+ * Adds items to cache, gets them, and removes them. The item count is more
+ * than the size of the memory cache, so items should spool to disk.
+ *
+ * @param region
+ * Name of the region to access
+ * @param start
+ * @param end
+ *
+ * @exception Exception
+ * If an error occurs
+ */
+ public void runTestForRegionInRange( String region, int start, int end )
+ throws Exception
+ {
+ JCS jcs = JCS.getInstance( region );
+
+ // Add items to cache
+ for ( int i = start; i <= end; i++ )
+ {
+ jcs.put( i + ":key", region + " data " + i );
+ }
+
+ // Test that all items are in cache
+ for ( int i = start; i <= end; i++ )
+ {
+ String value = (String) jcs.get( i + ":key" );
+
+ assertEquals( region + " data " + i, value );
+ }
+
+ // Remove all the items
+ for ( int i = start; i <= end; i++ )
+ {
+ jcs.remove( i + ":key" );
+ }
+
+ System.out.println( jcs.getStats() );
+
+ // Verify removal
+ // another thread may have inserted since
+ for ( int i = start; i <= end; i++ )
+ {
+ assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
+ + ":key" ) );
+ }
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheKeyStoreUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheKeyStoreUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheKeyStoreUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheKeyStoreUnitTest.java Thu May 10 09:03:42 2007
@@ -1,121 +1,131 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-/*
- * 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 for the keyStore.
- * <p>
- * @author Aaron Smuts
- */
-public class BlockDiskCacheKeyStoreUnitTest
- extends TestCase
-{
- /** Directory name */
- private String rootDirName = "target/test-sandbox/block";
-
- /**
- * Put a bunch of keys inthe key store and verify that they are present.
- * <p>
- * @throws Exception
- */
- public void testPutKeys()
- throws Exception
- {
- // SETUP
- String regionName = "testPutKeys";
- int maxKeys = 1000;
- int bytesPerBlock = 2000;
-
- BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
- attributes.setCacheName( regionName );
- attributes.setDiskPath( rootDirName );
- attributes.setMaxKeySize( maxKeys );
- attributes.setBlockSizeBytes( bytesPerBlock );
-
- BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
-
- BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
-
- // DO WORK
- int numElements = 100;
- for ( int i = 0; i < numElements; i++ )
- {
- keyStore.put( String.valueOf( i ), new int[i] );
- }
- System.out.println( "testPutKeys " + keyStore );
-
- // VERIFY
- assertEquals( "Wrong number of keys", numElements, keyStore.size() );
- for ( int i = 0; i < numElements; i++ )
- {
- int[] result = keyStore.get( String.valueOf( i ) );
- assertEquals( "Wrong array returned.", i, result.length );
- }
- }
-
- /**
- * Verify that we can load keys that we saved. Add a bunch. Save them. Clear the memory keyhash.
- * Load the keys. Verify.
- * <p>
- * @throws Exception
- */
- public void testSaveLoadKeys()
- throws Exception
- {
- // SETUP
- String regionName = "testSaveLoadKeys";
- int maxKeys = 10000;
- int bytesPerBlock = 2000;
-
- BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
- attributes.setCacheName( regionName );
- attributes.setDiskPath( rootDirName );
- attributes.setMaxKeySize( maxKeys );
- attributes.setBlockSizeBytes( bytesPerBlock );
-
- BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
-
- BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
-
- // DO WORK
- int numElements = 1000;
- //Random random = new Random( 89 );
- for ( int i = 0; i < numElements; i++ )
- {
- int blocks = i;//random.nextInt( 10 );
- keyStore.put( String.valueOf( i ), new int[blocks] );
- keyStore.put( String.valueOf( i ), new int[i] );
- }
- System.out.println( "testSaveLoadKeys " + keyStore );
-
- // VERIFY
- assertEquals( "Wrong number of keys", numElements, keyStore.size() );
-
- // DO WORK
- keyStore.saveKeys();
- keyStore.clearMemoryMap();
-
- // VERIFY
- assertEquals( "Wrong number of keys after clearing memory", 0, keyStore.size() );
-
- // DO WORK
- keyStore.loadKeys();
-
- // VERIFY
- assertEquals( "Wrong number of keys after loading", numElements, keyStore.size() );
- for ( int i = 0; i < numElements; i++ )
- {
- int[] result = keyStore.get( String.valueOf( i ) );
- assertEquals( "Wrong array returned.", i, result.length );
- }
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 for the keyStore.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class BlockDiskCacheKeyStoreUnitTest
+ extends TestCase
+{
+ /** Directory name */
+ private String rootDirName = "target/test-sandbox/block";
+
+ /**
+ * Put a bunch of keys inthe key store and verify that they are present.
+ * <p>
+ * @throws Exception
+ */
+ public void testPutKeys()
+ throws Exception
+ {
+ // SETUP
+ String regionName = "testPutKeys";
+ int maxKeys = 1000;
+ int bytesPerBlock = 2000;
+
+ BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
+ attributes.setCacheName( regionName );
+ attributes.setDiskPath( rootDirName );
+ attributes.setMaxKeySize( maxKeys );
+ attributes.setBlockSizeBytes( bytesPerBlock );
+
+ BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
+
+ BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
+
+ // DO WORK
+ int numElements = 100;
+ for ( int i = 0; i < numElements; i++ )
+ {
+ keyStore.put( String.valueOf( i ), new int[i] );
+ }
+ System.out.println( "testPutKeys " + keyStore );
+
+ // VERIFY
+ assertEquals( "Wrong number of keys", numElements, keyStore.size() );
+ for ( int i = 0; i < numElements; i++ )
+ {
+ int[] result = keyStore.get( String.valueOf( i ) );
+ assertEquals( "Wrong array returned.", i, result.length );
+ }
+ }
+
+ /**
+ * Verify that we can load keys that we saved. Add a bunch. Save them. Clear the memory keyhash.
+ * Load the keys. Verify.
+ * <p>
+ * @throws Exception
+ */
+ public void testSaveLoadKeys()
+ throws Exception
+ {
+ // SETUP
+ String regionName = "testSaveLoadKeys";
+ int maxKeys = 10000;
+ int bytesPerBlock = 2000;
+
+ BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
+ attributes.setCacheName( regionName );
+ attributes.setDiskPath( rootDirName );
+ attributes.setMaxKeySize( maxKeys );
+ attributes.setBlockSizeBytes( bytesPerBlock );
+
+ BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
+
+ BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
+
+ // DO WORK
+ int numElements = 1000;
+ //Random random = new Random( 89 );
+ for ( int i = 0; i < numElements; i++ )
+ {
+ int blocks = i;//random.nextInt( 10 );
+ keyStore.put( String.valueOf( i ), new int[blocks] );
+ keyStore.put( String.valueOf( i ), new int[i] );
+ }
+ System.out.println( "testSaveLoadKeys " + keyStore );
+
+ // VERIFY
+ assertEquals( "Wrong number of keys", numElements, keyStore.size() );
+
+ // DO WORK
+ keyStore.saveKeys();
+ keyStore.clearMemoryMap();
+
+ // VERIFY
+ assertEquals( "Wrong number of keys after clearing memory", 0, keyStore.size() );
+
+ // DO WORK
+ keyStore.loadKeys();
+
+ // VERIFY
+ assertEquals( "Wrong number of keys after loading", numElements, keyStore.size() );
+ for ( int i = 0; i < numElements; i++ )
+ {
+ int[] result = keyStore.get( String.valueOf( i ) );
+ assertEquals( "Wrong array returned.", i, result.length );
+ }
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java Thu May 10 09:03:42 2007
@@ -1,63 +1,82 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-import junit.framework.TestCase;
-
-import org.apache.jcs.JCS;
-import org.apache.jcs.access.TestCacheAccess;
-
-/**
- * This is used by other tests to generate a random load on the disk cache.
- */
-public class BlockDiskCacheRandomConcurrentTestUtil
- extends TestCase
-{
- /**
- * Constructor for the TestDiskCache object.
- *
- * @param testName
- */
- public BlockDiskCacheRandomConcurrentTestUtil( String testName )
- {
- super( testName );
- }
-
- /**
- * Randomly adds items to cache, gets them, and removes them. The range
- * count is more than the size of the memory cache, so items should spool to
- * disk.
- * <p>
- * @param region
- * Name of the region to access
- * @param range
- * @param numOps
- * @param testNum
- *
- * @exception Exception
- * If an error occurs
- */
- public void runTestForRegion( String region, int range, int numOps, int testNum )
- throws Exception
- {
- // run a rondom operation test to detect deadlocks
- TestCacheAccess tca = new TestCacheAccess( "/TestBlockDiskCacheCon.ccf" );
- tca.setRegion( region );
- tca.random( range, numOps );
-
- // make sure a simple put then get works
- // this may fail if the other tests are flooding the disk cache
- JCS jcs = JCS.getInstance( region );
- String key = "testKey" + testNum;
- String data = "testData" + testNum;
- jcs.put( key, data );
- String value = (String) jcs.get( key );
- assertEquals( data, value );
- }
-
- /**
- * Test setup
- */
- public void setUp()
- {
- JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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;
+
+import org.apache.jcs.JCS;
+import org.apache.jcs.access.TestCacheAccess;
+
+/**
+ * This is used by other tests to generate a random load on the disk cache.
+ */
+public class BlockDiskCacheRandomConcurrentTestUtil
+ extends TestCase
+{
+ /**
+ * Constructor for the TestDiskCache object.
+ *
+ * @param testName
+ */
+ public BlockDiskCacheRandomConcurrentTestUtil( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * Randomly adds items to cache, gets them, and removes them. The range
+ * count is more than the size of the memory cache, so items should spool to
+ * disk.
+ * <p>
+ * @param region
+ * Name of the region to access
+ * @param range
+ * @param numOps
+ * @param testNum
+ *
+ * @exception Exception
+ * If an error occurs
+ */
+ public void runTestForRegion( String region, int range, int numOps, int testNum )
+ throws Exception
+ {
+ // run a rondom operation test to detect deadlocks
+ TestCacheAccess tca = new TestCacheAccess( "/TestBlockDiskCacheCon.ccf" );
+ tca.setRegion( region );
+ tca.random( range, numOps );
+
+ // make sure a simple put then get works
+ // this may fail if the other tests are flooding the disk cache
+ JCS jcs = JCS.getInstance( region );
+ String key = "testKey" + testNum;
+ String data = "testData" + testNum;
+ jcs.put( key, data );
+ String value = (String) jcs.get( key );
+ assertEquals( data, value );
+ }
+
+ /**
+ * Test setup
+ */
+ public void setUp()
+ {
+ JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java Thu May 10 09:03:42 2007
@@ -1,140 +1,150 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-/*
- * 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;
-
-import org.apache.jcs.JCS;
-
-/**
- * Test which exercises the block disk cache. Runs three threads against the same region.
- */
-public class BlockDiskCacheSameRegionConcurrentUnitTest
- extends TestCase
-{
- /**
- * Constructor for the TestDiskCache object.
- * @param testName
- */
- public BlockDiskCacheSameRegionConcurrentUnitTest( String testName )
- {
- super( testName );
- }
-
- /**
- * Main method passes this test to the text test runner.
- * @param args
- */
- public static void main( String args[] )
- {
- String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() };
- junit.textui.TestRunner.main( testCaseName );
- }
-
- /**
- * A unit test suite for JUnit
- * @return The test suite
- */
- public static Test suite()
- {
- ActiveTestSuite suite = new ActiveTestSuite();
-
- suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "blockRegion4", 0, 200 );
- }
- } );
-
- suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "blockRegion4", 1000, 1200 );
- }
- } );
-
- suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "blockRegion4", 2000, 2200 );
- }
- } );
-
- suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "blockRegion4", 2200, 5200 );
- }
- } );
-
- suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache5" )
- {
- public void runTest()
- throws Exception
- {
- this.runTestForRegion( "blockRegion4", 0, 5200 );
- }
- } );
-
- return suite;
- }
-
- /**
- * Test setup. Sets the config name and clears the region.
- * <p>
- * @throws Exception
- */
- public void setUp() throws Exception
- {
- JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
- JCS.getInstance( "blockRegion4" ).clear();
- }
-
- /**
- * Adds items to cache, gets them, and removes them. The item count is more than the size of the
- * memory cache, so items should spool to disk.
- * @param region Name of the region to access
- * @param start
- * @param end
- * @exception Exception If an error occurs
- */
- public void runTestForRegion( String region, int start, int end )
- throws Exception
- {
- JCS jcs = JCS.getInstance( region );
-
- // Add items to cache
-
- for ( int i = start; i <= end; i++ )
- {
- jcs.put( i + ":key", region + " data " + i + "-" + region );
- }
-
- // Test that all items are in cache
-
- for ( int i = start; i <= end; i++ )
- {
- String key = i + ":key";
- String value = (String) jcs.get( key );
-
- assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
- }
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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;
+
+import org.apache.jcs.JCS;
+
+/**
+ * Test which exercises the block disk cache. Runs three threads against the same region.
+ */
+public class BlockDiskCacheSameRegionConcurrentUnitTest
+ extends TestCase
+{
+ /**
+ * Constructor for the TestDiskCache object.
+ * @param testName
+ */
+ public BlockDiskCacheSameRegionConcurrentUnitTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * Main method passes this test to the text test runner.
+ * @param args
+ */
+ public static void main( String args[] )
+ {
+ String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() };
+ junit.textui.TestRunner.main( testCaseName );
+ }
+
+ /**
+ * A unit test suite for JUnit
+ * @return The test suite
+ */
+ public static Test suite()
+ {
+ ActiveTestSuite suite = new ActiveTestSuite();
+
+ suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "blockRegion4", 0, 200 );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "blockRegion4", 1000, 1200 );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "blockRegion4", 2000, 2200 );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "blockRegion4", 2200, 5200 );
+ }
+ } );
+
+ suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache5" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runTestForRegion( "blockRegion4", 0, 5200 );
+ }
+ } );
+
+ return suite;
+ }
+
+ /**
+ * Test setup. Sets the config name and clears the region.
+ * <p>
+ * @throws Exception
+ */
+ public void setUp() throws Exception
+ {
+ JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
+ JCS.getInstance( "blockRegion4" ).clear();
+ }
+
+ /**
+ * Adds items to cache, gets them, and removes them. The item count is more than the size of the
+ * memory cache, so items should spool to disk.
+ * @param region Name of the region to access
+ * @param start
+ * @param end
+ * @exception Exception If an error occurs
+ */
+ public void runTestForRegion( String region, int start, int end )
+ throws Exception
+ {
+ JCS jcs = JCS.getInstance( region );
+
+ // Add items to cache
+
+ for ( int i = start; i <= end; i++ )
+ {
+ jcs.put( i + ":key", region + " data " + i + "-" + region );
+ }
+
+ // Test that all items are in cache
+
+ for ( int i = start; i <= end; i++ )
+ {
+ String key = i + ":key";
+ String value = (String) jcs.get( key );
+
+ assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
+ }
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java Thu May 10 09:03:42 2007
@@ -1,141 +1,160 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-import java.text.DecimalFormat;
-import java.util.Random;
-
-import junit.framework.TestCase;
-
-import org.apache.jcs.JCS;
-import org.apache.jcs.auxiliary.disk.DiskTestObject;
-import org.apache.jcs.utils.timing.ElapsedTimer;
-
-/**
- * This allows you to put thousands of large objects into the disk cache and to force removes to
- * trigger optimizations along the way.
- * <p>
- * @author Aaron Smuts
- */
-public class BlockDiskCacheSteadyLoadTest
- extends TestCase
-{
- private static final String LOG_DIVIDER = "---------------------------";
-
- private static Runtime rt = Runtime.getRuntime();
-
- private static DecimalFormat format = new DecimalFormat( "#,###" );
-
- /**
- * Insert 2000 wait 1 second, repeat. Average 1000 / sec.
- * <p>
- * @throws Exception
- */
- public void testRunSteadyLoadTest()
- throws Exception
- {
- JCS.setConfigFilename( "/TestBlockDiskCacheSteadyLoad.ccf" );
-
- System.out.println( "runSteadyLoadTest" );
-
- logMemoryUsage();
-
- int numPerRun = 250;
- long pauseBetweenRuns = 1000;
- int runCount = 0;
- int runs = 1000;
- int upperKB = 50;
-
- JCS jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" );
-
- ElapsedTimer timer = new ElapsedTimer();
- int numToGet = numPerRun * ( runs / 10 );
- for ( int i = 0; i < numToGet; i++ )
- {
- jcs.get( String.valueOf( i ) );
- }
- System.out.println( LOG_DIVIDER );
- System.out.println( "After getting " + numToGet );
- System.out.println( "Elapsed " + timer.getElapsedTimeString() );
- logMemoryUsage();
-
- jcs.clear();
- Thread.sleep( 3000 );
- System.out.println( LOG_DIVIDER );
- System.out.println( "Start putting" );
-
- long totalSize = 0;
- int totalPut = 0;
-
- Random random = new Random( 89 );
- while ( runCount < runs )
- {
- runCount++;
- for ( int i = 0; i < numPerRun; i++ )
- {
- // 1/2 upper to upperKB-4 KB
- int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) );
- int bytes = ( kiloBytes ) * 1024;
- totalSize += bytes;
- totalPut++;
- DiskTestObject object = new DiskTestObject( new Integer( i ), new byte[bytes] );
- jcs.put( String.valueOf( totalPut ), object );
- }
-
- // get half of those inserted the previous run
- if ( runCount > 1 )
- {
- for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
- {
- jcs.get( String.valueOf( j ) );
- }
- }
-
- // remove half of those inserted the previous run
- if ( runCount > 1 )
- {
- for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
- {
- jcs.remove( String.valueOf( j ) );
- }
- }
-
-
- Thread.sleep( pauseBetweenRuns );
- if ( runCount % 1 == 0 )
- {
- System.out.println( LOG_DIVIDER );
- System.out.println( "Elapsed " + timer.getElapsedTimeString() );
- System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
- + jcs.getStats() );
- logMemoryUsage();
- }
- }
-
- Thread.sleep( 3000 );
- System.out.println( jcs.getStats() );
- logMemoryUsage();
-
- Thread.sleep( 10000 );
- System.out.println( jcs.getStats() );
- logMemoryUsage();
-
- System.gc();
- Thread.sleep( 3000 );
- System.gc();
- System.out.println( jcs.getStats() );
- logMemoryUsage();
- }
-
- /**
- * Logs the memory usage.
- */
- private static void logMemoryUsage()
- {
- long byte2MB = 1024 * 1024;
- long total = rt.totalMemory() / byte2MB;
- long free = rt.freeMemory() / byte2MB;
- long used = total - free;
- System.out.println( LOG_DIVIDER );
- System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
- + "MB" + " Total:" + format.format( total ) + "MB" );
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 java.text.DecimalFormat;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.jcs.JCS;
+import org.apache.jcs.auxiliary.disk.DiskTestObject;
+import org.apache.jcs.utils.timing.ElapsedTimer;
+
+/**
+ * This allows you to put thousands of large objects into the disk cache and to force removes to
+ * trigger optimizations along the way.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class BlockDiskCacheSteadyLoadTest
+ extends TestCase
+{
+ private static final String LOG_DIVIDER = "---------------------------";
+
+ private static Runtime rt = Runtime.getRuntime();
+
+ private static DecimalFormat format = new DecimalFormat( "#,###" );
+
+ /**
+ * Insert 2000 wait 1 second, repeat. Average 1000 / sec.
+ * <p>
+ * @throws Exception
+ */
+ public void testRunSteadyLoadTest()
+ throws Exception
+ {
+ JCS.setConfigFilename( "/TestBlockDiskCacheSteadyLoad.ccf" );
+
+ System.out.println( "runSteadyLoadTest" );
+
+ logMemoryUsage();
+
+ int numPerRun = 250;
+ long pauseBetweenRuns = 1000;
+ int runCount = 0;
+ int runs = 1000;
+ int upperKB = 50;
+
+ JCS jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" );
+
+ ElapsedTimer timer = new ElapsedTimer();
+ int numToGet = numPerRun * ( runs / 10 );
+ for ( int i = 0; i < numToGet; i++ )
+ {
+ jcs.get( String.valueOf( i ) );
+ }
+ System.out.println( LOG_DIVIDER );
+ System.out.println( "After getting " + numToGet );
+ System.out.println( "Elapsed " + timer.getElapsedTimeString() );
+ logMemoryUsage();
+
+ jcs.clear();
+ Thread.sleep( 3000 );
+ System.out.println( LOG_DIVIDER );
+ System.out.println( "Start putting" );
+
+ long totalSize = 0;
+ int totalPut = 0;
+
+ Random random = new Random( 89 );
+ while ( runCount < runs )
+ {
+ runCount++;
+ for ( int i = 0; i < numPerRun; i++ )
+ {
+ // 1/2 upper to upperKB-4 KB
+ int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) );
+ int bytes = ( kiloBytes ) * 1024;
+ totalSize += bytes;
+ totalPut++;
+ DiskTestObject object = new DiskTestObject( new Integer( i ), new byte[bytes] );
+ jcs.put( String.valueOf( totalPut ), object );
+ }
+
+ // get half of those inserted the previous run
+ if ( runCount > 1 )
+ {
+ for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
+ {
+ jcs.get( String.valueOf( j ) );
+ }
+ }
+
+ // remove half of those inserted the previous run
+ if ( runCount > 1 )
+ {
+ for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
+ {
+ jcs.remove( String.valueOf( j ) );
+ }
+ }
+
+
+ Thread.sleep( pauseBetweenRuns );
+ if ( runCount % 1 == 0 )
+ {
+ System.out.println( LOG_DIVIDER );
+ System.out.println( "Elapsed " + timer.getElapsedTimeString() );
+ System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
+ + jcs.getStats() );
+ logMemoryUsage();
+ }
+ }
+
+ Thread.sleep( 3000 );
+ System.out.println( jcs.getStats() );
+ logMemoryUsage();
+
+ Thread.sleep( 10000 );
+ System.out.println( jcs.getStats() );
+ logMemoryUsage();
+
+ System.gc();
+ Thread.sleep( 3000 );
+ System.gc();
+ System.out.println( jcs.getStats() );
+ logMemoryUsage();
+ }
+
+ /**
+ * Logs the memory usage.
+ */
+ private static void logMemoryUsage()
+ {
+ long byte2MB = 1024 * 1024;
+ long total = rt.totalMemory() / byte2MB;
+ long free = rt.freeMemory() / byte2MB;
+ long used = total - free;
+ System.out.println( LOG_DIVIDER );
+ System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
+ + "MB" + " Total:" + format.format( total ) + "MB" );
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java Thu May 10 09:03:42 2007
@@ -1,79 +1,89 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-/*
- * 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 tests for the element descriptor
- * <p>
- * @author Aaron Smuts
- */
-public class BlockDiskElementDescriptorUnitTest
- extends TestCase
-{
-
- /**
- * Verify that the memory used per element is reasonable.
- * <p>
- * TODO figure out a more precise expectation.
- * <p>
- * @throws Exception
- */
- public void testMemorySize()
- throws Exception
- {
- // SETUP
- long memoryBefore = measureMemoryUse();
- System.out.println( "Before: " + memoryBefore );
-
- int numElements = 25000;
- BlockDiskElementDescriptor[] elements = new BlockDiskElementDescriptor[numElements];
-
- long memoryStart = measureMemoryUse();
- System.out.println( "Start: " + memoryStart );
-
- // DO WORK
- for ( int i = 0; i < numElements; i++ )
- {
- BlockDiskElementDescriptor descriptor = new BlockDiskElementDescriptor();
- descriptor.setKey( new Integer( i ) );
- descriptor.setBlocks( new int[] { 1, 2 } );
- elements[i] = descriptor;
- }
-
- // VERIFY
- long memoryEnd = measureMemoryUse();
- System.out.println( "End: " + memoryEnd );
-
- long diff = memoryEnd - memoryStart;
- System.out.println( "diff: " + diff );
-
- long perDiff = diff / numElements;
- System.out.println( "per diff: " + perDiff );
-
- // about 20 bytes each
- assertTrue( "Too much was used.", perDiff < 75 );
- }
-
- /**
- * Measure memory used by the VM.
- * @return
- * @throws InterruptedException
- */
- protected long measureMemoryUse()
- throws InterruptedException
- {
- System.gc();
- Thread.sleep( 3000 );
- System.gc();
- return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 tests for the element descriptor
+ * <p>
+ * @author Aaron Smuts
+ */
+public class BlockDiskElementDescriptorUnitTest
+ extends TestCase
+{
+
+ /**
+ * Verify that the memory used per element is reasonable.
+ * <p>
+ * TODO figure out a more precise expectation.
+ * <p>
+ * @throws Exception
+ */
+ public void testMemorySize()
+ throws Exception
+ {
+ // SETUP
+ long memoryBefore = measureMemoryUse();
+ System.out.println( "Before: " + memoryBefore );
+
+ int numElements = 25000;
+ BlockDiskElementDescriptor[] elements = new BlockDiskElementDescriptor[numElements];
+
+ long memoryStart = measureMemoryUse();
+ System.out.println( "Start: " + memoryStart );
+
+ // DO WORK
+ for ( int i = 0; i < numElements; i++ )
+ {
+ BlockDiskElementDescriptor descriptor = new BlockDiskElementDescriptor();
+ descriptor.setKey( new Integer( i ) );
+ descriptor.setBlocks( new int[] { 1, 2 } );
+ elements[i] = descriptor;
+ }
+
+ // VERIFY
+ long memoryEnd = measureMemoryUse();
+ System.out.println( "End: " + memoryEnd );
+
+ long diff = memoryEnd - memoryStart;
+ System.out.println( "diff: " + diff );
+
+ long perDiff = diff / numElements;
+ System.out.println( "per diff: " + perDiff );
+
+ // about 20 bytes each
+ assertTrue( "Too much was used.", perDiff < 75 );
+ }
+
+ /**
+ * Measure memory used by the VM.
+ * @return
+ * @throws InterruptedException
+ */
+ protected long measureMemoryUse()
+ throws InterruptedException
+ {
+ System.gc();
+ Thread.sleep( 3000 );
+ System.gc();
+ return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java Thu May 10 09:03:42 2007
@@ -1,235 +1,245 @@
-package org.apache.jcs.auxiliary.disk.block;
-
-/*
- * 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 java.io.File;
-
-import junit.framework.TestCase;
-
-/**
- * Test for the disk acces layer of the Block Disk Cache.
- * <p>
- * @author Aaron Smuts
- */
-public class BlockDiskUnitTest
- extends TestCase
-{
- private File rafDir;
-
- /**
- * Creates the base directory
- */
- public BlockDiskUnitTest()
- {
- String rootDirName = "target/test-sandbox/block";
- this.rafDir = new File( rootDirName );
- this.rafDir.mkdirs();
- }
-
- /**
- * Test writing an element within a single block size.
- * <p>
- * @throws Exception
- */
- public void testWriteSingleBlockElement()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteSingleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- int bytes = 1 * 1024;
- int[] blocks = disk.write( new byte[bytes] );
-
- // VERIFY
- System.out.println( "testWriteSingleBlockElement " + disk );
- assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() );
- assertEquals( "Wrong number of blocks returned.", 1, blocks.length );
- assertEquals( "Wrong block returned.", 0, blocks[0] );
- }
-
- /**
- * Test writing and reading an element within a single block size.
- * <p>
- * @throws Exception
- */
- public void testWriteAndReadSingleBlockElement()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteAndReadSingleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- int bytes = 1 * 1024;
- int[] blocks = disk.write( new byte[bytes] );
-
- byte[] result = (byte[]) disk.read( blocks );
-
- // VERIFY
- assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
- }
-
- /**
- * Test writing two elements that each fit within a single block size.
- * <p>
- * @throws Exception
- */
- public void testWriteTwoSingleBlockElements()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteSingleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- int bytes = 1 * 1024;
- int[] blocks1 = disk.write( new byte[bytes] );
- int[] blocks2 = disk.write( new byte[bytes] );
-
- // VERIFY
- assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
- assertEquals( "Wrong number of blocks returned.", 1, blocks1.length );
- assertEquals( "Wrong block returned.", 0, blocks1[0] );
- assertEquals( "Wrong number of blocks returned.", 1, blocks2.length );
- assertEquals( "Wrong block returned.", 1, blocks2[0] );
- }
-
- /**
- * Verify that it says we need two blocks if the total size will fit.
- * <p>
- * @throws Exception
- */
- public void testCalculateBlocksNeededDouble()
- throws Exception
- {
- // SETUP
- String fileName = "testCalculateBlocksNeededDouble";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2
- - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] );
-
- // Verify
- assertEquals( "Wrong number of blocks", 2, result );
- }
-
- /**
- * Test writing an element that takes two blocks.
- * <p>
- * @throws Exception
- */
- public void testWriteDoubleBlockElement()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteDoubleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- // byte arrays encur 27 bytes of serialization overhead.
- int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 );
- int[] blocks = disk.write( new byte[bytes] );
-
- // VERIFY
- System.out.println( "testWriteDoubleBlockElement " + disk );
- assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
- assertEquals( "Wrong number of blocks returned.", 2, blocks.length );
- assertEquals( "Wrong block returned.", 0, blocks[0] );
- }
-
- /**
- * Test writing and reading elements that do not fit within a single block.
- * <p>
- * @throws Exception
- */
- public void testWriteAndReadMultipleMultiBlockElement()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteAndReadSingleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- BlockDisk disk = new BlockDisk( file );
-
- // DO WORK
- int numBlocksPerElement = 4;
- int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
-
- int numElements = 100;
- for ( int i = 0; i < numElements; i++ )
- {
- int[] blocks = disk.write( new byte[bytes] );
- byte[] result = (byte[]) disk.read( blocks );
-
- // VERIFY
- assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
- assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
- }
- System.out.println( "testWriteAndReadMultipleMultiBlockElement " + disk );
- }
-
- /**
- * Test writing and reading elements that do not fit within a single block.
- * <p>
- * @throws Exception
- */
- public void testWriteAndReadMultipleMultiBlockElement_setSize()
- throws Exception
- {
- // SETUP
- String fileName = "testWriteAndReadSingleBlockElement";
- File file = new File( rafDir, fileName + ".data" );
- file.delete();
- int blockSizeBytes = 1024;
- BlockDisk disk = new BlockDisk( file, blockSizeBytes );
-
- // DO WORK
- int numBlocksPerElement = 4;
- int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
-
- int numElements = 100;
- for ( int i = 0; i < numElements; i++ )
- {
- int[] blocks = disk.write( new byte[bytes] );
- byte[] result = (byte[]) disk.read( blocks );
-
- // VERIFY
- assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
- assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
- }
- System.out.println( "testWriteAndReadMultipleMultiBlockElement_setSize " + disk );
- assertEquals( "Wrong number of elements.", numBlocksPerElement * numElements, disk.getNumberOfBlocks() );
- }
-
- /**
- * Used to get the size for byte arrays that will take up the number of blocks specified.
- * <p>
- * @param blockSize
- * @param numBlocks
- * @return num bytes.
- */
- private int getBytesForBlocksOfByteArrays( int blockSize, int numBlocks )
- {
- // byte arrays encur some bytes of serialization overhead.
- return blockSize * numBlocks - ( numBlocks * BlockDisk.HEADER_SIZE_BYTES ) - ( numBlocks * 14 );
- }
-}
+package org.apache.jcs.auxiliary.disk.block;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 java.io.File;
+
+import junit.framework.TestCase;
+
+/**
+ * Test for the disk acces layer of the Block Disk Cache.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class BlockDiskUnitTest
+ extends TestCase
+{
+ private File rafDir;
+
+ /**
+ * Creates the base directory
+ */
+ public BlockDiskUnitTest()
+ {
+ String rootDirName = "target/test-sandbox/block";
+ this.rafDir = new File( rootDirName );
+ this.rafDir.mkdirs();
+ }
+
+ /**
+ * Test writing an element within a single block size.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteSingleBlockElement()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteSingleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ int bytes = 1 * 1024;
+ int[] blocks = disk.write( new byte[bytes] );
+
+ // VERIFY
+ System.out.println( "testWriteSingleBlockElement " + disk );
+ assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() );
+ assertEquals( "Wrong number of blocks returned.", 1, blocks.length );
+ assertEquals( "Wrong block returned.", 0, blocks[0] );
+ }
+
+ /**
+ * Test writing and reading an element within a single block size.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteAndReadSingleBlockElement()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteAndReadSingleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ int bytes = 1 * 1024;
+ int[] blocks = disk.write( new byte[bytes] );
+
+ byte[] result = (byte[]) disk.read( blocks );
+
+ // VERIFY
+ assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
+ }
+
+ /**
+ * Test writing two elements that each fit within a single block size.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteTwoSingleBlockElements()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteSingleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ int bytes = 1 * 1024;
+ int[] blocks1 = disk.write( new byte[bytes] );
+ int[] blocks2 = disk.write( new byte[bytes] );
+
+ // VERIFY
+ assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
+ assertEquals( "Wrong number of blocks returned.", 1, blocks1.length );
+ assertEquals( "Wrong block returned.", 0, blocks1[0] );
+ assertEquals( "Wrong number of blocks returned.", 1, blocks2.length );
+ assertEquals( "Wrong block returned.", 1, blocks2[0] );
+ }
+
+ /**
+ * Verify that it says we need two blocks if the total size will fit.
+ * <p>
+ * @throws Exception
+ */
+ public void testCalculateBlocksNeededDouble()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testCalculateBlocksNeededDouble";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2
+ - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] );
+
+ // Verify
+ assertEquals( "Wrong number of blocks", 2, result );
+ }
+
+ /**
+ * Test writing an element that takes two blocks.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteDoubleBlockElement()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteDoubleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ // byte arrays encur 27 bytes of serialization overhead.
+ int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 );
+ int[] blocks = disk.write( new byte[bytes] );
+
+ // VERIFY
+ System.out.println( "testWriteDoubleBlockElement " + disk );
+ assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
+ assertEquals( "Wrong number of blocks returned.", 2, blocks.length );
+ assertEquals( "Wrong block returned.", 0, blocks[0] );
+ }
+
+ /**
+ * Test writing and reading elements that do not fit within a single block.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteAndReadMultipleMultiBlockElement()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteAndReadSingleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ BlockDisk disk = new BlockDisk( file );
+
+ // DO WORK
+ int numBlocksPerElement = 4;
+ int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
+
+ int numElements = 100;
+ for ( int i = 0; i < numElements; i++ )
+ {
+ int[] blocks = disk.write( new byte[bytes] );
+ byte[] result = (byte[]) disk.read( blocks );
+
+ // VERIFY
+ assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
+ assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
+ }
+ System.out.println( "testWriteAndReadMultipleMultiBlockElement " + disk );
+ }
+
+ /**
+ * Test writing and reading elements that do not fit within a single block.
+ * <p>
+ * @throws Exception
+ */
+ public void testWriteAndReadMultipleMultiBlockElement_setSize()
+ throws Exception
+ {
+ // SETUP
+ String fileName = "testWriteAndReadSingleBlockElement";
+ File file = new File( rafDir, fileName + ".data" );
+ file.delete();
+ int blockSizeBytes = 1024;
+ BlockDisk disk = new BlockDisk( file, blockSizeBytes );
+
+ // DO WORK
+ int numBlocksPerElement = 4;
+ int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
+
+ int numElements = 100;
+ for ( int i = 0; i < numElements; i++ )
+ {
+ int[] blocks = disk.write( new byte[bytes] );
+ byte[] result = (byte[]) disk.read( blocks );
+
+ // VERIFY
+ assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
+ assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
+ }
+ System.out.println( "testWriteAndReadMultipleMultiBlockElement_setSize " + disk );
+ assertEquals( "Wrong number of elements.", numBlocksPerElement * numElements, disk.getNumberOfBlocks() );
+ }
+
+ /**
+ * Used to get the size for byte arrays that will take up the number of blocks specified.
+ * <p>
+ * @param blockSize
+ * @param numBlocks
+ * @return num bytes.
+ */
+ private int getBytesForBlocksOfByteArrays( int blockSize, int numBlocks )
+ {
+ // byte arrays encur some bytes of serialization overhead.
+ return blockSize * numBlocks - ( numBlocks * BlockDisk.HEADER_SIZE_BYTES ) - ( numBlocks * 14 );
+ }
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.