001: package org.objectweb.celtix.bus.configuration;
002:
003: import java.io.ByteArrayInputStream;
004:
005: import javax.xml.namespace.QName;
006: import javax.xml.parsers.DocumentBuilder;
007: import javax.xml.parsers.DocumentBuilderFactory;
008:
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011: import org.w3c.dom.Node;
012:
013: import org.xml.sax.InputSource;
014:
015: import junit.framework.TestCase;
016:
017: import org.objectweb.celtix.configuration.ConfigurationException;
018: import org.objectweb.celtix.configuration.impl.ConfigurationMetadataUtils;
019:
020: public class ConfigurationMetadataUtilsTest extends TestCase {
021:
022: private static final String FRAGMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
023: + "<a xmlns:tt=\"http://celtix.objectweb.org/configuration/test/types\">"
024: + "<b1><c1 name=\"tt:un\">tt:one</c1></b1>"
025: + "<b2><c2 xmlns:tt=\"http://celtix.objectweb.org/configuration/types\" name=\"tt:deux\">"
026: + "tt:two</c2></b2>"
027: + "<b3 xmlns:tt=\"http://celtix.objectweb.org/configuration/types\">"
028: + "<c3 xmlns:tt=\"http://celtix.objectweb.org/configuration/metadata\" name=\"tt:trois\">"
029: + "tt:three</c3></b3>"
030: + "<b4></b4>"
031: + "<b5>BEFORE<c5></c5>AFTER</b5>"
032: + "<b55><c5></c5>AFTER</b55>"
033: + "<b6><c6>:noprefix</c6></b6>"
034: + "<b7><c7>tt:</c7></b7>"
035: + "<b8><c8>dd:unknownprefix</c8></b8>" + "</a>";
036:
037: private static Document document;
038:
039: public void testGetElementValue() throws Exception {
040: Document doc = getDocument(FRAGMENT);
041: Node node = doc.getElementsByTagName("c1").item(0);
042: assertEquals("tt:one", ConfigurationMetadataUtils
043: .getElementValue(node));
044: node = doc.getElementsByTagName("b4").item(0);
045: assertNull(ConfigurationMetadataUtils.getElementValue(node));
046: node = doc.getElementsByTagName("b5").item(0);
047: assertEquals("BEFORE", ConfigurationMetadataUtils
048: .getElementValue(node));
049: node = doc.getElementsByTagName("b55").item(0);
050: assertEquals("AFTER", ConfigurationMetadataUtils
051: .getElementValue(node));
052: }
053:
054: public void testGetElementValueToQName() throws Exception {
055: Document doc = getDocument(FRAGMENT);
056: Element elem = (Element) (doc.getElementsByTagName("c1")
057: .item(0));
058: QName qn = null;
059: qn = new QName(
060: "http://celtix.objectweb.org/configuration/test/types",
061: "one");
062: assertEquals(qn, ConfigurationMetadataUtils
063: .elementValueToQName(doc, elem));
064: elem = (Element) (doc.getElementsByTagName("c2").item(0));
065: qn = new QName(
066: "http://celtix.objectweb.org/configuration/types",
067: "two");
068: assertEquals(qn, ConfigurationMetadataUtils
069: .elementValueToQName(doc, elem));
070: elem = (Element) (doc.getElementsByTagName("c3").item(0));
071: qn = new QName(
072: "http://celtix.objectweb.org/configuration/metadata",
073: "three");
074: assertEquals(qn, ConfigurationMetadataUtils
075: .elementValueToQName(doc, elem));
076:
077: elem = (Element) (doc.getElementsByTagName("c6").item(0));
078: try {
079: ConfigurationMetadataUtils.elementValueToQName(doc, elem);
080: } catch (ConfigurationException ex) {
081: assertEquals("ILLEGAL_QNAME_EXC", ex.getCode());
082: }
083: elem = (Element) (doc.getElementsByTagName("c7").item(0));
084: try {
085: ConfigurationMetadataUtils.elementValueToQName(doc, elem);
086: } catch (ConfigurationException ex) {
087: assertEquals("ILLEGAL_QNAME_EXC", ex.getCode());
088: }
089: elem = (Element) (doc.getElementsByTagName("c8").item(0));
090: try {
091: ConfigurationMetadataUtils.elementValueToQName(doc, elem);
092: } catch (ConfigurationException ex) {
093: assertEquals("ILLEGAL_PREFIX_EXC", ex.getCode());
094: }
095: }
096:
097: public void testGetElementAttributeToQName() throws Exception {
098: Document doc = getDocument(FRAGMENT);
099: Element elem = (Element) (doc.getElementsByTagName("c1")
100: .item(0));
101: QName qn = null;
102: qn = new QName(
103: "http://celtix.objectweb.org/configuration/test/types",
104: "un");
105: assertEquals(qn, ConfigurationMetadataUtils
106: .elementAttributeToQName(doc, elem, "name"));
107: elem = (Element) (doc.getElementsByTagName("c2").item(0));
108: qn = new QName(
109: "http://celtix.objectweb.org/configuration/types",
110: "deux");
111: assertEquals(qn, ConfigurationMetadataUtils
112: .elementAttributeToQName(doc, elem, "name"));
113: elem = (Element) (doc.getElementsByTagName("c3").item(0));
114: qn = new QName(
115: "http://celtix.objectweb.org/configuration/metadata",
116: "trois");
117: assertEquals(qn, ConfigurationMetadataUtils
118: .elementAttributeToQName(doc, elem, "name"));
119: }
120:
121: private Document getDocument(String str) throws Exception {
122: if (null == document) {
123: DocumentBuilderFactory factory = DocumentBuilderFactory
124: .newInstance();
125: factory.setNamespaceAware(true);
126: DocumentBuilder parser = factory.newDocumentBuilder();
127: document = parser.parse(new InputSource(
128: new ByteArrayInputStream(str.getBytes())));
129: }
130: return document;
131: }
132: }
|