001: package org.objectweb.celtix.configuration.impl;
002:
003: import java.util.Properties;
004:
005: import junit.framework.TestCase;
006:
007: import org.easymock.EasyMock;
008: import org.objectweb.celtix.configuration.Configuration;
009: import org.objectweb.celtix.configuration.ConfigurationBuilder;
010: import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
011: import org.objectweb.celtix.configuration.ConfigurationException;
012: import org.objectweb.celtix.configuration.ConfigurationMetadata;
013:
014: public class ConfigurationBuilderImplTest extends TestCase {
015:
016: private static final String BUS_CONFIGURATION_URI = "http://celtix.objectweb.org/bus/bus-config";
017: private static final String HTTP_LISTENER_CONFIGURATION_URI = "http://celtix.objectweb.org/bus/transports/http/http-listener-config";
018: private static final String HTTP_LISTENER_CONFIGURATION_ID = "http-listener.44959";
019: private static final String UNKNOWN_CONFIGURATION_URI = "http://celtix.objectweb.org/unknown/unknown-config";
020: private static final String DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME = TestProvider.class
021: .getName();
022: private static final String DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY = "org.objectweb.celtix.bus.configuration.ConfigurationProvider";
023:
024: private String orgProviderClassname;
025: private String orgBuilderClassname;
026:
027: public void setUp() {
028: orgProviderClassname = System
029: .getProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
030: System.setProperty(
031: DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY,
032: DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME);
033: orgBuilderClassname = System
034: .getProperty(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY);
035: System
036: .setProperty(
037: ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY,
038: ConfigurationBuilderImpl.class.getName());
039:
040: }
041:
042: public void tearDown() {
043: if (null != orgProviderClassname) {
044: System.setProperty(
045: DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY,
046: orgProviderClassname);
047: } else {
048: Properties p = System.getProperties();
049: p.remove(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
050: System.setProperties(p);
051: }
052:
053: if (null != orgBuilderClassname) {
054: System
055: .setProperty(
056: ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY,
057: orgBuilderClassname);
058: } else {
059: Properties p = System.getProperties();
060: p
061: .remove(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY);
062: System.setProperties(p);
063: }
064: }
065:
066: public void testGetBuilder() {
067: ConfigurationBuilder builder = ConfigurationBuilderFactory
068: .getBuilder(null);
069: assertNotNull(builder);
070: assertTrue(builder instanceof ConfigurationBuilderImpl);
071: }
072:
073: public void testGetConfigurationUnknownNamespace() {
074: ConfigurationBuilder builder = new ConfigurationBuilderImpl();
075: try {
076: builder.getConfiguration(UNKNOWN_CONFIGURATION_URI,
077: "celtix");
078: } catch (ConfigurationException ex) {
079: assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
080: }
081: Configuration parent = EasyMock.createMock(Configuration.class);
082: try {
083: builder.getConfiguration(UNKNOWN_CONFIGURATION_URI,
084: "celtix", parent);
085: } catch (ConfigurationException ex) {
086: assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
087: }
088: }
089:
090: public void testGetAddModel() {
091: ConfigurationBuilder builder = ConfigurationBuilderFactory
092: .getBuilder(null);
093: try {
094: builder.getModel(UNKNOWN_CONFIGURATION_URI);
095: } catch (ConfigurationException ex) {
096: assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
097: }
098:
099: ConfigurationMetadata unknownModel = EasyMock
100: .createMock(ConfigurationMetadata.class);
101: unknownModel.getNamespaceURI();
102: EasyMock.expectLastCall().andReturn(UNKNOWN_CONFIGURATION_URI);
103: EasyMock.replay(unknownModel);
104: builder.addModel(unknownModel);
105: assertSame(unknownModel, builder
106: .getModel(UNKNOWN_CONFIGURATION_URI));
107: EasyMock.verify(unknownModel);
108: }
109:
110: public void testAddModel() throws Exception {
111: ConfigurationBuilder builder = ConfigurationBuilderFactory
112: .getBuilder(null);
113: try {
114: builder.getModel("a.wsdl");
115: } catch (ConfigurationException ex) {
116: assertEquals("METADATA_RESOURCE_EXC", ex.getCode());
117: }
118: }
119:
120: public void testGetConfiguration() {
121: ConfigurationBuilder builder = new ConfigurationBuilderImpl();
122: ConfigurationMetadata model = EasyMock
123: .createMock(ConfigurationMetadata.class);
124: model.getNamespaceURI();
125: EasyMock.expectLastCall().andReturn(BUS_CONFIGURATION_URI);
126: EasyMock.replay(model);
127: builder.addModel(model);
128: assertNull(builder.getConfiguration(BUS_CONFIGURATION_URI,
129: "celtix"));
130: EasyMock.verify(model);
131:
132: model = EasyMock.createMock(ConfigurationMetadata.class);
133: model.getNamespaceURI();
134: EasyMock.expectLastCall().andReturn(
135: HTTP_LISTENER_CONFIGURATION_URI);
136: EasyMock.replay(model);
137: builder.addModel(model);
138: Configuration parent = EasyMock.createMock(Configuration.class);
139: assertNull(builder.getConfiguration(
140: HTTP_LISTENER_CONFIGURATION_URI,
141: HTTP_LISTENER_CONFIGURATION_ID, parent));
142: }
143:
144: public void testInvalidParentConfiguration() {
145: String id = "celtix";
146: ConfigurationBuilder builder = new ConfigurationBuilderImpl();
147: ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
148: model.setNamespaceURI(BUS_CONFIGURATION_URI);
149: model.setParentNamespaceURI(null);
150: builder.addModel(model);
151: model = new ConfigurationMetadataImpl();
152: model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
153: model.setParentNamespaceURI(BUS_CONFIGURATION_URI);
154: builder.addModel(model);
155:
156: Configuration parent = builder.buildConfiguration(
157: BUS_CONFIGURATION_URI, id, null);
158: assertNotNull(parent);
159:
160: try {
161: builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
162: HTTP_LISTENER_CONFIGURATION_ID, null);
163: fail("Did not throw expected exception");
164: } catch (ConfigurationException e) {
165: String expectedErrorMsg = "Configuration "
166: + HTTP_LISTENER_CONFIGURATION_URI
167: + " is not a valid top configuration.";
168: assertEquals("Unexpected exception message",
169: expectedErrorMsg, e.getMessage());
170: } catch (Exception e) {
171: fail("Caught unexpected exception");
172: }
173: }
174:
175: /*
176: public void testInvalidChildConfiguration() {
177: String id = "celtix";
178: ConfigurationBuilder builder = new ConfigurationBuilderImpl();
179: ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
180: model.setNamespaceURI(BUS_CONFIGURATION_URI);
181: model.setParentNamespaceURI(null);
182: builder.addModel(model);
183: model = new ConfigurationMetadataImpl();
184: model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
185: model.setParentNamespaceURI(BUS_CONFIGURATION_URI);
186: builder.addModel(model);
187:
188: Configuration parent = builder.buildConfiguration(BUS_CONFIGURATION_URI, id, null);
189: assertNotNull(parent);
190:
191: //build a http configuration that is the child of bus config
192: Configuration wrongParent = builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
193: HTTP_LISTENER_CONFIGURATION_ID, parent);
194:
195: assertNotNull(parent);
196:
197: try {
198: builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
199: HTTP_LISTENER_CONFIGURATION_ID, wrongParent);
200: fail("Did not throw expected exception");
201: } catch (ConfigurationException e) {
202: String expectedErrorMsg = "Configuration " + HTTP_LISTENER_CONFIGURATION_URI
203: + " is not a valid child configuration of " + HTTP_LISTENER_CONFIGURATION_URI + ".";
204: assertEquals("Unexpected exception message", expectedErrorMsg, e.getMessage());
205: } catch (Exception e) {
206: fail("Caught unexpected exception");
207: }
208: }
209: */
210:
211: public void testBuildConfiguration() throws Exception {
212:
213: String id = "celtix";
214: ConfigurationBuilder builder = new ConfigurationBuilderImpl();
215: ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
216: model.setNamespaceURI(BUS_CONFIGURATION_URI);
217: builder.addModel(model);
218: model = new ConfigurationMetadataImpl();
219: model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
220: builder.addModel(model);
221: Configuration parent = builder.buildConfiguration(
222: BUS_CONFIGURATION_URI, id);
223: assertNotNull(parent);
224: Configuration child = builder.buildConfiguration(
225: HTTP_LISTENER_CONFIGURATION_URI,
226: HTTP_LISTENER_CONFIGURATION_ID);
227: assertNotNull(child);
228: }
229: }
|