001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import java.io.StringReader;
011: import java.lang.reflect.Proxy;
012: import javax.xml.parsers.DocumentBuilder;
013: import javax.xml.parsers.DocumentBuilderFactory;
014: import javax.xml.parsers.ParserConfigurationException;
015: import javax.xml.transform.sax.SAXResult;
016: import junit.framework.TestCase;
017:
018: import org.codehaus.dna.Configuration;
019: import org.codehaus.dna.impl.ConfigurationUtil;
020: import org.codehaus.dna.impl.DefaultConfiguration;
021: import org.w3c.dom.Comment;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.NamedNodeMap;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027: import org.w3c.dom.Text;
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.InputSource;
030:
031: public class ConfigurationUtilTestCase extends TestCase {
032: private static final String DOC_FACTORY = "javax.xml.parsers.DocumentBuilderFactory";
033:
034: public void testToElementWithBasicConfiguration() throws Exception {
035: final String name = "meep";
036: final DefaultConfiguration configuration = new DefaultConfiguration(
037: name, "", "");
038: final Element element = ConfigurationUtil
039: .toElement(configuration);
040: assertEquals("element.getNodeName()", name, element
041: .getNodeName());
042:
043: final NodeList nodeList = element.getChildNodes();
044: assertEquals("nodeList.getLength()", 0, nodeList.getLength());
045:
046: final NamedNodeMap attributes = element.getAttributes();
047: assertEquals("attributes.getLength()", 0, attributes
048: .getLength());
049: }
050:
051: public void testToElementWithConfigurationWithValue()
052: throws Exception {
053: final String name = "meep";
054: final String value = "blah";
055: final DefaultConfiguration configuration = new DefaultConfiguration(
056: name, "", "");
057: configuration.setValue(value);
058: final Element element = ConfigurationUtil
059: .toElement(configuration);
060: assertEquals("element.getNodeName()", name, element
061: .getNodeName());
062:
063: final NodeList nodeList = element.getChildNodes();
064: assertEquals("nodeList.getLength()", 1, nodeList.getLength());
065: final Node node = nodeList.item(0);
066: assertEquals("element[0].value", value, node.getNodeValue());
067:
068: final NamedNodeMap attributes = element.getAttributes();
069: assertEquals("attributes.getLength()", 0, attributes
070: .getLength());
071: }
072:
073: public void testToElementWithConfigurationWithAttributes()
074: throws Exception {
075: final String name = "meep";
076: final String key = "key";
077: final String value = "value";
078:
079: final DefaultConfiguration configuration = new DefaultConfiguration(
080: name, "", "");
081: configuration.setAttribute(key, value);
082:
083: final Element element = ConfigurationUtil
084: .toElement(configuration);
085: assertEquals("element.getNodeName()", name, element
086: .getNodeName());
087:
088: final NodeList nodeList = element.getChildNodes();
089: assertEquals("nodeList.getLength()", 0, nodeList.getLength());
090:
091: final NamedNodeMap attributes = element.getAttributes();
092: assertEquals("attributes.getLength()", 1, attributes
093: .getLength());
094: final Node node = attributes.item(0);
095: assertEquals("attribute[0].name", key, node.getNodeName());
096: assertEquals("attribute[0].value", value, node.getNodeValue());
097: }
098:
099: public void testToElementWithConfigurationWithChildren()
100: throws Exception {
101: final String name = "meep";
102: final String childName = "moop";
103:
104: final DefaultConfiguration configuration = new DefaultConfiguration(
105: name, "", "");
106: final DefaultConfiguration child = new DefaultConfiguration(
107: childName, "", "");
108: configuration.addChild(child);
109:
110: final Element element = ConfigurationUtil
111: .toElement(configuration);
112: assertEquals("element.getNodeName()", name, element
113: .getNodeName());
114:
115: final NodeList nodeList = element.getChildNodes();
116: assertEquals("nodeList.getLength()", 1, nodeList.getLength());
117: final Node node = nodeList.item(0);
118: assertEquals("element[0].name", childName, node.getNodeName());
119:
120: final NamedNodeMap attributes = element.getAttributes();
121: assertEquals("attributes.getLength()", 0, attributes
122: .getLength());
123:
124: }
125:
126: public void testToElementWithSevereError() throws Exception {
127: final String name = "meep";
128: final DefaultConfiguration configuration = new DefaultConfiguration(
129: name, "", "");
130:
131: final String property = System.getProperty(DOC_FACTORY);
132: try {
133: System.setProperty(
134: "javax.xml.parsers.DocumentBuilderFactory",
135: "I dont exist!!!");
136: ConfigurationUtil.toElement(configuration);
137: } catch (final IllegalStateException ise) {
138: return;
139: } finally {
140: if (null != property) {
141: System.setProperty(DOC_FACTORY, property);
142: } else {
143: System.getProperties().remove(DOC_FACTORY);
144: }
145: }
146: fail("Expected to fail to create element as "
147: + "invlaid document factory property");
148: }
149:
150: public void testToConfigurationFromBasicElement() throws Exception {
151: final Document document = createDocument();
152: final String name = "meep";
153: final Element element = document.createElement(name);
154: final Configuration configuration = ConfigurationUtil
155: .toConfiguration(element);
156:
157: assertEquals("configuration.getName()", name, configuration
158: .getName());
159: assertEquals("configuration.getPath()",
160: ConfigurationUtil.ROOT_PATH, configuration.getPath());
161: assertEquals("configuration.getLocation()", "dom-gen",
162: configuration.getLocation());
163: }
164:
165: public void testToConfigurationFromElementWithValue()
166: throws Exception {
167: final Document document = createDocument();
168: final String name = "meep";
169: final String value = "text";
170: final Element element = document.createElement(name);
171: final Text text = document.createTextNode(value);
172: element.appendChild(text);
173: final Configuration configuration = ConfigurationUtil
174: .toConfiguration(element);
175:
176: assertEquals("configuration.getName()", name, configuration
177: .getName());
178: assertEquals("configuration.getPath()",
179: ConfigurationUtil.ROOT_PATH, configuration.getPath());
180: assertEquals("configuration.getLocation()", "dom-gen",
181: configuration.getLocation());
182: assertEquals("configuration.getValue()", value, configuration
183: .getValue());
184: }
185:
186: public void testToConfigurationFromElementWithMultipleValueFragments()
187: throws Exception {
188: final Document document = createDocument();
189: final String name = "meep";
190: final String value = "text";
191: final Element element = document.createElement(name);
192: final Text text = document.createTextNode(value);
193: element.appendChild(text);
194: final Text text2 = document.createTextNode(value);
195: element.appendChild(text2);
196: final Configuration configuration = ConfigurationUtil
197: .toConfiguration(element);
198:
199: assertEquals("configuration.getName()", name, configuration
200: .getName());
201: assertEquals("configuration.getPath()",
202: ConfigurationUtil.ROOT_PATH, configuration.getPath());
203: assertEquals("configuration.getLocation()", "dom-gen",
204: configuration.getLocation());
205: assertEquals("configuration.getValue()", value + value,
206: configuration.getValue());
207: }
208:
209: public void testToConfigurationFromElementWithInternalComment()
210: throws Exception {
211: final Document document = createDocument();
212: final String name = "meep";
213: final Element element = document.createElement(name);
214: final Comment comment = document.createComment("comment");
215: element.appendChild(comment);
216: final Configuration configuration = ConfigurationUtil
217: .toConfiguration(element);
218:
219: assertEquals("configuration.getName()", name, configuration
220: .getName());
221: assertEquals("configuration.getPath()",
222: ConfigurationUtil.ROOT_PATH, configuration.getPath());
223: assertEquals("configuration.getLocation()", "dom-gen",
224: configuration.getLocation());
225: assertEquals("configuration.getValue()", null, configuration
226: .getValue(null));
227: }
228:
229: public void testToConfigurationFromElementWithAttributes()
230: throws Exception {
231: final Document document = createDocument();
232: final String name = "meep";
233: final String key = "key";
234: final String value = "value";
235: final Element element = document.createElement(name);
236: element.setAttribute(key, value);
237: final Configuration configuration = ConfigurationUtil
238: .toConfiguration(element);
239:
240: assertEquals("configuration.getName()", name, configuration
241: .getName());
242: assertEquals("configuration.getPath()",
243: ConfigurationUtil.ROOT_PATH, configuration.getPath());
244: assertEquals("configuration.getLocation()", "dom-gen",
245: configuration.getLocation());
246: assertEquals("configuration.getAttributeNames().length", 1,
247: configuration.getAttributeNames().length);
248: assertEquals("configuration.getAttribute( key )", value,
249: configuration.getAttribute(key));
250: }
251:
252: public void testToConfigurationFromElementWithChildren()
253: throws Exception {
254: final Document document = createDocument();
255: final String name = "meep";
256: final String childName = "lilmeep";
257: final Element element = document.createElement(name);
258: final Element childElement = document.createElement(childName);
259: element.appendChild(childElement);
260:
261: final Configuration configuration = ConfigurationUtil
262: .toConfiguration(element);
263:
264: assertEquals("configuration.getName()", name, configuration
265: .getName());
266: assertEquals("configuration.getPath()",
267: ConfigurationUtil.ROOT_PATH, configuration.getPath());
268: assertEquals("configuration.getLocation()", "dom-gen",
269: configuration.getLocation());
270: assertEquals("configuration.getAttributeNames().length", 0,
271: configuration.getAttributeNames().length);
272: assertEquals("configuration.getChildren().length", 1,
273: configuration.getChildren().length);
274: final Configuration child = configuration.getChildren()[0];
275: assertEquals("child.name", childName, child.getName());
276: assertEquals("child.getPath()", "meep", child.getPath());
277: }
278:
279: public void testSerializeToResult() throws Exception {
280: final String name = "meep";
281: final DefaultConfiguration configuration = new DefaultConfiguration(
282: name, "", "");
283:
284: final MockInvocationRecorder recorder = new MockInvocationRecorder();
285: recorder.addInvocation(SAXMethods.START_DOCUMENT,
286: new Object[0], null);
287: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
288: "", name, name,
289: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
290: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
291: "", name, name }, null);
292: recorder.addInvocation(SAXMethods.END_DOCUMENT, new Object[0],
293: null);
294:
295: final ContentHandler handler = (ContentHandler) Proxy
296: .newProxyInstance(getClass().getClassLoader(),
297: new Class[] { ContentHandler.class }, recorder);
298:
299: final SAXResult result = new SAXResult(handler);
300:
301: ConfigurationUtil.serializeToResult(result, configuration);
302: }
303:
304: public void testBuildFromXML() throws Exception {
305: final String data = "<element/>";
306: final InputSource input = new InputSource();
307: input.setCharacterStream(new StringReader(data));
308: final Configuration configuration = ConfigurationUtil
309: .buildFromXML(input);
310: assertEquals("configuration.name", "element", configuration
311: .getName());
312: assertEquals("configuration.path", "", configuration.getPath());
313: assertEquals("configuration.location", "", configuration
314: .getLocation());
315: }
316:
317: private Document createDocument()
318: throws ParserConfigurationException {
319: final DocumentBuilderFactory factory = DocumentBuilderFactory
320: .newInstance();
321: final DocumentBuilder builder = factory.newDocumentBuilder();
322: return builder.newDocument();
323: }
324:
325: public void testGeneratePathNameFromRootForRoot() throws Exception {
326: final String path = ConfigurationUtil.generatePathName("", "");
327: assertEquals("", path);
328: }
329:
330: public void testGeneratePathNameFromRoot() throws Exception {
331: final String path = ConfigurationUtil.generatePathName("",
332: "element");
333:
334: assertEquals("element", path);
335: }
336:
337: public void testGeneratePathNameFromNonRoot() throws Exception {
338: final String path = ConfigurationUtil.generatePathName(
339: "element", "child");
340:
341: assertEquals("element/child", path);
342: }
343:
344: public void testEqualsOnEmptyConfigurations() throws Exception {
345: final String name = "x";
346: final DefaultConfiguration configuration1 = new DefaultConfiguration(
347: name, "", "");
348: final DefaultConfiguration configuration2 = new DefaultConfiguration(
349: name, "", "");
350:
351: final boolean equal = ConfigurationUtil.equals(configuration1,
352: configuration2);
353: assertEquals("config1 == config2", true, equal);
354: }
355:
356: public void testEqualsWithDifferentNames() throws Exception {
357: final DefaultConfiguration configuration1 = new DefaultConfiguration(
358: "x", "", "");
359: final DefaultConfiguration configuration2 = new DefaultConfiguration(
360: "y", "", "");
361:
362: final boolean equal = ConfigurationUtil.equals(configuration1,
363: configuration2);
364: assertEquals("config1 == config2", false, equal);
365: }
366:
367: public void testEqualsWithAttributes() throws Exception {
368: final DefaultConfiguration configuration1 = new DefaultConfiguration(
369: "x", "", "");
370: configuration1.setAttribute("key", "value");
371: final DefaultConfiguration configuration2 = new DefaultConfiguration(
372: "x", "", "");
373: configuration2.setAttribute("key", "value");
374:
375: final boolean equal = ConfigurationUtil.equals(configuration1,
376: configuration2);
377: assertEquals("config1 == config2", true, equal);
378: }
379:
380: public void testEqualsWithDifferentNumberOfAttributes()
381: throws Exception {
382: final DefaultConfiguration configuration1 = new DefaultConfiguration(
383: "x", "", "");
384: configuration1.setAttribute("key", "value");
385: configuration1.setAttribute("key2", "value");
386: final DefaultConfiguration configuration2 = new DefaultConfiguration(
387: "x", "", "");
388: configuration2.setAttribute("key", "value");
389:
390: final boolean equal = ConfigurationUtil.equals(configuration1,
391: configuration2);
392: assertEquals("config1 == config2", false, equal);
393: }
394:
395: public void testEqualsWithDifferentAttributeNames()
396: throws Exception {
397: final DefaultConfiguration configuration1 = new DefaultConfiguration(
398: "x", "", "");
399: configuration1.setAttribute("key1", "value");
400: final DefaultConfiguration configuration2 = new DefaultConfiguration(
401: "x", "", "");
402: configuration2.setAttribute("key2", "value");
403:
404: final boolean equal = ConfigurationUtil.equals(configuration1,
405: configuration2);
406: assertEquals("config1 == config2", false, equal);
407: }
408:
409: public void testEqualsWithDifferentAttributeValues()
410: throws Exception {
411: final DefaultConfiguration configuration1 = new DefaultConfiguration(
412: "x", "", "");
413: configuration1.setAttribute("key", "value1");
414: final DefaultConfiguration configuration2 = new DefaultConfiguration(
415: "x", "", "");
416: configuration2.setAttribute("key", "value2");
417:
418: final boolean equal = ConfigurationUtil.equals(configuration1,
419: configuration2);
420: assertEquals("config1 == config2", false, equal);
421: }
422:
423: public void testEqualsWithChild() throws Exception {
424: final DefaultConfiguration configuration1 = new DefaultConfiguration(
425: "x", "", "");
426: final DefaultConfiguration child1 = new DefaultConfiguration(
427: "x", "", "");
428: configuration1.addChild(child1);
429: final DefaultConfiguration configuration2 = new DefaultConfiguration(
430: "x", "", "");
431: final DefaultConfiguration child2 = new DefaultConfiguration(
432: "x", "", "");
433: configuration2.addChild(child2);
434:
435: final boolean equal = ConfigurationUtil.equals(configuration1,
436: configuration2);
437: assertEquals("config1 == config2", true, equal);
438: }
439:
440: public void testEqualsWithDifferentChildCount() throws Exception {
441: final DefaultConfiguration configuration1 = new DefaultConfiguration(
442: "x", "", "");
443: final DefaultConfiguration child1 = new DefaultConfiguration(
444: "x", "", "");
445: configuration1.addChild(child1);
446: final DefaultConfiguration configuration2 = new DefaultConfiguration(
447: "x", "", "");
448:
449: final boolean equal = ConfigurationUtil.equals(configuration1,
450: configuration2);
451: assertEquals("config1 == config2", false, equal);
452: }
453:
454: public void testEqualsWithDifferentChildren() throws Exception {
455: final DefaultConfiguration configuration1 = new DefaultConfiguration(
456: "x", "", "");
457: final DefaultConfiguration child1 = new DefaultConfiguration(
458: "x", "", "");
459: configuration1.addChild(child1);
460: final DefaultConfiguration configuration2 = new DefaultConfiguration(
461: "x", "", "");
462: final DefaultConfiguration child2 = new DefaultConfiguration(
463: "y", "", "");
464: configuration2.addChild(child2);
465:
466: final boolean equal = ConfigurationUtil.equals(configuration1,
467: configuration2);
468: assertEquals("config1 == config2", false, equal);
469: }
470:
471: public void testEqualsWithContent() throws Exception {
472: final DefaultConfiguration configuration1 = new DefaultConfiguration(
473: "x", "", "");
474: configuration1.setValue("content");
475: final DefaultConfiguration configuration2 = new DefaultConfiguration(
476: "x", "", "");
477: configuration2.setValue("content");
478:
479: final boolean equal = ConfigurationUtil.equals(configuration1,
480: configuration2);
481: assertEquals("config1 == config2", true, equal);
482: }
483:
484: public void testEqualsWithDifferentContent() throws Exception {
485: final DefaultConfiguration configuration1 = new DefaultConfiguration(
486: "x", "", "");
487: configuration1.setValue("content1");
488: final DefaultConfiguration configuration2 = new DefaultConfiguration(
489: "x", "", "");
490: configuration2.setValue("content2");
491:
492: final boolean equal = ConfigurationUtil.equals(configuration1,
493: configuration2);
494: assertEquals("config1 == config2", false, equal);
495: }
496:
497: public void testEqualsWithContentOnOne() throws Exception {
498: final DefaultConfiguration configuration1 = new DefaultConfiguration(
499: "x", "", "");
500: final DefaultConfiguration configuration2 = new DefaultConfiguration(
501: "x", "", "");
502: configuration2.setValue("content2");
503:
504: final boolean equal = ConfigurationUtil.equals(configuration1,
505: configuration2);
506: assertEquals("config1 == config2", false, equal);
507: }
508: }
|