1 17 18 package org.sape.carbon.services.uniqueid.test; 19 20 import java.sql.PreparedStatement ; 21 import java.sql.SQLException ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.sape.carbon.core.component.Lookup; 27 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor; 28 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum; 29 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 30 import org.sape.carbon.core.config.InvalidConfigurationException; 31 import org.sape.carbon.core.exception.ExceptionUtility; 32 import org.sape.carbon.services.sql.StatementFactory; 33 import org.sape.carbon.services.sql.StatementFactoryException; 34 import org.sape.carbon.services.uniqueid.UniqueIDNotFoundException; 35 import org.sape.carbon.services.uniqueid.UniqueIDService; 36 import org.sape.carbon.services.uniqueid.UniqueIDServiceException; 37 38 import junit.extensions.ActiveTestSuite; 39 import junit.framework.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 44 53 public class UniqueIDServiceTest extends TestCase { 54 55 private static Set usedIDs = 56 Collections.synchronizedSet(new HashSet ()); 57 private static final int NUM_ITERATIONS = 100; 58 private static final int NUM_THREADS = 2; 59 60 private static final String ID_NOT_FOUND_NAME = 61 "/uniqueid/test/NotFoundUniqueIDService"; 62 private static final String INVALID_ID_SERVICE = 63 "/uniqueid/test/InvalidUniqueIDService"; 64 private static final String INVALID_CONNECTION_SERVICE = 65 "/uniqueid/test/InvalidConnectionUniqueIDService"; 66 private static final String ID_SERVICE_BLOCK_SIZE_1 = 67 "/uniqueid/test/BlockSize1UniqueIDService"; 68 private static final String ID_SERVICE_1 = 69 "/uniqueid/test/UniqueIDService1"; 70 private static final String ID_SERVICE_2 = 71 "/uniqueid/test/UniqueIDService2"; 72 private static final String ID_SERVICE_3 = 73 "/uniqueid/test/UniqueIDService3"; 74 private static final String UNIQUE_ID_STATEMENT_FACTORY = 75 "/uniqueid/test/UniqueIDStatementFactory"; 76 private static final String CREATE_TABLE_UNIQUE_ID_QUERY = 77 "CreateTableUniqueID"; 78 private static final String DROP_TABLE_UNIQUE_ID_QUERY = 79 "DropTableUniqueID"; 80 81 82 public UniqueIDServiceTest(String name) { 83 super(name); 84 } 85 86 90 public void createUniqueIdTable() { 91 PreparedStatement preparedStatement=null; 92 93 try { 94 95 StatementFactory sf = (StatementFactory) 96 Lookup.getInstance().fetchComponent(UNIQUE_ID_STATEMENT_FACTORY); 97 preparedStatement = 98 sf.createPreparedStatement(CREATE_TABLE_UNIQUE_ID_QUERY); 99 preparedStatement.executeUpdate(); 100 } catch (SQLException se) { 101 } catch (StatementFactoryException sfe) { 103 } 105 106 } 107 108 109 113 public void testIDNotFound() { 114 UniqueIDService uniqueIDService = 115 (UniqueIDService) Lookup.getInstance().fetchComponent( 116 ID_NOT_FOUND_NAME); 117 118 try { 119 uniqueIDService.getNextID(); 120 121 fail("Did not catch expected UniqueIDNotFoundException"); 122 } catch (UniqueIDNotFoundException uidnfe) { 123 } catch (UniqueIDServiceException uidse) { 125 fail( 126 "Did not catch expected UniqueIDNotFoundException, " 127 + "caught UniqueIDServiceException: " 128 + uidse 129 + ExceptionUtility.captureStackTrace(uidse)); 130 } 131 } 132 133 137 public void testIDsNotSkipped() { 138 UniqueIDService uniqueIDService = 139 (UniqueIDService) Lookup.getInstance().fetchComponent( 140 ID_SERVICE_BLOCK_SIZE_1); 141 142 try { 143 long currentID = uniqueIDService.getNextID(); 145 long lastID = currentID; 146 147 for (int i = 0; i < 10; i++) { 148 currentID = uniqueIDService.getNextID(); 149 150 if (currentID <= lastID) { 151 fail( 152 "UniqueIDService returned an ID out of sequence. " 153 + "IDs should be in sequence under test conditions"); 154 } 155 156 lastID = currentID; 157 } 158 159 } catch (UniqueIDServiceException uidse) { 160 fail( 161 "Caught UniqueIDServiceException: " 162 + uidse 163 + ExceptionUtility.captureStackTrace(uidse)); 164 } 165 } 166 167 171 public void testInvalidConfiguration() { 172 try { 173 Lookup.getInstance().fetchComponent(INVALID_ID_SERVICE); 174 175 fail("Did not catch expected StateTransition or " 176 + "InvalidConfiguration exception"); 177 } catch (InvalidConfigurationException ice) { 178 } catch (StateTransitionException ste) { 180 } 182 183 } 184 185 189 public void testInvalidConnectionFactory() throws Exception { 190 try { 191 UniqueIDService uniqueIDService = 192 (UniqueIDService) Lookup.getInstance().fetchComponent( 193 INVALID_CONNECTION_SERVICE); 194 195 uniqueIDService.getNextID(); 196 197 fail("Did not catch expected UniqueIDServiceException " 198 + "when using an invalid connection to the database."); 199 200 } catch (UniqueIDServiceException uidse) { 201 } 203 } 204 205 210 public void testMultiComponentAccess1() { 211 testMultiComponentAccess(ID_SERVICE_1); 212 } 213 214 215 public void testMultiComponentAccess2() { 216 testMultiComponentAccess(ID_SERVICE_2); 217 } 218 219 220 public void testMultiComponentAccess3() { 221 testMultiComponentAccess(ID_SERVICE_3); 222 } 223 224 225 private void testMultiComponentAccess(String uniqueIDServiceName) { 226 UniqueIDService uniqueIDService = 227 (UniqueIDService) Lookup.getInstance().fetchComponent( 228 uniqueIDServiceName); 229 230 while (((LifecycleInterceptor) uniqueIDService).getLifecycleState() 231 != LifecycleStateEnum.RUNNING) { 232 233 try { 234 Thread.sleep(100); 235 } catch (InterruptedException e) { 236 } 237 } 238 239 try { 240 for (int i = 0; i < NUM_ITERATIONS; i++) { 241 boolean idNotUsed = 242 UniqueIDServiceTest.usedIDs.add( 243 new Long (uniqueIDService.getNextID())); 244 245 if (!idNotUsed) { 246 fail("UniqueIDService returned a non-unique ID"); 247 } 248 } 249 250 } catch (UniqueIDServiceException uidse) { 251 fail( 252 "Caught UniqueIDServiceException: " 253 + uidse 254 + ExceptionUtility.captureStackTrace(uidse)); 255 } 256 } 257 258 262 public static Test suite() { 263 TestSuite masterSuite = new TestSuite(); 264 Test singleThreadedTests = getSingleThreadedTests(); 266 if (singleThreadedTests != null) { 267 masterSuite.addTest(singleThreadedTests); 268 } 269 Test multiThreadedTests = getMultiThreadedTests(); 271 if (multiThreadedTests != null) { 272 masterSuite.addTest(multiThreadedTests); 273 } 274 return masterSuite; 275 } 276 277 287 private static Test getSingleThreadedTests() { 288 TestSuite suite = new TestSuite(); 289 suite.addTest(new UniqueIDServiceTest("createUniqueIdTable")); 290 suite.addTest(new UniqueIDServiceTest("testIDNotFound")); 291 suite.addTest(new UniqueIDServiceTest("testIDsNotSkipped")); 292 suite.addTest(new UniqueIDServiceTest("testInvalidConfiguration")); 293 suite.addTest(new UniqueIDServiceTest("testInvalidConnectionFactory")); 294 295 return suite; 296 } 297 298 307 private static Test getMultiThreadedTests() { 308 TestSuite suite = new ActiveTestSuite(); 309 310 addTest( 311 suite, 312 "testMultiComponentAccess1", 313 UniqueIDServiceTest.NUM_THREADS); 314 315 316 325 addTest( 326 suite, 327 "testMultiComponentAccess2", 328 UniqueIDServiceTest.NUM_THREADS); 329 addTest( 330 suite, 331 "testMultiComponentAccess3", 332 UniqueIDServiceTest.NUM_THREADS); 333 334 return suite; 335 } 336 337 346 private static void addTest( 347 TestSuite suite, 348 String testName, 349 int number) { 350 for (int count = 0; count < number; count++) { 351 suite.addTest(new UniqueIDServiceTest(testName)); 352 } 353 } 354 } | Popular Tags |