1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.Flushable;
26 import java.io.InputStream;
27 import java.io.ObjectOutputStream;
28 import java.io.Serializable;
29
30 import org.apache.commons.lang3.SerializationUtils;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 class ClassLoaderObjectInputStreamTest {
37
38
39
40
41
42 private enum E {
43 A, B, C
44 }
45
46 private static final class TestFixture implements Serializable {
47 private static final long serialVersionUID = 1L;
48 private final int i;
49
50 private final Object o;
51
52 private final E e;
53
54 TestFixture(final int i, final Object o) {
55 this.i = i;
56 this.e = E.A;
57 this.o = o;
58 }
59
60 private boolean equalObject(final Object other) {
61 if (this.o == null) {
62 return other == null;
63 }
64 return o.equals(other);
65 }
66
67 @Override
68 public boolean equals(final Object other) {
69 if (other instanceof TestFixture) {
70 final TestFixture tOther = (TestFixture) other;
71 return this.i == tOther.i & this.e == tOther.e & equalObject(tOther.o);
72 }
73 return false;
74 }
75
76 @Override
77 public int hashCode() {
78 return super.hashCode();
79 }
80 }
81
82 @Test
83 void testExpected() throws Exception {
84 final Boolean input = Boolean.FALSE;
85 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
86 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
87 final Object result = clois.readObject();
88 assertEquals(input, result);
89 }
90 }
91
92 @Test
93 void testLong() throws Exception {
94 final Long input = 123L;
95 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
96 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
97 final Object result = clois.readObject();
98 assertEquals(input, result);
99 }
100 }
101
102 @Test
103 void testObject1() throws Exception {
104 final TestFixture input = new TestFixture(123, null);
105 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
106 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
107 final Object result = clois.readObject();
108 assertEquals(input, result);
109 }
110 }
111
112 @Test
113 void testObject2() throws Exception {
114 final TestFixture input = new TestFixture(123, 0);
115 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
116 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
117 final Object result = clois.readObject();
118 assertEquals(input, result);
119 }
120 }
121
122 @Test
123 void testPrimitiveLong() throws Exception {
124 final long input = 12345L;
125 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
126 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
127 oos.writeLong(input);
128 }
129 final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
130 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
131 final long result = clois.readLong();
132 assertEquals(input, result);
133 }
134 }
135
136 @Test
137 void testResolveProxyClass() throws Exception {
138 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(Boolean.FALSE));
139 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
140 final String[] interfaces = {Comparable.class.getName()};
141 final Class<?> result = clois.resolveProxyClass(interfaces);
142 assertTrue(Comparable.class.isAssignableFrom(result), "Assignable");
143 }
144 }
145
146 @Test
147 void testResolveProxyClassWithMultipleInterfaces() throws Exception {
148 final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(Boolean.FALSE));
149 try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
150 final String[] interfaces = {Comparable.class.getName(), Serializable.class.getName(), Runnable.class.getName()};
151 final Class<?> result = clois.resolveProxyClass(interfaces);
152 assertTrue(Comparable.class.isAssignableFrom(result), "Assignable");
153 assertTrue(Runnable.class.isAssignableFrom(result), "Assignable");
154 assertTrue(Serializable.class.isAssignableFrom(result), "Assignable");
155 assertFalse(Flushable.class.isAssignableFrom(result), "Not Assignable");
156 }
157 }
158 }