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 java.util.Arrays;
018: import java.util.Locale;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.tapestry.internal.InternalComponentResources;
022: import org.apache.tapestry.internal.parser.ComponentTemplate;
023: import org.apache.tapestry.internal.parser.EndElementToken;
024: import org.apache.tapestry.internal.parser.StartComponentToken;
025: import org.apache.tapestry.internal.structure.ComponentPageElement;
026: import org.apache.tapestry.internal.structure.Page;
027: import org.apache.tapestry.internal.structure.PageElement;
028: import org.apache.tapestry.internal.test.InternalBaseTestCase;
029: import org.apache.tapestry.ioc.Location;
030: import org.apache.tapestry.model.ComponentModel;
031: import org.apache.tapestry.model.EmbeddedComponentModel;
032: import org.apache.tapestry.services.ComponentClassResolver;
033: import org.testng.annotations.Test;
034:
035: public class PageLoaderImplTest extends InternalBaseTestCase {
036: private static final String LOGICAL_PAGE_NAME = "Bar";
037:
038: private static final String PAGE_CLASS_NAME = "foo.page.Bar";
039:
040: private static final String CHILD_CLASS_NAME = "foo.component.Baz";
041:
042: private static final Locale LOCALE = Locale.ENGLISH;
043:
044: @Test
045: public void not_all_embedded_components_in_template() {
046: ComponentTemplateSource templateSource = mockComponentTemplateSource();
047: PageElementFactory elementFactory = mockPageElementFactory();
048: ComponentPageElement rootElement = mockComponentPageElement();
049: InternalComponentResources resources = mockInternalComponentResources();
050: ComponentModel model = mockComponentModel();
051: ComponentTemplate template = mockComponentTemplate();
052: Log log = mockLog();
053: ComponentClassResolver resolver = mockComponentClassResolver();
054:
055: train_resolvePageNameToClassName(resolver, LOGICAL_PAGE_NAME,
056: PAGE_CLASS_NAME);
057:
058: train_newRootComponentElement(elementFactory, PAGE_CLASS_NAME,
059: rootElement);
060:
061: train_getComponentResources(rootElement, resources);
062: train_getComponentModel(resources, model);
063:
064: train_getComponentClassName(model, PAGE_CLASS_NAME);
065:
066: train_getTemplate(templateSource, model, LOCALE, template);
067:
068: train_isMissing(template, false);
069:
070: train_getLog(model, log);
071:
072: train_getEmbeddedIds(model, "foo", "bar", "baz");
073:
074: train_getComponentIds(template, "baz", "biff");
075:
076: log.error(ServicesMessages.embeddedComponentsNotInTemplate(
077: Arrays.asList("foo", "bar"), PAGE_CLASS_NAME));
078:
079: train_getTokens(template);
080:
081: replay();
082:
083: PageLoader loader = new PageLoaderImpl(templateSource,
084: elementFactory, null, null, resolver);
085:
086: Page page = loader.loadPage(LOGICAL_PAGE_NAME, LOCALE);
087:
088: assertSame(page.getLogicalName(), LOGICAL_PAGE_NAME);
089:
090: verify();
091: }
092:
093: @Test
094: public void type_conflict_between_template_and_class() {
095: ComponentTemplateSource templateSource = mockComponentTemplateSource();
096: PageElementFactory elementFactory = mockPageElementFactory();
097: ComponentPageElement rootElement = mockComponentPageElement();
098: InternalComponentResources resources = mockInternalComponentResources();
099: ComponentModel model = mockComponentModel();
100: ComponentModel childModel = mockComponentModel();
101: ComponentTemplate template = mockComponentTemplate();
102: Log log = mockLog();
103: EmbeddedComponentModel emodel = mockEmbeddedComponentModel();
104: ComponentPageElement childElement = mockComponentPageElement();
105: InternalComponentResources childResources = mockInternalComponentResources();
106: Location l = mockLocation();
107: PageElement body = mockPageElement();
108: ComponentTemplate childTemplate = mockComponentTemplate();
109: ComponentClassResolver resolver = mockComponentClassResolver();
110:
111: train_resolvePageNameToClassName(resolver, LOGICAL_PAGE_NAME,
112: PAGE_CLASS_NAME);
113: train_newRootComponentElement(elementFactory, PAGE_CLASS_NAME,
114: rootElement);
115:
116: train_getComponentResources(rootElement, resources);
117: train_getComponentModel(resources, model);
118:
119: train_getComponentClassName(model, PAGE_CLASS_NAME);
120:
121: train_getTemplate(templateSource, model, LOCALE, template);
122:
123: train_isMissing(template, false);
124:
125: train_getLog(model, log);
126:
127: train_getEmbeddedIds(model, "foo");
128:
129: train_getComponentIds(template, "foo");
130:
131: train_getTokens(template, new StartComponentToken(null, "foo",
132: "Fred", null, l), new EndElementToken(null));
133:
134: train_getEmbeddedComponentModel(model, "foo", emodel);
135:
136: train_getComponentType(emodel, "Barney");
137:
138: train_getMixinClassNames(emodel);
139:
140: log.error(ServicesMessages.compTypeConflict("foo", "Fred",
141: "Barney"));
142:
143: train_getComponentClassName(emodel, "foo.components.Barney");
144:
145: train_newComponentElement(elementFactory, rootElement, "foo",
146: "Barney", "foo.components.Barney", null, l,
147: childElement);
148:
149: train_getCompleteId(childElement, PAGE_CLASS_NAME + "/Barney");
150:
151: rootElement.addToTemplate(childElement);
152:
153: train_getParameterNames(emodel);
154:
155: // Alas, what comes next is the recursive call to load the child element
156:
157: train_getComponentResources(childElement, childResources);
158: train_getComponentModel(childResources, childModel);
159: train_getComponentClassName(childModel, CHILD_CLASS_NAME);
160: train_getTemplate(templateSource, childModel, LOCALE,
161: childTemplate);
162: train_isMissing(childTemplate, true);
163: train_newRenderBodyElement(elementFactory, childElement, body);
164: childElement.addToTemplate(body);
165:
166: replay();
167:
168: PageLoader loader = new PageLoaderImpl(templateSource,
169: elementFactory, null, null, resolver);
170:
171: loader.loadPage(LOGICAL_PAGE_NAME, LOCALE);
172:
173: verify();
174: }
175: }
|