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.hc.client5.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 TestBasicClientCookie {
42
43 @SuppressWarnings("unused")
44 @Test
45 public void testConstructor() {
46 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
47 Assert.assertEquals("name", cookie.getName());
48 Assert.assertEquals("value", cookie.getValue());
49 try {
50 new BasicClientCookie(null, null);
51 Assert.fail("NullPointerException should have been thrown");
52 } catch (final NullPointerException ex) {
53
54 }
55 }
56
57 @Test
58 public void testCloning() throws Exception {
59 final BasicClientCookie orig = new BasicClientCookie("name", "value");
60 orig.setDomain("domain");
61 orig.setPath("/");
62 orig.setAttribute("attrib", "stuff");
63 final BasicClientCookie clone = (BasicClientCookie) orig.clone();
64 Assert.assertEquals(orig.getName(), clone.getName());
65 Assert.assertEquals(orig.getValue(), clone.getValue());
66 Assert.assertEquals(orig.getDomain(), clone.getDomain());
67 Assert.assertEquals(orig.getPath(), clone.getPath());
68 Assert.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
69 }
70
71 @Test
72 public void testSerialization() throws Exception {
73 final BasicClientCookie orig = new BasicClientCookie("name", "value");
74 orig.setDomain("domain");
75 orig.setPath("/");
76 orig.setAttribute("attrib", "stuff");
77 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
78 final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
79 outStream.writeObject(orig);
80 outStream.close();
81 final byte[] raw = outbuffer.toByteArray();
82 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
83 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
84 final BasicClientCookie clone = (BasicClientCookie) inStream.readObject();
85 Assert.assertEquals(orig.getName(), clone.getName());
86 Assert.assertEquals(orig.getValue(), clone.getValue());
87 Assert.assertEquals(orig.getDomain(), clone.getDomain());
88 Assert.assertEquals(orig.getPath(), clone.getPath());
89 Assert.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
90 }
91
92 }