View Javadoc

1   package org.apache.velocity.tools.test.blackbox;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Proxy;
23  import java.lang.reflect.InvocationHandler;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import org.apache.velocity.tools.view.CookieTool;
28  import javax.servlet.http.Cookie;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import static org.junit.Assert.*;
33  import org.junit.Test;
34  
35  /**
36   * <p>CookieTool tests.</p>
37   *
38   * @author Nathan Bubna
39   * @version $Id$
40   */
41  public class CookieToolTests
42  {
43      private CookieTool newCookieTool(InvocationHandler handler)
44      {
45          Object proxy
46              = Proxy.newProxyInstance(this.getClass().getClassLoader(),
47                                       new Class[] { HttpServletRequest.class,
48                                                     HttpServletResponse.class },
49                                       handler);
50  
51          HttpServletRequest request = (HttpServletRequest)proxy;
52          HttpServletResponse response = (HttpServletResponse)proxy;
53  
54          CookieTool cookies = new CookieTool();
55          cookies.setRequest(request);
56          cookies.setResponse(response);
57          return cookies;
58      }
59  
60      private CookieTool newCookieTool(Map cookies)
61      {
62          return newCookieTool(new ServletAdaptor(cookies));
63      }
64  
65      private CookieTool newCookieTool(String name, Object value)
66      {
67          Map cookies = new LinkedHashMap();
68          cookies.put(name, value);
69          return newCookieTool(cookies);
70      }
71  
72      public @Test void testCreate_StringString()
73      {
74          CookieTool cookies = newCookieTool(new LinkedHashMap());
75          Cookie c = cookies.create("a", "b");
76          assertNotNull(c);
77          assertEquals("a", c.getName());
78          assertEquals("b", c.getValue());
79          assertEquals(-1, c.getMaxAge());
80      }
81  
82      public @Test void testCreate_StringStringObject()
83      {
84          CookieTool cookies = newCookieTool(new LinkedHashMap());
85          Cookie c = cookies.create("a", "b", 10);
86          assertNotNull(c);
87          assertEquals("a", c.getName());
88          assertEquals("b", c.getValue());
89          assertEquals(10, c.getMaxAge());
90          c = cookies.create("a", "b", "500");
91          assertNotNull(c);
92          assertEquals(500, c.getMaxAge());
93          c = cookies.create("a", "b", "asd");
94          assertNull(c);
95      }
96  
97      public @Test void testGet_String()
98      {
99          CookieTool cookies = newCookieTool("a", "b");
100         assertEquals("b", cookies.get("a").toString());
101     }
102 
103     public @Test void testGetAll()
104     {
105         CookieTool cookies = newCookieTool("a", "b");
106         assertEquals("[b]", cookies.getAll().toString());
107 
108         Map jar = new LinkedHashMap();
109         jar.put("a", "b");
110         jar.put("foo", "bar");
111         cookies = newCookieTool(jar);
112         List<Cookie> all = cookies.getAll();
113         assertEquals(2, all.size());
114         assertEquals("[b, bar]", all.toString());
115         assertEquals("a", all.get(0).getName());
116         assertEquals("foo", all.get(1).getName());
117     }
118 
119     public @Test void testToString()
120     {
121         CookieTool cookies = newCookieTool("a", "b");
122         assertEquals("[a=b]", cookies.toString());
123 
124         Map jar = new LinkedHashMap();
125         jar.put("a", "b");
126         jar.put("foo", "bar");
127         cookies = newCookieTool(jar);
128         assertEquals("[a=b, foo=bar]", cookies.toString());
129     }
130 
131     public @Test void testAdd_StringString()
132     {
133         Map jar = new LinkedHashMap();
134         jar.put("a", "b");
135         ServletAdaptor proxy = new ServletAdaptor(jar);
136         CookieTool cookies = newCookieTool(proxy);
137         assertEquals("", cookies.add("a","b"));
138 
139         cookies = newCookieTool(proxy);
140         assertNotNull(cookies.get("a"));
141         assertEquals("b", cookies.get("a").getValue());
142     }
143 
144     public @Test void testAdd_StringStringObject()
145     {
146         Map jar = new LinkedHashMap();
147         jar.put("a", "b");
148         ServletAdaptor proxy = new ServletAdaptor(jar);
149         CookieTool cookies = newCookieTool(proxy);
150         assertEquals("", cookies.add("a","b", 10));
151 
152         cookies = newCookieTool(proxy);
153         Cookie c = cookies.get("a");
154         assertNotNull(c);
155         assertEquals("b", c.getValue());
156         assertEquals(10, c.getMaxAge());
157     }
158 
159     public @Test void testDelete_String()
160     {
161         Map jar = new LinkedHashMap();
162         jar.put("a", "b");
163         ServletAdaptor proxy = new ServletAdaptor(jar);
164         CookieTool cookies = newCookieTool(proxy);
165         assertEquals("b", cookies.get("a").toString());
166         cookies.delete("a");
167 
168         cookies = newCookieTool(proxy);
169         assertNull(cookies.get("a"));
170     }
171 }