1 26 27 package net.sourceforge.groboutils.util.datastruct.v1; 28 29 import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 42 public class ObjectCacheUTest extends TestCase 43 { 44 private static final Class THIS_CLASS = ObjectCacheUTest.class; 45 46 public ObjectCacheUTest( String name ) 47 { 48 super( name ); 49 } 50 51 public static Test suite() 52 { 53 TestSuite suite = new TestSuite( THIS_CLASS ); 54 55 return suite; 56 } 57 58 public static void main( String [] args ) 59 { 60 String [] name = { THIS_CLASS.getName() }; 61 62 65 junit.textui.TestRunner.main( name ); 66 } 67 68 protected void setUp() throws Exception 69 { 70 super.setUp(); 71 72 } 74 75 76 protected void tearDown() throws Exception 77 { 78 80 81 super.tearDown(); 82 } 83 84 85 public class OCreator implements ObjectCache.ObjectCreator 86 { 87 private int count = 0; 88 public synchronized Object createObject() 89 { 90 return "Object "+(++count); 91 } 92 93 public int getCreationCount() 94 { 95 return count; 96 } 97 } 98 99 100 101 public void testInstantiate() 102 { 103 ObjectCache oc = new ObjectCache(); 104 assertEquals( "Size must be at default of UNLIMITED.", 105 oc.UNLIMITED_SIZE, oc.getMaxSize() ); 106 assertEquals( "Underflows must be at zero.", 107 0, oc.getUnderflows() ); 108 assertEquals( "Overflows must be at zero.", 109 0, oc.getOverflows() ); 110 assertTrue( "Cache should be empty after creation.", oc.get() == null ); 113 114 oc = new ObjectCache( 10 ); 115 assertEquals( "Size must be at specified size.", 116 10, oc.getMaxSize() ); 117 assertEquals( "Underflows must be at zero.", 118 0, oc.getUnderflows() ); 119 assertEquals( "Overflows must be at zero.", 120 0, oc.getOverflows() ); 121 assertTrue( "Cache should be empty after creation.", oc.get() == null ); 124 125 oc = new ObjectCache( Object .class ); 126 assertEquals( "Size must be at default of UNLIMITED.", 127 oc.UNLIMITED_SIZE, oc.getMaxSize() ); 128 assertEquals( "Underflows must be at zero.", 129 0, oc.getUnderflows() ); 130 assertEquals( "Overflows must be at zero.", 131 0, oc.getOverflows() ); 132 133 oc = new ObjectCache( Object .class, 10 ); 134 assertEquals( "Size must be at specified size.", 135 10, oc.getMaxSize() ); 136 assertEquals( "Underflows must be at zero.", 137 0, oc.getUnderflows() ); 138 assertEquals( "Overflows must be at zero.", 139 0, oc.getOverflows() ); 140 141 oc = new ObjectCache( new OCreator() ); 142 assertEquals( "Size must be at default of UNLIMITED.", 143 oc.UNLIMITED_SIZE, oc.getMaxSize() ); 144 assertEquals( "Underflows must be at zero.", 145 0, oc.getUnderflows() ); 146 assertEquals( "Overflows must be at zero.", 147 0, oc.getOverflows() ); 148 149 oc = new ObjectCache( new OCreator(), 10 ); 150 assertEquals( "Size must be at specified size.", 151 10, oc.getMaxSize() ); 152 assertEquals( "Underflows must be at zero.", 153 0, oc.getUnderflows() ); 154 assertEquals( "Overflows must be at zero.", 155 0, oc.getOverflows() ); 156 } 157 158 159 public void testSimpleAdd() throws InterruptedException 160 { 161 ObjectCache oc = new ObjectCache(); 162 Object o1 = new Object (); 163 assertTrue( "Cache should be empty after creation.", oc.get() == null ); 165 oc.putBack( o1 ); 166 Object o2 = oc.get(); 167 assertEquals( "What gets put in, needs to equal what gets put out.", 168 o1, o2 ); 169 assertTrue( "Cache should be empty after removing.", oc.get() == null ); 171 } 172 173 public void testClassCreator() 174 { 175 ObjectCache oc = new ObjectCache( Object .class ); 176 Object o1 = oc.get(); 177 assertNotNull( "Cache should have created an entry.", o1 ); 178 assertEquals( "Cache should have created an Object.", 179 Object .class, o1.getClass() ); 180 } 181 182 public void testObjectCreator() 183 { 184 ObjectCache oc = new ObjectCache( new OCreator() ); 185 Object o1 = oc.get(); 186 assertNotNull( "Cache should have created an entry.", o1 ); 187 assertEquals( "Cache should have created a String.", 188 String .class, o1.getClass() ); 189 } 190 191 public void testFill() 192 { 193 OCreator creator = new OCreator(); 194 ObjectCache oc = new ObjectCache( creator, 5, true ); 195 assertEquals( "Cache did not fill itself.", 196 5, creator.getCreationCount() ); 197 198 for (int i = 0; i < 5; i++) 200 { 201 Object o = oc.get(); 202 assertNotNull( "Cache with creator should never return null.", 203 o ); 204 } 205 206 oc.fillCache(); 208 assertEquals( "Cache did not fill itself.", 209 10, creator.getCreationCount() ); 210 } 211 212 public void testPutBack() 213 { 214 ObjectCache oc = new ObjectCache( 1 ); 215 Object o1 = new Object (); 216 Object o2 = new Object (); 217 oc.putBack( o1 ); 218 oc.putBack( o2 ); 219 assertEquals( "ObjectCache should have an overflow.", 220 1, oc.getOverflows() ); 221 Object o3 = oc.get(); 222 assertEquals( "Cache should have returned 1st object.", 223 o1, o3 ); 224 Object o4 = oc.get(); 225 assertEquals( "Cache should have returned null.", 226 null, o4 ); 227 } 228 } 229 | Popular Tags |