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 HashCacheUTest extends TestCase 43 { 44 private static final Class THIS_CLASS = HashCacheUTest.class; 45 46 public HashCacheUTest( 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 OMgr implements HashCache.ObjectManager 86 { 87 private int count = 0; 88 private int closecount = 0; 89 public synchronized Object createObject( Object key ) 90 { 91 return "["+key+"] "+(++count); 92 } 93 94 public synchronized void cleanUpObject( Object key, Object value ) 95 { 96 ++this.closecount; 97 } 98 99 public int getCreationCount() 100 { 101 return this.count; 102 } 103 104 public int getCloseCount() 105 { 106 return this.closecount; 107 } 108 } 109 110 111 112 public void testConstructor1() 113 { 114 HashCache oc = new HashCache( new OMgr(), 10 ); 115 assertEquals( "Size must be instantiation size.", 116 10, oc.getMaxSize() ); 117 assertEquals( "Overflows must be at zero.", 118 0, oc.getOverflowCount() ); 119 } 120 121 122 public void testConstructor2() 123 { 124 try 125 { 126 new HashCache( null, 10 ); 127 fail("Did not throw IllegalArgumentException."); 128 } 129 catch (IllegalArgumentException iae) 130 { 131 } 133 } 134 135 136 public void testConstructor3() 137 { 138 try 139 { 140 new HashCache( null, 0 ); 141 fail("Did not throw IllegalArgumentException."); 142 } 143 catch (IllegalArgumentException iae) 144 { 145 } 147 } 148 149 150 public void testConstructor4() 151 { 152 try 153 { 154 new HashCache( new OMgr(), 1 ); 155 fail("Did not throw IllegalArgumentException."); 156 } 157 catch (IllegalArgumentException iae) 158 { 159 } 161 } 162 163 164 public void testConstructor5() 165 { 166 try 167 { 168 new HashCache( new OMgr(), -1 ); 169 fail("Did not throw IllegalArgumentException."); 170 } 171 catch (IllegalArgumentException iae) 172 { 173 } 175 } 176 177 178 public void testGet1() 179 { 180 OMgr om = new OMgr(); 181 HashCache hc = new HashCache( om, 10 ); 182 try 183 { 184 hc.get( null ); 185 } 186 catch (NullPointerException npe) 187 { 188 } 190 } 191 192 193 public void testGet2() 194 { 195 OMgr om = new OMgr(); 196 HashCache hc = new HashCache( om, 10 ); 197 Object o = hc.get( "a" ); 198 assertNotNull( 199 "get returned null.", 200 o ); 201 assertTrue( 202 "incorrect type returned.", 203 o instanceof String ); 204 assertEquals( 205 "Incorrect value of returned object.", 206 "[a] 1", 207 (String )o ); 208 assertEquals( 209 "The mgr reported the creation.", 210 1, 211 om.getCreationCount() ); 212 213 Object o2 = hc.get( "a" ); 214 assertSame( 215 "Did not return the same object.", 216 o, 217 o2 ); 218 assertEquals( 219 "The mgr was not called for the creation.", 220 1, 221 om.getCreationCount() ); 222 } 223 224 225 public void testGet3() 226 { 227 OMgr om = new OMgr(); 228 HashCache hc = new HashCache( om, 2 ); 229 Object o = hc.get( "a" ); 230 Object o2 = hc.get( "b" ); 231 Object o3 = hc.get( "c" ); 232 assertEquals( 233 "The mgr was not called for the creation correctly.", 234 3, 235 om.getCreationCount() ); 236 assertEquals( 237 "The mgr was not called for the clean-up.", 238 1, 239 om.getCloseCount() ); 240 assertEquals( 241 "The overflow count is wrong.", 242 1, 243 hc.getOverflowCount() ); 244 assertEquals( 245 "The size is wrong.", 246 2, 247 hc.getSize() ); 248 } 249 250 251 public void testGet4() 252 { 253 System.out.println("Get4:"); 254 OMgr om = new OMgr(); 255 HashCache hc = new HashCache( om, 10 ); 256 int requests[] = { 257 0,1,2,3, 258 0,2, 4,5,6, 260 0,3,6, 7,8,9,10,11, 262 1,2, 12,13,14,15,16,17,18,19, 264 0 }; 266 for (int i = 0; i < requests.length; ++i) 267 { 268 hc.get( new Integer ( requests[i] ) ); 269 } 271 assertEquals( 272 "The mgr was not called for the creation correctly.", 273 23, 274 om.getCreationCount() ); 275 assertEquals( 276 "The mgr was not called for the clean-up.", 277 13, 278 om.getCloseCount() ); 279 assertEquals( 280 "The overflow count is wrong.", 281 13, 282 hc.getOverflowCount() ); 283 assertEquals( 284 "The size is wrong.", 285 10, 286 hc.getSize() ); 287 } 288 289 290 public void testResetOverflowCount1() 291 { 292 System.out.println("Get4:"); 293 OMgr om = new OMgr(); 294 HashCache hc = new HashCache( om, 10 ); 295 hc.resetOverflowCount(); 296 } 297 } 298 | Popular Tags |