SharedTypesSerializationCache implements SerializationCache { @Override public Object getCached(Object src) { for (Class<?> type: shareableTypes) { if (type.isInstance(src)) { Object prevValue = cache.putIfAbsent(src, src); return prevValue != null ? prevValue : src; } } return src; } SharedTypesSerializationCache(Class<?>... shareableTypes); @Override Object getCached(Object src);  }
@Test public void testGetMultipleTimes() { SerializationCache cache = new SharedTypesSerializationCache(TestType.class); String value = "TEST-ORIG"; TestType origObj = new TestType(value); Object firstGetValue = cache.getCached(origObj); assertSame(origObj, firstGetValue); Object secondGetValue = cache.getCached(new TestType(value)); assertSame(origObj, secondGetValue); }
@Test public void testGetNonEqual() { SerializationCache cache = new SharedTypesSerializationCache(TestType.class); TestType origObj = new TestType("TEST-ORIG"); Object firstGetValue = cache.getCached(origObj); assertSame(origObj, firstGetValue); TestType newObj = new TestType("TEST-MOD"); Object secondGetValue = cache.getCached(newObj); assertSame(newObj, secondGetValue); }
@Test public void testGetUnknownType() { SerializationCache cache = new SharedTypesSerializationCache(TestType.class); Object origObj = new Object(); Object firstGetValue = cache.getCached(origObj); assertSame(origObj, firstGetValue); Object newObj = new Object(); Object secondGetValue = cache.getCached(newObj); assertSame(newObj, secondGetValue); }