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.internal.services;
016:
017: import org.apache.tapestry.Link;
018: import org.apache.tapestry.MarkupWriter;
019: import org.apache.tapestry.dom.Element;
020: import org.apache.tapestry.dom.XMLMarkupModel;
021: import org.apache.tapestry.internal.test.InternalBaseTestCase;
022: import org.testng.annotations.Test;
023:
024: public class MarkupWriterImplTest extends InternalBaseTestCase {
025: @Test(expectedExceptions=IllegalStateException.class)
026: public void write_with_no_current_element() {
027: MarkupWriter w = new MarkupWriterImpl();
028:
029: w.write("fail!");
030: }
031:
032: @Test
033: public void write_whitespace_before_start_of_root_element_is_ignored() {
034: MarkupWriter w = new MarkupWriterImpl(new XMLMarkupModel(),
035: null);
036:
037: w.write(" ");
038:
039: w.element("root");
040: w.end();
041:
042: assertEquals(w.toString(), "<root/>");
043: }
044:
045: @Test
046: public void write_whitespace_after_end_of_root_element_is_ignored() {
047: MarkupWriter w = new MarkupWriterImpl(new XMLMarkupModel(),
048: null);
049:
050: w.element("root");
051: w.end();
052:
053: w.write(" ");
054:
055: assertEquals(w.toString(), "<root/>");
056: }
057:
058: @Test(expectedExceptions=IllegalStateException.class)
059: public void comment_with_no_current_element() {
060: MarkupWriter w = new MarkupWriterImpl();
061:
062: w.comment("fail!");
063: }
064:
065: @Test(expectedExceptions=IllegalStateException.class)
066: public void end_with_no_current_element() {
067: MarkupWriter w = new MarkupWriterImpl();
068:
069: w.end();
070: }
071:
072: @Test(expectedExceptions=IllegalStateException.class)
073: public void attributes_with_no_current_element() {
074: MarkupWriter w = new MarkupWriterImpl();
075:
076: w.attributes("fail", "now");
077: }
078:
079: @Test
080: public void current_element_at_end_of_root_element_is_null() {
081: MarkupWriter w = new MarkupWriterImpl();
082:
083: w.element("root");
084:
085: assertNull(w.end());
086: }
087:
088: @Test
089: public void element_nesting() {
090: MarkupWriter w = new MarkupWriterImpl();
091:
092: Element root = w.element("root");
093:
094: w.attributes("foo", "bar");
095:
096: w.write("before child");
097:
098: assertNotSame(w.element("nested"), root);
099:
100: w.write("inner text");
101:
102: assertSame(w.end(), root);
103:
104: w.write("after child");
105:
106: root.attribute("gnip", "gnop");
107:
108: assertEquals(
109: w.toString(),
110: "<root foo=\"bar\" gnip=\"gnop\">before child<nested>inner text</nested>after child</root>");
111: }
112:
113: @Test
114: public void element_with_attributes() {
115: MarkupWriter w = new MarkupWriterImpl();
116:
117: w.element("img", "src", "foo.png", "width", 20, "height", 20);
118: w.end();
119:
120: // img is a tag with an end tag style of omit, so no close tag is written.
121:
122: assertEquals(w.toString(),
123: "<img height=\"20\" src=\"foo.png\" width=\"20\">");
124: }
125:
126: @Test
127: public void attributes() {
128: MarkupWriter w = new MarkupWriterImpl();
129:
130: w.element("root");
131:
132: w.attributes("foo", "bar", "gnip", "gnop");
133:
134: assertEquals(w.toString(),
135: "<root foo=\"bar\" gnip=\"gnop\"></root>");
136: }
137:
138: @Test
139: public void comment() {
140: MarkupWriter w = new MarkupWriterImpl();
141:
142: w.element("root");
143: w.comment("A comment");
144: w.end();
145:
146: assertEquals(w.toString(), "<root><!-- A comment --></root>");
147: }
148:
149: @Test
150: public void new_text_node_after_comment_node() {
151: MarkupWriter w = new MarkupWriterImpl();
152:
153: w.element("root");
154: w.write("before");
155: w.comment("A comment");
156: w.write("after");
157: w.end();
158:
159: assertEquals(w.toString(),
160: "<root>before<!-- A comment -->after</root>");
161: }
162:
163: @Test
164: public void null_write_is_ok() {
165: MarkupWriter w = new MarkupWriterImpl();
166:
167: w.element("root");
168: w.write(null);
169: w.end();
170:
171: assertEquals(w.toString(), "<root></root>");
172: }
173:
174: @Test
175: public void writef() {
176: MarkupWriter w = new MarkupWriterImpl();
177:
178: w.element("root");
179: w.writef("Test name: %s", "writef");
180:
181: assertEquals(w.toString(), "<root>Test name: writef</root>");
182: }
183:
184: @Test
185: public void writer_notifies_map_about_links() {
186: ComponentInvocationMap map = mockComponentInvocationMap();
187:
188: MarkupWriter writer = new MarkupWriterImpl(
189: new XMLMarkupModel(), map);
190: Link link = mockLink();
191:
192: Element e = writer.element("form");
193:
194: map.store(e, link);
195:
196: replay();
197:
198: writer.attributes("action", link);
199:
200: verify();
201: }
202:
203: @Test
204: public void write_raw() {
205: MarkupWriter w = new MarkupWriterImpl();
206:
207: w.element("root");
208: w.write("<");
209: w.writeRaw(" ");
210: w.write(">");
211: w.end();
212:
213: assertEquals(w.toString(), "<root>< ></root>");
214: }
215: }
|