View Javadoc

1   /*
2    * $Id: AbstractObjectStoreContractTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.AbstractMuleTestCase;
18  
19  import java.io.Serializable;
20  
21  /**
22   * This test verifies the contract described in {@link ObjectStore}
23   */
24  public abstract class AbstractObjectStoreContractTestCase extends AbstractMuleTestCase
25  {
26      public void testContainsWithNullKey()
27      {
28          try
29          {
30              getObjectStore().contains(null);
31              fail("contains() called with null key must throw ObjectStoreException");
32          }
33          catch (ObjectStoreException ose)
34          {
35              // this one was expected
36          }
37      }
38      
39      public void testStoreWithNullKey()
40      {
41          try
42          {
43              Serializable value = getStorableValue();
44              getObjectStore().store(null, value);
45              fail("store() called with null key must throw ObjectStoreException");
46          }
47          catch (ObjectStoreException ose)
48          {
49              // this one was expected
50          }
51      }
52      
53      public void testRetrieveWithNullKey()
54      {
55          try
56          {
57              getObjectStore().retrieve(null);
58              fail("retrieve() called with null key must throw ObjectStoreException");
59          }
60          catch (ObjectStoreException ose)
61          {
62              // this one was expected
63          }
64      }
65      
66      public void testRemoveWithNullKey()
67      {
68          try
69          {
70              getObjectStore().remove(null);
71              fail("remove() called with null key must throw ObjectStoreException");
72          }
73          catch (ObjectStoreException ose)
74          {
75              // this one was expected
76          }
77      }
78      
79      public void testRetrieveUnboundKey() throws ObjectStoreException
80      {
81          try
82          {
83              getObjectStore().retrieve("this_key_does_not_exist");
84              fail("retrieve() with unbound key must throw ObjectDoesNotExistException");
85          }
86          catch (ObjectDoesNotExistException odne)
87          {
88              // this one was expected
89          }
90      }
91      
92      public void testRemoveWithUnboundKey() throws ObjectStoreException
93      {
94          try
95          {
96              getObjectStore().remove("this_key_does_not_exist");
97              fail("remove() with unbound key must throw ObjectDoesNotExistException");
98          }
99          catch (ObjectDoesNotExistException odnee)
100         {
101             // this one was expected
102         }
103     }
104     
105     public void testStoreWithExistingKey() throws ObjectStoreException
106     {
107         String key = "theKey";
108         Serializable value = getStorableValue();
109         ObjectStore<Serializable> objectStore = getObjectStore();
110         
111         // storing for the first time must work
112         objectStore.store(key, value);
113         
114         // storing with the same key again must fail
115         try
116         {
117             objectStore.store(key, value);
118             fail("store() with and existing key must throw ObjectAlreadyExistsException");
119         }
120         catch (ObjectAlreadyExistsException oaee)
121         {
122             // this one was expected
123         }
124     }
125     
126     public abstract ObjectStore<Serializable> getObjectStore();
127     
128     public abstract Serializable getStorableValue();
129 }