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.ComponentResources;
018: import org.apache.tapestry.MarkupWriter;
019: import org.apache.tapestry.dom.MarkupModel;
020: import org.apache.tapestry.dom.XMLMarkupModel;
021: import org.apache.tapestry.internal.parser.AttributeToken;
022: import org.apache.tapestry.internal.parser.StartElementToken;
023: import org.apache.tapestry.internal.parser.TextToken;
024: import org.apache.tapestry.internal.structure.ComponentPageElement;
025: import org.apache.tapestry.internal.structure.PageElement;
026: import org.apache.tapestry.internal.test.InternalBaseTestCase;
027: import org.apache.tapestry.ioc.Location;
028: import org.apache.tapestry.ioc.internal.util.TapestryException;
029: import org.apache.tapestry.ioc.services.TypeCoercer;
030: import org.apache.tapestry.runtime.RenderQueue;
031: import org.apache.tapestry.services.BindingSource;
032: import org.apache.tapestry.services.ComponentClassResolver;
033: import org.apache.tapestry.services.ComponentMessagesSource;
034: import org.testng.annotations.Test;
035:
036: public class PageElementFactoryImplTest extends InternalBaseTestCase {
037: private static MarkupModel _xmlModel = new XMLMarkupModel();
038:
039: @Test
040: public void start_element() {
041: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
042: ComponentClassResolver resolver = mockComponentClassResolver();
043: MarkupWriter writer = new MarkupWriterImpl();
044: Location l = mockLocation();
045: RenderQueue queue = mockRenderQueue();
046:
047: replay();
048:
049: PageElementFactory factory = new PageElementFactoryImpl(source,
050: resolver, null, null, null);
051: StartElementToken token = new StartElementToken("fred", l);
052:
053: PageElement element = factory.newStartElement(token);
054:
055: element.render(writer, queue);
056:
057: verify();
058:
059: assertEquals(element.toString(), "Start[fred]");
060: assertEquals(writer.toString(), "<fred></fred>");
061: }
062:
063: @Test
064: public void attribute() {
065: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
066: ComponentClassResolver resolver = mockComponentClassResolver();
067: MarkupWriter writer = new MarkupWriterImpl(_xmlModel, null);
068: Location l = mockLocation();
069: RenderQueue queue = mockRenderQueue();
070:
071: replay();
072:
073: PageElementFactory factory = new PageElementFactoryImpl(source,
074: resolver, null, null, null);
075: AttributeToken token = new AttributeToken("name", "value", l);
076:
077: PageElement element = factory.newAttributeElement(null, token);
078:
079: writer.element("root");
080:
081: element.render(writer, queue);
082:
083: verify();
084:
085: assertEquals(writer.toString(), "<root name=\"value\"/>");
086: }
087:
088: @Test
089: public void end_element() {
090: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
091: ComponentClassResolver resolver = mockComponentClassResolver();
092: MarkupWriter writer = new MarkupWriterImpl(_xmlModel, null);
093: RenderQueue queue = mockRenderQueue();
094:
095: replay();
096:
097: PageElementFactory factory = new PageElementFactoryImpl(source,
098: resolver, null, null, null);
099:
100: PageElement element = factory.newEndElement();
101:
102: writer.element("root");
103: writer.write("before");
104: writer.element("nested");
105:
106: element.render(writer, queue);
107:
108: writer.write("after");
109:
110: verify();
111:
112: assertEquals(element.toString(), "End");
113: assertEquals(writer.toString(),
114: "<root>before<nested/>after</root>");
115: }
116:
117: @Test
118: public void end_element_is_singleton() {
119: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
120: ComponentClassResolver resolver = mockComponentClassResolver();
121:
122: replay();
123:
124: PageElementFactory factory = new PageElementFactoryImpl(source,
125: resolver, null, null, null);
126:
127: PageElement element1 = factory.newEndElement();
128: PageElement element2 = factory.newEndElement();
129:
130: assertSame(element2, element1);
131:
132: verify();
133: }
134:
135: @Test
136: public void text_element() {
137: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
138: ComponentClassResolver resolver = mockComponentClassResolver();
139: MarkupWriter writer = new MarkupWriterImpl();
140: Location l = mockLocation();
141: RenderQueue queue = mockRenderQueue();
142:
143: replay();
144:
145: PageElementFactory factory = new PageElementFactoryImpl(source,
146: resolver, null, null, null);
147: TextToken token = new TextToken("some text", l);
148:
149: PageElement element = factory.newTextElement(token);
150:
151: writer.element("root");
152: element.render(writer, queue);
153:
154: verify();
155:
156: assertEquals(element.toString(), "Text[some text]");
157: assertEquals(writer.toString(), "<root>some text</root>");
158: }
159:
160: @Test
161: public void render_body_element() {
162: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
163: ComponentClassResolver resolver = mockComponentClassResolver();
164: RenderQueue queue = mockRenderQueue();
165: ComponentPageElement component = mockComponentPageElement();
166: MarkupWriter writer = newMock(MarkupWriter.class);
167:
168: component.enqueueBeforeRenderBody(queue);
169:
170: replay();
171:
172: PageElementFactory factory = new PageElementFactoryImpl(source,
173: resolver, null, null, null);
174:
175: PageElement element = factory.newRenderBodyElement(component);
176:
177: element.render(writer, queue);
178:
179: verify();
180: }
181:
182: @Test
183: public void unclosed_attribute_expression() {
184: ComponentInstantiatorSource source = mockComponentInstantiatorSource();
185: ComponentClassResolver resolver = mockComponentClassResolver();
186: TypeCoercer typeCoercer = mockTypeCoercer();
187: BindingSource bindingSource = mockBindingSource();
188: ComponentMessagesSource messagesSource = newMock(ComponentMessagesSource.class);
189: ComponentResources resources = mockComponentResources();
190: Location location = mockLocation();
191:
192: AttributeToken token = new AttributeToken("fred",
193: "${flintstone", location);
194:
195: replay();
196:
197: PageElementFactory factory = new PageElementFactoryImpl(source,
198: resolver, typeCoercer, bindingSource, messagesSource);
199:
200: try {
201: factory.newAttributeElement(resources, token);
202: unreachable();
203: } catch (TapestryException ex) {
204: assertEquals(ex.getMessage(),
205: "Attribute expression \'${flintstone\' is missing a closing brace.");
206: assertSame(ex.getLocation(), location);
207: }
208:
209: verify();
210: }
211: }
|