KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > test > TestIDGenerator


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

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 JavaDoc;
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 JavaDoc name) {
37         super(name);
38     }
39
40     public void testIDCacheLoad() {
41         Integer JavaDoc nextId;
42
43         try {
44             InitialContext ic = new InitialContext();
45             Object JavaDoc 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 JavaDoc 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 JavaDoc idgRef = ic.lookup(IDGenerator.JNDI_NAME);
66             IDGeneratorHome idgHome = (IDGeneratorHome) PortableRemoteObject.narrow(idgRef, IDGeneratorHome.class);
67             IDGenerator idGen = idgHome.create();
68
69
70             Integer JavaDoc nextId = null;
71             Integer JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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