Search in sources :

Example 1 with InvalidationListener

use of org.apache.tapestry5.commons.services.InvalidationListener in project tapestry-5 by apache.

the class ComponentTemplateSourceImplTest method invalidation.

/**
 * Tests resource invalidation.
 */
@Test
public void invalidation() throws Exception {
    File rootDir = createClasspathRoot();
    URLClassLoader loader = newLoaderWithClasspathRoot(rootDir);
    ComponentModel model = mockComponentModel();
    File packageDir = new File(rootDir, "baz");
    packageDir.mkdirs();
    File f = new File(packageDir, "Biff.tml");
    f.createNewFile();
    Resource baseResource = new ClasspathResource(loader, "baz/Biff.class");
    Resource localized = baseResource.withExtension(TapestryConstants.TEMPLATE_EXTENSION);
    TemplateParser parser = mockTemplateParser();
    ComponentTemplate template = mockComponentTemplate();
    InvalidationListener listener = mockInvalidationListener();
    train_getComponentClassName(model, "baz.Biff");
    ComponentResourceLocator locator = mockLocator(model, english, localized);
    train_parseTemplate(parser, localized, template);
    replay();
    ComponentTemplateSourceImpl source = new ComponentTemplateSourceImpl(false, parser, locator, converter, componentRequestSelectorAnalyzer, threadLocale);
    source.addInvalidationListener(listener);
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    // Check for updates (which won't be found).
    source.checkForUpdates();
    // A second pass will test the caching (the
    // parser is not invoked).
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    verify();
    // Now, change the file and processInbound an UpdateEvent.
    touch(f);
    listener.objectWasInvalidated();
    replay();
    // Check for updates (which will be found).
    source.checkForUpdates();
    verify();
    // Check that the cache really is cleared.
    train_getComponentClassName(model, "baz.Biff");
    expect(locator.locateTemplate(model, english)).andReturn(localized);
    train_parseTemplate(parser, localized, template);
    replay();
    assertSame(source.getTemplate(model, Locale.ENGLISH), template);
    verify();
}
Also used : ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) URLClassLoader(java.net.URLClassLoader) ComponentModel(org.apache.tapestry5.model.ComponentModel) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) ComponentResourceLocator(org.apache.tapestry5.services.pageload.ComponentResourceLocator) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate) InvalidationListener(org.apache.tapestry5.commons.services.InvalidationListener) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1 Resource (org.apache.tapestry5.commons.Resource)1 InvalidationListener (org.apache.tapestry5.commons.services.InvalidationListener)1 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)1 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)1 ComponentModel (org.apache.tapestry5.model.ComponentModel)1 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)1 Test (org.testng.annotations.Test)1