1 7 package com.inversoft.verge.config.test; 8 9 10 import java.io.File ; 11 import java.io.FileReader ; 12 import java.io.FileWriter ; 13 import java.io.IOException ; 14 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServletRequest ; 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 39 public class VergeConfigMonitorTest extends WebTestCase { 40 41 44 public static final String TEST_FILE = "src/com/inversoft/verge/config/test/actionflow-test.xml"; 45 46 47 50 public VergeConfigMonitorTest(String name) { 51 super(name); 52 setLocal(true); 53 } 54 55 56 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 try { 67 ConfigServlet cs = new ConfigServlet(); 68 cs.init(createConfig("ConfigServlet")); 69 } catch (ServletException se) { 70 fail(se.toString()); 71 } 72 73 File file = new File (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 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 HttpServletRequest 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 111 public static void touchFile(File file) { 112 113 StringBuffer buf = new StringBuffer (); 114 try { 115 FileReader reader = new FileReader (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 ioe) { 127 return; 129 } 130 131 try { 132 File touched = new File (file.getAbsolutePath()); 133 FileWriter writer = new FileWriter (touched); 134 writer.write(buf.toString()); 135 writer.close(); 136 } catch (IOException ioe) { 137 } 139 } 140 } | Popular Tags |