001: /*
002: * Copyright (c) 2007, Your Corporation. All Rights Reserved.
003: */
004:
005: package org.jboss.portal.test.registration;
006:
007: import junit.framework.TestCase;
008: import org.jboss.portal.registration.PropertyDescription;
009: import org.jboss.portal.registration.RegistrationException;
010: import org.jboss.portal.registration.RegistrationManager;
011: import org.jboss.portal.registration.impl.RegistrationManagerImpl;
012: import org.jboss.portal.registration.impl.RegistrationPersistenceManagerImpl;
013: import org.jboss.portal.registration.policies.DefaultRegistrationPolicy;
014: import org.jboss.portal.registration.policies.DefaultRegistrationPropertyValidator;
015:
016: import javax.xml.namespace.QName;
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: /**
022: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
023: * @version $Revision: 9180 $
024: * @since 2.6.3
025: */
026: public class DefaultRegistrationPolicyTestCase extends TestCase {
027: DefaultRegistrationPolicy policy;
028: Map registrationProperties;
029: HashMap<QName, PropertyDescription> expectations;
030: private static final String CONSUMER = "consumer";
031: private static final QName PROP1 = new QName("prop1");
032: private static final QName PROP2 = new QName("prop2");
033: private static final QName PROP3 = new QName("prop3");
034:
035: @Override
036: protected void setUp() throws Exception {
037: policy = new DefaultRegistrationPolicy();
038:
039: policy.setValidator(new DefaultRegistrationPropertyValidator());
040:
041: RegistrationManager manager = new RegistrationManagerImpl();
042: manager.setPolicy(policy);
043: manager
044: .setPersistenceManager(new RegistrationPersistenceManagerImpl());
045: manager.createConsumer(CONSUMER);
046:
047: policy.setManager(manager);
048:
049: registrationProperties = new HashMap();
050: registrationProperties.put(PROP1, "value1");
051: registrationProperties.put(PROP2, "value2");
052:
053: expectations = new HashMap<QName, PropertyDescription>();
054: policy.setExpectations(expectations);
055: }
056:
057: public void testValidateRegistrationDataForNull()
058: throws RegistrationException {
059: try {
060: policy.validateRegistrationDataFor(null, "foo");
061: fail("null data cannot be validated");
062: } catch (IllegalArgumentException e) {
063: // expected
064: }
065:
066: try {
067: policy.validateRegistrationDataFor(Collections.EMPTY_MAP,
068: null);
069: fail("null data cannot be validated");
070: } catch (IllegalArgumentException e) {
071: // expected
072: }
073: }
074:
075: public void testValidateRegistrationDataForInexistentConsumer() {
076: try {
077: policy.validateRegistrationDataFor(Collections.EMPTY_MAP,
078: "foo");
079: } catch (RegistrationException e) {
080: fail("Should be possible to validate information for inexistent consumer (otherwise, how would we register!)");
081: }
082: }
083:
084: public void testValidateRegistrationDataMissingProps() {
085: expectations.put(PROP1, new TestPropertyDescription(PROP1));
086: expectations.put(PROP2, new TestPropertyDescription(PROP2));
087: expectations.put(PROP3, new TestPropertyDescription(PROP3));
088:
089: try {
090: policy.validateRegistrationDataFor(registrationProperties,
091: CONSUMER);
092: fail("Missing prop3 should have been detected");
093: } catch (RegistrationException e) {
094: assertTrue(e.getLocalizedMessage().contains("prop3"));
095: }
096: }
097:
098: public void testValidateRegistrationDataExtraProps() {
099: expectations.put(PROP1, new TestPropertyDescription(PROP1));
100:
101: try {
102: policy.validateRegistrationDataFor(registrationProperties,
103: CONSUMER);
104: fail("Extra prop2 should have been detected");
105: } catch (RegistrationException e) {
106: assertTrue(e.getLocalizedMessage().contains("prop2"));
107: }
108: }
109:
110: public void testValidateRegistrationDataInvalidValue() {
111: expectations.put(PROP1, new TestPropertyDescription(PROP1));
112:
113: registrationProperties.remove(PROP2);
114: registrationProperties.put(PROP1, null);
115:
116: try {
117: policy.validateRegistrationDataFor(registrationProperties,
118: CONSUMER);
119: fail("Should have detected null value for prop1");
120: } catch (RegistrationException e) {
121: assertTrue(e.getLocalizedMessage().contains("prop1"));
122: }
123: }
124:
125: static class TestPropertyDescription implements PropertyDescription {
126: private QName name;
127: private static final QName TYPE = new QName("type");
128:
129: TestPropertyDescription(QName name) {
130: this .name = name;
131: }
132:
133: public QName getName() {
134: return name;
135: }
136:
137: public QName getType() {
138: return TYPE;
139: }
140: }
141: }
|