KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > configuration > impl > ConfigurationBuilderImplTest


1 package org.objectweb.celtix.configuration.impl;
2
3 import java.util.Properties JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.easymock.EasyMock;
8 import org.objectweb.celtix.configuration.Configuration;
9 import org.objectweb.celtix.configuration.ConfigurationBuilder;
10 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
11 import org.objectweb.celtix.configuration.ConfigurationException;
12 import org.objectweb.celtix.configuration.ConfigurationMetadata;
13
14 public class ConfigurationBuilderImplTest extends TestCase {
15     
16     private static final String JavaDoc BUS_CONFIGURATION_URI = "http://celtix.objectweb.org/bus/bus-config";
17     private static final String JavaDoc HTTP_LISTENER_CONFIGURATION_URI =
18         "http://celtix.objectweb.org/bus/transports/http/http-listener-config";
19     private static final String JavaDoc HTTP_LISTENER_CONFIGURATION_ID = "http-listener.44959";
20     private static final String JavaDoc UNKNOWN_CONFIGURATION_URI =
21         "http://celtix.objectweb.org/unknown/unknown-config";
22     private static final String JavaDoc DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME =
23         TestProvider.class.getName();
24     private static final String JavaDoc DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY =
25         "org.objectweb.celtix.bus.configuration.ConfigurationProvider";
26     
27     
28     private String JavaDoc orgProviderClassname;
29     private String JavaDoc orgBuilderClassname;
30     
31     public void setUp() {
32         orgProviderClassname = System.getProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
33         System.setProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY,
34                            DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME);
35         orgBuilderClassname = System.getProperty(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY);
36         System.setProperty(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY,
37                            ConfigurationBuilderImpl.class.getName());
38         
39     }
40     
41     public void tearDown() {
42         if (null != orgProviderClassname) {
43             System.setProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY, orgProviderClassname);
44         } else {
45             Properties JavaDoc p = System.getProperties();
46             p.remove(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
47             System.setProperties(p);
48         }
49         
50         if (null != orgBuilderClassname) {
51             System.setProperty(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY,
52                                orgBuilderClassname);
53         } else {
54             Properties JavaDoc p = System.getProperties();
55             p.remove(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY);
56             System.setProperties(p);
57         }
58     }
59     
60     public void testGetBuilder() {
61         ConfigurationBuilder builder = ConfigurationBuilderFactory.getBuilder(null);
62         assertNotNull(builder);
63         assertTrue(builder instanceof ConfigurationBuilderImpl);
64     }
65     
66     public void testGetConfigurationUnknownNamespace() {
67         ConfigurationBuilder builder = new ConfigurationBuilderImpl();
68         try {
69             builder.getConfiguration(UNKNOWN_CONFIGURATION_URI, "celtix");
70         } catch (ConfigurationException ex) {
71             assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
72         }
73         Configuration parent = EasyMock.createMock(Configuration.class);
74         try {
75             builder.getConfiguration(UNKNOWN_CONFIGURATION_URI, "celtix", parent);
76         } catch (ConfigurationException ex) {
77             assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
78         }
79     }
80     
81     public void testGetAddModel() {
82         ConfigurationBuilder builder = ConfigurationBuilderFactory.getBuilder(null);
83         try {
84             builder.getModel(UNKNOWN_CONFIGURATION_URI);
85         } catch (ConfigurationException ex) {
86             assertEquals("UNKNOWN_NAMESPACE_EXC", ex.getCode());
87         }
88         
89         ConfigurationMetadata unknownModel = EasyMock.createMock(ConfigurationMetadata.class);
90         unknownModel.getNamespaceURI();
91         EasyMock.expectLastCall().andReturn(UNKNOWN_CONFIGURATION_URI);
92         EasyMock.replay(unknownModel);
93         builder.addModel(unknownModel);
94         assertSame(unknownModel, builder.getModel(UNKNOWN_CONFIGURATION_URI));
95         EasyMock.verify(unknownModel);
96     }
97     
98     public void testAddModel() throws Exception JavaDoc {
99         ConfigurationBuilder builder = ConfigurationBuilderFactory.getBuilder(null);
100         try {
101             builder.getModel("a.wsdl");
102         } catch (ConfigurationException ex) {
103             assertEquals("METADATA_RESOURCE_EXC", ex.getCode());
104         }
105     }
106     
107     public void testGetConfiguration() {
108         ConfigurationBuilder builder = new ConfigurationBuilderImpl();
109         ConfigurationMetadata model = EasyMock.createMock(ConfigurationMetadata.class);
110         model.getNamespaceURI();
111         EasyMock.expectLastCall().andReturn(BUS_CONFIGURATION_URI);
112         EasyMock.replay(model);
113         builder.addModel(model);
114         assertNull(builder.getConfiguration(BUS_CONFIGURATION_URI, "celtix"));
115         EasyMock.verify(model);
116         
117         model = EasyMock.createMock(ConfigurationMetadata.class);
118         model.getNamespaceURI();
119         EasyMock.expectLastCall().andReturn(HTTP_LISTENER_CONFIGURATION_URI);
120         EasyMock.replay(model);
121         builder.addModel(model);
122         Configuration parent = EasyMock.createMock(Configuration.class);
123         assertNull(builder.getConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
124                                             HTTP_LISTENER_CONFIGURATION_ID, parent));
125     }
126
127     public void testInvalidParentConfiguration() {
128         String JavaDoc id = "celtix";
129         ConfigurationBuilder builder = new ConfigurationBuilderImpl();
130         ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
131         model.setNamespaceURI(BUS_CONFIGURATION_URI);
132         model.setParentNamespaceURI(null);
133         builder.addModel(model);
134         model = new ConfigurationMetadataImpl();
135         model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
136         model.setParentNamespaceURI(BUS_CONFIGURATION_URI);
137         builder.addModel(model);
138         
139         Configuration parent = builder.buildConfiguration(BUS_CONFIGURATION_URI, id, null);
140         assertNotNull(parent);
141
142         try {
143             builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
144                                        HTTP_LISTENER_CONFIGURATION_ID, null);
145             fail("Did not throw expected exception");
146         } catch (ConfigurationException e) {
147             String JavaDoc expectedErrorMsg = "Configuration " + HTTP_LISTENER_CONFIGURATION_URI
148                 + " is not a valid top configuration.";
149             assertEquals("Unexpected exception message", expectedErrorMsg, e.getMessage());
150         } catch (Exception JavaDoc e) {
151             fail("Caught unexpected exception");
152         }
153     }
154
155     /*
156     public void testInvalidChildConfiguration() {
157         String id = "celtix";
158         ConfigurationBuilder builder = new ConfigurationBuilderImpl();
159         ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
160         model.setNamespaceURI(BUS_CONFIGURATION_URI);
161         model.setParentNamespaceURI(null);
162         builder.addModel(model);
163         model = new ConfigurationMetadataImpl();
164         model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
165         model.setParentNamespaceURI(BUS_CONFIGURATION_URI);
166         builder.addModel(model);
167         
168         Configuration parent = builder.buildConfiguration(BUS_CONFIGURATION_URI, id, null);
169         assertNotNull(parent);
170
171         //build a http configuration that is the child of bus config
172         Configuration wrongParent = builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
173                                        HTTP_LISTENER_CONFIGURATION_ID, parent);
174
175         assertNotNull(parent);
176
177         try {
178             builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
179                                        HTTP_LISTENER_CONFIGURATION_ID, wrongParent);
180             fail("Did not throw expected exception");
181         } catch (ConfigurationException e) {
182             String expectedErrorMsg = "Configuration " + HTTP_LISTENER_CONFIGURATION_URI
183                 + " is not a valid child configuration of " + HTTP_LISTENER_CONFIGURATION_URI + ".";
184             assertEquals("Unexpected exception message", expectedErrorMsg, e.getMessage());
185         } catch (Exception e) {
186             fail("Caught unexpected exception");
187         }
188     }
189     */

190
191     public void testBuildConfiguration() throws Exception JavaDoc {
192                                                        
193         String JavaDoc id = "celtix";
194         ConfigurationBuilder builder = new ConfigurationBuilderImpl();
195         ConfigurationMetadataImpl model = new ConfigurationMetadataImpl();
196         model.setNamespaceURI(BUS_CONFIGURATION_URI);
197         builder.addModel(model);
198         model = new ConfigurationMetadataImpl();
199         model.setNamespaceURI(HTTP_LISTENER_CONFIGURATION_URI);
200         builder.addModel(model);
201         Configuration parent = builder.buildConfiguration(BUS_CONFIGURATION_URI, id);
202         assertNotNull(parent);
203         Configuration child = builder.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI,
204                                                          HTTP_LISTENER_CONFIGURATION_ID);
205         assertNotNull(child);
206     }
207 }
208
Popular Tags