View Javadoc

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