Search in sources :

Example 6 with StringResourceStream

use of org.apache.wicket.util.resource.StringResourceStream in project wicket by apache.

the class BaseWicketTester method startComponentInPage.

/**
 * Process a component. A web page will be automatically created with the {@code pageMarkup}
 * provided. In case {@code pageMarkup} is null, the markup will be automatically created with
 * {@link #createPageMarkup(String)}.
 * <p>
 * <strong>Note</strong>: the component id is set by the user. To reach any of its children use
 * this id + their relative path to the component itself. For example if the started component
 * has id <em>compId</em> and a Link child component component with id "link" then after
 * starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
 * </p>
 *
 * @param <C>
 *            the type of the component
 * @param component
 *            the component to be tested
 * @param pageMarkup
 *            the markup for the Page that will be automatically created. May be {@code null}.
 * @return The component processed
 */
public final <C extends Component> C startComponentInPage(final C component, IMarkupFragment pageMarkup) {
    Args.notNull(component, "component");
    // Create a page object and assign the markup
    Page page = createPage();
    if (page == null) {
        fail("The automatically created page should not be null.");
    }
    // Automatically create the page markup if not provided
    if (pageMarkup == null) {
        String markup = createPageMarkup(component.getId());
        if (markup == null) {
            fail("The markup for the automatically created page should not be null.");
        }
        try {
            // set a ContainerInfo to be able to use HtmlHeaderContainer so header contribution
            // still work. WICKET-3700
            ContainerInfo containerInfo = new ContainerInfo(page);
            MarkupResourceStream markupResourceStream = new MarkupResourceStream(new StringResourceStream(markup), containerInfo, page.getClass());
            MarkupParser markupParser = getApplication().getMarkupSettings().getMarkupFactory().newMarkupParser(markupResourceStream);
            pageMarkup = markupParser.parse();
        } catch (Exception e) {
            String errorMessage = "Error while parsing the markup for the autogenerated page: " + e.getMessage();
            log.error(errorMessage, e);
            fail(errorMessage);
        }
    }
    if (page instanceof StartComponentInPage) {
        ((StartComponentInPage) page).setPageMarkup(pageMarkup);
    } else {
        page.setMarkup(pageMarkup);
    }
    // Add the child component
    page.add(component);
    // Preserve 'componentInPage' because #startPage() needs to null-fy it
    ComponentInPage oldComponentInPage = componentInPage;
    // Process the page
    startPage(page);
    // Remember the "root" component processes and return it
    if (oldComponentInPage != null) {
        componentInPage = oldComponentInPage;
    } else {
        componentInPage = new ComponentInPage();
        componentInPage.component = component;
    }
    return component;
}
Also used : ContainerInfo(org.apache.wicket.markup.ContainerInfo) WebPage(org.apache.wicket.markup.html.WebPage) Page(org.apache.wicket.Page) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ParseException(java.text.ParseException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) MarkupParser(org.apache.wicket.markup.MarkupParser)

Example 7 with StringResourceStream

use of org.apache.wicket.util.resource.StringResourceStream in project wicket by apache.

the class BasicResourceReferenceMapperTest method versionStringInResourceFilename.

/**
 */
@Test
public void versionStringInResourceFilename() {
    final IStaticCacheableResource resource = new IStaticCacheableResource() {

        private static final long serialVersionUID = 1L;

        @Override
        public Serializable getCacheKey() {
            return null;
        }

        @Override
        public IResourceStream getResourceStream() {
            return new StringResourceStream("foo-bar");
        }

        @Override
        public void respond(Attributes attributes) {
        }

        @Override
        public boolean isCachingEnabled() {
            return true;
        }
    };
    IResourceCachingStrategy strategy = new FilenameWithVersionResourceCachingStrategy("-version-", new AlphaDigitResourceVersion("foobar"));
    INamedParameters params = new PageParameters();
    ResourceUrl url = new ResourceUrl("test.js", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-foobar.js", url.getFileName());
    strategy.undecorateUrl(url);
    assertEquals("test.js", url.getFileName());
    url = new ResourceUrl("test", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-foobar", url.getFileName());
    strategy.undecorateUrl(url);
    assertEquals("test", url.getFileName());
    // this behavior is o.k. since a browser could request an
    // previous version of the resource. for example we
    // could first have 'test-alpha.txt' which would be later replaced
    // by 'test-beta.txt' but in any case will point to
    // internal resource 'test.txt'
    url = new ResourceUrl("test-version-older.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version-.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version--------", params);
    strategy.undecorateUrl(url);
    assertEquals("test", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version-1.0.3-alpha.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // check a version that contains a dot which also marks the filename
    // extension
    strategy = new FilenameWithVersionResourceCachingStrategy("-version-", new StaticResourceVersion("1.0.4-beta"));
    url = new ResourceUrl("test.txt", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-1.0.4-beta.txt", url.getFileName());
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) FilenameWithVersionResourceCachingStrategy(org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy) INamedParameters(org.apache.wicket.request.mapper.parameter.INamedParameters) StaticResourceVersion(org.apache.wicket.request.resource.caching.version.StaticResourceVersion) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IResourceCachingStrategy(org.apache.wicket.request.resource.caching.IResourceCachingStrategy) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Test(org.junit.Test)

Example 8 with StringResourceStream

use of org.apache.wicket.util.resource.StringResourceStream in project wicket by apache.

the class ResourceTest method stringResourceStream.

/**
 * testStringResourceStream()
 */
@Test
public void stringResourceStream() {
    StringResourceStream resourceStream = new StringResourceStream(TEST_STRING);
    bindToApplicationAsResourceAndRequestIt(resourceStream);
    assertEquals(TEST_STRING.length(), tester.getContentLengthFromResponseHeader());
}
Also used : StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) Test(org.junit.Test)

Example 9 with StringResourceStream

use of org.apache.wicket.util.resource.StringResourceStream in project wicket by apache.

the class EnclosureTest method testAtrribute.

/**
 * https://issues.apache.org/jira/browse/WICKET-3842
 */
@Test
public void testAtrribute() {
    /**
     * Page for the test
     */
    class TestPage extends WebPage implements IMarkupResourceStreamProvider {

        private static final long serialVersionUID = 1L;

        public TestPage() {
            final Label l = new Label("msg", "$label$");
            add(l);
            add(new Link<Void>("b") {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick() {
                    l.setVisible(!l.isVisible());
                }
            });
        }

        @Override
        public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
            return new StringResourceStream("<html><body><div wicket:enclosure='msg'><span wicket:id='msg'></span></div><input type='button' value='Toggle' wicket:id='b'/></body></html>");
        }
    }
    tester.startPage(new TestPage());
    assertTrue(tester.getLastResponseAsString().contains("$label$"));
    // toggle visibility of enclosure to false
    tester.clickLink("b");
    assertFalse(tester.getLastResponseAsString().contains("$label$"));
    // toggle visibility of enclosure to back to true
    tester.clickLink("b");
    assertTrue(tester.getLastResponseAsString().contains("$label$"));
}
Also used : WebPage(org.apache.wicket.markup.html.WebPage) MarkupContainer(org.apache.wicket.MarkupContainer) Label(org.apache.wicket.markup.html.basic.Label) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) IMarkupResourceStreamProvider(org.apache.wicket.markup.IMarkupResourceStreamProvider) Test(org.junit.Test)

Example 10 with StringResourceStream

use of org.apache.wicket.util.resource.StringResourceStream in project wicket by apache.

the class CustomMarkupLabel method getMarkupResourceStream.

@Override
public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
    // the markup is loaded from database in our real application
    StringResourceStream res = new StringResourceStream(SAMPLE_MARKUP);
    res.setCharset(Charset.forName("UTF-8"));
    return res;
}
Also used : StringResourceStream(org.apache.wicket.util.resource.StringResourceStream)

Aggregations

StringResourceStream (org.apache.wicket.util.resource.StringResourceStream)13 Test (org.junit.Test)9 MarkupContainer (org.apache.wicket.MarkupContainer)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)3 WebPage (org.apache.wicket.markup.html.WebPage)3 ParseException (java.text.ParseException)2 IMarkupResourceStreamProvider (org.apache.wicket.markup.IMarkupResourceStreamProvider)2 TagTester (org.apache.wicket.util.tester.TagTester)2 Template (freemarker.template.Template)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Page (org.apache.wicket.Page)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 AjaxSubmitLink (org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)1 ContainerInfo (org.apache.wicket.markup.ContainerInfo)1 MarkupParser (org.apache.wicket.markup.MarkupParser)1