1 5 package com.opensymphony.oscache.web; 6 7 import com.meterware.httpunit.WebConversation; 8 import com.meterware.httpunit.WebResponse; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 14 21 public final class TestOscacheFilter extends TestCase { 22 WebConversation wc = null; 24 private final String BASE_PAGE = "filter/filterTest.jsp"; 25 26 private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL"; 28 private final String PARAM_1 = "abc=123"; 29 private final String PARAM_2 = "xyz=321"; 30 private final String SESSION_ID = "jsessionid=12345678"; 31 private final String SERVLET_URL = "cacheServlet/?"; 33 private final String FORCE_REFRESH = "forceRefresh=true&"; 34 35 36 41 public TestOscacheFilter(String str) { 42 super(str); 43 } 44 45 50 public static Test suite() { 51 return new TestSuite(TestOscacheFilter.class); 52 } 53 54 57 public void setUp() { 58 if (wc == null) { 60 wc = new WebConversation(); 61 } 62 compileJSP(constructURL(BASE_PAGE)); 63 } 64 65 68 public void testOscacheFilter() { 69 String baseUrl = constructURL(BASE_PAGE); 70 71 flushCache(); 73 74 String stringResponse = invokeURL(baseUrl, 500); 76 77 String newResponse = invokeURL(baseUrl, 0); 79 assertTrue("new response " + newResponse + " should be the same to " + stringResponse, stringResponse.equals(newResponse)); 80 81 newResponse = invokeURL(baseUrl + "?" + SESSION_ID, 500); 84 assertTrue("new response " + newResponse + " should be the same to " + stringResponse, stringResponse.equals(newResponse)); 85 86 newResponse = invokeURL(baseUrl + "?" + PARAM_1 + "&" + PARAM_2, 1000); 88 assertFalse("new response " + newResponse + " expected it to be different to last one.", stringResponse.equals(newResponse)); 89 90 stringResponse = newResponse; 91 92 newResponse = invokeURL(baseUrl + "?" + PARAM_2 + "&" + PARAM_1, 0); 95 assertTrue(stringResponse.equals(newResponse)); 96 97 newResponse = invokeURL(baseUrl + "?" + SESSION_ID + "&" + PARAM_1 + "&" + PARAM_2, 0); 100 assertTrue(stringResponse.equals(newResponse)); 101 } 102 103 106 public void testOSCacheFilterFast() { 107 String baseUrl = constructURL(BASE_PAGE); 108 109 for (int i = 0; i < 5; i++) { 110 flushCache(); 112 String url = baseUrl + "?i=" + i; 114 String response = invokeURL(url, 500); 115 for (int j = 0; j < 3; j++) { 116 String newResponse = invokeURL(url, 500); 117 assertTrue("new response " + newResponse + " should be the same to " + response, response.equals(newResponse)); 118 } 119 } 120 } 121 122 125 public void testOscacheFilterBasicForLoad() { 126 String baseUrl = constructURL(BASE_PAGE); 127 128 for (int i = 0; i < 5; i++) { 129 String stringResponse = invokeURL(baseUrl, 0); 130 131 assertTrue(stringResponse.indexOf("Current Time") > 0); 133 } 134 } 135 136 142 private void compileJSP(String URL) { 143 try { 144 wc.getResponse(URL); 146 } catch (Exception ex) { 147 ex.printStackTrace(); 148 fail("Exception raised!!"); 149 } 150 } 151 152 155 private void flushCache() { 156 String flushUrl = constructURL(SERVLET_URL + FORCE_REFRESH); 157 158 String stringResponse = invokeURL(flushUrl, 0); 159 160 assertTrue("Flushing the cache failed!", stringResponse.indexOf("This is some cache content") > 0); 161 } 162 163 170 private String constructURL(String url) { 171 String base = System.getProperty(BASE_URL_SYSTEM_PRP); 172 String constructedUrl = null; 173 174 if (base != null) { 175 if (!base.endsWith("/")) { 176 base = base + "/"; 177 } 178 179 constructedUrl = base + url; 180 } else { 181 fail("System property test.web.baseURL needs to be set to the proper server to use."); 182 } 183 184 return constructedUrl; 185 } 186 187 194 private String invokeURL(String url, int sleepTime) { 195 try { 196 WebResponse resp = wc.getResponse(url); 198 Thread.sleep(sleepTime); 199 200 return resp.getText(); 201 } catch (Exception ex) { 202 ex.printStackTrace(); 203 fail("Exception raised!!"); 204 205 return null; 206 } 207 } 208 } 209 | Popular Tags |