001: package org.objectweb.celtix.bus.configuration.spring;
002:
003: import java.math.BigInteger;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.util.List;
007: import java.util.Properties;
008:
009: import junit.framework.TestCase;
010:
011: import org.objectweb.celtix.bus.configuration.LeafConfigurationBuilder;
012: import org.objectweb.celtix.bus.configuration.TopConfigurationBuilder;
013: import org.objectweb.celtix.configuration.Configuration;
014: import org.objectweb.celtix.configuration.ConfigurationException;
015: import org.objectweb.celtix.configuration.ConfigurationProvider;
016: import org.objectweb.celtix.configuration.types.StringListType;
017:
018: public class ConfigurationProviderImplTest extends TestCase {
019:
020: private String originalConfigFile;
021:
022: public void setUp() {
023: originalConfigFile = System
024: .getProperty(ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME);
025: }
026:
027: public void tearDown() {
028: if (null == originalConfigFile) {
029: Properties properties = System.getProperties();
030: properties
031: .remove(ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME);
032: System.setProperties(properties);
033: } else {
034: System
035: .setProperty(
036: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
037: originalConfigFile);
038: }
039: ConfigurationProviderImpl.getBeanFactories().clear();
040: }
041:
042: public void testInvalidBeanDefinitionsResourceURL() {
043: System.setProperty(
044: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
045: "resources\top1.xml");
046: try {
047: new TopConfigurationBuilder().build("top");
048: } catch (ConfigurationException ex) {
049: assertEquals("MALFORMED_URL_PROPERTY", ex.getCode());
050: }
051: }
052:
053: public void testNoBeanDefinitionsFile() {
054: Configuration top = new TopConfigurationBuilder().build("top");
055: List<ConfigurationProvider> providers = top.getProviders();
056: assertEquals(1, providers.size());
057: assertTrue(providers.get(0) instanceof ConfigurationProviderImpl);
058: ConfigurationProviderImpl cpi = (ConfigurationProviderImpl) providers
059: .get(0);
060: assertNull(cpi.getBean());
061: }
062:
063: public void testInvalidBeanDefinitionFile() {
064: URL url = ConfigurationProviderImplTest.class
065: .getResource("resources/top-invalid.xml");
066: System.setProperty(
067: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
068: url.toExternalForm());
069: new TopConfigurationBuilder().build("top");
070: assertNull(ConfigurationProviderImpl.getBeanFactories()
071: .get(url));
072: }
073:
074: public void testBeanClassNotFound() {
075:
076: URL url = ConfigurationProviderImplTest.class
077: .getResource("resources/top-wrong-class.xml");
078: System.setProperty(
079: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
080: url.toExternalForm());
081:
082: // The non-existing class is detected when the bean factory is initialised - not
083: // later when the bean is created.
084:
085: new TopConfigurationBuilder().build("top");
086: assertNull(ConfigurationProviderImpl.getBeanFactories()
087: .get(url));
088: }
089:
090: public void testNoSuchBean() {
091:
092: URL url = ConfigurationProviderImplTest.class
093: .getResource("resources/top1.xml");
094: System.setProperty(
095: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
096: url.toExternalForm());
097:
098: Configuration top = new TopConfigurationBuilder().build("top2");
099:
100: List<ConfigurationProvider> providers = top.getProviders();
101: assertEquals(1, providers.size());
102:
103: ConfigurationProviderImpl cpi = (ConfigurationProviderImpl) providers
104: .get(0);
105: assertNull(cpi.getBean());
106:
107: }
108:
109: public void testDefaultBeanCreation() throws MalformedURLException {
110: URL url = ConfigurationProviderImplTest.class
111: .getResource("resources/top1.xml");
112: System.setProperty(
113: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
114: url.toExternalForm());
115:
116: Configuration top = new TopConfigurationBuilder().build("top1");
117: List<ConfigurationProvider> providers = top.getProviders();
118: assertEquals(1, providers.size());
119: assertTrue(providers.get(0) instanceof ConfigurationProviderImpl);
120: }
121:
122: public void testBeanCreationUsingValueAsText()
123: throws MalformedURLException {
124: URL url = ConfigurationProviderImplTest.class
125: .getResource("resources/top2.xml");
126: System.setProperty(
127: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
128: url.toExternalForm());
129: Configuration top = new TopConfigurationBuilder().build("top2");
130:
131: List<ConfigurationProvider> providers = top.getProviders();
132: assertEquals(1, providers.size());
133: assertTrue(providers.get(0) instanceof ConfigurationProviderImpl);
134:
135: ConfigurationProviderImpl cpi = (ConfigurationProviderImpl) providers
136: .get(0);
137: Object o;
138:
139: o = cpi.getObject("booleanItem");
140: assertTrue(o instanceof Boolean);
141: assertTrue(!((Boolean) o).booleanValue());
142:
143: o = cpi.getObject("integerItem");
144: assertTrue(o instanceof BigInteger);
145: assertEquals(1997, ((BigInteger) o).intValue());
146:
147: o = cpi.getObject("longItem");
148: assertNull(o);
149:
150: o = cpi.getObject("doubleItem");
151: assertTrue(o instanceof Double);
152: assertTrue(Math.abs(((Double) o).doubleValue() - 9876.54321) < 0.5E-5);
153:
154: o = cpi.getObject("stringItem");
155: assertTrue(o instanceof String);
156: assertEquals("not the default", (String) o);
157:
158: try {
159: cpi.getObject("noItem");
160: } catch (ConfigurationException ex) {
161: assertEquals("BEAN_INCOVATION_EXC", ex.getCode());
162: }
163: }
164:
165: public void testBeanCreationUsingValueAsElements()
166: throws MalformedURLException {
167: URL url = ConfigurationProviderImplTest.class
168: .getResource("resources/top2.xml");
169: System.setProperty(
170: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
171: url.toExternalForm());
172: Configuration top = new TopConfigurationBuilder()
173: .build("top22");
174:
175: List<ConfigurationProvider> providers = top.getProviders();
176: assertEquals(1, providers.size());
177: assertTrue(providers.get(0) instanceof ConfigurationProviderImpl);
178:
179: ConfigurationProviderImpl cpi = (ConfigurationProviderImpl) providers
180: .get(0);
181: Object o;
182:
183: o = cpi.getObject("booleanItem");
184: assertTrue(o instanceof Boolean);
185: assertTrue(!((Boolean) o).booleanValue());
186:
187: o = cpi.getObject("integerItem");
188: assertTrue(o instanceof BigInteger);
189: assertEquals(1997, ((BigInteger) o).intValue());
190:
191: o = cpi.getObject("longItem");
192: assertTrue(o instanceof Long);
193: assertEquals(99, ((Long) o).longValue());
194:
195: o = cpi.getObject("doubleItem");
196: assertTrue(o instanceof Double);
197: assertTrue(Math.abs(((Double) o).doubleValue() - 9876.54321) < 0.5E-5);
198:
199: o = cpi.getObject("stringItem");
200: assertTrue(o instanceof String);
201: assertEquals("not the default", (String) o);
202:
203: o = cpi.getObject("stringListItem");
204: assertTrue(o instanceof StringListType);
205:
206: List<String> l = ((StringListType) o).getItem();
207: assertNotNull(l);
208: assertEquals(2, l.size());
209: assertEquals("something", l.get(0));
210: assertEquals("else", l.get(1));
211:
212: }
213:
214: public void testBeanCreationSimpleHierarchy() {
215: URL url = ConfigurationProviderImplTest.class
216: .getResource("resources/top2.xml");
217: System.setProperty(
218: ConfigurationProviderImpl.CONFIG_FILE_PROPERTY_NAME,
219: url.toExternalForm());
220:
221: Configuration top = new TopConfigurationBuilder().build("top2");
222: Configuration leaf = new LeafConfigurationBuilder().build(top,
223: "leaf");
224:
225: List<ConfigurationProvider> providers = leaf.getProviders();
226: assertEquals(1, providers.size());
227:
228: Object o = providers.get(0).getObject("stringLeafItem");
229: assertTrue(o instanceof String);
230: assertEquals("Don't fear the reaper", (String) o);
231:
232: o = providers.get(0).getObject("longLeafItemNoDefault");
233: assertEquals(99, ((Long) o).longValue());
234: }
235:
236: }
|