View Javadoc

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