View Javadoc

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