01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import org.apache.tapestry.dom.Document;
18: import org.apache.tapestry.dom.XMLMarkupModel;
19: import org.apache.tapestry.internal.test.InternalBaseTestCase;
20: import org.testng.annotations.Test;
21:
22: public class DocumentScriptBuilderImplTest extends InternalBaseTestCase {
23: @Test
24: public void do_nothing_if_no_body() {
25: Document document = new Document();
26:
27: document.newRootElement("not-html")
28: .text("not an HTML document");
29:
30: DocumentScriptBuilder builder = new DocumentScriptBuilderImpl();
31:
32: builder.addScript("foo.js");
33: builder.addScript("doSomething();");
34:
35: builder.updateDocument(document);
36:
37: assertEquals(document.toString(),
38: "<not-html>not an HTML document</not-html>");
39: }
40:
41: @Test
42: public void add_script_links() throws Exception {
43: Document document = new Document(new XMLMarkupModel());
44:
45: document.newRootElement("html").element("body").element("p")
46: .text("Ready to be updated with scripts.");
47:
48: DocumentScriptBuilder builder = new DocumentScriptBuilderImpl();
49:
50: builder.addScriptLink("foo.js");
51: builder.addScriptLink("bar/baz.js");
52:
53: builder.updateDocument(document);
54:
55: assertEquals(document.toString(), readFile(
56: "add_script_links.txt", true));
57: }
58:
59: @Test
60: public void duplicate_script_links_ignored() throws Exception {
61: Document document = new Document();
62:
63: document.newRootElement("html").element("body").element("p")
64: .text("Ready to be updated with scripts.");
65:
66: DocumentScriptBuilder builder = new DocumentScriptBuilderImpl();
67:
68: for (int i = 0; i < 3; i++) {
69: builder.addScriptLink("foo.js");
70: builder.addScriptLink("bar/baz.js");
71: builder.addScriptLink("biff.js");
72: }
73:
74: builder.updateDocument(document);
75:
76: assertEquals(document.toString(), readFile(
77: "duplicate_script_links_ignored.txt", true));
78: }
79:
80: @Test
81: public void add_script() throws Exception {
82: Document document = new Document();
83:
84: document.newRootElement("html").element("body").element("p")
85: .text("Ready to be updated with scripts.");
86:
87: DocumentScriptBuilder builder = new DocumentScriptBuilderImpl();
88:
89: builder.addScript("doSomething();");
90: builder.addScript("doSomethingElse();");
91:
92: builder.updateDocument(document);
93:
94: assertEquals(document.toString(), readFile("add_script.txt",
95: false).trim());
96: }
97: }
|