Search in sources :

Example 6 with IResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.IResourceStreamLocator in project wicket by apache.

the class CachingResourceStreamLocatorTest method notExistingResource.

/**
 * Tests NullResourceStreamReference
 */
@Test
public void notExistingResource() {
    IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    cachingLocator.locate(String.class, "path");
    cachingLocator.locate(String.class, "path");
    // there is no resource with that Key so a "miss" will be cached and expect 1 call to the
    // delegate
    verify(resourceStreamLocator, times(1)).locate(String.class, "path");
}
Also used : CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) Test(org.junit.Test)

Example 7 with IResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.IResourceStreamLocator in project wicket by apache.

the class CachingResourceStreamLocatorTest method fileResourceDifferentExtensions.

/**
 * Tests two FileResourceStreamReferences with different extensions
 */
@Test
public void fileResourceDifferentExtensions() {
    IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
    FileResourceStream frs = new FileResourceStream(new File("."));
    when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, "extension", true)).thenReturn(frs);
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension2", true);
    // there is a file resource with that Key so expect just one call to the delegate
    verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", null, "extension", true);
    verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", null, "extension2", true);
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) File(java.io.File) Test(org.junit.Test)

Example 8 with IResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.IResourceStreamLocator in project wicket by apache.

the class CachingResourceStreamLocatorTest method strictMatchingDoesntInvalidateNonStrictMatching.

/**
 * Tests strict before non-strict matching with a specific locale.
 */
@Test
public void strictMatchingDoesntInvalidateNonStrictMatching() {
    IResourceStreamLocator resourceStreamLocator = new ResourceStreamLocator(new ClassPathResourceFinder(""));
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    String style = null;
    String variation = null;
    Locale locale = new Locale("nl", "NL");
    String extension = null;
    String filename = "org/apache/wicket/ajax/res/js/wicket-ajax-jquery-debug.js";
    // a strict lookup of a localized resource should not find the non-localized resource
    IResourceStream strictLocate = cachingLocator.locate(AbstractDefaultAjaxBehavior.class, filename, style, variation, locale, extension, true);
    assertThat(strictLocate, is(nullValue()));
    // but a non-strict lookup with a locale should fall back to the non-localized resource
    IResourceStream nonStrictLocate = cachingLocator.locate(AbstractDefaultAjaxBehavior.class, filename, style, variation, locale, extension, false);
    assertThat(nonStrictLocate, is(not(nullValue())));
}
Also used : Locale(java.util.Locale) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStream(org.apache.wicket.util.resource.IResourceStream) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) ClassPathResourceFinder(org.apache.wicket.core.util.resource.ClassPathResourceFinder) Test(org.junit.Test)

Example 9 with IResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.IResourceStreamLocator in project wicket by apache.

the class MarkupParserTest method fileDocument.

/**
 * @throws ParseException
 * @throws ResourceStreamNotFoundException
 * @throws IOException
 */
@Test
public void fileDocument() throws ParseException, ResourceStreamNotFoundException, IOException {
    IResourceStreamLocator locator = new ResourceStreamLocator();
    MarkupResourceStream resource = newMarkupResourceStream(locator, getClass(), "1", null, null, "html");
    MarkupParser parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    IMarkupFragment tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "2", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "3", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "4", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    // File from jar (URL resource)
    resource = newMarkupResourceStream(locator, PageExpiredErrorPage.class, null, null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "5", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "6", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "7", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "8", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "9", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
// Assert.assertEquals(docText, tokens.get(0).toString());
}
Also used : PageExpiredErrorPage(org.apache.wicket.markup.html.pages.PageExpiredErrorPage) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) Test(org.junit.Test)

Example 10 with IResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.IResourceStreamLocator in project wicket by apache.

the class DefaultMarkupResourceStreamProvider method getMarkupResourceStream.

/**
 * Create a new markup resource stream for the container.
 * <p>
 * Note: it will only called once, the IResourceStream will be cached by MarkupCache.
 * <p>
 * Note: IResourceStreamLocators should be used in case the strategy to find a markup resource
 * should be extended for ALL components of your application.
 *
 * @see org.apache.wicket.core.util.resource.locator.IResourceStreamLocator
 * @see org.apache.wicket.markup.DefaultMarkupResourceStreamProvider
 *
 * @param container
 * @param containerClass
 *            The container the markup should be associated with
 * @return A IResourceStream if the resource was found
 */
@Override
public IResourceStream getMarkupResourceStream(final MarkupContainer container, Class<?> containerClass) {
    // Get locator to search for the resource
    final IResourceStreamLocator locator = Application.get().getResourceSettings().getResourceStreamLocator();
    String style = container.getStyle();
    String variation = container.getVariation();
    Locale locale = container.getLocale();
    MarkupType markupType = container.getMarkupType();
    String ext = (markupType != null ? markupType.getExtension() : null);
    // resource.
    while (containerClass != MarkupContainer.class) {
        String path = containerClass.getName().replace('.', '/');
        IResourceStream resourceStream = locator.locate(containerClass, path, style, variation, locale, ext, false);
        // Did we find it already?
        if (resourceStream != null) {
            return new MarkupResourceStream(resourceStream, new ContainerInfo(containerClass, container), containerClass);
        }
        // Walk up the class hierarchy one level, if markup has not
        // yet been found
        containerClass = containerClass.getSuperclass();
    }
    return null;
}
Also used : Locale(java.util.Locale) IResourceStream(org.apache.wicket.util.resource.IResourceStream) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)

Aggregations

IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)17 Test (org.junit.Test)11 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)9 ResourceStreamLocator (org.apache.wicket.core.util.resource.locator.ResourceStreamLocator)7 IResourceStream (org.apache.wicket.util.resource.IResourceStream)7 Locale (java.util.Locale)6 ClassPathResourceFinder (org.apache.wicket.core.util.resource.ClassPathResourceFinder)4 File (java.io.File)2 FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)2 IOException (java.io.IOException)1 URL (java.net.URL)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 UrlResourceStream (org.apache.wicket.core.util.resource.UrlResourceStream)1 ContainerInfo (org.apache.wicket.markup.ContainerInfo)1 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)1 MarkupResourceStream (org.apache.wicket.markup.MarkupResourceStream)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 MarkupType (org.apache.wicket.markup.MarkupType)1 PageExpiredErrorPage (org.apache.wicket.markup.html.pages.PageExpiredErrorPage)1 IFixedLocationResourceStream (org.apache.wicket.util.resource.IFixedLocationResourceStream)1