1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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 }