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.jupiter.api.Assertions;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 class TestBasicClientCookie {
42
43 @SuppressWarnings("unused")
44 @Test
45 void testConstructor() {
46 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
47 Assertions.assertEquals("name", cookie.getName());
48 Assertions.assertEquals("value", cookie.getValue());
49 Assertions.assertThrows(NullPointerException.class, () -> new BasicClientCookie(null, null));
50 }
51
52 @Test
53 void testCloning() throws Exception {
54 final BasicClientCookie orig = new BasicClientCookie("name", "value");
55 orig.setDomain("domain");
56 orig.setPath("/");
57 orig.setAttribute("attrib", "stuff");
58 final BasicClientCookie clone = (BasicClientCookie) orig.clone();
59 Assertions.assertEquals(orig.getName(), clone.getName());
60 Assertions.assertEquals(orig.getValue(), clone.getValue());
61 Assertions.assertEquals(orig.getDomain(), clone.getDomain());
62 Assertions.assertEquals(orig.getPath(), clone.getPath());
63 Assertions.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
64 }
65
66 @Test
67 void testSerialization() throws Exception {
68 final BasicClientCookie orig = new BasicClientCookie("name", "value");
69 orig.setDomain("domain");
70 orig.setPath("/");
71 orig.setAttribute("attrib", "stuff");
72 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
73 final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
74 outStream.writeObject(orig);
75 outStream.close();
76 final byte[] raw = outbuffer.toByteArray();
77 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
78 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
79 final BasicClientCookie clone = (BasicClientCookie) inStream.readObject();
80 Assertions.assertEquals(orig.getName(), clone.getName());
81 Assertions.assertEquals(orig.getValue(), clone.getValue());
82 Assertions.assertEquals(orig.getDomain(), clone.getDomain());
83 Assertions.assertEquals(orig.getPath(), clone.getPath());
84 Assertions.assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib"));
85 }
86
87 }