View Javadoc

1   /*
2    * $Id: TestReloadableDefinitionsFactory.java 527536 2007-04-11 15:44:51Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // Set up multiple data sources.
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          // The following madness is necessary b/c of the way Windows hanndles URLs.
93          // We must add a slash to the protocol if Windows does not.  But we cannot
94          // add a slash to Unix paths b/c they already have one.
95          if (url.getPath().startsWith("/")) {
96              urlPath = "file:" + url.getPath();
97          } else {
98              urlPath = "file:/" + url.getPath();
99          }
100 
101         // The following second madness is necessary b/c sometimes spaces
102         // are encoded as '%20', sometimes they are not. For example in
103         // Windows 2000 under Eclipse they are encoded, under the prompt of
104         // Windows 2000 they are not.
105         // It seems to be in the different behaviour of
106         // sun.misc.Launcher$AppClassLoader (called under Eclipse) and
107         // java.net.URLClassLoader (under maven).
108         // And an URL accepts spaces while URIs need '%20'.
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         // Parse files.
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         // Make sure the system actually updates the timestamp.
148         Thread.sleep(SLEEP_MILLIS);
149 
150         // Set up multiple data sources.
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 }