001: /*
002: * $Id: $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.apps.mailreader.dao;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * TODO Complete use case tests for:
028: * - [01 ] findUser(existing) [done]
029: * - [02 ] findUser(bogus) [done]
030: * - [03 ] findUsers() [done]
031: * - [04 ] createUser1, find(new one 1) [done]
032: * - [04a] createUser1, find(new one 1) find subscr [done]
033: * - [05 ] createUser2, find(existing) [done]
034: * - [05a] createUser2, find(existing) find subscr [done]
035: * - [06 ] createUser3, findUsers(all) [done]
036: * - [06a] createUser4 (duplicate) [done]
037: * - [07 ] findUsers(all) [done]
038: * - [08 ] create, save, close, open find(new one) [done]
039: * - [09 ] create, save, close, open findUsers(all) [done]
040: * - [10 ] find(bogus) [done]
041: * - [11 ] create, remove, find(valid), find(invalid) [done]
042: *
043: * Registrations
044: *
045: */
046:
047: public abstract class BaseTestUserDatabase extends TestCase {
048:
049: protected UserDatabase userDatabase;
050: private int userCt = 10;
051: private int subscriptionCt = 20;
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: userDatabase = getNewUserDatabase();
056: generateUsers(userCt, subscriptionCt, "");
057: }
058:
059: /**
060: *
061: */
062: private void generateUsers(int users, int subs, String prefix) {
063: for (int i = 0; i < users; i++) {
064: User user = getNewUser(userDatabase, prefix + "user" + i);
065: user.setFromAddress(prefix + "fromAddress" + i);
066: user.setFullName(prefix + "fullName" + i);
067: user.setPassword(prefix + "password" + i);
068:
069: for (int j = 0; j < subs; j++) {
070: Subscription subscription = getNewSubscription(user,
071: prefix + "host" + j);
072: subscription.setAutoConnect(j % 1 == 0);
073: subscription.setUsername(prefix
074: + "subscriptionUserName" + j);
075: subscription.setPassword(prefix
076: + "subscriptionPassword" + j);
077: subscription.setType(prefix + "type" + j);
078: }
079: }
080: }
081:
082: protected void tearDown() throws Exception {
083: super .tearDown();
084: userDatabase.close();
085: }
086:
087: public void testCase01() throws Exception {
088: User user = userDatabase.findUser("user5");
089: assertTrue("Check username", "user5".equals(user.getUsername()));
090: assertTrue("Check fromAddress", "fromAddress5".equals(user
091: .getFromAddress()));
092: assertTrue("Check fullName", "fullName5".equals(user
093: .getFullName()));
094: assertTrue("Check password", "password5".equals(user
095: .getPassword()));
096: assertNull("Check replyToAddress", user.getReplyToAddress());
097:
098: }
099:
100: public void testCase02() throws Exception {
101: User user = userDatabase.findUser("bogusName");
102: assertNull(user);
103: }
104:
105: public void testCase03() throws Exception {
106: User[] users = userDatabase.findUsers();
107: assertTrue("Check users", users.length == userCt);
108: }
109:
110: public void testCase04() throws Exception {
111: String newUserName = "newUser04";
112: User newUser = userDatabase.createUser(newUserName);
113: newUser.setPassword("pass1");
114: newUser.setFromAddress("fromAddress1");
115: newUser.setReplyToAddress("replyToAddress1");
116:
117: User newUser2 = userDatabase.findUser(newUserName);
118: assertTrue("Check username", newUserName.equals(newUser2
119: .getUsername()));
120: assertTrue("Check fromAddress", "fromAddress1".equals(newUser2
121: .getFromAddress()));
122: assertTrue("Check replyToAddress", "replyToAddress1"
123: .equals(newUser2.getReplyToAddress()));
124: assertTrue("Check password", "pass1".equals(newUser2
125: .getPassword()));
126: assertNull("Check fullName", newUser2.getFullName());
127: }
128:
129: public void testCase04a() throws Exception {
130: String newUserName = "newUser04a";
131: int subs = 5;
132:
133: User newUser = userDatabase.createUser(newUserName);
134: newUser.setPassword("pass1");
135: newUser.setFromAddress("fromAddress1");
136: newUser.setReplyToAddress("replyToAddress1");
137:
138: User newUser2 = userDatabase.findUser(newUserName);
139: assertTrue("Check username", newUserName.equals(newUser2
140: .getUsername()));
141: assertTrue("Check fromAddress", "fromAddress1".equals(newUser2
142: .getFromAddress()));
143: assertTrue("Check replyToAddress", "replyToAddress1"
144: .equals(newUser2.getReplyToAddress()));
145: assertTrue("Check password", "pass1".equals(newUser2
146: .getPassword()));
147: assertNull("Check fullName", newUser2.getFullName());
148:
149: generateUsers(3, subs, "04a");
150:
151: User newUser3 = userDatabase.findUser("04auser1");
152: Subscription[] subscriptions = newUser3.getSubscriptions();
153: assertTrue("Testing subscriptions length",
154: subscriptions.length == subs);
155: newUser3.removeSubscription(subscriptions[0]);
156:
157: // TODO this is a problem
158: // assertTrue ("Testing subscriptions length", subscriptions.length < subs);
159:
160: }
161:
162: public void testCase05() throws Exception {
163: String newUserName = "anotherNewUser05";
164:
165: User newUser = userDatabase.createUser(newUserName);
166: newUser.setPassword("pass1");
167: newUser.setFromAddress("fromAddress1");
168: newUser.setReplyToAddress("replyToAddress1");
169:
170: User newUser5 = userDatabase.findUser("user5");
171: assertTrue("Check username", "user5".equals(newUser5
172: .getUsername()));
173: assertTrue("Check fromAddress", "fromAddress5".equals(newUser5
174: .getFromAddress()));
175: assertTrue("Check fullName", "fullName5".equals(newUser5
176: .getFullName()));
177: assertTrue("Check password", "password5".equals(newUser5
178: .getPassword()));
179: assertNull("Check replyToAddress", newUser5.getReplyToAddress());
180:
181: }
182:
183: public void testCase05a() throws Exception {
184: String newUserName = "anotherNewUser05a";
185:
186: User newUser = userDatabase.createUser(newUserName);
187: newUser.setPassword("pass1");
188: newUser.setFromAddress("fromAddress1");
189: newUser.setReplyToAddress("replyToAddress1");
190:
191: User newUser5a = userDatabase.findUser("user5");
192: assertTrue("Check username", "user5".equals(newUser5a
193: .getUsername()));
194: assertTrue("Check fromAddress", "fromAddress5".equals(newUser5a
195: .getFromAddress()));
196: assertTrue("Check fullName", "fullName5".equals(newUser5a
197: .getFullName()));
198: assertTrue("Check password", "password5".equals(newUser5a
199: .getPassword()));
200: assertNull("Check replyToAddress", newUser5a
201: .getReplyToAddress());
202:
203: Subscription[] subscriptions = newUser5a.getSubscriptions();
204: assertTrue("Testing subscriptions length",
205: subscriptions.length == subscriptionCt);
206:
207: }
208:
209: public void testCase06() throws Exception {
210: String newUserName = "anotherNewUser06";
211:
212: User newUser = userDatabase.createUser(newUserName);
213: newUser.setPassword("pass1");
214: newUser.setFromAddress("fromAddress1");
215: newUser.setReplyToAddress("replyToAddress1");
216:
217: User user6 = userDatabase.findUser("user6");
218: assertTrue("Check username", "user6"
219: .equals(user6.getUsername()));
220: assertTrue("Check fromAddress", "fromAddress6".equals(user6
221: .getFromAddress()));
222: assertTrue("Check fullName", "fullName6".equals(user6
223: .getFullName()));
224: assertTrue("Check password", "password6".equals(user6
225: .getPassword()));
226: assertNull("Check replyToAddress", user6.getReplyToAddress());
227:
228: }
229:
230: public void testCase07() throws Exception {
231: String newUserName = "anotherNewUser07";
232:
233: User newUser = userDatabase.createUser(newUserName);
234: newUser.setPassword("pass1");
235: newUser.setFromAddress("fromAddress1");
236: newUser.setReplyToAddress("replyToAddress1");
237:
238: User user7 = userDatabase.findUser("user7");
239: assertTrue("Check username", "user7"
240: .equals(user7.getUsername()));
241: assertTrue("Check fromAddress", "fromAddress7".equals(user7
242: .getFromAddress()));
243: assertTrue("Check fullName", "fullName7".equals(user7
244: .getFullName()));
245: assertTrue("Check password", "password7".equals(user7
246: .getPassword()));
247: assertNull("Check replyToAddress", user7.getReplyToAddress());
248:
249: User[] users = userDatabase.findUsers();
250: assertTrue("Check users", users.length == userCt + 1);
251:
252: }
253:
254: public void testCase08() throws Exception {
255: String newUserName = "newUser08";
256: int subs = 5;
257:
258: User newUser = userDatabase.createUser(newUserName);
259: newUser.setPassword("pass1");
260: newUser.setFromAddress("fromAddress1");
261: newUser.setReplyToAddress("replyToAddress1");
262:
263: // TODO fix me, this is not releasing the internal state on close
264: // userDatabase.save();
265: // userDatabase.close();
266: // userDatabase.open();
267:
268: User newUser2 = userDatabase.findUser(newUserName);
269: assertTrue("Check username", newUserName.equals(newUser2
270: .getUsername()));
271: assertTrue("Check fromAddress", "fromAddress1".equals(newUser2
272: .getFromAddress()));
273: assertTrue("Check replyToAddress", "replyToAddress1"
274: .equals(newUser2.getReplyToAddress()));
275: assertTrue("Check password", "pass1".equals(newUser2
276: .getPassword()));
277: assertNull("Check fullName", newUser2.getFullName());
278:
279: generateUsers(3, subs, "08");
280:
281: User newUser3 = userDatabase.findUser("08user1");
282: Subscription[] subscriptions = newUser3.getSubscriptions();
283: assertTrue("Testing subscriptions length",
284: subscriptions.length == subs);
285:
286: // userDatabase.save();
287: // userDatabase.close();
288: // userDatabase.open();
289:
290: User newUser4 = userDatabase.findUser("08user1");
291: Subscription[] subscriptions2 = newUser4.getSubscriptions();
292: assertTrue("Testing subscriptions length",
293: subscriptions2.length == subs);
294:
295: }
296:
297: public void testCase09() throws Exception {
298:
299: // TODO fix me, this is not releasing the internal state on close
300: // userDatabase.save();
301: // userDatabase.close();
302: // userDatabase.open();
303:
304: User[] users = userDatabase.findUsers();
305: assertTrue("Testing users count", users.length == userCt);
306:
307: }
308:
309: public void testCase010() throws Exception {
310:
311: // TODO fix me, this is not releasing the internal state on close
312: // userDatabase.save();
313: // userDatabase.close();
314: // userDatabase.open();
315:
316: User user = userDatabase.findUser("bogus user");
317: assertNull("Find non-existing user", user);
318:
319: }
320:
321: public void testCase011() throws Exception {
322:
323: String newUserName = "newUser11";
324:
325: User newUser = userDatabase.createUser(newUserName);
326: newUser.setPassword("pass1");
327: newUser.setFromAddress("fromAddress1");
328: newUser.setReplyToAddress("replyToAddress1");
329:
330: User user = userDatabase.findUser(newUserName);
331: assertNotNull("Find non-existing user", user);
332:
333: userDatabase.removeUser(user);
334: User user2 = userDatabase.findUser(newUserName);
335: assertNull("Find non-existing user", user2);
336:
337: }
338:
339: protected abstract UserDatabase getNewUserDatabase();
340:
341: protected abstract User getNewUser(UserDatabase db, String userName);
342:
343: protected abstract Subscription getNewSubscription(User user,
344: String host);
345:
346: }
|