1 18 19 package cowsultants.itracker.ejb.test; 20 21 import java.rmi.*; 22 import java.sql.*; 23 import java.util.*; 24 import javax.rmi.*; 25 import javax.sql.*; 26 import javax.naming.*; 27 import javax.ejb.CreateException ; 28 29 import junit.framework.*; 30 31 import cowsultants.itracker.ejb.client.interfaces.*; 32 import cowsultants.itracker.ejb.util.*; 33 34 public class TestIDGenerator extends TestCase { 35 36 public TestIDGenerator(String name) { 37 super(name); 38 } 39 40 public void testIDCacheLoad() { 41 Integer nextId; 42 43 try { 44 InitialContext ic = new InitialContext(); 45 Object idgRef = ic.lookup(IDGenerator.JNDI_NAME); 46 IDGeneratorHome idgHome = (IDGeneratorHome) PortableRemoteObject.narrow(idgRef, IDGeneratorHome.class); 47 IDGenerator idGen = idgHome.create(); 48 49 nextId = idGen.getId("test"); 50 assertNotNull("First call to IDGenerator returned null", nextId); 51 assertTrue("First call to IDGenerator did not return '1', returned " + nextId, nextId.intValue() == 1); 52 nextId = idGen.getId("test"); 53 assertNotNull("Second call to IDGenerator returned null", nextId); 54 assertTrue("Second call to IDGenerator did not return '2'", nextId.intValue() == 2); 55 } catch(CreateException ce) { 56 fail("CreateException caught during testIDCacheLoad: " + ce.getMessage()); 57 } catch(NamingException ne) { 58 fail("NamingException caught during testIDCacheLoad: " + ne.getMessage()); 59 } 60 } 61 62 public void testIDCacheReload() { 63 try { 64 InitialContext ic = new InitialContext(); 65 Object idgRef = ic.lookup(IDGenerator.JNDI_NAME); 66 IDGeneratorHome idgHome = (IDGeneratorHome) PortableRemoteObject.narrow(idgRef, IDGeneratorHome.class); 67 IDGenerator idGen = idgHome.create(); 68 69 70 Integer nextId = null; 71 Integer firstId = idGen.getId("test"); 72 for(int i = 0; i < IDCache.getGroupSize(); i++) { 73 nextId = idGen.getId("test"); 74 assertNotNull("IDGenerator returned null", nextId); 75 } 76 assertNotNull("NextId is null", nextId); 77 assertNotNull("FirstId is null", firstId); 78 int change = nextId.intValue() - firstId.intValue(); 79 assertTrue("IDGenerator did not create correct number of ids. Should be " + IDCache.getGroupSize() + 80 " was " + change, change == IDCache.getGroupSize()); 81 } catch(NamingException ne) { 82 fail("NamingException caught during testIDCacheReload: " + ne.getMessage()); 83 } catch(CreateException ce) { 84 fail("CreateException caught during testIDCacheReload: " + ce.getMessage()); 85 } 86 } 87 88 public void tearDown() { 89 try { 90 InitialContext ic = new InitialContext(); 91 Object idgRef = ic.lookup(IDGenerator.JNDI_NAME); 92 IDGeneratorHome idgHome = (IDGeneratorHome) PortableRemoteObject.narrow(idgRef, IDGeneratorHome.class); 93 IDGenerator idGen = idgHome.create(); 94 95 idGen.resetCache("test"); 96 idGen.deleteCache("test"); 97 } catch(NamingException ne) { 98 fail("NamingException caught during tearDown: " + ne.getMessage()); 99 } catch(CreateException ce) { 100 fail("CreateException caught during tearDown: " + ce.getMessage()); 101 } 102 } 103 104 public static Test suite() { 105 return new TestSuite(TestIDGenerator.class); 106 } 107 } | Popular Tags |