001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.dom;
016:
017: import org.apache.tapestry.internal.test.InternalBaseTestCase;
018: import org.testng.annotations.Test;
019:
020: /**
021: * Tests for a number of DOM node classes, including {@link org.apache.tapestry.dom.Element} and
022: * {@link org.apache.tapestry.dom.Document}.
023: */
024: public class DOMTest extends InternalBaseTestCase {
025: @Test
026: public void document_with_empty_root_element() {
027: Document d = new Document();
028:
029: d.newRootElement("empty");
030:
031: assertEquals(d.toString(), "<empty></empty>");
032: }
033:
034: @Test
035: public void xml_style_empty_element() {
036: Document d = new Document(new XMLMarkupModel());
037:
038: d.newRootElement("empty");
039:
040: assertEquals(d.toString(), "<empty/>");
041: }
042:
043: /** Also demonstrates that attributes are provided in alphabetical order. */
044: @Test
045: public void document_with_root_element_and_attributes()
046: throws Exception {
047: Document d = new Document();
048:
049: Element e = d.newRootElement("has-attributes");
050:
051: e.attribute("fred", "flintstone");
052: e.attribute("barney", "rubble");
053:
054: assertEquals(d.toString(), readFile(
055: "document_with_root_element_and_attributes.txt", true));
056: }
057:
058: @Test
059: public void nested_elements() throws Exception {
060: Document d = new Document();
061:
062: Element e = d.newRootElement("population");
063:
064: Element p = e.element("person");
065: p.attribute("first-name", "Fred");
066: p.attribute("last-name", "Flintstone");
067:
068: assertSame(p.getParent(), e);
069:
070: p = e.element("person");
071: p.attribute("first-name", "Barney");
072: p.attribute("last-name", "Rubble");
073:
074: assertSame(p.getParent(), e);
075:
076: assertEquals(d.toString(),
077: readFile("nested_elements.txt", true));
078: }
079:
080: @Test
081: public void to_string_on_empty_document() {
082: Document d = new Document();
083:
084: assertEquals(d.toString(), "[empty Document]");
085: }
086:
087: @Test(expectedExceptions=IllegalArgumentException.class)
088: public void attribute_names_may_not_be_blank() {
089: Document d = new Document();
090:
091: Element e = d.newRootElement("fred");
092:
093: e.attribute("", "value");
094: }
095:
096: @Test
097: public void element_name_may_not_be_blank() {
098: Document d = new Document();
099:
100: d.newRootElement("");
101: }
102:
103: @Test
104: public void attribute_value_null_is_no_op() {
105: Document d = new Document();
106:
107: Element e = d.newRootElement("root");
108:
109: e.attribute("foo", "bar");
110:
111: final String expected = "<root foo=\"bar\"></root>";
112:
113: assertEquals(d.toString(), expected);
114:
115: e.attribute("foo", null);
116:
117: assertEquals(d.toString(), expected);
118:
119: e.attribute("gnip", null);
120:
121: assertEquals(d.toString(), expected);
122: }
123:
124: @Test
125: public void comments() throws Exception {
126: Document d = new Document();
127:
128: // Can't add comments to the document, not yet.
129:
130: Element e = d.newRootElement("html");
131:
132: e.comment("Created by Tapestry 5.0");
133:
134: assertEquals(d.toString(),
135: "<html><!-- Created by Tapestry 5.0 --></html>");
136: }
137:
138: @Test
139: public void text() {
140: Document d = new Document();
141:
142: Element e = d.newRootElement("body");
143:
144: e.text("Tapestry does DOM.");
145:
146: assertEquals(d.toString(), "<body>Tapestry does DOM.</body>");
147: }
148:
149: @Test
150: public void text_with_control_characters() {
151: Document d = new Document();
152:
153: Element e = d.newRootElement("root");
154:
155: e.text("<this> & <that>");
156:
157: assertEquals(d.toString(),
158: "<root><this> & <that></root>");
159: }
160:
161: @Test
162: public void specify_attributes_with_new_element() {
163: Document d = new Document();
164:
165: Element e = d.newRootElement("root");
166:
167: e.element("foo", "alpha", "legion");
168:
169: assertEquals(d.toString(),
170: "<root><foo alpha=\"legion\"></foo></root>");
171: }
172:
173: @Test
174: public void writef_with_text() {
175: Document d = new Document();
176:
177: Element e = d.newRootElement("root");
178:
179: Text t = e.text("Start: ");
180:
181: t.writef("** %s: %d **", "foo", 5);
182:
183: assertEquals(d.toString(), "<root>Start: ** foo: 5 **</root>");
184: }
185:
186: @Test
187: public void get_element_by_id() {
188: Document d = new Document();
189: Element e = d.newRootElement("root");
190: Element e1 = e.element("e1", "id", "x");
191: Element e2 = e.element("e2", "id", "y");
192: assertSame(e1.getElementById("x"), e1);
193: assertSame(e.getElementById("y"), e2);
194: assertNull(e.getElementById("z"));
195: }
196:
197: @Test
198: public void get_child_markup() {
199: Document d = new Document();
200: Element e0 = d.newRootElement("root");
201: Element e1 = e0.element("e1");
202: e1.text("123");
203: assertEquals(e1.getChildText(), "123");
204: assertEquals(e0.getChildText(), "<e1>123</e1>");
205: }
206:
207: @Test
208: public void document_find_no_root_element() {
209: Document d = new Document();
210:
211: assertNull(d.find("does/not/matter"));
212: }
213:
214: @Test
215: public void document_find_not_a_match() {
216: Document d = new Document();
217:
218: d.newRootElement("fred");
219:
220: assertNull(d.find("barney"));
221: assertNull(d.find("wilma/betty"));
222: }
223:
224: @Test
225: public void document_find_root_is_match() {
226: Document d = new Document();
227:
228: Element root = d.newRootElement("fred");
229:
230: assertSame(d.find("fred"), root);
231: }
232:
233: @Test
234: public void document_find_match() {
235: Document d = new Document();
236:
237: Element root = d.newRootElement("fred");
238:
239: root.text("text");
240: Element barney = root.element("barney");
241: Element bambam = barney.element("bambam");
242:
243: assertSame(d.find("fred/barney/bambam"), bambam);
244: assertSame(root.find("barney/bambam"), bambam);
245: }
246:
247: @Test
248: public void document_find_no_match() {
249: Document d = new Document();
250:
251: Element root = d.newRootElement("fred");
252:
253: root.text("text");
254: Element barney = root.element("barney");
255: barney.element("bambam");
256:
257: assertNull(d.find("fred/barney/pebbles"));
258: assertNull(root.find("barney/pebbles"));
259: }
260:
261: @Test
262: public void insert_element_at() {
263: Document d = new Document(new XMLMarkupModel());
264:
265: Element root = d.newRootElement("fred");
266:
267: root.element("start");
268: root.element("end");
269:
270: root.elementAt(1, "one").element("tiny");
271: root.elementAt(2, "two").element("bubbles");
272:
273: assertEquals(d.toString(),
274: "<fred><start/><one><tiny/></one><two><bubbles/></two><end/></fred>");
275: }
276:
277: @Test
278: public void force_attributes_overrides_existing() {
279: Document d = new Document(new XMLMarkupModel());
280:
281: Element root = d.newRootElement("fred");
282:
283: root.attributes("hi", "ho", "gnip", "gnop");
284:
285: assertEquals(root.toString(), "<fred gnip=\"gnop\" hi=\"ho\"/>");
286:
287: root.forceAttributes("hi", "bit", "gnip", null);
288:
289: assertEquals(root.toString(), "<fred hi=\"bit\"/>");
290: }
291:
292: @Test
293: public void raw_output() {
294: Document d = new Document(new XMLMarkupModel());
295:
296: Element root = d.newRootElement("fred");
297:
298: Element em = root.element("em");
299:
300: em.text("<");
301: em.raw(" ");
302: em.text(">");
303:
304: // The '<' and '>' are filtered into entities, but the '&' in is left alone (left
305: // raw).
306:
307: assertEquals(root.toString(),
308: "<fred><em>< ></em></fred>");
309: }
310:
311: @Test
312: public void dtd_with_markup() {
313: Document d = new Document(new XMLMarkupModel());
314: Element root = d.newRootElement("prime");
315: root.element("slag");
316: d.dtd("prime", "-//TF", "tf");
317: String expected = "<!DOCTYPE prime PUBLIC \"-//TF\" \"tf\"><prime><slag/></prime>";
318: assertEquals(d.toString(), expected);
319: }
320:
321: @Test
322: public void dtd_with_nullids() {
323: Document d = new Document(new XMLMarkupModel());
324: d.newRootElement("prime");
325: d.dtd("prime", null, null);
326: assertEquals(d.toString(), "<prime/>");
327: d.dtd("prime", "-//TF", null);
328: assertEquals(d.toString(),
329: "<!DOCTYPE prime PUBLIC \"-//TF\"><prime/>");
330:
331: d.dtd("prime", null, "tf");
332: assertEquals(d.toString(),
333: "<!DOCTYPE prime SYSTEM \"tf\"><prime/>");
334: }
335: }
|