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 TestOscacheServlet extends TestCase { 23 24 static WebConversation wc = null; 26 private final String APPLICATION_SCOPE = "scope=application&"; 27 28 private final String BASE_URL_SYSTEM_PRP = "test.web.baseURL"; 30 private final String FORCE_CACHE_USE = "forcecacheuse=yes&"; 31 private final String FORCE_REFRESH = "forceRefresh=true&"; 32 private final String KEY = "key=ServletKeyItem&"; 33 private final String REFRESH_PERIOD = "refreshPeriod="; 34 private final String SERVLET_URL = "/cacheServlet/?"; 35 private final int NO_REFRESH_WANTED = 2000; 36 private final int REFRESH_WANTED = 0; 37 38 43 public TestOscacheServlet(String str) { 44 super(str); 45 } 46 47 52 public static Test suite() { 53 return new TestSuite(TestOscacheServlet.class); 54 } 55 56 60 public void setUp() { 61 if (wc == null) { 63 wc = new WebConversation(); 64 } 65 } 66 67 70 public void testOscacheServlet() { 71 String newResponse = invokeServlet(NO_REFRESH_WANTED); 73 74 String previousReponse = invokeServlet(NO_REFRESH_WANTED); 76 77 newResponse = invokeServlet(NO_REFRESH_WANTED); 79 assertTrue("new response " + newResponse + " should be the same to " + previousReponse, previousReponse.equals(newResponse)); 80 81 newResponse = invokeServlet(REFRESH_WANTED); 83 assertFalse("new response " + newResponse + " expected it to be different to last one.", previousReponse.equals(newResponse)); 84 previousReponse = newResponse; 85 86 newResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE); 89 assertTrue("new response " + newResponse + " should be the same to " + previousReponse, previousReponse.equals(newResponse)); 90 91 newResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH); 94 assertFalse("new response " + newResponse + " expected it to be different to last one.", previousReponse.equals(newResponse)); 95 96 assertTrue("response '" + previousReponse + "' does not contain oscache string", previousReponse.indexOf("oscache") != -1); 99 100 assertTrue("response '" + previousReponse + "' does not contain /Test_key string", previousReponse.indexOf("/Test_key") != -1); 101 } 102 103 106 public void testOscacheServletBasicForLoad() { 107 String stringResponse = invokeServlet(NO_REFRESH_WANTED); 109 110 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 114 115 stringResponse = invokeServlet(REFRESH_WANTED); 117 118 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 120 121 stringResponse = invokeServlet(REFRESH_WANTED, FORCE_CACHE_USE); 123 124 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 126 127 stringResponse = invokeServlet(NO_REFRESH_WANTED, FORCE_REFRESH); 129 130 assertTrue(stringResponse.indexOf("This is some cache content") > 0); 132 } 133 134 141 private String constructURL(String Url) { 142 String base = System.getProperty(BASE_URL_SYSTEM_PRP); 143 String constructedUrl = null; 144 145 if (base != null) { 146 if (base.endsWith("/")) { 147 base = base.substring(0, base.length() - 1); 148 } 149 150 constructedUrl = base + Url; 151 } else { 152 fail("System property test.web.baseURL needs to be set to the proper server to use."); 153 } 154 155 return constructedUrl; 156 } 157 158 164 private String invokeServlet(int refresh) { 165 return invokeServlet(refresh, ""); 167 } 168 169 176 private String invokeServlet(int refresh, String URL) { 177 try { 179 Thread.sleep(10); 180 } catch (InterruptedException ignore) { 181 } 182 183 try { 185 String request = constructURL(SERVLET_URL) + APPLICATION_SCOPE + KEY + REFRESH_PERIOD + refresh + "&" + URL; 186 WebResponse resp = wc.getResponse(request); 187 return resp.getText(); 188 } catch (Exception ex) { 189 ex.printStackTrace(); 190 fail("Exception raised! " + ex.getMessage()); 191 return ""; 192 } 193 } 194 } 195 | Popular Tags |