|
1 | 1 | /*
|
2 |
| - * Copyright 2011-2013 the original author or authors. |
| 2 | + * Copyright 2011-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.data.redis.serializer;
|
17 | 17 |
|
| 18 | +import static org.hamcrest.CoreMatchers.*; |
18 | 19 | import static org.junit.Assert.*;
|
19 | 20 |
|
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
20 | 23 | import java.io.Serializable;
|
| 24 | +import java.net.URL; |
21 | 25 | import java.util.UUID;
|
22 | 26 |
|
23 | 27 | import org.junit.After;
|
|
26 | 30 | import org.springframework.data.redis.Address;
|
27 | 31 | import org.springframework.data.redis.Person;
|
28 | 32 | import org.springframework.oxm.xstream.XStreamMarshaller;
|
| 33 | +import org.springframework.util.StreamUtils; |
29 | 34 |
|
30 | 35 | /**
|
31 | 36 | * @author Jennifer Hickey
|
| 37 | + * @author Mark Paluch |
32 | 38 | */
|
33 | 39 | public class SimpleRedisSerializerTests {
|
34 | 40 |
|
@@ -119,6 +125,22 @@ private void verifySerializedObjects(Object... objects) {
|
119 | 125 | }
|
120 | 126 | }
|
121 | 127 |
|
| 128 | + @Test |
| 129 | + public void jdkSerializerShouldUseCustomClassLoader() throws ClassNotFoundException { |
| 130 | + |
| 131 | + ClassLoader customClassLoader = new CustomClassLoader(); |
| 132 | + |
| 133 | + JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer(customClassLoader); |
| 134 | + SerializableDomainClass domainClass = new SerializableDomainClass(); |
| 135 | + |
| 136 | + byte[] serialized = serializer.serialize(domainClass); |
| 137 | + Object deserialized = serializer.deserialize(serialized); |
| 138 | + |
| 139 | + assertThat(deserialized.getClass().getName(), is(equalTo(SerializableDomainClass.class.getName()))); |
| 140 | + assertThat(deserialized, is(not(instanceOf(SerializableDomainClass.class)))); |
| 141 | + assertThat(deserialized.getClass().getClassLoader(), is(equalTo(customClassLoader))); |
| 142 | + } |
| 143 | + |
122 | 144 | @Test
|
123 | 145 | public void testStringEncodedSerialization() {
|
124 | 146 | String value = UUID.randomUUID().toString();
|
@@ -157,4 +179,43 @@ public void testJsonSerializer() throws Exception {
|
157 | 179 | assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
158 | 180 | }
|
159 | 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 | + } |
160 | 221 | }
|
0 commit comments