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 22 public final class TestOscacheJsp extends TestCase { 23 WebConversation wc = null; 25 private final String APPLICATION_SCOPE = "scope=application&"; 26 27 private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL"; 29 private final String FIRST_PAGE = "oscacheTest.jsp?"; 30 private final String FORCE_CACHE_USE = "forcecacheuse=yes&"; 31 private final String FORCE_REFRESH = "refresh=true"; 32 private final String PAGE_SCOPE = "scope=page&"; 33 private final String REQUEST_SCOPE = "scope=request&"; 34 private final String SECOND_PAGE = "oscacheTestMultipleTagNoKey.jsp?"; 35 private final String SESSION_SCOPE = "scope=session&"; 36 private final int CACHE_TAG_EXPIRATION = 2000; 37 private final int HALF_CACHE_TAG_EXPIRATION = CACHE_TAG_EXPIRATION / 2; 38 39 44 public TestOscacheJsp(String str) { 45 super(str); 46 } 47 48 53 public static Test suite() { 54 return new TestSuite(TestOscacheJsp.class); 55 } 56 57 60 public void setUp() { 61 if (wc == null) { 63 wc = new WebConversation(); 64 } 65 } 66 67 70 public void testOscacheBasicForLoad() { 71 String baseUrl = constructURL(FIRST_PAGE); 72 73 String stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION); 75 76 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 80 81 baseUrl = constructURL(SECOND_PAGE); 83 84 stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION); 86 87 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 91 } 92 93 96 public void testOscacheJsp() { 97 String baseUrl = constructURL(FIRST_PAGE); 98 99 compileJSP(baseUrl + SESSION_SCOPE); 101 102 String stringResponse = invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION); 104 105 assertTrue(stringResponse.equals(invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION))); 108 109 String newResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION + (CACHE_TAG_EXPIRATION / 4)); 111 assertTrue(!stringResponse.equals(newResponse)); 112 stringResponse = newResponse; 113 114 assertTrue(stringResponse.equals(invokeJSP(baseUrl, FORCE_CACHE_USE, 0))); 116 117 newResponse = invokeJSP(baseUrl, HALF_CACHE_TAG_EXPIRATION); 119 assertTrue(!stringResponse.equals(newResponse)); 120 stringResponse = newResponse; 121 122 assertTrue(!stringResponse.equals(invokeJSP(baseUrl, FORCE_REFRESH, 0))); 125 126 baseUrl = constructURL(SECOND_PAGE); 128 compileJSP(baseUrl + SESSION_SCOPE); 129 stringResponse = invokeJSP(baseUrl, CACHE_TAG_EXPIRATION); 130 131 assertTrue(stringResponse.equals(invokeJSP(baseUrl, CACHE_TAG_EXPIRATION))); 133 } 134 135 141 private void compileJSP(String URL) { 142 try { 143 WebResponse resp = wc.getResponse(URL); 145 } catch (Exception ex) { 146 ex.printStackTrace(); 147 fail("Exception raised!!"); 148 } 149 } 150 151 158 private String constructURL(String Url) { 159 String base = System.getProperty(BASE_URL_SYSTEM_PRP); 160 String constructedUrl = null; 161 162 if (base != null) { 163 if (!base.endsWith("/")) { 164 base = base + "/"; 165 } 166 167 constructedUrl = base + Url; 168 } else { 169 fail("System property test.web.baseURL needs to be set to the proper server to use."); 170 } 171 172 return constructedUrl; 173 } 174 175 182 private String invokeJSP(String baseUrl, int sleepTime) { 183 return invokeJSP(baseUrl, "", sleepTime); 184 } 185 186 194 private String invokeJSP(String baseUrl, String URLparam, int sleepTime) { 195 try { 196 WebResponse resp = wc.getResponse(baseUrl + APPLICATION_SCOPE + URLparam); 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 |