View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import org.apache.http.Header;
34  import org.apache.http.cookie.Cookie;
35  import org.apache.http.cookie.CookieAttributeHandler;
36  import org.apache.http.cookie.CookieOrigin;
37  import org.apache.http.cookie.MalformedCookieException;
38  import org.apache.http.cookie.SetCookie;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  public class TestAbstractCookieSpec {
43  
44      private static class DummyCookieSpec extends AbstractCookieSpec {
45  
46          @Override
47          public List<Header> formatCookies(final List<Cookie> cookies) {
48              return null;
49          }
50  
51          @Override
52          public boolean match(final Cookie cookie, final CookieOrigin origin) {
53              return true;
54          }
55  
56          @Override
57          public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException {
58              return null;
59          }
60  
61          @Override
62          public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
63          }
64  
65          @Override
66          public int getVersion() {
67              return 0;
68          }
69  
70          @Override
71          public Header getVersionHeader() {
72              return null;
73          }
74  
75      }
76  
77      private static class DummyCookieAttribHandler implements CookieAttributeHandler {
78  
79          @Override
80          public boolean match(final Cookie cookie, final CookieOrigin origin) {
81              return true;
82          }
83  
84          @Override
85          public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
86          }
87  
88          @Override
89          public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
90          }
91  
92      }
93  
94      @Test
95      public void testSimpleRegisterAndGet() {
96          final CookieAttributeHandler h1 = new DummyCookieAttribHandler();
97          final CookieAttributeHandler h2 = new DummyCookieAttribHandler();
98  
99          final AbstractCookieSpec cookiespec = new DummyCookieSpec();
100         cookiespec.registerAttribHandler("this", h1);
101         cookiespec.registerAttribHandler("that", h2);
102         cookiespec.registerAttribHandler("thistoo", h1);
103         cookiespec.registerAttribHandler("thattoo", h2);
104 
105         Assert.assertTrue(h1 == cookiespec.getAttribHandler("this"));
106         Assert.assertTrue(h2 == cookiespec.getAttribHandler("that"));
107         Assert.assertTrue(h1 == cookiespec.getAttribHandler("thistoo"));
108         Assert.assertTrue(h2 == cookiespec.getAttribHandler("thattoo"));
109 
110         final Iterator<CookieAttributeHandler> it = cookiespec.getAttribHandlers().iterator();
111         Assert.assertNotNull(it.next());
112         Assert.assertNotNull(it.next());
113         Assert.assertNotNull(it.next());
114         Assert.assertNotNull(it.next());
115         Assert.assertFalse(it.hasNext());
116     }
117 
118     @Test(expected=IllegalStateException.class)
119     public void testInvalidHandler() {
120         final CookieAttributeHandler h1 = new DummyCookieAttribHandler();
121         final CookieAttributeHandler h2 = new DummyCookieAttribHandler();
122 
123         final AbstractCookieSpec cookiespec = new DummyCookieSpec();
124         cookiespec.registerAttribHandler("this", h1);
125         cookiespec.registerAttribHandler("that", h2);
126 
127         Assert.assertNull(cookiespec.findAttribHandler("whatever"));
128         cookiespec.getAttribHandler("whatever");
129     }
130 
131     @Test(expected=IllegalArgumentException.class)
132     public void testBasicPathInvalidInput1() throws Exception {
133         final AbstractCookieSpec cookiespec = new DummyCookieSpec();
134         cookiespec.registerAttribHandler(null, null);
135     }
136 
137     @Test(expected=IllegalArgumentException.class)
138     public void testBasicPathInvalidInput2() throws Exception {
139         final AbstractCookieSpec cookiespec = new DummyCookieSpec();
140         cookiespec.registerAttribHandler("whatever", null);
141     }
142 
143 }