001: package org.objectweb.celtix.bus.configuration;
002:
003: import java.io.IOException;
004: import java.lang.reflect.Method;
005: import java.math.BigInteger;
006: import java.util.Collection;
007: import java.util.List;
008:
009: import junit.framework.TestCase;
010:
011: import org.easymock.classextension.EasyMock;
012: import org.objectweb.celtix.configuration.Configuration;
013: import org.objectweb.celtix.configuration.ConfigurationException;
014: import org.objectweb.celtix.configuration.ConfigurationMetadata;
015: import org.objectweb.celtix.configuration.Configurator;
016: import org.objectweb.celtix.configuration.impl.AbstractConfigurationImpl;
017: import org.objectweb.celtix.configuration.impl.DefaultConfigurationProviderFactory;
018:
019: public class AbstractConfigurationImplTest extends TestCase {
020:
021: private final Configuration top;
022:
023: public AbstractConfigurationImplTest(String name) {
024: super (name);
025: top = new TopConfigurationBuilder().build("top");
026: }
027:
028: public void testDefaultConfigurationProviderFactory()
029: throws NoSuchMethodException, IOException {
030: Method m = DefaultConfigurationProviderFactory.class
031: .getDeclaredMethod("getDefaultProviderClassName");
032: DefaultConfigurationProviderFactory factory = EasyMock
033: .createMock(DefaultConfigurationProviderFactory.class,
034: new Method[] { m });
035: factory.getDefaultProviderClassName();
036: org.easymock.EasyMock.expectLastCall().andReturn(
037: "org.objectweb.celtix.some.Unknown");
038: EasyMock.replay(factory);
039: try {
040: factory.createDefaultProvider(null);
041: fail("Expected ConfigurationException not thrown.");
042: } catch (ConfigurationException ex) {
043: assertEquals("DEFAULT_PROVIDER_INSTANTIATION_EXC", ex
044: .getCode());
045: }
046:
047: factory = DefaultConfigurationProviderFactory.getInstance();
048: assertNotNull(factory.createDefaultProvider(null));
049: }
050:
051: public void testConstruction() {
052: assertNotNull(top);
053: ConfigurationMetadata model = top.getModel();
054: assertNotNull(model);
055: assertEquals(18, model.getDefinitions().size());
056: }
057:
058: public void testConfigurators() {
059: AbstractConfigurationImpl topConfiguration = (AbstractConfigurationImpl) new TopConfigurationBuilder()
060: .build("TOP");
061: Configurator topConfigurator = topConfiguration
062: .getConfigurator();
063: assertNotNull(topConfigurator);
064: assertTrue(topConfiguration == topConfigurator
065: .getConfiguration());
066: assertNull(topConfigurator.getHook());
067: Collection<Configurator> topClients = topConfigurator
068: .getClients();
069: assertEquals(0, topClients.size());
070:
071: AbstractConfigurationImpl leafConfiguration = (AbstractConfigurationImpl) new LeafConfigurationBuilder()
072: .build(topConfiguration, "LEAF");
073: assertEquals(1, topClients.size());
074: Configurator leafConfigurator = leafConfiguration
075: .getConfigurator();
076: assertNotNull(leafConfigurator);
077: assertTrue(leafConfiguration == leafConfigurator
078: .getConfiguration());
079: Configurator hook = leafConfigurator.getHook();
080: assertNotNull(hook);
081: assertTrue(hook == topConfigurator);
082: Collection<Configurator> leafClients = leafConfigurator
083: .getClients();
084: assertEquals(0, leafClients.size());
085:
086: Object cidTop = topConfiguration.getId();
087: assertEquals("TOP", cidTop.toString());
088:
089: Object cidLeaf = leafConfiguration.getId();
090: assertEquals("LEAF", cidLeaf.toString());
091:
092: assertTrue(cidTop.equals(cidTop));
093: assertTrue(!cidTop.equals(cidLeaf));
094: assertTrue(!cidTop.equals(this ));
095:
096: assertTrue(!cidTop.toString().equals(cidLeaf.toString()));
097: assertTrue(cidTop.hashCode() != cidLeaf.hashCode());
098:
099: topConfigurator.unregisterClient(leafConfigurator);
100: assertEquals(0, topClients.size());
101: assertNotNull(leafConfigurator.getHook());
102:
103: }
104:
105: public void testUndefined() {
106: try {
107: top.getObject("undefinedStringItem");
108: } catch (ConfigurationException ex) {
109: assertEquals("ITEM_NOT_DEFINED_EXC", ex.getCode());
110: }
111: }
112:
113: public void testNoDefaults() {
114:
115: assertNull(top.getObject("booleanItemNoDefault"));
116: try {
117: top.getBoolean("booleanItemNoDefault");
118: } catch (ConfigurationException ex) {
119: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
120: }
121: assertNull(top.getObject("shortItemNoDefault"));
122: try {
123: top.getShort("shortItemNoDefault");
124: } catch (ConfigurationException ex) {
125: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
126: }
127: assertNull(top.getObject("intItemNoDefault"));
128: try {
129: top.getInt("integerItemNoDefault");
130: } catch (ConfigurationException ex) {
131: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
132: }
133: assertNull(top.getObject("longItemNoDefault"));
134: try {
135: top.getLong("longItemNoDefault");
136: } catch (ConfigurationException ex) {
137: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
138: }
139: assertNull(top.getObject("floatItemNoDefault"));
140: try {
141: top.getFloat("floatItemNoDefault");
142: } catch (ConfigurationException ex) {
143: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
144: }
145: assertNull(top.getObject("doubleItemNoDefault"));
146: try {
147: top.getDouble("doubleItemNoDefault");
148: } catch (ConfigurationException ex) {
149: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
150: }
151: assertNull(top.getObject("stringItemNoDefault"));
152: try {
153: top.getObject("stringItemNoDefault");
154: } catch (ConfigurationException ex) {
155: assertEquals("ITEM_NO_VALUE_EXC", ex.getCode());
156: }
157: assertNull(top.getObject("stringListItemNoDefault"));
158: }
159:
160: public void testDefaults() {
161: Object value = null;
162: assertNotNull(top.getObject("booleanItem"));
163: assertTrue(top.getBoolean("booleanItem"));
164: assertNotNull(top.getObject("shortItem"));
165: assertEquals(3, top.getShort("shortItem"));
166: assertNotNull(top.getObject("intItem"));
167: assertEquals(44959, top.getInt("intItem"));
168: value = top.getObject("integerItem");
169: assertNotNull(value);
170: assertEquals(44959, ((BigInteger) value).intValue());
171: assertNotNull(top.getObject("longItem"));
172: assertEquals(-99, top.getLong("longItem"));
173: assertNotNull(top.getObject("floatItem"));
174: assertTrue(Math.abs(1234.5678 - top.getFloat("floatItem")) < 0.5E-3);
175: assertNotNull(top.getObject("doubleItem"));
176: assertTrue(Math.abs(1234.5678 - top.getDouble("doubleItem")) < 0.5E-5);
177: assertNotNull(top.getObject("stringItem"));
178: assertEquals("\"Hello World!\"", top.getString("stringItem"));
179: value = top.getObject("stringListItem");
180: assertNotNull(value);
181: List<String> l = top.getStringList("stringListItem");
182: assertNotNull(l);
183: assertEquals(3, l.size());
184: assertEquals("a", l.get(0));
185: assertEquals("b", l.get(1));
186: assertEquals("c", l.get(2));
187: }
188:
189: public void testTypeMismatch() {
190: try {
191: top.getStringList("booleanItem");
192: } catch (ConfigurationException ex) {
193: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
194: }
195: try {
196: top.getBoolean("shortItem");
197: } catch (ConfigurationException ex) {
198: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
199: }
200: try {
201: top.getShort("intItem");
202: } catch (ConfigurationException ex) {
203: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
204: }
205: try {
206: top.getInt("integerItem");
207: } catch (ConfigurationException ex) {
208: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
209: }
210: try {
211: top.getLong("doubleItem");
212: } catch (ConfigurationException ex) {
213: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
214: }
215: try {
216: top.getDouble("stringItem");
217: } catch (ConfigurationException ex) {
218: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
219: }
220: try {
221: top.getString("stringListItem");
222: } catch (ConfigurationException ex) {
223: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
224: }
225: }
226:
227: public void testTypeMismatchWrite() {
228: try {
229: top.setBoolean("shortItem", true);
230: } catch (ConfigurationException ex) {
231: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
232: }
233: try {
234: top.setShort("intItem", (short) 99);
235: } catch (ConfigurationException ex) {
236: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
237: }
238: try {
239: top.setInt("integerItem", 99);
240: } catch (ConfigurationException ex) {
241: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
242: }
243: try {
244: top.setLong("doubleItem", 99);
245: } catch (ConfigurationException ex) {
246: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
247: }
248: try {
249: top.setDouble("stringItem", 99.9);
250: } catch (ConfigurationException ex) {
251: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
252: }
253: try {
254: top.setString("stringListItem", "testString");
255: } catch (ConfigurationException ex) {
256: assertEquals("ITEM_TYPE_MISMATCH_EXC", ex.getCode());
257: }
258: }
259:
260: }
|