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.test;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newSet;
018: import static org.easymock.EasyMock.eq;
019: import static org.easymock.EasyMock.isA;
020:
021: import java.io.BufferedInputStream;
022: import java.io.BufferedReader;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.LineNumberReader;
026: import java.io.Reader;
027: import java.util.Arrays;
028: import java.util.Locale;
029: import java.util.ResourceBundle;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.tapestry.ComponentResources;
033: import org.apache.tapestry.ComponentResourcesCommon;
034: import org.apache.tapestry.Link;
035: import org.apache.tapestry.internal.InternalComponentResources;
036: import org.apache.tapestry.internal.InternalConstants;
037: import org.apache.tapestry.internal.SingleKeySymbolProvider;
038: import org.apache.tapestry.internal.SyntheticModuleDef;
039: import org.apache.tapestry.internal.SyntheticSymbolSourceContributionDef;
040: import org.apache.tapestry.internal.events.InvalidationListener;
041: import org.apache.tapestry.internal.parser.ComponentTemplate;
042: import org.apache.tapestry.internal.parser.TemplateToken;
043: import org.apache.tapestry.internal.services.ComponentInstantiatorSource;
044: import org.apache.tapestry.internal.services.ComponentInvocationMap;
045: import org.apache.tapestry.internal.services.ComponentTemplateSource;
046: import org.apache.tapestry.internal.services.DocumentScriptBuilder;
047: import org.apache.tapestry.internal.services.Instantiator;
048: import org.apache.tapestry.internal.services.LinkFactory;
049: import org.apache.tapestry.internal.services.LinkFactoryListener;
050: import org.apache.tapestry.internal.services.PageElementFactory;
051: import org.apache.tapestry.internal.services.PageLoader;
052: import org.apache.tapestry.internal.services.PagePool;
053: import org.apache.tapestry.internal.services.PageResponseRenderer;
054: import org.apache.tapestry.internal.services.PageTemplateLocator;
055: import org.apache.tapestry.internal.services.RequestPageCache;
056: import org.apache.tapestry.internal.services.ResourceCache;
057: import org.apache.tapestry.internal.services.ResourceStreamer;
058: import org.apache.tapestry.internal.services.TemplateParser;
059: import org.apache.tapestry.internal.structure.ComponentPageElement;
060: import org.apache.tapestry.internal.structure.Page;
061: import org.apache.tapestry.internal.structure.PageElement;
062: import org.apache.tapestry.ioc.AnnotationProvider;
063: import org.apache.tapestry.ioc.Location;
064: import org.apache.tapestry.ioc.Messages;
065: import org.apache.tapestry.ioc.Registry;
066: import org.apache.tapestry.ioc.RegistryBuilder;
067: import org.apache.tapestry.ioc.Resource;
068: import org.apache.tapestry.ioc.def.ContributionDef;
069: import org.apache.tapestry.ioc.def.ModuleDef;
070: import org.apache.tapestry.ioc.internal.InternalRegistry;
071: import org.apache.tapestry.ioc.internal.util.MessagesImpl;
072: import org.apache.tapestry.ioc.services.SymbolProvider;
073: import org.apache.tapestry.model.ComponentModel;
074: import org.apache.tapestry.model.EmbeddedComponentModel;
075: import org.apache.tapestry.runtime.Component;
076: import org.apache.tapestry.runtime.RenderQueue;
077: import org.apache.tapestry.services.ComponentClassResolver;
078: import org.apache.tapestry.services.Request;
079: import org.apache.tapestry.services.TapestryModule;
080: import org.apache.tapestry.test.TapestryTestCase;
081: import org.easymock.EasyMock;
082: import org.testng.annotations.AfterMethod;
083: import org.testng.annotations.AfterSuite;
084: import org.testng.annotations.BeforeSuite;
085:
086: /**
087: * Contains additional factory and training methods related to internal interfaces.
088: */
089: public class InternalBaseTestCase extends TapestryTestCase implements
090: Registry {
091: private static Registry _registry;
092:
093: private Messages _validationMessages;
094:
095: @BeforeSuite
096: public final void setup_registry() {
097: RegistryBuilder builder = new RegistryBuilder();
098:
099: builder.add(TapestryModule.class);
100:
101: // A synthetic module to ensure that the tapestry.alias-mode is set correctly.
102:
103: SymbolProvider provider = new SingleKeySymbolProvider(
104: InternalConstants.TAPESTRY_ALIAS_MODE_SYMBOL, "servlet");
105: ContributionDef contribution = new SyntheticSymbolSourceContributionDef(
106: "AliasMode", provider, "before:ApplicationDefaults");
107:
108: ModuleDef module = new SyntheticModuleDef(contribution);
109:
110: builder.add(module);
111:
112: _registry = builder.build();
113:
114: // _registry.getService(Alias.class).setMode("servlet");
115:
116: _registry.eagerLoadServices();
117: }
118:
119: @AfterSuite
120: public final void shutdown_registry() {
121: _registry.shutdown();
122:
123: _registry = null;
124: }
125:
126: @AfterMethod
127: public final void cleanupThread() {
128: _registry.cleanupThread();
129: }
130:
131: public void eagerLoadServices() {
132: _registry.eagerLoadServices();
133: }
134:
135: public final <T> T getObject(Class<T> objectType,
136: AnnotationProvider annotationProvider) {
137: return _registry.getObject(objectType, annotationProvider);
138: }
139:
140: public final <T> T getService(Class<T> serviceInterface) {
141: return _registry.getService(serviceInterface);
142: }
143:
144: public final <T> T getService(String serviceId,
145: Class<T> serviceInterface) {
146: return _registry.getService(serviceId, serviceInterface);
147: }
148:
149: public final <T> T autobuild(Class<T> clazz) {
150: return _registry.autobuild(clazz);
151: }
152:
153: public final void shutdown() {
154: throw new UnsupportedOperationException(
155: "No registry shutdown until @AfterSuite.");
156: }
157:
158: protected final InternalComponentResources mockInternalComponentResources() {
159: return newMock(InternalComponentResources.class);
160: }
161:
162: protected final ComponentTemplate mockComponentTemplate() {
163: return newMock(ComponentTemplate.class);
164: }
165:
166: protected final <T> void train_getService(
167: InternalRegistry registry, String serviceId,
168: Class<T> serviceInterface, T service) {
169: expect(registry.getService(serviceId, serviceInterface))
170: .andReturn(service);
171:
172: }
173:
174: protected final ComponentInstantiatorSource mockComponentInstantiatorSource() {
175: return newMock(ComponentInstantiatorSource.class);
176: }
177:
178: protected final Page mockPage() {
179: return newMock(Page.class);
180: }
181:
182: protected final PageLoader mockPageLoader() {
183: return newMock(PageLoader.class);
184: }
185:
186: protected final void train_loadPage(PageLoader loader,
187: String pageName, Locale locale, Page page) {
188: expect(loader.loadPage(pageName, locale)).andReturn(page);
189: }
190:
191: protected final PagePool mockPagePool() {
192: return newMock(PagePool.class);
193: }
194:
195: protected RenderQueue mockRenderQueue() {
196: return newMock(RenderQueue.class);
197: }
198:
199: protected final void train_parseTemplate(TemplateParser parser,
200: Resource resource, ComponentTemplate template) {
201: expect(parser.parseTemplate(resource)).andReturn(template);
202: }
203:
204: protected final TemplateParser mockTemplateParser() {
205: return newMock(TemplateParser.class);
206: }
207:
208: protected final ComponentPageElement mockComponentPageElement() {
209: return newMock(ComponentPageElement.class);
210: }
211:
212: protected final void train_getComponent(
213: ComponentPageElement element, Component component) {
214: expect(element.getComponent()).andReturn(component)
215: .atLeastOnce();
216: }
217:
218: protected final void train_getId(
219: ComponentResourcesCommon resources, String id) {
220: expect(resources.getId()).andReturn(id).atLeastOnce();
221: }
222:
223: protected final void train_getNestedId(
224: ComponentResourcesCommon resources, String nestedId) {
225: expect(resources.getNestedId()).andReturn(nestedId)
226: .atLeastOnce();
227: }
228:
229: protected final void train_getContextPath(Request request,
230: String contextPath) {
231: expect(request.getContextPath()).andReturn(contextPath)
232: .atLeastOnce();
233: }
234:
235: protected final void train_resolvePageClassNameToPageName(
236: ComponentClassResolver resolver, String pageClassName,
237: String pageName) {
238: expect(resolver.resolvePageClassNameToPageName(pageClassName))
239: .andReturn(pageName);
240: }
241:
242: protected final void train_getContainingPage(
243: ComponentPageElement element, Page page) {
244: expect(element.getContainingPage()).andReturn(page)
245: .atLeastOnce();
246: }
247:
248: protected final void train_getComponentResources(
249: ComponentPageElement element,
250: InternalComponentResources resources) {
251: expect(element.getComponentResources()).andReturn(resources)
252: .atLeastOnce();
253: }
254:
255: protected final void train_getComponentClassName(
256: EmbeddedComponentModel model, String className) {
257: expect(model.getComponentClassName()).andReturn(className)
258: .atLeastOnce();
259: }
260:
261: protected final void train_newRenderBodyElement(
262: PageElementFactory elementFactory,
263: ComponentPageElement component, PageElement body) {
264: expect(elementFactory.newRenderBodyElement(component))
265: .andReturn(body);
266: }
267:
268: protected final PageElement mockPageElement() {
269: return newMock(PageElement.class);
270: }
271:
272: protected final void train_getParameterNames(
273: EmbeddedComponentModel model, String... names) {
274: expect(model.getParameterNames()).andReturn(
275: Arrays.asList(names));
276: }
277:
278: protected final void train_newComponentElement(
279: PageElementFactory elementFactory,
280: ComponentPageElement container, String embeddedId,
281: String embeddedType, String componentClassName,
282: String elementName, Location location,
283: ComponentPageElement embedded) {
284: expect(
285: elementFactory.newComponentElement(isA(Page.class),
286: eq(container), eq(embeddedId),
287: eq(embeddedType), eq(componentClassName),
288: eq(elementName), eq(location))).andReturn(
289: embedded);
290: }
291:
292: protected final void train_getComponentType(
293: EmbeddedComponentModel emodel, String componentType) {
294: expect(emodel.getComponentType()).andReturn(componentType)
295: .atLeastOnce();
296: }
297:
298: protected final void train_getEmbeddedComponentModel(
299: ComponentModel model, String embeddedId,
300: EmbeddedComponentModel emodel) {
301: expect(model.getEmbeddedComponentModel(embeddedId)).andReturn(
302: emodel).atLeastOnce();
303: }
304:
305: protected final EmbeddedComponentModel mockEmbeddedComponentModel() {
306: return newMock(EmbeddedComponentModel.class);
307: }
308:
309: protected final PageElementFactory mockPageElementFactory() {
310: return newMock(PageElementFactory.class);
311: }
312:
313: protected final ComponentTemplateSource mockComponentTemplateSource() {
314: return newMock(ComponentTemplateSource.class);
315: }
316:
317: protected final void train_getLog(ComponentModel model, Log log) {
318: expect(model.getLog()).andReturn(log).atLeastOnce();
319: }
320:
321: protected final void train_getTokens(ComponentTemplate template,
322: TemplateToken... tokens) {
323: expect(template.getTokens()).andReturn(Arrays.asList(tokens));
324: }
325:
326: protected final void train_getComponentIds(
327: ComponentTemplate template, String... ids) {
328: expect(template.getComponentIds()).andReturn(
329: newSet(Arrays.asList(ids)));
330: }
331:
332: protected final void train_getEmbeddedIds(ComponentModel model,
333: String... ids) {
334: expect(model.getEmbeddedComponentIds()).andReturn(
335: Arrays.asList(ids));
336: }
337:
338: protected void train_getTemplate(
339: ComponentTemplateSource templateSource,
340: ComponentModel model, Locale locale,
341: ComponentTemplate template) {
342: expect(templateSource.getTemplate(model, locale)).andReturn(
343: template);
344: }
345:
346: protected final void train_getComponentModel(
347: ComponentResources resources, ComponentModel model) {
348: expect(resources.getComponentModel()).andReturn(model)
349: .atLeastOnce();
350: }
351:
352: protected final void train_newRootComponentElement(
353: PageElementFactory elementFactory, String className,
354: ComponentPageElement rootElement) {
355: expect(
356: elementFactory.newRootComponentElement(isA(Page.class),
357: eq(className))).andReturn(rootElement);
358: }
359:
360: protected final void train_getModel(Instantiator ins,
361: ComponentModel model) {
362: expect(ins.getModel()).andReturn(model).atLeastOnce();
363: }
364:
365: protected final Instantiator mockInstantiator(Component component) {
366: Instantiator ins = newMock(Instantiator.class);
367:
368: expect(
369: ins.newInstance(EasyMock
370: .isA(InternalComponentResources.class)))
371: .andReturn(component);
372:
373: return ins;
374: }
375:
376: protected final RequestPageCache mockRequestPageCache() {
377: return newMock(RequestPageCache.class);
378: }
379:
380: protected final void train_getComponentElementByNestedId(Page page,
381: String nestedId, ComponentPageElement element) {
382: expect(page.getComponentElementByNestedId(nestedId)).andReturn(
383: element).atLeastOnce();
384: }
385:
386: protected final void train_getRootElement(Page page,
387: ComponentPageElement element) {
388: expect(page.getRootElement()).andReturn(element).atLeastOnce();
389: }
390:
391: protected final void train_isMissing(ComponentTemplate template,
392: boolean isMissing) {
393: expect(template.isMissing()).andReturn(isMissing).atLeastOnce();
394: }
395:
396: protected final void train_getMixinClassNames(
397: EmbeddedComponentModel model, String... names) {
398: expect(model.getMixinClassNames()).andReturn(
399: Arrays.asList(names));
400: }
401:
402: protected final void train_getRootComponent(Page page,
403: Component component) {
404: expect(page.getRootComponent()).andReturn(component)
405: .atLeastOnce();
406: }
407:
408: protected final ResourceCache mockResourceCache() {
409: return newMock(ResourceCache.class);
410: }
411:
412: protected final void train_requiresDigest(ResourceCache cache,
413: Resource resource, boolean requiresChecksum) {
414: expect(cache.requiresDigest(resource)).andReturn(
415: requiresChecksum);
416: }
417:
418: protected final InvalidationListener mockInvalidationListener() {
419: return newMock(InvalidationListener.class);
420: }
421:
422: protected final void train_getTimeModified(ResourceCache cache,
423: Resource resource, long timeModified) {
424: expect(cache.getTimeModified(resource)).andReturn(timeModified)
425: .atLeastOnce();
426: }
427:
428: protected final ResourceStreamer mockResourceStreamer() {
429: return newMock(ResourceStreamer.class);
430: }
431:
432: protected final void train_get(RequestPageCache cache,
433: String pageName, Page page) {
434: expect(cache.get(pageName)).andReturn(page).atLeastOnce();
435: }
436:
437: protected final void train_findPageTemplateResource(
438: PageTemplateLocator locator, ComponentModel model,
439: Locale locale, Resource resource) {
440: expect(locator.findPageTemplateResource(model, locale))
441: .andReturn(resource).atLeastOnce();
442: }
443:
444: protected final PageTemplateLocator mockPageTemplateLocator() {
445: return newMock(PageTemplateLocator.class);
446: }
447:
448: /** Returns the default validator messages. */
449: protected final Messages validationMessages() {
450: if (_validationMessages == null) {
451: ResourceBundle bundle = ResourceBundle
452: .getBundle("org.apache.tapestry.internal.ValidationMessages");
453:
454: _validationMessages = new MessagesImpl(bundle);
455: }
456:
457: return _validationMessages;
458: }
459:
460: protected final LinkFactoryListener mockLinkFactoryListener() {
461: return newMock(LinkFactoryListener.class);
462: }
463:
464: protected final ComponentInvocationMap mockComponentInvocationMap() {
465: return newMock(ComponentInvocationMap.class);
466: }
467:
468: protected final LinkFactory mockLinkFactory() {
469: return newMock(LinkFactory.class);
470: }
471:
472: protected final void train_createPageLink(LinkFactory factory,
473: Page page, Link link) {
474: expect(factory.createPageLink(page, false)).andReturn(link);
475: }
476:
477: protected final void train_isLoaded(
478: InternalComponentResources resources, boolean isLoaded) {
479: expect(resources.isLoaded()).andReturn(isLoaded);
480: }
481:
482: protected final void stub_isPageName(
483: ComponentClassResolver resolver, boolean result) {
484: expect(resolver.isPageName(isA(String.class))).andStubReturn(
485: result);
486: }
487:
488: protected final void train_isPageName(
489: ComponentClassResolver resolver, String pageName,
490: boolean result) {
491: expect(resolver.isPageName(pageName)).andReturn(result);
492: }
493:
494: protected final PageResponseRenderer mockPageResponseRenderer() {
495: return newMock(PageResponseRenderer.class);
496: }
497:
498: /**
499: * Reads the content of a file into a string. Each line is trimmed of line separators and
500: * leading/trailing whitespace.
501: *
502: * @param trim
503: * trim each line of whitespace
504: */
505: protected final String readFile(String file, boolean trim)
506: throws Exception {
507: InputStream is = getClass().getResourceAsStream(file);
508: is = new BufferedInputStream(is);
509: Reader reader = new BufferedReader(new InputStreamReader(is));
510: LineNumberReader in = new LineNumberReader(reader);
511:
512: StringBuilder buffer = new StringBuilder();
513:
514: while (true) {
515: String line = in.readLine();
516:
517: if (line == null)
518: break;
519:
520: if (trim)
521: line = line.trim();
522:
523: buffer.append(line);
524:
525: if (!trim)
526: buffer.append("\n");
527: }
528:
529: in.close();
530:
531: return buffer.toString();
532: }
533:
534: protected final DocumentScriptBuilder mockDocumentScriptBuilder() {
535: return newMock(DocumentScriptBuilder.class);
536: }
537:
538: protected final void train_canonicalizePageName(
539: ComponentClassResolver resolver, String pageName,
540: String canonicalized) {
541: expect(resolver.canonicalizePageName(pageName)).andReturn(
542: canonicalized);
543: }
544:
545: protected final void train_getLogicalName(Page page,
546: String logicalName) {
547: expect(page.getLogicalName()).andReturn(logicalName)
548: .atLeastOnce();
549: }
550:
551: protected final void train_resolvePageNameToClassName(
552: ComponentClassResolver resolver, String pageName,
553: String pageClassName) {
554: expect(resolver.resolvePageNameToClassName(pageName))
555: .andReturn(pageClassName).atLeastOnce();
556: }
557:
558: protected final void train_getLocale(Page page, Locale locale) {
559: expect(page.getLocale()).andReturn(locale).atLeastOnce();
560: }
561:
562: protected final void train_detached(Page page, boolean dirty) {
563: expect(page.detached()).andReturn(dirty);
564: }
565: }
|