1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.jcache.extras.web;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.URL;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServlet;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.catalina.Context;
34 import org.apache.catalina.LifecycleException;
35 import org.apache.catalina.LifecycleState;
36 import org.apache.catalina.core.StandardContext;
37 import org.apache.catalina.deploy.FilterDef;
38 import org.apache.catalina.deploy.FilterMap;
39 import org.apache.catalina.startup.Tomcat;
40 import org.apache.commons.io.IOUtils;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43
44 public class JCacheFilterTest
45 {
46 private static File docBase;
47
48 @BeforeClass
49 public static void createEmptyDir() {
50 docBase = new File("target/missing/");
51 docBase.mkdirs();
52 docBase.deleteOnExit();
53 }
54
55 @Test
56 public void testFilterNoOutput() throws Exception
57 {
58 Empty.COUNTER.set(0);
59 final Tomcat tomcat = new Tomcat();
60 tomcat.setHostname("localhost");
61 tomcat.setPort(0);
62 try {
63 tomcat.getEngine();
64 tomcat.start();
65 final Context ctx = tomcat.addWebapp("/sample", docBase.getAbsolutePath());
66 Tomcat.addServlet(ctx, "empty", Empty.class.getName());
67 ctx.addServletMapping("/", "empty");
68 addJcsFilter(ctx);
69 StandardContext.class.cast(ctx).filterStart();
70
71 final URL url = new URL("http://localhost:" + tomcat.getConnector().getLocalPort() + "/sample/");
72
73 assertEquals("", IOUtils.toString(url.openStream()));
74 assertEquals(1, Empty.COUNTER.get());
75
76 assertEquals("", IOUtils.toString(url.openStream()));
77 assertEquals(1, Empty.COUNTER.get());
78 } finally {
79 stop(tomcat);
80 }
81 }
82
83 @Test
84 public void testFilter() throws Exception
85 {
86 Hello.COUNTER.set(0);
87 final Tomcat tomcat = new Tomcat();
88 tomcat.setPort(0);
89 try {
90 tomcat.getEngine();
91 tomcat.start();
92 final Context ctx = tomcat.addContext("/sample", docBase.getAbsolutePath());
93 Tomcat.addServlet(ctx, "hello", Hello.class.getName());
94 ctx.addServletMapping("/", "hello");
95 addJcsFilter(ctx);
96 StandardContext.class.cast(ctx).filterStart();
97
98 final URL url = new URL("http://localhost:" + tomcat.getConnector().getLocalPort() + "/sample/");
99 assertEquals("hello", IOUtils.toString(url.openStream()));
100 assertEquals(1, Hello.COUNTER.get());
101
102 assertEquals("hello", IOUtils.toString(url.openStream()));
103 assertEquals(1, Hello.COUNTER.get());
104 } finally {
105 stop(tomcat);
106 }
107 }
108
109 private void stop(final Tomcat tomcat) throws LifecycleException {
110 if (LifecycleState.STARTED.equals(tomcat.getServer().getState())) {
111 tomcat.stop();
112 tomcat.destroy();
113 }
114 }
115
116 private void addJcsFilter(final Context ctx) {
117 final FilterDef filterDef = new FilterDef();
118 filterDef.setFilterName("jcs");
119 filterDef.setFilterClass(JCacheFilter.class.getName());
120 ctx.addFilterDef(filterDef);
121
122 final FilterMap filterMap = new FilterMap();
123 filterMap.setFilterName(filterDef.getFilterName());
124 filterMap.addURLPattern("/*");
125 ctx.addFilterMap(filterMap);
126 }
127
128 public static class Hello extends HttpServlet {
129 public static final AtomicInteger COUNTER = new AtomicInteger();
130
131 @Override
132 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
133 resp.getWriter().write("hello");
134 COUNTER.incrementAndGet();
135 }
136 }
137
138 public static class Empty extends HttpServlet {
139 public static final AtomicInteger COUNTER = new AtomicInteger();
140
141 @Override
142 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
143 resp.getWriter().write("");
144 COUNTER.incrementAndGet();
145 }
146 }
147 }