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.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests for {@link BasicClientCookie2}.
40   */
41  public class TestBasicClientCookie2 {
42  
43      @SuppressWarnings("unused")
44      @Test
45      public void testConstructor() {
46          final BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
47          Assert.assertEquals("name", cookie.getName());
48          Assert.assertEquals("value", cookie.getValue());
49          try {
50              new BasicClientCookie2(null, null);
51              Assert.fail("IllegalArgumentException should have been thrown");
52          } catch (final IllegalArgumentException ex) {
53              //expected
54          }
55      }
56  
57      @Test
58      public void testCloning() throws Exception {
59          final BasicClientCookie2 orig = new BasicClientCookie2("name", "value");
60          orig.setDomain("domain");
61          orig.setPath("/");
62          orig.setAttribute("attrib", "stuff");
63          orig.setPorts(new int[] {80, 8080});
64          final BasicClientCookie2 clone = (BasicClientCookie2) orig.clone();
65          Assert.assertEquals(orig.getName(), clone.getName());
66          Assert.assertEquals(orig.getValue(), clone.getValue());
67          Assert.assertEquals(orig.getDomain(), clone.getDomain());
68          Assert.assertEquals(orig.getPath(), clone.getPath());
69          Assert.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
70          Assert.assertEquals(orig.getPorts().length, clone.getPorts().length);
71          Assert.assertEquals(orig.getPorts()[0], clone.getPorts()[0]);
72          Assert.assertEquals(orig.getPorts()[1], clone.getPorts()[1]);
73      }
74  
75      @Test
76      public void testHTTPCLIENT_1031() throws Exception {
77          final BasicClientCookie2 orig = new BasicClientCookie2("name", "value");
78          orig.setDomain("domain");
79          orig.setPath("/");
80          orig.setAttribute("attrib", "stuff");
81          final BasicClientCookie2 clone = (BasicClientCookie2) orig.clone();
82          Assert.assertEquals(orig.getName(), clone.getName());
83          Assert.assertEquals(orig.getValue(), clone.getValue());
84          Assert.assertEquals(orig.getDomain(), clone.getDomain());
85          Assert.assertEquals(orig.getPath(), clone.getPath());
86          Assert.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
87          Assert.assertNull(clone.getPorts());
88      }
89  
90      @Test
91      public void testSerialization() throws Exception {
92          final BasicClientCookie2 orig = new BasicClientCookie2("name", "value");
93          orig.setDomain("domain");
94          orig.setPath("/");
95          orig.setAttribute("attrib", "stuff");
96          orig.setPorts(new int[] {80, 8080});
97          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
98          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
99          outStream.writeObject(orig);
100         outStream.close();
101         final byte[] raw = outbuffer.toByteArray();
102         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
103         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
104         final BasicClientCookie2 clone = (BasicClientCookie2) inStream.readObject();
105         Assert.assertEquals(orig.getName(), clone.getName());
106         Assert.assertEquals(orig.getValue(), clone.getValue());
107         Assert.assertEquals(orig.getDomain(), clone.getDomain());
108         Assert.assertEquals(orig.getPath(), clone.getPath());
109         Assert.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
110         final int[] expected = orig.getPorts();
111         final int[] clones = clone.getPorts();
112         Assert.assertNotNull(expected);
113         Assert.assertNotNull(clones);
114         Assert.assertEquals(expected.length, clones.length);
115         for (int i = 0; i < expected.length; i++) {
116             Assert.assertEquals(expected[i], clones[i]);
117         }
118     }
119 
120 }