/**
* Tests reloading definitions impl.
*/
public void testReloadableDefinitionsFactory() throws Exception {
DefinitionsFactory factory = new UrlDefinitionsFactory();
// Set up multiple data sources.
URL url = this.getClass().getClassLoader().getResource(
"org/apache/tiles/config/temp-defs.xml");
URI uri = null;
String urlPath = null;
// The following madness is necessary b/c of the way Windows hanndles URLs.
// We must add a slash to the protocol if Windows does not. But we cannot
// add a slash to Unix paths b/c they already have one.
if (url.getPath().startsWith("/")) {
urlPath = "file:" + url.getPath();
} else {
urlPath = "file:/" + url.getPath();
}
// The following second madness is necessary b/c sometimes spaces
// are encoded as '%20', sometimes they are not. For example in
// Windows 2000 under Eclipse they are encoded, under the prompt of
// Windows 2000 they are not.
// It seems to be in the different behaviour of
// sun.misc.Launcher$AppClassLoader (called under Eclipse) and
// java.net.URLClassLoader (under maven).
// And an URL accepts spaces while URIs need '%20'.
try {
uri = new URI(urlPath);
} catch (URISyntaxException e) {
uri = new URI(urlPath.replaceAll(" ", "%20"));
}
String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +
"<!DOCTYPE tiles-definitions PUBLIC " +
"\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" " +
"\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n" +
"<tiles-definitions>" +
"<definition name=\"rewrite.test\" template=\"/test.jsp\">" +
"<put-attribute name=\"testparm\" value=\"testval\"/>" +
"</definition>" +
"</tiles-definitions>";
File file = new File(uri);
FileOutputStream fileOut = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(fileOut));
writer.write(xml);
writer.close();
factory.init(new HashMap());
factory.addSource(url);
// Parse files.
ComponentDefinitions definitions = factory.readDefinitions();
assertNotNull("rewrite.test definition not found.",
definitions.getDefinition("rewrite.test"));
assertEquals("Incorrect initial template value", "/test.jsp",
definitions.getDefinition("rewrite.test").getTemplate());
ReloadableDefinitionsFactory reloadable = (ReloadableDefinitionsFactory) factory;
assertEquals("Factory should be fresh.", false,
reloadable.refreshRequired());
// Make sure the system actually updates the timestamp.
Thread.sleep(30000);
// Set up multiple data sources.
xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +
"<!DOCTYPE tiles-definitions PUBLIC " +
"\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" " +
"\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n" +
"<tiles-definitions>" +
"<definition name=\"rewrite.test\" template=\"/newtest.jsp\">" +
"<put-attribute name=\"testparm\" value=\"testval\"/>" +
"</definition>" +
"</tiles-definitions>";
file = new File(uri);
fileOut = new FileOutputStream(file);
writer = new BufferedWriter(new OutputStreamWriter(fileOut));
writer.write(xml);
writer.close();
assertEquals("Factory should be stale.", true,
reloadable.refreshRequired());
definitions = factory.readDefinitions();
assertNotNull("rewrite.test definition not found.",
definitions.getDefinition("rewrite.test"));
assertEquals("Incorrect initial template value", "/newtest.jsp",
definitions.getDefinition("rewrite.test").getTemplate());
}