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;
}
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());
}
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());
}
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$"));
}
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;
}
Aggregations