KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > config > test > VergeConfigMonitorTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.config.test;
8
9
10 import java.io.File JavaDoc;
11 import java.io.FileReader JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17
18 import junit.framework.Assert;
19
20 import com.inversoft.junit.WebTestCase;
21 import com.inversoft.junit.internal.http.MockHttpServletRequest;
22 import com.inversoft.junit.internal.http.MockServletContext;
23 import com.inversoft.verge.config.VergeConfigConstants;
24 import com.inversoft.verge.config.VergeConfigMonitor;
25 import com.inversoft.verge.config.servlet.ConfigServlet;
26 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigRegistry;
27 import com.inversoft.verge.mvc.controller.actionflow.config.Namespace;
28
29
30 /**
31  * <p>
32  * This class tests the verge config monitor.
33  * </p>
34  *
35  * @author Brian Pontarelli
36  * @since 2.0
37  * @version 2.0
38  */

39 public class VergeConfigMonitorTest extends WebTestCase {
40
41     /**
42      * The name of the test configuration file
43      */

44     public static final String JavaDoc TEST_FILE = "src/com/inversoft/verge/config/test/actionflow-test.xml";
45
46
47     /**
48      * Constructs a new <code>VergeConfigMonitorTest</code>
49      */

50     public VergeConfigMonitorTest(String JavaDoc name) {
51         super(name);
52         setLocal(true);
53     }
54
55
56     /**
57      * This method tests that the ActionFlowConfigBuilder starts a timer that works correctly
58      */

59     public void testConfigBuilderComplex() {
60
61         MockServletContext mock = getContext();
62         mock.setInitParameter(VergeConfigConstants.CONTEXT_PARAM, TEST_FILE);
63         mock.setInitParameter(VergeConfigMonitor.REBUILD_INTERVAL_PARAM, "10");
64
65         // This should spawn the Timer
66
try {
67             ConfigServlet cs = new ConfigServlet();
68             cs.init(createConfig("ConfigServlet"));
69         } catch (ServletException JavaDoc se) {
70             fail(se.toString());
71         }
72
73         // Touch the file so that it rebuilds next time
74
File JavaDoc file = new File JavaDoc(TEST_FILE);
75         touchFile(file);
76
77         ActionFlowConfigRegistry firstStore = ActionFlowConfigRegistry.getInstance(request);
78         Namespace first = ActionFlowConfigRegistry.getInstance(request).lookup("testNamespace");
79         assertNotNull("Should have found namespace", first);
80
81         try {
82             Thread.sleep(20000);
83         } catch (InterruptedException JavaDoc ie) {
84             fail(ie.toString());
85         }
86
87         ActionFlowConfigRegistry secondStore = ActionFlowConfigRegistry.getInstance(request);
88         Namespace second = ActionFlowConfigRegistry.getInstance(request).lookup("testNamespace");
89         assertNotNull("This request should have found the namespace", second);
90         assertSame("This request should have first and second store be the same object",
91             firstStore, secondStore);
92         assertSame("This request should have first and second namespace be the same object",
93             first, second);
94
95         // Create a new request to test the configuration
96
HttpServletRequest JavaDoc newRequest = new MockHttpServletRequest(null);
97         System.out.println("Doing new request test");
98         secondStore = ActionFlowConfigRegistry.getInstance(newRequest);
99         second = ActionFlowConfigRegistry.getInstance(newRequest).lookup("testNamespace");
100         Assert.assertNotNull("This thread should have found the namespace", secondStore);
101         Assert.assertNotSame("This thread should have a new instance of store", secondStore, firstStore);
102         Assert.assertNotSame("This thread should have a new isntance of namespace", second, first);
103
104         VergeConfigMonitor.shutdown();
105     }
106
107
108     /**
109      * Touchs the file
110      */

111     public static void touchFile(File JavaDoc file) {
112
113         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
114         try {
115             FileReader JavaDoc reader = new FileReader JavaDoc(file);
116             char [] chars = new char[1024];
117             int count;
118             do {
119                 count = reader.read(chars);
120                 if (count != -1) {
121                     buf.append(chars, 0, count);
122                 }
123             } while (count != -1);
124
125             reader.close();
126         } catch (IOException JavaDoc ioe) {
127             // smother
128
return;
129         }
130
131         try {
132             File JavaDoc touched = new File JavaDoc(file.getAbsolutePath());
133             FileWriter JavaDoc writer = new FileWriter JavaDoc(touched);
134             writer.write(buf.toString());
135             writer.close();
136         } catch (IOException JavaDoc ioe) {
137             // smother
138
}
139     }
140 }
Popular Tags