Skip to content

Commit 5a33c5c

Browse files
author
Gary Gregory
committed
Use SerializationUtils to build test fixtures
1 parent 4b52a6f commit 5a33c5c

1 file changed

Lines changed: 33 additions & 85 deletions

File tree

src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java

Lines changed: 33 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@
2727
import java.io.ObjectOutputStream;
2828
import java.io.Serializable;
2929

30+
import org.apache.commons.lang3.SerializationUtils;
31+
3032
/**
31-
* Tests the CountingInputStream.
32-
*
33+
* Tests {@link ClassLoaderObjectInputStream}.
3334
*/
3435
public class ClassLoaderObjectInputStreamTest {
3536

36-
/* Note: This test case tests the simplest functionality of
37-
* ObjectInputStream. IF we really wanted to test ClassLoaderObjectInputStream
38-
* we would probably need to create a transient Class Loader. -TO
37+
/*
38+
* Note: This test case tests the simplest functionality of ObjectInputStream. IF we really wanted to test
39+
* ClassLoaderObjectInputStream we would probably need to create a transient Class Loader. -TO
3940
*/
4041

41-
42-
private enum E {A, B, C}
42+
private enum E {
43+
A, B, C
44+
}
4345

4446
private static class Test implements Serializable {
4547
private static final long serialVersionUID = 1L;
@@ -66,9 +68,7 @@ private boolean equalObject(final Object other) {
6668
public boolean equals(final Object other) {
6769
if (other instanceof Test) {
6870
final Test tother = (Test) other;
69-
return this.i == tother.i
70-
& this.e == tother.e
71-
& equalObject(tother.o);
71+
return this.i == tother.i & this.e == tother.e & equalObject(tother.o);
7272
}
7373
return false;
7474
}
@@ -81,125 +81,73 @@ public int hashCode() {
8181

8282
@org.junit.jupiter.api.Test
8383
public void testExpected() throws Exception {
84-
85-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
86-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
87-
88-
final Object input = Boolean.FALSE;
89-
oos.writeObject(input);
90-
91-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
92-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
93-
bais)) {
84+
final Boolean input = Boolean.FALSE;
85+
final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
86+
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
9487
final Object result = clois.readObject();
95-
9688
assertEquals(input, result);
9789
}
9890
}
9991

10092
@org.junit.jupiter.api.Test
10193
public void testLong() throws Exception {
102-
103-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
104-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
105-
106-
final Object input = (long) 123;
107-
oos.writeObject(input);
108-
109-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
110-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
111-
bais)) {
94+
final Long input = 123L;
95+
final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
96+
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
11297
final Object result = clois.readObject();
113-
11498
assertEquals(input, result);
11599
}
116100
}
117101

118102
@org.junit.jupiter.api.Test
119103
public void testObject1() throws Exception {
120-
121-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
122-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
123-
124-
final Object input = new Test(123, null);
125-
oos.writeObject(input);
126-
oos.close();
127-
128-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
129-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
130-
bais)) {
104+
final Test input = new Test(123, null);
105+
final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
106+
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
131107
final Object result = clois.readObject();
132-
133108
assertEquals(input, result);
134109
}
135110
}
136111

137112
@org.junit.jupiter.api.Test
138113
public void testObject2() throws Exception {
139-
140-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
141-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
142-
143-
final Object input = new Test(123, 0);
144-
oos.writeObject(input);
145-
oos.close();
146-
147-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
148-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
149-
bais)) {
114+
final Test input = new Test(123, 0);
115+
final InputStream bais = new ByteArrayInputStream(SerializationUtils.serialize(input));
116+
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
150117
final Object result = clois.readObject();
151-
152118
assertEquals(input, result);
153119
}
154120
}
155121

156122
@org.junit.jupiter.api.Test
157123
public void testPrimitiveLong() throws Exception {
158-
159-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
160-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
161-
162124
final long input = 12345L;
163-
oos.writeLong(input);
164-
oos.close();
165-
125+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
126+
try (final ObjectOutputStream oos = new ObjectOutputStream(baos)) {
127+
oos.writeLong(input);
128+
}
166129
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
167-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
168-
bais)) {
130+
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais)) {
169131
final long result = clois.readLong();
170-
171132
assertEquals(input, result);
172133
}
173134
}
174135

175136
@org.junit.jupiter.api.Test
176137
public void testResolveProxyClass() throws Exception {
177-
178-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
179-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
180-
oos.writeObject(Boolean.FALSE);
181-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
182-
183-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
184-
bais)) {
185-
final String[] interfaces = { Comparable.class.getName() };
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()};
186141
final Class<?> result = clois.resolveProxyClass(interfaces);
187142
assertTrue(Comparable.class.isAssignableFrom(result), "Assignable");
188143
}
189144
}
190145

191146
@org.junit.jupiter.api.Test
192147
public void testResolveProxyClassWithMultipleInterfaces() throws Exception {
193-
194-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
195-
final ObjectOutputStream oos = new ObjectOutputStream(baos);
196-
oos.writeObject(Boolean.FALSE);
197-
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
198-
199-
try (ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(),
200-
bais)) {
201-
final String[] interfaces = { Comparable.class.getName(), Serializable.class.getName(),
202-
Runnable.class.getName() };
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()};
203151
final Class<?> result = clois.resolveProxyClass(interfaces);
204152
assertTrue(Comparable.class.isAssignableFrom(result), "Assignable");
205153
assertTrue(Runnable.class.isAssignableFrom(result), "Assignable");

0 commit comments

Comments
 (0)