View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.store;
8   
9   import org.mule.api.store.ObjectAlreadyExistsException;
10  import org.mule.api.store.ObjectDoesNotExistException;
11  import org.mule.api.store.ObjectStore;
12  import org.mule.api.store.ObjectStoreException;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  
15  import java.io.Serializable;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.fail;
20  
21  /**
22   * This test verifies the contract described in {@link ObjectStore}
23   */
24  public abstract class AbstractObjectStoreContractTestCase extends AbstractMuleContextTestCase
25  {
26      @Test
27      public void testContainsWithNullKey()
28      {
29          try
30          {
31              getObjectStore().contains(null);
32              fail("contains() called with null key must throw ObjectStoreException");
33          }
34          catch (ObjectStoreException ose)
35          {
36              // this one was expected
37          }
38      }
39      
40      @Test
41      public void testStoreWithNullKey()
42      {
43          try
44          {
45              Serializable value = getStorableValue();
46              getObjectStore().store(null, value);
47              fail("store() called with null key must throw ObjectStoreException");
48          }
49          catch (ObjectStoreException ose)
50          {
51              // this one was expected
52          }
53      }
54      
55      @Test
56      public void testRetrieveWithNullKey()
57      {
58          try
59          {
60              getObjectStore().retrieve(null);
61              fail("retrieve() called with null key must throw ObjectStoreException");
62          }
63          catch (ObjectStoreException ose)
64          {
65              // this one was expected
66          }
67      }
68      
69      @Test
70      public void testRemoveWithNullKey()
71      {
72          try
73          {
74              getObjectStore().remove(null);
75              fail("remove() called with null key must throw ObjectStoreException");
76          }
77          catch (ObjectStoreException ose)
78          {
79              // this one was expected
80          }
81      }
82      
83      @Test
84      public void testRetrieveUnboundKey() throws ObjectStoreException
85      {
86          try
87          {
88              getObjectStore().retrieve("this_key_does_not_exist");
89              fail("retrieve() with unbound key must throw ObjectDoesNotExistException");
90          }
91          catch (ObjectDoesNotExistException odne)
92          {
93              // this one was expected
94          }
95      }
96      
97      @Test
98      public void testRemoveWithUnboundKey() throws ObjectStoreException
99      {
100         try
101         {
102             getObjectStore().remove("this_key_does_not_exist");
103             fail("remove() with unbound key must throw ObjectDoesNotExistException");
104         }
105         catch (ObjectDoesNotExistException odnee)
106         {
107             // this one was expected
108         }
109     }
110     
111     @Test
112     public void testStoreWithExistingKey() throws ObjectStoreException
113     {
114         String key = "theKey";
115         Serializable value = getStorableValue();
116         ObjectStore<Serializable> objectStore = getObjectStore();
117         
118         // storing for the first time must work
119         objectStore.store(key, value);
120         
121         // storing with the same key again must fail
122         try
123         {
124             objectStore.store(key, value);
125             fail("store() with and existing key must throw ObjectAlreadyExistsException");
126         }
127         catch (ObjectAlreadyExistsException oaee)
128         {
129             // this one was expected
130         }
131     }
132     
133     public abstract ObjectStore<Serializable> getObjectStore();
134     
135     public abstract Serializable getStorableValue();
136 }