001: package org.objectweb.celtix.bus.configuration;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.PrintStream;
007: import java.math.BigInteger;
008: import java.util.Collection;
009: import java.util.List;
010:
011: import javax.xml.XMLConstants;
012: import javax.xml.namespace.QName;
013:
014: import junit.framework.TestCase;
015:
016: import org.objectweb.celtix.configuration.ConfigurationException;
017: import org.objectweb.celtix.configuration.ConfigurationItemMetadata;
018: import org.objectweb.celtix.configuration.ConfigurationItemMetadata.LifecyclePolicy;
019: import org.objectweb.celtix.configuration.ConfigurationMetadata;
020: import org.objectweb.celtix.configuration.impl.ConfigurationMetadataBuilder;
021: import org.objectweb.celtix.configuration.types.StringListType;
022:
023: public class ConfigurationMetadataImplTest extends TestCase {
024:
025: private static final String TYPES_NAMESPACE_URI = "http://celtix.objectweb.org/configuration/types";
026:
027: public void testStandardTypes() {
028:
029: ConfigurationMetadata model = buildMetadata("meta1.xml");
030: assertEquals(
031: "http://celtix.objectweb.org/configuration/test/meta1",
032: model.getNamespaceURI());
033: Collection<ConfigurationItemMetadata> definitions = model
034: .getDefinitions();
035: assertEquals(9, definitions.size());
036: ConfigurationItemMetadata definition = null;
037:
038: definition = model.getDefinition("booleanItem");
039: assertNotNull(definition);
040: assertEquals("booleanItem", definition.getName());
041: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
042: "boolean"), definition.getType());
043: assertEquals(LifecyclePolicy.STATIC, definition
044: .getLifecyclePolicy());
045:
046: definition = model.getDefinition("shortItem");
047: assertNotNull(definition);
048: assertEquals("shortItem", definition.getName());
049: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
050: "short"), definition.getType());
051:
052: definition = model.getDefinition("intItem");
053: assertNotNull(definition);
054: assertEquals("intItem", definition.getName());
055: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
056: "int"), definition.getType());
057:
058: definition = model.getDefinition("integerItem");
059: assertNotNull(definition);
060: assertEquals("integerItem", definition.getName());
061: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
062: "integer"), definition.getType());
063: // assertNull(definition.getDescription());
064: assertEquals(LifecyclePolicy.PROCESS, definition
065: .getLifecyclePolicy());
066:
067: definition = model.getDefinition("longItem");
068: assertNotNull(definition);
069: assertEquals("longItem", definition.getName());
070: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
071: "long"), definition.getType());
072: // assertEquals(definition.getName() + " description", definition.getDescription());
073: assertEquals(LifecyclePolicy.BUS, definition
074: .getLifecyclePolicy());
075:
076: definition = model.getDefinition("floatItem");
077: assertNotNull(definition);
078: assertEquals("floatItem", definition.getName());
079: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
080: "float"), definition.getType());
081:
082: definition = model.getDefinition("doubleItem");
083: assertNotNull(definition);
084: assertEquals("doubleItem", definition.getName());
085: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
086: "double"), definition.getType());
087: // assertEquals(definition.getName() + " description", definition.getDescription());
088: assertEquals(LifecyclePolicy.DYNAMIC, definition
089: .getLifecyclePolicy());
090:
091: definition = model.getDefinition("stringItem");
092: assertNotNull(definition);
093: assertEquals("stringItem", definition.getName());
094: assertEquals(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI,
095: "string"), definition.getType());
096:
097: definition = model.getDefinition("stringListItem");
098: assertNotNull(definition);
099: assertEquals("stringListItem", definition.getName());
100: assertEquals(new QName(TYPES_NAMESPACE_URI, "stringListType"),
101: definition.getType());
102: assertEquals(LifecyclePolicy.STATIC, definition
103: .getLifecyclePolicy());
104: assertNull(definition.getDefaultValue());
105: }
106:
107: public void testIllegalQNameInType() {
108: try {
109: buildMetadata("meta2.xml");
110: fail("Expected ConfigurationException not thrown.");
111: } catch (ConfigurationException ex) {
112: assertEquals("ILLEGAL_QNAME_EXC", ex.getCode());
113: }
114: }
115:
116: public void testIllegalPrefixInType() {
117: try {
118: buildMetadata("meta3.xml");
119: fail("Expected ConfigurationException not thrown.");
120: } catch (ConfigurationException ex) {
121: assertEquals("ILLEGAL_PREFIX_EXC", ex.getCode());
122: }
123: }
124:
125: public void testUnknownType() {
126: try {
127: buildMetadata("meta4.xml");
128: fail("Expected ConfigurationException not thrown.");
129: } catch (ConfigurationException ex) {
130: assertEquals("TYPE_NOT_DEFINED_IN_NAMESPACE_EXC", ex
131: .getCode());
132: }
133: assertNotNull(buildMetadata("meta4.xml", false));
134: }
135:
136: public void testUniqueName() {
137: PrintStream perr = System.err;
138: ByteArrayOutputStream bout = new ByteArrayOutputStream();
139: PrintStream newPerr = new PrintStream(bout);
140: System.setErr(newPerr);
141: try {
142: buildMetadata("meta5.xml");
143: fail("Expected ConfigurationException not thrown.");
144: } catch (ConfigurationException ex) {
145: assertEquals("METADATA_VALIDATION_ERROR_EXC", ex.getCode());
146: } finally {
147: System.setErr(perr);
148: }
149: assertNotNull(buildMetadata("meta5.xml", false));
150: }
151:
152: public void testInvalidTypeInDefaultValue() {
153: try {
154: buildMetadata("meta6.xml");
155: fail("Expected ConfigurationException not thrown.");
156: } catch (ConfigurationException ex) {
157: assertEquals("INVALID_TYPE_FOR_DEFAULT_VALUE_EXC", ex
158: .getCode());
159: }
160: }
161:
162: public void testIllegalDefaultValue() {
163:
164: try {
165: buildMetadata("meta7.xml");
166: fail("Expected ConfigurationException not thrown.");
167: } catch (ConfigurationException ex) {
168: // assertEquals("INVALID_DEFAULT_VALUE_EXC", ex.getCode());
169: assertEquals("DEFAULT_VALUE_UNMARSHAL_ERROR_EXC", ex
170: .getCode());
171: }
172: assertNotNull(buildMetadata("meta7.xml", false));
173: }
174:
175: public void testDefaultValue() {
176: ConfigurationMetadata model = buildMetadata("meta8.xml");
177: Collection<ConfigurationItemMetadata> definitions = model
178: .getDefinitions();
179: assertEquals(9, definitions.size());
180:
181: ConfigurationItemMetadata definition = null;
182: Object defaultValue = null;
183:
184: definition = model.getDefinition("booleanItem");
185: defaultValue = definition.getDefaultValue();
186: assertNotNull(defaultValue);
187: assertEquals("java.lang.Boolean", defaultValue.getClass()
188: .getName());
189: assertEquals(true, ((Boolean) defaultValue).booleanValue());
190:
191: definition = model.getDefinition("integerItem");
192: defaultValue = definition.getDefaultValue();
193: assertNotNull(defaultValue);
194: assertEquals("java.math.BigInteger", defaultValue.getClass()
195: .getName());
196: assertEquals(44959, ((BigInteger) defaultValue).intValue());
197:
198: definition = model.getDefinition("longItem");
199: defaultValue = definition.getDefaultValue();
200: assertNotNull(defaultValue);
201: assertEquals("java.lang.Long", defaultValue.getClass()
202: .getName());
203: assertEquals(-99, ((Long) defaultValue).longValue());
204:
205: definition = model.getDefinition("doubleItem");
206: defaultValue = definition.getDefaultValue();
207: assertNotNull(defaultValue);
208: assertEquals("java.lang.Double", defaultValue.getClass()
209: .getName());
210: assertTrue(Math.abs(1234.5678 - ((Double) defaultValue)
211: .doubleValue()) < 0.5E-5);
212:
213: definition = model.getDefinition("stringItem");
214: defaultValue = definition.getDefaultValue();
215: assertNotNull(defaultValue);
216: assertEquals("java.lang.String", defaultValue.getClass()
217: .getName());
218: assertEquals("\"Hello World!\"", (String) defaultValue);
219:
220: definition = model.getDefinition("stringListItem");
221: defaultValue = definition.getDefaultValue();
222: assertNotNull(defaultValue);
223:
224: assertEquals(
225: "org.objectweb.celtix.configuration.types.StringListType",
226: defaultValue.getClass().getName());
227: List<String> l = ((StringListType) defaultValue).getItem();
228: assertNotNull(l);
229: assertEquals(3, l.size());
230: assertEquals("a", l.get(0));
231: assertEquals("b", l.get(1));
232: assertEquals("c", l.get(2));
233: }
234:
235: private ConfigurationMetadata buildMetadata(String filename) {
236: return buildMetadata(filename, true);
237: }
238:
239: private ConfigurationMetadata buildMetadata(String filename,
240: boolean doValidate) {
241: InputStream is = getClass().getResourceAsStream(
242: "resources/" + filename);
243: ConfigurationMetadataBuilder builder = new ConfigurationMetadataBuilder(
244: true);
245: builder.setValidation(doValidate);
246: try {
247: return builder.build(is);
248: } catch (IOException ex) {
249: return null;
250: }
251: }
252:
253: }
|