|
18 | 18 | import static org.hamcrest.CoreMatchers.*;
|
19 | 19 | import static org.junit.Assert.*;
|
20 | 20 |
|
21 |
| -import java.io.IOException; |
22 |
| -import java.io.InputStream; |
23 | 21 | import java.io.Serializable;
|
24 |
| -import java.net.URL; |
25 | 22 | import java.util.UUID;
|
26 | 23 |
|
27 | 24 | import org.junit.After;
|
28 | 25 | import org.junit.Before;
|
29 | 26 | import org.junit.Test;
|
30 | 27 | import org.springframework.data.redis.Address;
|
31 | 28 | import org.springframework.data.redis.Person;
|
| 29 | +import org.springframework.instrument.classloading.ShadowingClassLoader; |
32 | 30 | import org.springframework.oxm.xstream.XStreamMarshaller;
|
33 |
| -import org.springframework.util.StreamUtils; |
34 | 31 |
|
35 | 32 | /**
|
36 | 33 | * @author Jennifer Hickey
|
@@ -125,10 +122,13 @@ private void verifySerializedObjects(Object... objects) {
|
125 | 122 | }
|
126 | 123 | }
|
127 | 124 |
|
| 125 | + /** |
| 126 | + * @see DATAREDIS-427 |
| 127 | + */ |
128 | 128 | @Test
|
129 | 129 | public void jdkSerializerShouldUseCustomClassLoader() throws ClassNotFoundException {
|
130 | 130 |
|
131 |
| - ClassLoader customClassLoader = new CustomClassLoader(); |
| 131 | + ClassLoader customClassLoader = new ShadowingClassLoader(ClassLoader.getSystemClassLoader()); |
132 | 132 |
|
133 | 133 | JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer(customClassLoader);
|
134 | 134 | SerializableDomainClass domainClass = new SerializableDomainClass();
|
@@ -178,44 +178,4 @@ public void testJsonSerializer() throws Exception {
|
178 | 178 | assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
179 | 179 | assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
180 | 180 | }
|
181 |
| - |
182 |
| - /** |
183 |
| - * Custom class loader that loads class files from the test's class path. This {@link ClassLoader} does not delegate |
184 |
| - * to a parent class loader to truly load classes that are defined by this class loader and not interfere with any |
185 |
| - * parent class loader. The class loader uses simple class definition which is fine for the test but do not use this |
186 |
| - * as sample for production class loaders. |
187 |
| - */ |
188 |
| - private static class CustomClassLoader extends ClassLoader { |
189 |
| - |
190 |
| - public CustomClassLoader() { |
191 |
| - super(null); |
192 |
| - } |
193 |
| - |
194 |
| - @Override |
195 |
| - protected Class<?> findClass(String name) throws ClassNotFoundException { |
196 |
| - |
197 |
| - URL resource = SimpleRedisSerializerTests.class.getResource("/" + name.replace('.', '/') + ".class"); |
198 |
| - |
199 |
| - InputStream is = null; |
200 |
| - try { |
201 |
| - |
202 |
| - is = resource.openStream(); |
203 |
| - byte[] bytes = StreamUtils.copyToByteArray(is); |
204 |
| - return defineClass(name, bytes, 0, bytes.length); |
205 |
| - } catch (IOException o_O) { |
206 |
| - throw new ClassNotFoundException("Cannot read class file", o_O); |
207 |
| - } finally { |
208 |
| - |
209 |
| - if (is != null) { |
210 |
| - try { |
211 |
| - is.close(); |
212 |
| - } catch (IOException e) { |
213 |
| - // ignore |
214 |
| - } |
215 |
| - } |
216 |
| - } |
217 |
| - |
218 |
| - } |
219 |
| - |
220 |
| - } |
221 | 181 | }
|
0 commit comments