001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.registration;
023:
024: import junit.framework.TestCase;
025: import org.jboss.portal.registration.Consumer;
026: import org.jboss.portal.registration.ConsumerGroup;
027: import org.jboss.portal.registration.DuplicateRegistrationException;
028: import org.jboss.portal.registration.NoSuchRegistrationException;
029: import org.jboss.portal.registration.Registration;
030: import org.jboss.portal.registration.RegistrationPersistenceManager;
031: import org.jboss.portal.common.util.MapBuilder;
032:
033: import javax.xml.namespace.QName;
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.HashMap;
038: import java.util.HashSet;
039: import java.util.Map;
040:
041: /**
042: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
043: * @version $Revision: 9218 $
044: * @since 2.6
045: */
046: public abstract class AbstractRegistrationPersistenceManagerTestCase
047: extends TestCase {
048:
049: /** . */
050: private Map registrationProperties;
051:
052: public abstract RegistrationPersistenceManager getManager();
053:
054: public void startInteraction() {
055: }
056:
057: public void stopInteraction() {
058: }
059:
060: public void setUp() throws Exception {
061: registrationProperties = new HashMap();
062: registrationProperties.put(new QName("prop1"), "value1");
063: registrationProperties.put(new QName("prop2"), "value2");
064: }
065:
066: protected void tearDown() throws Exception {
067: registrationProperties = null;
068: }
069:
070: public void testGetGroupThrowsIAE() throws Exception {
071: startInteraction();
072: try {
073: getManager().getConsumerGroup(null);
074: fail();
075: } catch (IllegalArgumentException expected) {
076: }
077: stopInteraction();
078: }
079:
080: public void testAddGroup() throws Exception {
081: startInteraction();
082: ConsumerGroup group = getManager().createConsumerGroup("Foo");
083: assertNotNull(group);
084: assertEquals("Foo", group.getName());
085: assertEquals(Collections.EMPTY_LIST, new ArrayList(group
086: .getConsumers()));
087: stopInteraction();
088:
089: // Test by retrieving the same consumer
090: startInteraction();
091: group = getManager().getConsumerGroup("Foo");
092: assertNotNull(group);
093: assertEquals("Foo", group.getName());
094: assertEquals(Collections.EMPTY_LIST, new ArrayList(group
095: .getConsumers()));
096: stopInteraction();
097:
098: // Test by retrieving the consumer list
099: startInteraction();
100: Collection groups = getManager().getConsumerGroups();
101: assertNotNull(groups);
102: assertEquals(1, groups.size());
103: group = (ConsumerGroup) groups.iterator().next();
104: assertNotNull(group);
105: assertEquals("Foo", group.getName());
106: assertEquals(Collections.EMPTY_LIST, new ArrayList(group
107: .getConsumers()));
108: stopInteraction();
109: }
110:
111: public void testAddDuplicateGroup() throws Exception {
112: startInteraction();
113: getManager().createConsumerGroup("Foo");
114: try {
115: getManager().createConsumerGroup("Foo");
116: fail();
117: } catch (DuplicateRegistrationException expected) {
118: }
119: stopInteraction();
120: }
121:
122: public void testAddGroupThrowsIAE() throws Exception {
123: startInteraction();
124: try {
125: getManager().createConsumerGroup(null);
126: } catch (IllegalArgumentException expected) {
127: assertEquals(Collections.EMPTY_SET, new HashSet(
128: getManager().getConsumerGroups()));
129: }
130: stopInteraction();
131: }
132:
133: public void testRemoveGroup() throws Exception {
134: startInteraction();
135: getManager().createConsumerGroup("Foo");
136: stopInteraction();
137:
138: startInteraction();
139: getManager().removeConsumerGroup("Foo");
140: assertNull(getManager().getConsumerGroup("Foo"));
141: assertEquals(Collections.EMPTY_SET, new HashSet(getManager()
142: .getConsumerGroups()));
143: stopInteraction();
144: }
145:
146: public void testRemoveGroupThrowsIAE() throws Exception {
147: startInteraction();
148: try {
149: getManager().removeConsumerGroup(null);
150: } catch (IllegalArgumentException expected) {
151: }
152: stopInteraction();
153: }
154:
155: public void testRemoveNonExistingGroup() throws Exception {
156: startInteraction();
157: try {
158: getManager().removeConsumerGroup("Foo");
159: } catch (NoSuchRegistrationException expected) {
160: }
161: stopInteraction();
162: }
163:
164: public void testGetConsumerThrowsIAE() throws Exception {
165: startInteraction();
166: try {
167: ConsumerGroup group = getManager().createConsumerGroup(
168: "Foo");
169: group.getConsumer(null);
170: fail();
171: } catch (IllegalArgumentException expected) {
172: }
173: stopInteraction();
174: }
175:
176: public void testAddConsumer() throws Exception {
177: startInteraction();
178: ConsumerGroup group = getManager().createConsumerGroup("Foo");
179: stopInteraction();
180:
181: startInteraction();
182: Consumer consumer = getManager().createConsumer("Bar", "Bar");
183: group.addConsumer(consumer);
184: assertNotNull(consumer);
185: assertEquals("Bar", consumer.getName());
186: assertEquals(Collections.EMPTY_LIST, new ArrayList(consumer
187: .getRegistrations()));
188: assertEquals("Foo", consumer.getGroup().getName());
189: stopInteraction();
190:
191: // Test by retrieving the same consumer
192: startInteraction();
193: consumer = group.getConsumer("Bar");
194: assertNotNull(consumer);
195: assertEquals("Bar", consumer.getName());
196: assertEquals(Collections.EMPTY_LIST, new ArrayList(consumer
197: .getRegistrations()));
198: assertEquals("Foo", consumer.getGroup().getName());
199: stopInteraction();
200:
201: // Test by retrieving the consumer list
202: startInteraction();
203: Collection consumers = group.getConsumers();
204: assertNotNull(consumers);
205: assertEquals(1, consumers.size());
206: consumer = (Consumer) consumers.iterator().next();
207: assertNotNull(consumer);
208: assertEquals("Bar", consumer.getName());
209: assertEquals(Collections.EMPTY_LIST, new ArrayList(consumer
210: .getRegistrations()));
211: assertEquals("Foo", consumer.getGroup().getName());
212: stopInteraction();
213: }
214:
215: public void testAddDuplicateConsumer() throws Exception {
216: startInteraction();
217: ConsumerGroup group = getManager().createConsumerGroup("Foo");
218: Consumer consumer = getManager().createConsumer("Bar", "Bar");
219: group.addConsumer(consumer);
220: stopInteraction();
221:
222: startInteraction();
223: try {
224: group.addConsumer(consumer);
225: fail();
226: } catch (IllegalArgumentException expected) {
227: }
228: stopInteraction();
229: }
230:
231: public void testAddConsumerThrowsIAE() throws Exception {
232: startInteraction();
233: ConsumerGroup group = getManager().createConsumerGroup("Foo");
234: try {
235: group.addConsumer(null);
236: } catch (IllegalArgumentException expected) {
237: assertEquals(Collections.EMPTY_SET, new HashSet(group
238: .getConsumers()));
239: }
240: stopInteraction();
241: }
242:
243: public void testRemoveConsumer() throws Exception {
244: startInteraction();
245: ConsumerGroup group = getManager().createConsumerGroup("Foo");
246: Consumer consumer = getManager().createConsumer("Bar", "Bar");
247: group.addConsumer(consumer);
248: group.removeConsumer(consumer);
249: assertNull(group.getConsumer("Bar"));
250: assertEquals(Collections.EMPTY_SET, new HashSet(group
251: .getConsumers()));
252: stopInteraction();
253: }
254:
255: public void testRemoveConsumerThrowsIAE() throws Exception {
256: startInteraction();
257: ConsumerGroup group = getManager().createConsumerGroup("Foo");
258: try {
259: group.removeConsumer(null);
260: } catch (IllegalArgumentException expected) {
261: }
262: stopInteraction();
263: }
264:
265: public void testAddRegistration() throws Exception {
266: startInteraction();
267: ConsumerGroup group = getManager().createConsumerGroup("Foo");
268: Consumer consumer = getManager().createConsumer("Bar", "Bar");
269: group.addConsumer(consumer);
270: stopInteraction();
271:
272: startInteraction();
273: consumer = getManager().getConsumerById("Bar");
274: Registration reg1 = getManager().addRegistrationFor("Bar",
275: registrationProperties);
276: assertNotNull(reg1);
277: String regId = reg1.getId();
278: assertNotNull(regId);
279: assertEquals(consumer, reg1.getConsumer());
280: Map expectedProps = new HashMap();
281: expectedProps.put(new QName("prop1"), "value1");
282: expectedProps.put(new QName("prop2"), "value2");
283: assertEquals(expectedProps, reg1.getProperties());
284: stopInteraction();
285:
286: // Retrieve it from the list of consumer registrations
287: startInteraction();
288: consumer = getManager().getConsumerById("Bar");
289: Collection registrations = consumer.getRegistrations();
290: assertNotNull(registrations);
291: assertEquals(1, registrations.size());
292: Registration reg3 = (Registration) registrations.iterator()
293: .next();
294: assertEquals(regId, reg3.getId());
295: assertEquals(consumer, reg3.getConsumer());
296: assertEquals(expectedProps, reg3.getProperties());
297: stopInteraction();
298:
299: // Retrieve the same registration from the registry
300: startInteraction();
301: Registration reg2 = getManager().getRegistration(regId);
302: consumer = getManager().getConsumerById("Bar");
303: assertNotNull(reg2);
304: assertEquals(regId, reg2.getId());
305: assertEquals(consumer, reg2.getConsumer());
306: assertEquals(expectedProps, reg2.getProperties());
307: stopInteraction();
308: }
309:
310: public void testAddRegistrationThrowsIAE() throws Exception {
311: startInteraction();
312: ConsumerGroup group = getManager().createConsumerGroup("Foo");
313: Consumer consumer = getManager().createConsumer("Bar", "Bar");
314: group.addConsumer(consumer);
315:
316: try {
317: getManager().addRegistrationFor(consumer.getId(), null);
318: fail();
319: } catch (IllegalArgumentException expected) {
320: }
321: stopInteraction();
322: }
323:
324: public void testRemoveRegistrationThrowsIAE() throws Exception {
325: startInteraction();
326: try {
327: getManager().removeRegistration(null);
328: fail();
329: } catch (IllegalArgumentException expected) {
330: }
331: stopInteraction();
332: }
333:
334: public void testRemoveRegistration() throws Exception {
335: startInteraction();
336: ConsumerGroup group = getManager().createConsumerGroup("Foo");
337: Consumer consumer = getManager().createConsumer("Bar", "Bar");
338: group.addConsumer(consumer);
339: Registration reg = getManager().addRegistrationFor("Bar",
340: registrationProperties);
341: String regId = reg.getId();
342: getManager().removeRegistration(regId);
343: stopInteraction();
344:
345: // remove registration is the only method on RegistrationPersistenceManager that needs to "cascade"
346: // this is needed because there is no remove method on Consumer, hence the manager needs to remove the
347: // registration from its consumer since it's the only class that has access to the specific consumer impl
348: startInteraction();
349: consumer = getManager().getConsumerById("Bar");
350: Collection registrations = consumer.getRegistrations();
351: assertNotNull(registrations);
352: assertEquals(0, registrations.size());
353: stopInteraction();
354:
355: //
356: startInteraction();
357: assertEquals(null, getManager().getRegistration(regId));
358: stopInteraction();
359: }
360:
361: public void testBulkUpdateRegistrationProperties() throws Exception {
362: startInteraction();
363: ConsumerGroup group = getManager().createConsumerGroup("Foo");
364: Consumer consumer = getManager().createConsumer("Bar", "Bar");
365: group.addConsumer(consumer);
366: getManager().addRegistrationFor("Bar", registrationProperties);
367: stopInteraction();
368:
369: //
370: startInteraction();
371: consumer = getManager().getConsumerById("Bar");
372: Registration reg = (Registration) consumer.getRegistrations()
373: .iterator().next();
374: registrationProperties.remove(new QName("prop1"));
375: reg.updateProperties(registrationProperties);
376: assertEquals(Collections.singletonMap(new QName("prop2"),
377: "value2"), reg.getProperties());
378: stopInteraction();
379:
380: //
381: startInteraction();
382: consumer = getManager().getConsumerById("Bar");
383: reg = (Registration) consumer.getRegistrations().iterator()
384: .next();
385: assertEquals(Collections.singletonMap(new QName("prop2"),
386: "value2"), reg.getProperties());
387: registrationProperties.put(new QName("prop3"), "value3");
388: reg.updateProperties(registrationProperties);
389: assertEquals(MapBuilder.hashMap().put(new QName("prop2"),
390: "value2").put(new QName("prop3"), "value3").get(), reg
391: .getProperties());
392: stopInteraction();
393:
394: //
395: startInteraction();
396: consumer = getManager().getConsumerById("Bar");
397: reg = (Registration) consumer.getRegistrations().iterator()
398: .next();
399: assertEquals(MapBuilder.hashMap().put(new QName("prop2"),
400: "value2").put(new QName("prop3"), "value3").get(), reg
401: .getProperties());
402: stopInteraction();
403: }
404: }
|