View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.Field;
5   
6   import com.atlassian.cache.impl.ReferenceKey;
7   
8   import org.junit.Test;
9   
10  import static java.lang.reflect.Modifier.isFinal;
11  import static java.lang.reflect.Modifier.isPrivate;
12  import static org.apache.commons.lang3.SerializationUtils.deserialize;
13  import static org.apache.commons.lang3.SerializationUtils.serialize;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNotSame;
16  import static org.junit.Assert.assertTrue;
17  
18  public class ReferenceKeyTest
19  {
20      @Test
21      public void noArgConstructorShouldBePrivateToPreventInstantiationByOtherClasses() throws Exception
22      {
23          // Invoke
24          final Constructor<ReferenceKey> noArgConstructor = ReferenceKey.class.getDeclaredConstructor();
25  
26          // Check
27          assertTrue("No-arg constructor should be private", isPrivate(noArgConstructor.getModifiers()));
28      }
29  
30      @Test
31      public void singletonShouldBeFinalToPreventReassignmentForExampleToNull() throws Exception
32      {
33          // Invoke
34          final Field keyField = ReferenceKey.class.getDeclaredField("KEY");
35  
36          // Check
37          assertTrue("Key field should be final", isFinal(keyField.getModifiers()));
38      }
39  
40      @Test
41      public void instancesShouldBeEqualAfterDeserializing()
42      {
43          // Set up
44          final ReferenceKey key = ReferenceKey.KEY;
45  
46          // Invoke
47          final Object roundTrippedKey = deserialize(serialize(key));
48  
49          // Check
50          assertEquals(key, roundTrippedKey);
51          assertEquals(key.hashCode(), roundTrippedKey.hashCode());
52          assertNotSame(key, roundTrippedKey);
53      }
54  }