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.RegistrationException;
031: import org.jboss.portal.registration.RegistrationManager;
032: import org.jboss.portal.registration.RegistrationPolicy;
033: import org.jboss.portal.registration.RegistrationStatus;
034: import org.jboss.portal.registration.impl.RegistrationManagerImpl;
035: import org.jboss.portal.registration.impl.RegistrationPersistenceManagerImpl;
036: import org.jboss.portal.registration.policies.DefaultRegistrationPolicy;
037:
038: import javax.xml.namespace.QName;
039: import java.util.Collection;
040: import java.util.HashMap;
041: import java.util.Map;
042:
043: /**
044: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
045: * @version $Revision: 8784 $
046: * @since 2.6
047: */
048: public class RegistrationManagerTestCase extends TestCase {
049:
050: private RegistrationManager manager;
051: private Map registrationProperties;
052:
053: protected void setUp() throws Exception {
054: manager = new RegistrationManagerImpl();
055: // todo: policy will need access to registration configuration information to validate properties
056: RegistrationPolicy policy = new DefaultRegistrationPolicy();
057: manager.setPolicy(policy);
058: manager
059: .setPersistenceManager(new RegistrationPersistenceManagerImpl());
060: policy.setManager(manager);
061:
062: //todo: registration properties setup will need to be updated when property validation is implemented
063: registrationProperties = new HashMap();
064: registrationProperties.put(new QName("prop1"), "value1");
065: registrationProperties.put(new QName("prop2"), "value2");
066: }
067:
068: public void testPolicy() {
069: RegistrationPolicy policy = manager.getPolicy();
070: assertNotNull(policy);
071: assertEquals(manager, policy.getManager());
072: }
073:
074: public void testAddRegistrationTo() throws Exception {
075: Registration registration = manager.addRegistrationTo(
076: "consumerName", registrationProperties, true);
077: assertNotNull(registration);
078: assertNotNull(registration.getId());
079:
080: Consumer consumer = manager
081: .getConsumerByIdentity("consumerName");
082: assertNotNull(consumer); // default policy: name == identity
083: assertEquals(consumer, registration.getConsumer());
084:
085: String registrationHandle = registration
086: .getRegistrationHandle();
087: assertNotNull(registrationHandle);
088: assertEquals(consumer, manager
089: .getConsumerFor(registrationHandle));
090: }
091:
092: public void testAddRegistrationToInexistentConsumer()
093: throws RegistrationException {
094: try {
095: manager.addRegistrationTo("consumerName",
096: registrationProperties, false);
097: fail("Should have failed: consumer does not exist");
098: } catch (NoSuchRegistrationException expected) {
099: }
100:
101: assertNull(manager.getConsumerByIdentity("consumerName")); // default policy: name == identity
102: }
103:
104: public void testGetConsumerForNullRegistrationHandle()
105: throws Exception {
106: try {
107: manager.getConsumerFor(null);
108: fail("Should have failed: cannot find a consumer for a null registration handle");
109: } catch (IllegalArgumentException e) {
110: }
111: }
112:
113: public void testCreateConsumer() throws Exception {
114: String name = "consumerName";
115: Consumer consumer = manager.createConsumer(name);
116: assertNotNull(consumer);
117: assertEquals(name, consumer.getName());
118: assertNotNull(consumer.getId());
119: assertNull(consumer.getGroup());
120:
121: Collection consumers = manager.getConsumers();
122: assertEquals(1, consumers.size());
123: assertTrue(consumers.contains(consumer));
124: assertEquals(consumer, manager.getConsumerByIdentity(name)); // default policy: name == identity
125:
126: try {
127: consumers.add(consumer);
128: fail("Shouldn't be possible to directly modify consumer collection");
129: } catch (UnsupportedOperationException expected) {
130: }
131: }
132:
133: public void testCreateConsumerWithGroupFromPolicy()
134: throws RegistrationException {
135: // use a different policy: now specifies that when creating a consumer, it should be added to a group with the same name
136: DefaultRegistrationPolicy policy = new DefaultRegistrationPolicy() {
137: public String getAutomaticGroupNameFor(String consumerName) {
138: return "group_" + consumerName;
139: }
140: };
141: manager.setPolicy(policy);
142: policy.setManager(manager);
143:
144: String name = "name";
145: Consumer consumer = manager.createConsumer(name);
146: assertNotNull(consumer);
147:
148: ConsumerGroup group = manager.getConsumerGroup("group_" + name);
149: assertNotNull(group);
150: assertEquals(group, consumer.getGroup());
151: assertTrue(group.getConsumers().contains(consumer));
152: }
153:
154: public void testCreateDuplicateConsumer()
155: throws RegistrationException {
156: String name = "name";
157: assertNotNull(manager.createConsumer(name));
158:
159: try {
160: manager.createConsumer(name);
161: fail("Should have failed when trying to create a consumer with an existing name");
162: } catch (DuplicateRegistrationException expected) {
163: }
164: }
165:
166: public void testAddAutomaticallyCreatedConsumerToInexistentGroup()
167: throws RegistrationException {
168: try {
169: manager.addConsumerToGroupNamed("foo", "bar", false, true);
170: fail("Shouldn't be possible to add a consumer to an inexistent group without first creating it");
171: } catch (NoSuchRegistrationException expected) {
172: }
173:
174: assertNull(manager.getConsumerByIdentity("foo"));
175: assertNull(manager.getConsumerGroup("bar"));
176: }
177:
178: public void testAddInexistentConsumerToAutomaticallyCreatedGroup()
179: throws RegistrationException {
180: try {
181: manager.addConsumerToGroupNamed("foo", "bar", true, false);
182: fail("Shouldn't be possible to add an inexistent consumer to a group without first creating it");
183: } catch (NoSuchRegistrationException expected) {
184: }
185:
186: assertNull(manager.getConsumerByIdentity("foo"));
187: assertNull(manager.getConsumerGroup("bar"));
188: }
189:
190: public void testAddInexistentConsumerToGroup()
191: throws RegistrationException {
192: manager.createConsumerGroup("bar");
193: try {
194: manager.addConsumerToGroupNamed("foo", "bar", false, false);
195: fail("Shouldn't be possible to add an inexistent consumer to a group without first creating it");
196: } catch (NoSuchRegistrationException expected) {
197: }
198:
199: assertNull(manager.getConsumerByIdentity("foo"));
200: assertNotNull(manager.getConsumerGroup("bar"));
201: }
202:
203: public void testAddInexistentConsumerToInexistentGroup()
204: throws RegistrationException {
205: try {
206: manager.addConsumerToGroupNamed("foo", "bar", false, false);
207: fail("Shouldn't be possible to add a consumer to an inexistent group without first creating it");
208: } catch (NoSuchRegistrationException expected) {
209: }
210:
211: assertNull(manager.getConsumerByIdentity("foo"));
212: assertNull(manager.getConsumerGroup("bar"));
213: }
214:
215: public void testAddConsumerToGroup() throws Exception {
216: String groupName = "group";
217: String consumerName = "consumer";
218: Consumer consumer = manager.addConsumerToGroupNamed(
219: consumerName, groupName, true, true);
220:
221: Consumer consumer1 = manager
222: .getConsumerByIdentity(consumerName);
223: assertNotNull(consumer1);
224: assertEquals(consumer1, consumer); // default policy: identity == name
225:
226: ConsumerGroup group = manager.getConsumerGroup(groupName);
227: assertNotNull(group);
228: assertEquals(group, consumer.getGroup());
229: }
230:
231: public void testCreateConsumerGroup() throws Exception {
232: String groupName = "name";
233: ConsumerGroup group = manager.createConsumerGroup(groupName);
234: assertNotNull(group);
235: assertEquals(groupName, group.getName());
236:
237: Collection groups = manager.getConsumerGroups();
238: assertEquals(1, groups.size());
239: assertTrue(groups.contains(group));
240: assertEquals(group, manager.getConsumerGroup(groupName));
241:
242: try {
243: groups.add(group);
244: fail("Shouldn't be possible to directly modify group collection");
245: } catch (UnsupportedOperationException expected) {
246: }
247: }
248:
249: public void testRemoveConsumerGroup() throws RegistrationException {
250: String groupName = "name";
251: ConsumerGroup group = manager.createConsumerGroup(groupName);
252: manager.removeConsumerGroup(group);
253: assertNull(manager.getConsumerGroup(groupName));
254:
255: manager.createConsumerGroup(groupName);
256: manager.removeConsumerGroup(groupName);
257: assertNull(manager.getConsumerGroup(groupName));
258: }
259:
260: public void testCascadeRemovalOnConsumerGroupRemoval()
261: throws Exception {
262: String groupName = "group";
263: String consumerName = "consumer";
264: Consumer consumer = manager.addConsumerToGroupNamed(
265: consumerName, groupName, true, true);
266: String consumerIdentity = consumer.getId();
267:
268: Registration reg = manager.addRegistrationTo(consumerName,
269: registrationProperties, false);
270: String handle = reg.getRegistrationHandle();
271:
272: ConsumerGroup group = manager.getConsumerGroup(groupName);
273:
274: manager.removeConsumerGroup(group);
275: assertNull(manager.getConsumerByIdentity(consumerIdentity));
276: assertNull(manager.getRegistration(handle));
277: }
278:
279: public void testCascadeRemovalOnConsumerRemoval() throws Exception {
280: String consumerName = "consumer";
281: Consumer consumer = manager.createConsumer(consumerName);
282: String consumerIdentity = consumer.getId();
283:
284: Registration reg = manager.addRegistrationTo(consumerName,
285: registrationProperties, false);
286: String handle = reg.getRegistrationHandle();
287:
288: manager.removeConsumer(consumer);
289: assertNull(manager.getConsumerByIdentity(consumerIdentity));
290: assertNull(manager.getRegistration(handle));
291: }
292:
293: public void testRemoveSingleRegistration() throws Exception {
294: String consumerName = "consumer";
295: Consumer consumer = manager.createConsumer(consumerName);
296:
297: Registration reg = manager.addRegistrationTo(consumerName,
298: registrationProperties, false);
299: String handle = reg.getRegistrationHandle();
300:
301: assertTrue(consumer.getRegistrations().contains(reg));
302:
303: // set the consumer status to valid
304: consumer.setStatus(RegistrationStatus.VALID);
305:
306: manager.removeRegistration(handle);
307: assertTrue(!consumer.getRegistrations().contains(reg));
308: assertNull(manager.getRegistration(handle));
309:
310: // since the consumer doesn't have any registration anymore, its status should become pending
311: assertEquals(RegistrationStatus.PENDING, consumer.getStatus());
312:
313: // shouldn't be possible anymore to retrieve the consumer from the registration handle
314: assertNull(manager.getConsumerFor(handle));
315: }
316:
317: public void testRemoveRegistrationOnConsumerWithOtherRegistrations()
318: throws Exception {
319: String consumerName = "consumer";
320: Consumer consumer = manager.createConsumer(consumerName);
321:
322: Registration reg = manager.addRegistrationTo(consumerName,
323: registrationProperties, false);
324: String handle = reg.getRegistrationHandle();
325:
326: // todo: this won't work anymore when properties validation is implemented
327: registrationProperties.put(new QName("prop3"), "value3");
328: Registration r2 = manager.addRegistrationTo(consumerName,
329: registrationProperties, false);
330:
331: RegistrationStatus status = consumer.getStatus();
332:
333: manager.removeRegistration(handle);
334:
335: // consumer status shouldn't have changed
336: assertEquals(status, consumer.getStatus());
337:
338: // should still be able to retrieve consumer from r2 handle
339: assertEquals(consumer, manager.getConsumerFor(r2
340: .getRegistrationHandle()));
341:
342: // ... but not from handle
343: assertNull(manager.getConsumerFor(handle));
344: }
345:
346: public void testAddRegistrationWithInvalidRegistrationProperties()
347: throws Exception {
348: // todo: implement
349: }
350:
351: public void testRemoveInexistentRegistration()
352: throws RegistrationException {
353: try {
354: manager.removeRegistration((Registration) null);
355: fail("Should be possible to remove a null registration");
356: } catch (IllegalArgumentException expected) {
357: }
358:
359: try {
360: manager.removeRegistration((String) null);
361: fail("Should be possible to remove a registration with a null handle");
362: } catch (IllegalArgumentException expected) {
363: }
364:
365: try {
366: manager.removeRegistration("");
367: fail("Should be possible to remove a registration with an empty handle");
368: } catch (IllegalArgumentException expected) {
369: }
370:
371: try {
372: manager.removeRegistration("doesn't exist");
373: fail("Should be possible to remove a registration with an invalid handle");
374: } catch (NoSuchRegistrationException expected) {
375: }
376: }
377:
378: public void testClear() throws Exception {
379: manager.createConsumer("c1");
380: manager.createConsumer("c2");
381: manager.addConsumerToGroupNamed("c1g1", "g1", true, true);
382: manager.createConsumerGroup("g2");
383: Registration r1 = manager.addRegistrationTo("c1",
384: registrationProperties, false);
385: Registration r2 = manager.addRegistrationTo("c3",
386: registrationProperties, true);
387:
388: manager.clear();
389: assertTrue(manager.getConsumerGroups().isEmpty());
390: assertTrue(manager.getConsumers().isEmpty());
391: assertNull(manager.getRegistration(r1.getRegistrationHandle()));
392: assertNull(manager.getRegistration(r2.getRegistrationHandle()));
393: }
394: }
|