1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.definition;
23
24 import java.io.BufferedWriter;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.OutputStreamWriter;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.net.URL;
31 import java.util.HashMap;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 /***
38 * Tests the reloadable definitions impl.
39 *
40 * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (Wed, 11 Apr 2007) $
41 */
42 public class TestReloadableDefinitionsFactory extends TestCase {
43
44 /***
45 * The time (in milliseconds) to wait to be sure that the system updates the
46 * modify date of a file.
47 */
48 private static final int SLEEP_MILLIS = 30000;
49
50 /***
51 * Creates a new instance of TestReloadableDefinitionsFactory.
52 *
53 * @param name The name of the test.
54 */
55 public TestReloadableDefinitionsFactory(String name) {
56 super(name);
57 }
58
59 /***
60 * Start the tests.
61 *
62 * @param theArgs the arguments. Not used
63 */
64 public static void main(String[] theArgs) {
65 junit.textui.TestRunner.main(
66 new String[]{TestReloadableDefinitionsFactory.class.getName()});
67 }
68
69 /***
70 * @return a test suite (<code>TestSuite</code>) that includes all methods
71 * starting with "test"
72 */
73 public static Test suite() {
74 return new TestSuite(TestReloadableDefinitionsFactory.class);
75 }
76
77 /***
78 * Tests reloading definitions impl.
79 *
80 * @throws Exception If something goes wrong.
81 */
82 public void testReloadableDefinitionsFactory() throws Exception {
83 DefinitionsFactory factory = new UrlDefinitionsFactory();
84
85
86 URL url = this.getClass().getClassLoader().getResource(
87 "org/apache/tiles/config/temp-defs.xml");
88
89 URI uri = null;
90 String urlPath = null;
91
92
93
94
95 if (url.getPath().startsWith("/")) {
96 urlPath = "file:" + url.getPath();
97 } else {
98 urlPath = "file:/" + url.getPath();
99 }
100
101
102
103
104
105
106
107
108
109 try {
110 uri = new URI(urlPath);
111 } catch (URISyntaxException e) {
112 uri = new URI(urlPath.replaceAll(" ", "%20"));
113 }
114
115 String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"
116 + "<!DOCTYPE tiles-definitions PUBLIC "
117 + "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" "
118 + "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n"
119 + "<tiles-definitions>"
120 + "<definition name=\"rewrite.test\" template=\"/test.jsp\">"
121 + "<put-attribute name=\"testparm\" value=\"testval\"/>"
122 + "</definition>"
123 + "</tiles-definitions>";
124
125 File file = new File(uri);
126 FileOutputStream fileOut = new FileOutputStream(file);
127 BufferedWriter writer = new BufferedWriter(
128 new OutputStreamWriter(fileOut));
129 writer.write(xml);
130 writer.close();
131
132 factory.init(new HashMap<String, String>());
133 factory.addSource(url);
134
135
136 Definitions definitions = factory.readDefinitions();
137
138 assertNotNull("rewrite.test definition not found.",
139 definitions.getDefinition("rewrite.test"));
140 assertEquals("Incorrect initial template value", "/test.jsp",
141 definitions.getDefinition("rewrite.test").getTemplate());
142
143 ReloadableDefinitionsFactory reloadable = (ReloadableDefinitionsFactory) factory;
144 assertEquals("Factory should be fresh.", false,
145 reloadable.refreshRequired());
146
147
148 Thread.sleep(SLEEP_MILLIS);
149
150
151 xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"
152 + "<!DOCTYPE tiles-definitions PUBLIC "
153 + "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" "
154 + "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n"
155 + "<tiles-definitions>"
156 + "<definition name=\"rewrite.test\" template=\"/newtest.jsp\">"
157 + "<put-attribute name=\"testparm\" value=\"testval\"/>"
158 + "</definition>"
159 + "</tiles-definitions>";
160
161 file = new File(uri);
162 fileOut = new FileOutputStream(file);
163 writer = new BufferedWriter(new OutputStreamWriter(fileOut));
164 writer.write(xml);
165 writer.close();
166
167
168 assertEquals("Factory should be stale.", true,
169 reloadable.refreshRequired());
170 definitions = factory.readDefinitions();
171 assertNotNull("rewrite.test definition not found.",
172 definitions.getDefinition("rewrite.test"));
173 assertEquals("Incorrect initial template value", "/newtest.jsp",
174 definitions.getDefinition("rewrite.test").getTemplate());
175 }
176 }