KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > repository > test > RepositoryTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.repository.test;
8
9
10 import java.util.ResourceBundle JavaDoc;
11
12 import com.inversoft.config.ConfigFactoryRegistry;
13 import com.inversoft.junit.WebTestCase;
14 import com.inversoft.verge.config.VergeConfigConstants;
15 import com.inversoft.verge.config.VergeConfigMediator;
16 import com.inversoft.verge.repository.Repository;
17 import com.inversoft.verge.repository.RepositoryException;
18
19
20 /**
21  * This is the test class for the repository
22  *
23  * @author Brian Pontarelli
24  * @since 2.0
25  * @version 2.0
26  */

27 public class RepositoryTest extends WebTestCase {
28
29     /**
30      * Constructs the <code>RepositoryTest</code>
31      */

32     public RepositoryTest(String JavaDoc name) {
33         super(name);
34         setLocal(true);
35     }
36
37
38     /**
39      * This tests that the repository initializes without error
40      */

41     public void testInitialization() {
42
43         // Add the initParameter for local testing
44
if (isLocal()) {
45             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
46                 "src/com/inversoft/verge/repository/test/test-config.xml");
47         }
48
49         try {
50             ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
51                 "com.inversoft.verge.config.ConfigFactories");
52             ConfigFactoryRegistry.load(bundle);
53             new VergeConfigMediator().mediate(context);
54             //Repository.initialize();
55
} catch (Exception JavaDoc e) {
56             fail(e.toString());
57         }
58     }
59
60     /**
61      * This tests that the repository items are setup during initialization
62      */

63     public void testItemSetup() {
64
65         // Initialize it just incase it hasn't been already
66
testInitialization();
67
68         // Fetch out the item1 and verify that it is setup correctly
69
SimpleItem item1 =
70             (SimpleItem) Repository.getInstance().lookupItem(request, "item1");
71         assertTrue("item1 should not be null", item1 != null);
72
73         SimpleItem item2 =
74             (SimpleItem) Repository.getInstance().lookupItem(request, "item2");
75         assertTrue("item2 should be Brian Pontarelli",
76             item2.getName().equals("Brian Pontarelli"));
77         assertTrue("item2 should be 26 years old",
78             item2.getAge().intValue() == 26);
79         assertTrue("item2 should have 3.14 dollars",
80             item2.getMoney().floatValue() == 3.14f);
81
82         assertTrue("item1 and item2 should be in the request",
83             request.getAttribute("item1") != null &&
84             request.getAttribute("item2") != null);
85         assertTrue("item1 and item2 should be in the request and of correct type",
86             request.getAttribute("item1") instanceof SimpleItem &&
87             request.getAttribute("item2") instanceof SimpleItem);
88
89         assertTrue(Repository.getInstance().isItemLoaded(request, "item1"));
90         assertTrue(Repository.getInstance().isItemLoaded(request, "item2"));
91         assertFalse(Repository.getInstance().isItemLoaded(request, "user"));
92
93         assertTrue(Repository.getInstance().isValidItem(request, "item1"));
94     }
95
96     /**
97      * This tests that the repository items with references are setup
98      * correctly during initialization
99      */

100     public void testItemReferences() {
101
102         // Initialize it just incase it hasn't been already
103
testInitialization();
104
105         // Fetch out the item1 and verify that it is setup correctly
106
SimpleItem user =
107             (SimpleItem) Repository.getInstance().lookupItem(request, "user");
108         assertTrue("user should not be null", user != null);
109
110         SimpleItem2 address1 = user.getReference();
111         SimpleItem2 address2 =
112             (SimpleItem2) Repository.getInstance().lookupItem(request, "address");
113         assertTrue("address1 and address2 should be the same object", address1 == address2);
114         assertTrue("address1 should be Chicago", address1.getCity().equals("Chicago"));
115         assertTrue("address1 should be IL", address1.getState().equals("IL"));
116
117         assertTrue("user and address should be in the request",
118             request.getSession().getAttribute("user") != null &&
119             request.getSession().getAttribute("address") != null);
120         assertTrue("user and address should be in the request and of correct type",
121             request.getSession().getAttribute("user") instanceof SimpleItem &&
122             request.getSession().getAttribute("address") instanceof SimpleItem2);
123     }
124
125     /**
126      * This test a lookup failure
127      */

128     public void testLookupFailure() {
129
130         // Initialize it just incase it hasn't been already
131
testInitialization();
132
133         try {
134             Repository.getInstance().lookupItem(request, "notValid");
135             fail("Should not have found the notValid item");
136         } catch (RepositoryException re) {
137             // Smother
138
}
139     }
140
141     /**
142      * This test a create failure
143      */

144     public void testCreateFailure() {
145
146         // Initialize it just incase it hasn't been already
147
testInitialization();
148
149         try {
150             Repository.getInstance().lookupItem(request, "badCreate");
151             fail("Should not have been able to create the bad item");
152         } catch (RepositoryException re) {
153             // Smother
154
}
155     }
156
157     /**
158      * This test a bad reference
159      */

160     public void testBadReference() {
161
162         // Initialize it just incase it hasn't been already
163
testInitialization();
164
165         try {
166             Repository.getInstance().lookupItem(request, "badRef");
167             fail("Should not have been able to create the reference");
168         } catch (RepositoryException re) {
169             // Smother
170
}
171     }
172
173     /**
174      * This test a bad property
175      */

176     public void testBadProperty() {
177
178         // Initialize it just incase it hasn't been already
179
testInitialization();
180
181         try {
182             Repository.getInstance().lookupItem(request, "failProperty");
183             fail("Should not have been able to find the property");
184         } catch (RepositoryException re) {
185             // Smother
186
}
187     }
188 }
Popular Tags