001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: import java.io.IOException;
022: import java.io.StringWriter;
023:
024: import junit.framework.TestCase;
025:
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Tests for org.apache.tools.ant.util.DOMElementWriter.
031: *
032: */
033: public class DOMElementWriterTest extends TestCase {
034:
035: private DOMElementWriter w = new DOMElementWriter();
036:
037: public DOMElementWriterTest(String name) {
038: super (name);
039: }
040:
041: public void testIsReference() {
042: assertTrue("", w.isReference(""));
043: assertTrue(" ", w.isReference(" "));
044: assertTrue(" ", w.isReference(" "));
045: assertTrue("&#A0;", !w.isReference("&#A0;"));
046: assertTrue("20;", !w.isReference("20;"));
047: assertTrue("", !w.isReference(""));
048: assertTrue(""", w.isReference("""));
049: assertTrue("'", w.isReference("'"));
050: assertTrue(">", w.isReference(">"));
051: assertTrue("<", w.isReference("<"));
052: assertTrue("&", w.isReference("&"));
053: }
054:
055: public void testEncode() {
056: assertEquals("", w.encode(""));
057: assertEquals(" ", w.encode(" "));
058: assertEquals(" ", w.encode(" "));
059: assertEquals("&#A0;", w.encode("&#A0;"));
060: assertEquals("20;", w.encode("20;"));
061: assertEquals("&#20", w.encode(""));
062: assertEquals(""", w.encode("""));
063: assertEquals("'", w.encode("'"));
064: assertEquals(">", w.encode(">"));
065: assertEquals("<", w.encode("<"));
066: assertEquals("&", w.encode("&"));
067: assertEquals(""", w.encode("\""));
068: assertEquals("<", w.encode("<"));
069: assertEquals("&", w.encode("&"));
070: assertEquals("", w.encode("\u0017"));
071: assertEquals("\"20;&", w.encodedata("\"20;&"));
072: assertEquals("", w.encodedata("\u0017"));
073: }
074:
075: public void testIsLegalCharacter() {
076: assertTrue("0x00", !w.isLegalCharacter('\u0000'));
077: assertTrue("0x09", w.isLegalCharacter('\t'));
078: assertTrue("0x0A", w.isLegalCharacter('\n'));
079: assertTrue("0x0C", w.isLegalCharacter('\r'));
080: assertTrue("0x1F", !w.isLegalCharacter('\u001F'));
081: assertTrue("0x20", w.isLegalCharacter('\u0020'));
082: assertTrue("0xD7FF", w.isLegalCharacter('\uD7FF'));
083: assertTrue("0xD800", !w.isLegalCharacter('\uD800'));
084: assertTrue("0xDFFF", !w.isLegalCharacter('\uDFFF'));
085: assertTrue("0xE000", w.isLegalCharacter('\uE000'));
086: assertTrue("0xFFFD", w.isLegalCharacter('\uFFFD'));
087: assertTrue("0xFFFE", !w.isLegalCharacter('\uFFFE'));
088: }
089:
090: public void testCDATAEndEncoding() {
091: assertEquals("]>", w.encodedata("]>"));
092: assertEquals("]]", w.encodedata("]]"));
093: assertEquals("]]>", w.encodedata("]]>"));
094: assertEquals("]]>A", w.encodedata("]]>A"));
095: assertEquals("A]]>", w.encodedata("A]]>"));
096: assertEquals("A]]>A", w.encodedata("A]]>A"));
097: assertEquals("A]]>B]]>C", w
098: .encodedata("A]]>B]]>C"));
099: }
100:
101: public void testNoAdditionalWhiteSpaceForText() throws IOException {
102: Document d = DOMUtils.newDocument();
103: Element root = d.createElement("root");
104: DOMUtils.appendTextElement(root, "textElement", "content");
105:
106: StringWriter sw = new StringWriter();
107: DOMElementWriter w = new DOMElementWriter();
108: w.write(root, sw, 0, " ");
109: assertEquals("<root>" + StringUtils.LINE_SEP
110: + " <textElement>content</textElement>"
111: + StringUtils.LINE_SEP + "</root>"
112: + StringUtils.LINE_SEP, sw.toString());
113: }
114:
115: public void testNoAdditionalWhiteSpaceForCDATA() throws IOException {
116: Document d = DOMUtils.newDocument();
117: Element root = d.createElement("root");
118: DOMUtils.appendCDATAElement(root, "cdataElement", "content");
119:
120: StringWriter sw = new StringWriter();
121: DOMElementWriter w = new DOMElementWriter();
122: w.write(root, sw, 0, " ");
123: assertEquals("<root>" + StringUtils.LINE_SEP
124: + " <cdataElement><![CDATA[content]]></cdataElement>"
125: + StringUtils.LINE_SEP + "</root>"
126: + StringUtils.LINE_SEP, sw.toString());
127: }
128:
129: public void testNoAdditionalWhiteSpaceForEmptyElement()
130: throws IOException {
131: Document d = DOMUtils.newDocument();
132: Element root = d.createElement("root");
133: DOMUtils.createChildElement(root, "emptyElement");
134:
135: StringWriter sw = new StringWriter();
136: DOMElementWriter w = new DOMElementWriter();
137: w.write(root, sw, 0, " ");
138: assertEquals("<root>" + StringUtils.LINE_SEP
139: // + " <emptyElement></emptyElement>"
140: + " <emptyElement />" + StringUtils.LINE_SEP
141: + "</root>" + StringUtils.LINE_SEP, sw.toString());
142: }
143:
144: public void testNoNSPrefixByDefault() throws IOException {
145: Document d = DOMUtils.newDocument();
146: Element root = d.createElementNS("urn:foo", "root");
147: root.setAttributeNS("urn:foo2", "bar", "baz");
148:
149: StringWriter sw = new StringWriter();
150: DOMElementWriter w = new DOMElementWriter();
151: w.write(root, sw, 0, " ");
152: assertEquals("<root bar=\"baz\" />" + StringUtils.LINE_SEP, sw
153: .toString());
154: }
155:
156: public void testNSOnElement() throws IOException {
157: Document d = DOMUtils.newDocument();
158: Element root = d.createElementNS("urn:foo", "root");
159: root.setAttributeNS("urn:foo2", "bar", "baz");
160:
161: StringWriter sw = new StringWriter();
162: DOMElementWriter w = new DOMElementWriter(
163: false,
164: DOMElementWriter.XmlNamespacePolicy.ONLY_QUALIFY_ELEMENTS);
165: w.write(root, sw, 0, " ");
166: assertEquals("<root bar=\"baz\" xmlns=\"urn:foo\" />"
167: + StringUtils.LINE_SEP, sw.toString());
168: }
169:
170: public void testNSPrefixOnAttribute() throws IOException {
171: Document d = DOMUtils.newDocument();
172: Element root = d.createElementNS("urn:foo", "root");
173: root.setAttributeNS("urn:foo2", "bar", "baz");
174:
175: StringWriter sw = new StringWriter();
176: DOMElementWriter w = new DOMElementWriter(false,
177: DOMElementWriter.XmlNamespacePolicy.QUALIFY_ALL);
178: w.write(root, sw, 0, " ");
179: assertEquals("<root ns0:bar=\"baz\" xmlns=\"urn:foo\""
180: + " xmlns:ns0=\"urn:foo2\" />" + StringUtils.LINE_SEP,
181: sw.toString());
182: }
183:
184: public void testNSPrefixOnAttributeEvenWithoutElement()
185: throws IOException {
186: Document d = DOMUtils.newDocument();
187: Element root = d.createElementNS("urn:foo", "root");
188: root.setAttributeNS("urn:foo2", "bar", "baz");
189:
190: StringWriter sw = new StringWriter();
191: DOMElementWriter w = new DOMElementWriter(false,
192: new DOMElementWriter.XmlNamespacePolicy(false, true));
193: w.write(root, sw, 0, " ");
194: assertEquals("<root ns0:bar=\"baz\" xmlns:ns0=\"urn:foo2\" />"
195: + StringUtils.LINE_SEP, sw.toString());
196: }
197:
198: public void testNSGetsReused() throws IOException {
199: Document d = DOMUtils.newDocument();
200: Element root = d.createElementNS("urn:foo", "root");
201: Element child = d.createElementNS("urn:foo", "child");
202: root.appendChild(child);
203: StringWriter sw = new StringWriter();
204: DOMElementWriter w = new DOMElementWriter(
205: false,
206: DOMElementWriter.XmlNamespacePolicy.ONLY_QUALIFY_ELEMENTS);
207: w.write(root, sw, 0, " ");
208: assertEquals("<root xmlns=\"urn:foo\">" + StringUtils.LINE_SEP
209: + " <child />" + StringUtils.LINE_SEP + "</root>"
210: + StringUtils.LINE_SEP, sw.toString());
211: }
212:
213: public void testNSGoesOutOfScope() throws IOException {
214: Document d = DOMUtils.newDocument();
215: Element root = d.createElementNS("urn:foo", "root");
216: Element child = d.createElementNS("urn:foo2", "child");
217: root.appendChild(child);
218: Element child2 = d.createElementNS("urn:foo2", "child");
219: root.appendChild(child2);
220: Element grandChild = d
221: .createElementNS("urn:foo2", "grandchild");
222: child2.appendChild(grandChild);
223: Element child3 = d.createElementNS("urn:foo2", "child");
224: root.appendChild(child3);
225: StringWriter sw = new StringWriter();
226: DOMElementWriter w = new DOMElementWriter(
227: false,
228: DOMElementWriter.XmlNamespacePolicy.ONLY_QUALIFY_ELEMENTS);
229: w.write(root, sw, 0, " ");
230: assertEquals("<root xmlns=\"urn:foo\">" + StringUtils.LINE_SEP
231: + " <ns0:child xmlns:ns0=\"urn:foo2\" />"
232: + StringUtils.LINE_SEP
233: + " <ns1:child xmlns:ns1=\"urn:foo2\">"
234: + StringUtils.LINE_SEP + " <ns1:grandchild />"
235: + StringUtils.LINE_SEP + " </ns1:child>"
236: + StringUtils.LINE_SEP
237: + " <ns2:child xmlns:ns2=\"urn:foo2\" />"
238: + StringUtils.LINE_SEP + "</root>"
239: + StringUtils.LINE_SEP, sw.toString());
240: }
241: }
|