1   package com.atlassian.core.filters.cache;
2   
3   import junit.framework.TestCase;
4   
5   import javax.servlet.Filter;
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import com.atlassian.core.filters.ServletStubs;
10  import com.atlassian.core.filters.NoOpFilterChain;
11  
12  public class TestAbstractCachingFilter extends TestCase
13  {
14      private ServletStubs.Request request;
15      private ServletStubs.Response response;
16  
17      protected void setUp() throws Exception
18      {
19          super.setUp();
20          request = ServletStubs.getRequestInstance();
21          response = ServletStubs.getResponseInstance();
22      }
23  
24      public void testSubclassThatReturnsNull() throws Exception
25      {
26          Filter filter = new AbstractCachingFilter() {
27              protected CachingStrategy[] getCachingStrategies()
28              {
29                  return null;
30              }
31          };
32  
33          // no asserts, just checking that there's no exception
34          filter.doFilter(request, response, NoOpFilterChain.getInstance());
35      }
36  
37      public void testSubclassThatReturnsEmptyArray() throws Exception
38      {
39          Filter filter = new AbstractCachingFilter() {
40              protected CachingStrategy[] getCachingStrategies()
41              {
42                  return new CachingStrategy[0];
43              }
44          };
45  
46          // no asserts, just checking that there's no exception
47          filter.doFilter(request, response, NoOpFilterChain.getInstance());
48      }
49  
50      public void testSingleStrategy() throws Exception
51      {
52          final StubCachingStrategy strategy = new StubCachingStrategy();
53  
54          Filter filter = new AbstractCachingFilter() {
55              protected CachingStrategy[] getCachingStrategies()
56              {
57                  return new CachingStrategy[]{ strategy };
58              }
59          };
60  
61          filter.doFilter(request, response, NoOpFilterChain.getInstance());
62  
63          assertEquals("true", response.getHeader("cache"));
64      }
65  
66      public void testTwoStrategies() throws Exception
67      {
68          final StubCachingStrategy strategy1 = new StubCachingStrategy();
69          strategy1.matches = false;
70          final StubCachingStrategy strategy2 = new StubCachingStrategy();
71          strategy2.cacheValue = "strategy2";
72  
73          Filter filter = new AbstractCachingFilter() {
74              protected CachingStrategy[] getCachingStrategies()
75              {
76                  return new CachingStrategy[]{ strategy1, strategy2 };
77              }
78          };
79  
80          filter.doFilter(request, response, NoOpFilterChain.getInstance());
81  
82          assertEquals("strategy2", response.getHeader("cache"));
83      }
84  
85      public void testFirstStrategyMatches() throws Exception
86      {
87          final StubCachingStrategy strategy1 = new StubCachingStrategy();
88          strategy1.cacheValue = "strategy1";
89          final StubCachingStrategy strategy2 = new StubCachingStrategy();
90          strategy2.cacheValue = "strategy2";
91  
92          Filter filter = new AbstractCachingFilter() {
93              protected CachingStrategy[] getCachingStrategies()
94              {
95                  return new CachingStrategy[]{ strategy1, strategy2 };
96              }
97          };
98  
99          filter.doFilter(request, response, NoOpFilterChain.getInstance());
100 
101         assertEquals("strategy1", response.getHeader("cache"));
102     }
103 
104     private class StubCachingStrategy implements CachingStrategy
105     {
106         boolean matches = true;
107         String cacheValue = "true";
108 
109         public boolean matches(HttpServletRequest request)
110         {
111             return matches;
112         }
113 
114         public void setCachingHeaders(HttpServletResponse response)
115         {
116             response.setHeader("cache", cacheValue);
117         }
118     }
119 }