001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestReloadDeclarations.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.meterware.httpunit.GetMethodWebRequest;
011: import com.meterware.httpunit.HttpNotFoundException;
012: import com.meterware.httpunit.WebConversation;
013: import com.meterware.httpunit.WebRequest;
014: import com.meterware.httpunit.WebResponse;
015: import com.uwyn.rife.TestCaseServerside;
016: import com.uwyn.rife.config.RifeConfig;
017: import com.uwyn.rife.resources.ResourceFinder;
018: import com.uwyn.rife.resources.ResourceFinderClasspath;
019: import com.uwyn.rife.tools.ExceptionUtils;
020: import com.uwyn.rife.tools.FileUtils;
021: import java.io.File;
022: import java.net.InetAddress;
023: import java.net.URL;
024:
025: public class TestReloadDeclarations extends TestCaseServerside {
026: public TestReloadDeclarations(int siteType, String name) {
027: super (siteType, name);
028: }
029:
030: public void testReloadElementDeclaration() throws Exception {
031: try {
032: // setup the temporary directory
033: String site_dir = RifeConfig.Global.getTempPath()
034: + File.separator + "reloadsite";
035: File site_dir_file = new File(site_dir);
036: site_dir_file.mkdirs();
037: String element_dir = RifeConfig.Global.getTempPath()
038: + File.separator + "reloadelement";
039: File element_dir_file = new File(element_dir);
040: element_dir_file.mkdirs();
041:
042: // setup the site xml file
043: ResourceFinder resource_finder = null;
044: URL resource = null;
045: File site_file = null;
046: File element_file = null;
047:
048: resource_finder = ResourceFinderClasspath.getInstance();
049:
050: // create the site xml file
051: site_file = new File(site_dir + File.separator + "site.xml");
052: site_file.delete();
053: element_file = new File(element_dir + File.separator
054: + "simple.xml");
055: element_file.delete();
056: resource = resource_finder
057: .getResource("site/reload_element_blueprint.xml");
058: FileUtils.copy(resource.openStream(), site_file);
059:
060: // create the first element xml file
061: resource = resource_finder
062: .getResource("element/engine/simple_plain.xml");
063: FileUtils.copy(resource.openStream(), element_file);
064:
065: // test the site
066: setupSite("reloadsite/site.xml");
067: WebConversation conversation = new WebConversation();
068: WebRequest request = null;
069: WebResponse response = null;
070:
071: request = new GetMethodWebRequest(
072: "http://localhost:8181/reloadtest");
073:
074: // get the host name
075: String hostname = InetAddress.getByName("127.0.0.1")
076: .getHostName();
077:
078: // get the content of the first element
079: response = conversation.getResponse(request);
080: assertEquals("text/plain", response.getContentType());
081: assertEquals("Just some text 127.0.0.1:" + hostname
082: + ":.SIMPLE:", response.getText());
083:
084: RifeConfig.Global.setAutoReloadDelay(3000);
085:
086: // wait a second
087: Thread.sleep(1000);
088:
089: // overwrite the element file with new content
090: resource = resource_finder
091: .getResource("element/engine/simple_html.xml");
092: FileUtils.copy(resource.openStream(), element_file);
093:
094: // wait a second
095: Thread.sleep(1000);
096:
097: // get the content of the first element
098: response = conversation.getResponse(request);
099: assertEquals("text/plain", response.getContentType());
100: assertEquals("Just some text 127.0.0.1:" + hostname
101: + ":.SIMPLE:", response.getText());
102:
103: // wait two seconds
104: Thread.sleep(2000);
105:
106: // get the content of the new element
107: response = conversation.getResponse(request);
108: assertEquals("text/html", response.getContentType());
109: assertEquals("Just some text 127.0.0.1:" + hostname
110: + ":.SIMPLE:", response.getText());
111:
112: // clean up the copied files
113: element_file.delete();
114: site_file.delete();
115: } catch (Throwable e) {
116: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
117: return;
118: } finally {
119: RifeConfig.Global.setAutoReloadDelay(0);
120: }
121: }
122:
123: public void testReloadSiteDeclaration() throws Exception {
124: try {
125: // setup the temporary directory
126: String site_dir = RifeConfig.Global.getTempPath()
127: + File.separator + "reloadsite";
128: File site_dir_file = new File(site_dir);
129: site_dir_file.mkdirs();
130: String element_dir = RifeConfig.Global.getTempPath()
131: + File.separator + "reloadelement";
132: File element_dir_file = new File(element_dir);
133: element_dir_file.mkdirs();
134:
135: // setup the site xml file
136: ResourceFinder resource_finder = null;
137: URL resource = null;
138: File site_file = null;
139: File element_file = null;
140:
141: resource_finder = ResourceFinderClasspath.getInstance();
142: site_file = new File(site_dir + File.separator + "site.xml");
143: site_file.delete();
144: element_file = new File(element_dir + File.separator
145: + "simple.xml");
146: element_file.delete();
147:
148: // create the site xml file
149: resource = resource_finder
150: .getResource("site/reload_element_blueprint.xml");
151: FileUtils.copy(resource.openStream(), site_file);
152:
153: // create the first element xml file
154: resource = resource_finder
155: .getResource("element/engine/simple_html.xml");
156: FileUtils.copy(resource.openStream(), element_file);
157:
158: // test the site
159: setupSite("reloadsite/site.xml");
160: WebConversation conversation = new WebConversation();
161: WebRequest request = null;
162: WebResponse response = null;
163:
164: // Get the host name
165: String hostname = InetAddress.getByName("127.0.0.1")
166: .getHostName();
167:
168: request = new GetMethodWebRequest(
169: "http://localhost:8181/reloadtest");
170:
171: // get the content of the first element
172: response = conversation.getResponse(request);
173: assertEquals("Just some text 127.0.0.1:" + hostname
174: + ":.SIMPLE:", response.getText());
175:
176: RifeConfig.Global.setAutoReloadDelay(2000);
177:
178: // wait a second
179: Thread.sleep(1000);
180:
181: // overwrite the element file with new content
182: resource = resource_finder
183: .getResource("site/reload_site_blueprint.xml");
184: FileUtils.copy(resource.openStream(), site_file);
185:
186: // get the content of the first element
187: response = conversation.getResponse(request);
188: assertEquals("Just some text 127.0.0.1:" + hostname
189: + ":.SIMPLE:", response.getText());
190:
191: // wait two seconds and a half
192: Thread.sleep(2500);
193:
194: // get the content of the new element
195: try {
196: response = conversation.getResponse(request);
197: fail();
198: } catch (HttpNotFoundException e) {
199: assertTrue(true);
200: }
201:
202: request = new GetMethodWebRequest(
203: "http://localhost:8181/newurl");
204: response = conversation.getResponse(request);
205: assertEquals("Just some text 127.0.0.1:" + hostname
206: + ":.SIMPLE:", response.getText());
207:
208: // clean up the copied files
209: element_file.delete();
210: site_file.delete();
211: } catch (Throwable e) {
212: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
213: return;
214: } finally {
215: RifeConfig.Global.setAutoReloadDelay(0);
216: }
217: }
218:
219: public void testReloadSubsiteDeclaration() throws Exception {
220: try {
221: // setup the temporary directory
222: String site_dir = RifeConfig.Global.getTempPath()
223: + File.separator + "reloadsite";
224: File site_dir_file = new File(site_dir);
225: site_dir_file.mkdirs();
226: String element_dir = RifeConfig.Global.getTempPath()
227: + File.separator + "reloadelement";
228: File element_dir_file = new File(element_dir);
229: element_dir_file.mkdirs();
230:
231: // setup the site xml file
232: ResourceFinder resource_finder = null;
233: URL resource = null;
234: File site_file = null;
235: File subsite_file = null;
236: File element_file = null;
237:
238: resource_finder = ResourceFinderClasspath.getInstance();
239: site_file = new File(site_dir + File.separator + "site.xml");
240: site_file.delete();
241: subsite_file = new File(site_dir + File.separator
242: + "subsite.xml");
243: subsite_file.delete();
244: element_file = new File(element_dir + File.separator
245: + "simple.xml");
246: element_file.delete();
247:
248: // create the site xml file
249: resource = resource_finder
250: .getResource("site/reload_subsite_blueprint.xml");
251: FileUtils.copy(resource.openStream(), site_file);
252:
253: // create the site xml file
254: resource = resource_finder
255: .getResource("site/reload_element_blueprint.xml");
256: FileUtils.copy(resource.openStream(), subsite_file);
257:
258: // create the first element xml file
259: resource = resource_finder
260: .getResource("element/engine/simple_html.xml");
261: FileUtils.copy(resource.openStream(), element_file);
262:
263: // test the site
264: setupSite("reloadsite/site.xml");
265: WebConversation conversation = new WebConversation();
266: WebRequest request = null;
267: WebResponse response = null;
268:
269: // Get the host name
270: String hostname = InetAddress.getByName("127.0.0.1")
271: .getHostName();
272:
273: request = new GetMethodWebRequest(
274: "http://localhost:8181/subsite/reloadtest");
275:
276: // get the content of the first element
277: response = conversation.getResponse(request);
278: assertEquals("Just some text 127.0.0.1:" + hostname
279: + ":.SUBSITE.SIMPLE:", response.getText());
280:
281: RifeConfig.Global.setAutoReloadDelay(2000);
282:
283: // wait a second
284: Thread.sleep(1000);
285:
286: // overwrite the element file with new content
287: resource = resource_finder
288: .getResource("site/reload_site_blueprint.xml");
289: FileUtils.copy(resource.openStream(), subsite_file);
290:
291: // get the content of the first element
292: response = conversation.getResponse(request);
293: assertEquals("Just some text 127.0.0.1:" + hostname
294: + ":.SUBSITE.SIMPLE:", response.getText());
295:
296: // wait two seconds and a half
297: Thread.sleep(2500);
298:
299: // get the content of the new element
300: try {
301: response = conversation.getResponse(request);
302: fail();
303: } catch (HttpNotFoundException e) {
304: assertTrue(true);
305: }
306:
307: request = new GetMethodWebRequest(
308: "http://localhost:8181/subsite/newurl");
309: response = conversation.getResponse(request);
310: assertEquals("Just some text 127.0.0.1:" + hostname
311: + ":.SUBSITE.SIMPLE:", response.getText());
312:
313: // clean up the copied files
314: element_file.delete();
315: subsite_file.delete();
316: site_file.delete();
317: } catch (Throwable e) {
318: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
319: return;
320: } finally {
321: RifeConfig.Global.setAutoReloadDelay(0);
322: }
323: }
324:
325: public void testMoveElementDeclaration() throws Exception {
326: try {
327: // setup the temporary directory
328: String site_dir = RifeConfig.Global.getTempPath()
329: + File.separator + "movesite";
330: File site_dir_file = new File(site_dir);
331: site_dir_file.mkdirs();
332: String element_dir = RifeConfig.Global.getTempPath()
333: + File.separator + "moveelement";
334: File element_dir_file = new File(element_dir);
335: element_dir_file.mkdirs();
336:
337: // setup the site xml file
338: ResourceFinder resource_finder = null;
339: URL resource = null;
340: File site_file = null;
341: File element_file = null;
342:
343: resource_finder = ResourceFinderClasspath.getInstance();
344: site_file = new File(site_dir + File.separator + "site.xml");
345: site_file.delete();
346: element_file = new File(element_dir + File.separator
347: + "simple1.xml");
348: element_file.delete();
349:
350: // create the site xml file
351: resource = resource_finder
352: .getResource("site/move_element_blueprint1.xml");
353: FileUtils.copy(resource.openStream(), site_file);
354:
355: // create the first element xml file
356: resource = resource_finder
357: .getResource("element/engine/simple_plain.xml");
358: FileUtils.copy(resource.openStream(), element_file);
359:
360: // test the site
361: setupSite("movesite/site.xml");
362: WebConversation conversation = new WebConversation();
363: WebRequest request = null;
364: WebResponse response = null;
365:
366: request = new GetMethodWebRequest(
367: "http://localhost:8181/movetest");
368:
369: // Get the host name
370: String hostname = InetAddress.getByName("127.0.0.1")
371: .getHostName();
372:
373: // get the content of the first element
374: response = conversation.getResponse(request);
375: assertEquals("text/plain", response.getContentType());
376: assertEquals("Just some text 127.0.0.1:" + hostname
377: + ":.SIMPLE:", response.getText());
378:
379: RifeConfig.Global.setAutoReloadDelay(2000);
380:
381: // wait a second
382: Thread.sleep(1000);
383:
384: // overwrite the site declaration file with new content to reference the new name of the element
385: resource = resource_finder
386: .getResource("site/move_element_blueprint2.xml");
387: FileUtils.copy(resource.openStream(), site_file);
388:
389: // move the element
390: File new_element_file = new File(element_dir
391: + File.separator + "simple2.xml");
392: new_element_file.delete();
393: element_file.renameTo(new_element_file);
394:
395: // get the content of the first element
396: response = conversation.getResponse(request);
397: assertEquals("text/plain", response.getContentType());
398: assertEquals("Just some text 127.0.0.1:" + hostname
399: + ":.SIMPLE:", response.getText());
400:
401: // wait two seconds and a half
402: Thread.sleep(2500);
403:
404: // get the content of the moved element
405: response = conversation.getResponse(request);
406: assertEquals("text/plain", response.getContentType());
407: assertEquals("Just some text 127.0.0.1:" + hostname
408: + ":.SIMPLE:", response.getText());
409:
410: // clean up the copied files
411: element_file.delete();
412: site_file.delete();
413: } catch (Throwable e) {
414: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
415: return;
416: } finally {
417: RifeConfig.Global.setAutoReloadDelay(0);
418: }
419: }
420:
421: public void testMoveSubsiteDeclaration() throws Exception {
422: try {
423: // setup the temporary directory
424: String site_dir = RifeConfig.Global.getTempPath()
425: + File.separator + "movesite";
426: File site_dir_file = new File(site_dir);
427: site_dir_file.mkdirs();
428: String element_dir = RifeConfig.Global.getTempPath()
429: + File.separator + "moveelement";
430: File element_dir_file = new File(element_dir);
431: element_dir_file.mkdirs();
432:
433: // setup the site xml file
434: ResourceFinder resource_finder = null;
435: URL resource = null;
436: File site_file = null;
437: File subsite_file = null;
438: File element_file = null;
439:
440: resource_finder = ResourceFinderClasspath.getInstance();
441: site_file = new File(site_dir + File.separator + "site.xml");
442: site_file.delete();
443: subsite_file = new File(site_dir + File.separator
444: + "subsite1.xml");
445: subsite_file.delete();
446: element_file = new File(element_dir + File.separator
447: + "simple1.xml");
448: element_file.delete();
449:
450: // create the site xml file
451: resource = resource_finder
452: .getResource("site/move_subsite_blueprint1.xml");
453: FileUtils.copy(resource.openStream(), site_file);
454:
455: // create the site xml file
456: resource = resource_finder
457: .getResource("site/move_element_blueprint1.xml");
458: FileUtils.copy(resource.openStream(), subsite_file);
459:
460: // create the first element xml file
461: resource = resource_finder
462: .getResource("element/engine/simple_html.xml");
463: FileUtils.copy(resource.openStream(), element_file);
464:
465: // test the site
466: setupSite("movesite/site.xml");
467: WebConversation conversation = new WebConversation();
468: WebRequest request = null;
469: WebResponse response = null;
470:
471: // Get the host name
472: String hostname = InetAddress.getByName("127.0.0.1")
473: .getHostName();
474:
475: request = new GetMethodWebRequest(
476: "http://localhost:8181/subsite/movetest");
477:
478: // get the content of the first element
479: response = conversation.getResponse(request);
480: assertEquals("text/html", response.getContentType());
481: assertEquals("Just some text 127.0.0.1:" + hostname
482: + ":.SUBSITE.SIMPLE:", response.getText());
483:
484: RifeConfig.Global.setAutoReloadDelay(2000);
485:
486: // wait a second
487: Thread.sleep(1000);
488:
489: // overwrite the site declaration file with new content to reference the new name of the subsite
490: resource = resource_finder
491: .getResource("site/move_subsite_blueprint2.xml");
492: FileUtils.copy(resource.openStream(), subsite_file);
493:
494: // move the subsite
495: File new_subsite_file = new File(site_dir + File.separator
496: + "subsite2.xml");
497: new_subsite_file.delete();
498: subsite_file.renameTo(new_subsite_file);
499:
500: // get the content of the first element
501: response = conversation.getResponse(request);
502: assertEquals("text/html", response.getContentType());
503: assertEquals("Just some text 127.0.0.1:" + hostname
504: + ":.SUBSITE.SIMPLE:", response.getText());
505:
506: // wait two seconds and a half
507: Thread.sleep(2500);
508:
509: // get the content of the moved subsite
510: response = conversation.getResponse(request);
511: assertEquals("text/html", response.getContentType());
512: assertEquals("Just some text 127.0.0.1:" + hostname
513: + ":.SUBSITE.SIMPLE:", response.getText());
514:
515: // clean up the copied files
516: element_file.delete();
517: subsite_file.delete();
518: site_file.delete();
519: } catch (Throwable e) {
520: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
521: return;
522: } finally {
523: RifeConfig.Global.setAutoReloadDelay(0);
524: }
525: }
526: }
|