[mule-scm] [mule][25317] branches/mule-3.x: Merged revisions 25316 via svnmerge from

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[mule-scm] [mule][25317] branches/mule-3.x: Merged revisions 25316 via svnmerge from

pablo.lagreca
[mule][25317] branches/mule-3.x: Merged revisions 25316 via svnmerge from

Diff

Property changes: branches/mule-3.x


Modified: svnmerge-integrated

+ /branches/mule-3.3.x:1-24195,24197-24200,24202-24241,24243-24289,24294,24297,24299,24304,24310,24313,24316,24320,24323,24326-24327,24330-24332,24337,24340-24341,24352,24356,24359-24360,24364,24366,24368,24372,24374,24377-24379,24382,24385,24387,24392,24394,24397,24403-24436,24438-24439,24442,24445-24446,24448-24457,24468,24472,24478,24484,24488,24491,24496,24502-24503,24508,24512,24518,24522,24527,24531,24535,24540,24543,24549,24551,24553,24564,24572,24575,24579,24583,24588,24590,24593,24597,24601,24604,24607,24615,24621,24626,24629,24637,24639,24642-24691,24693,24695-24757,24759-24798,24800-24803,24805-24828,24830,24833,24836,24838,24843,24847,24854-24855,24887,24893-24894,24911,24916,24920,24931,24940,24942,24960,24963-24971,24977,24981-24982,24984,24991,25008,25016,25021,25024,25026-25028,25032,25040-25046,25055,25059,25065,25067,25069,25073-25079,25081-25082,25091,25100,25117,25120,25124,25148,25151,25157,25160,25186,25190,25197,25203,25209,25211,252 28,25237,25239,25241,25246-25247,25250,25254,25258,25278,25299,25303,25316

Modified: svn:mergeinfo

/branches/mule-3.2.x:24203,24208,24237-24244,24246,24250,24256,24264,24276-24279,24293-24296,24319,24350,24355,24358,24363,24371,24376,24378,24381,24384,24389-24391,24404-24408,24415,24424,24434,24441,24467,24471,24477,24481,24483,24487,24526,24530,24534,24539,24542,24548,24571,24574,24578,24582,24587,24592,24596,24600,24603,24606,24613-24614,24617-24618,24620,24625,24628,24635-24636,24641,24650,24661,24666,24680-24681,24684,24686,24693,24695,24706,24719-24721,24724,24740,24743,24752-24753,24763,24768,24785-24786,24796,24832,24835,24841-24842,24846,24852,24875,24892,24897,24910,24915,24930,24957,24983,24990,25015,25030,25043,25064,25068,25072,25075,25077,25096,25156,25158,25185,25196,25202,25227,25233,25298 /branches/mule-3.3.x:24204,24209,24211-24214,24216-24218,24223-24224,24226,24231-24241,24245,24248,24257,24259-24265,24269-24284,24294,24297,24299,24304,24310,24313,24316,24320,24323,24326-24327,24330-24332,24337,24340-24341,24352,24356,24359-24360,24364,24366,24368,24372,24374,24377-24379,24382,24385,24387,24392,24394,24397,24403,24405-24407,24409-24410,24412-24413,24416-24418,24420,24422-24423,24425-24427,24430-24433,24435-24436,24438-24439,24442,24445-24446,24448,24450-24451,24453-24457,24468,24472,24478,24484,24488,24491,24496,24502,24508,24512,24518,24522,24527,24531,24535,24540,24543,24549,24551,24564,24572,24575,24579,24583,24588,24590,24593,24597,24601,24604,24607,24615,24621,24626,24629,24637,24639,24642-24646,24648,24651-24652,24655,24657-24658,24660,24663-24667,24669,24682,24685,24687,24690,24696,24698-24699,24702,24707,24714,24716-24718,24722,24725,24727-24738,24741,24744-24748,24750,24754-24755,24759-24760,24764,24769,24771,24773-24774,247 77,24780-24783,24787-24788,24791-24792,24794,24797,24801-24802,24806-24807,24824-24828,24830,24833,24836,24838,24843,24847,24854-24855,24887,24893-24894,24911,24916,24920,24931,24940,24942,24960,24963-24964,24966,24977,24981-24982,24984,24991,25008,25016,25021,25024,25026-25028,25032,25040-25041,25044,25046,25055,25059,25065,25067,25069,25073-25074,25076,25078-25079,25081-25082,25091,25100,25117,25124,25148,25151,25157,25160,25186,25197,25203,25211,25228,25237,25239,25241,25246-25247,25250,25254,25258,25278,25299,25303 + /branches/mule-3.1.x:24202,24318,24349,24354,24362,24370,24388,24414,24466,24470,24482,24486,24525,24529,24533,24537-24538,24547,24570,24577,24586,24595,24599,24619,24623-24624,24634,24665,24677-24679,24705,24739,24742,24749-24751,24851,24874,24891,24914,24956,24980,24988-24989,25042,25063,25095,25195,25198,25225,25232,25294 /branches/mule-3.2.x:24203,24208,24237-24244,24246,24250,24256,24264,24276-24279,24293-24296,24319,24350,24355,24358,24363,24371,24376,24378,24381,24384,24389-24391,24404-24408,24415,24424,24434,24441,24467,24471,24477,24481,24483,24487,24526,24530,24534,24539,24542,24548,24571,24574,24578,24582,24587,24592,24596,24600,24603,24606,24613-24614,24617-24618,24620,24625,24628,24635-24636,24641,24650,24661,24666,24680-24681,24684,24686,24693,24695,24706,24719-24721,24724,24740,24743,24752-24753,24763,24768,24785-24786,24796,24832,24835,24841-24842,24846,24852,24875,24892,24897,24910,24915,24930,24957,24983,24990,25015,25030,25043,25064,25068,25072,25075,25077,25096,25156,25158,25185,25196,25202,25227,25233,25298,25313-25315 /branches/mule-3.3.x:24204,24209,24211-24214,24216-24218,24223-24224,24226,24231-24241,24245,24248,24257,24259-24265,24269-24284,24294,24297,24299,24304,24310,24313,24316,24320,24323,24326-24327,24330-24332,24337,24340-24341,24352,24356,24359-24360,24364,24366,24368,24372,24374,24377-24379,24382,24385,24387,24392,24394,24397,24403,24405-24407,24409-24410,24412-24413,24416-24418,24420,24422-24423,24425-24427,24430-24433,24435-24436,24438-24439,24442,24445-24446,24448,24450-24451,24453-24457,24468,24472,24478,24484,24488,24491,24496,24502,24508,24512,24518,24522,24527,24531,24535,24540,24543,24549,24551,24564,24572,24575,24579,24583,24588,24590,24593,24597,24601,24604,24607,24615,24621,24626,24629,24637,24639,24642-24646,24648,24651-24652,24655,24657-24658,24660,24663-24667,24669,24682,24685,24687,24690,24696,24698-24699,24702,24707,24714,24716-24718,24722,24725,24727-24738,24741,24744-24748,24750,24754-24755,24759-24760,24764,24769,24771,24773-24774,247 77,24780-24783,24787-24788,24791-24792,24794,24797,24801-24802,24806-24807,24824-24828,24830,24833,24836,24838,24843,24847,24854-24855,24887,24893-24894,24911,24916,24920,24931,24940,24942,24960,24963-24964,24966,24977,24981-24982,24984,24991,25008,25016,25021,25024,25026-25028,25032,25040-25041,25044,25046,25055,25059,25065,25067,25069,25073-25074,25076,25078-25079,25081-25082,25091,25100,25117,25124,25148,25151,25157,25160,25186,25197,25203,25211,25228,25237,25239,25241,25246-25247,25250,25254,25258,25278,25299,25303,25316

Modified: branches/mule-3.x/core/src/main/java/org/mule/util/store/DefaultObjectStoreFactoryBean.java (25316 => 25317)


--- branches/mule-3.x/core/src/main/java/org/mule/util/store/DefaultObjectStoreFactoryBean.java	2013-02-27 20:45:18 UTC (rev 25316)
+++ branches/mule-3.x/core/src/main/java/org/mule/util/store/DefaultObjectStoreFactoryBean.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -73,7 +73,7 @@
         return delegate.createDefaultPersistentQueueStore();
     }
 
-    public static Object createDefaultUserObjectStore()
+    public static ObjectStore<Serializable> createDefaultUserObjectStore()
     {
         return delegate.createDefaultUserObjectStore();
     }

Modified: branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleDefaultObjectStoreFactory.java (25316 => 25317)


--- branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleDefaultObjectStoreFactory.java	2013-02-27 20:45:18 UTC (rev 25316)
+++ branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleDefaultObjectStoreFactory.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -27,7 +27,7 @@
     @Override
     public ObjectStore<Serializable> createDefaultPersistentObjectStore()
     {
-        return new QueuePersistenceObjectStore<Serializable>();
+        return new PartitionedPersistentObjectStore<Serializable>();
     }
 
     @Override

Modified: branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleObjectStoreManager.java (25316 => 25317)


--- branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleObjectStoreManager.java	2013-02-27 20:45:18 UTC (rev 25316)
+++ branches/mule-3.x/core/src/main/java/org/mule/util/store/MuleObjectStoreManager.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -36,7 +36,7 @@
 import org.apache.commons.logging.LogFactory;
 
 public class MuleObjectStoreManager
-    implements ObjectStoreManager, MuleContextAware, Initialisable, Disposable
+        implements ObjectStoreManager, MuleContextAware, Initialisable, Disposable
 {
     MuleContext muleContext;
     ConcurrentMap<String, ObjectStore<?>> stores = new ConcurrentHashMap<String, ObjectStore<?>>();
@@ -46,40 +46,41 @@
     @Override
     public <T extends ObjectStore<? extends Serializable>> T getObjectStore(String name)
     {
-        return this.<T> getObjectStore(name, false);
+        return this.getObjectStore(name, false);
     }
 
     @Override
     public <T extends ObjectStore<? extends Serializable>> T getObjectStore(String name, boolean isPersistent)
     {
-        return this.<T> getObjectStore(name, isPersistent, 0, 0, 0);
+        return internalCreateStore(getBaseStore(isPersistent), name, 0, 0, 0);
     }
 
     @Override
+    public <T extends ObjectStore<? extends Serializable>> T getObjectStore(String name, boolean isPersistent, int maxEntries, int entryTTL, int expirationInterval)
+    {
+        return internalCreateStore(getBaseStore(isPersistent), name, maxEntries, entryTTL, expirationInterval);
+    }
+
     @SuppressWarnings({"unchecked"})
-    synchronized public <T extends ObjectStore<? extends Serializable>> T getObjectStore(String name,
-                                                                                         boolean isPersistent,
-                                                                                         int maxEntries,
-                                                                                         int entryTTL,
-                                                                                         int expirationInterval)
+    synchronized public <T extends ObjectStore<? extends Serializable>> T internalCreateStore(ListableObjectStore<? extends Serializable> baseStore, String name,
+                                                                                              int maxEntries,
+                                                                                              int entryTTL,
+                                                                                              int expirationInterval)
     {
         if (stores.containsKey(name))
         {
             return (T) stores.get(name);
         }
-        ListableObjectStore<?> baseStore;
-        if (isPersistent)
+        T store= null;
+        try
         {
-            baseStore = (ListableObjectStore<?>) muleContext.getRegistry().lookupObject(
-                MuleProperties.OBJECT_STORE_DEFAULT_PERSISTENT_NAME);
+            store = this.<T>getPartitionFromBaseObjectStore(baseStore,name);
         }
-        else
+        catch (ObjectStoreException e)
         {
-            baseStore = (ListableObjectStore<?>) muleContext.getRegistry().lookupObject(
-                MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME);
+            //TODO In order to avoid breaking backward compatibility. In the future this method must throw object store creation exception
+            throw new MuleRuntimeException(e);
         }
-        T store=this.<T>getPartitionFromBaseObjectStore(baseStore,name);
-
         if (maxEntries == 0)
         {
             return putInStoreMap(name,store);
@@ -90,16 +91,36 @@
         }
     }
 
+    private <T extends ListableObjectStore<? extends Serializable>> T getBaseStore(boolean persistent)
+    {
+        T baseStore;
+        if (persistent)
+        {
+            baseStore = (T) muleContext.getRegistry().lookupObject(
+                    MuleProperties.OBJECT_STORE_DEFAULT_PERSISTENT_NAME);
+        }
+        else
+        {
+            baseStore = (T) muleContext.getRegistry().lookupObject(
+                    MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME);
+        }
+        return baseStore;
+    }
+
     @SuppressWarnings({"rawtypes", "unchecked"})
-    private <T extends ObjectStore<? extends Serializable>> T getPartitionFromBaseObjectStore(ListableObjectStore baseStore, String partitionName)
+    private <T extends ObjectStore<? extends Serializable>> T getPartitionFromBaseObjectStore(ListableObjectStore<? extends Serializable> baseStore, String partitionName) throws ObjectStoreException
     {
         if (baseStore instanceof PartitionableObjectStore)
         {
-            return (T) new ObjectStorePartition(partitionName, (PartitionableObjectStore) baseStore);
+            ObjectStorePartition objectStorePartition = new ObjectStorePartition(partitionName, (PartitionableObjectStore) baseStore);
+            objectStorePartition.open();
+            return (T) objectStorePartition;
         }
         else
         {
-            return (T) new PartitionedObjectStoreWrapper(partitionName, muleContext, baseStore);
+            PartitionedObjectStoreWrapper partitionedObjectStoreWrapper = new PartitionedObjectStoreWrapper(partitionName, muleContext, baseStore);
+            partitionedObjectStoreWrapper.open();
+            return (T) partitionedObjectStoreWrapper;
         }
     }
 
@@ -126,7 +147,7 @@
             if (previous == null)
             {
                 Monitor m = new Monitor(name, (PartitionableExpirableObjectStore) baseStore, entryTTL,
-                    maxEntries);
+                                        maxEntries);
                 scheduler.scheduleWithFixedDelay(m, 0, expirationInterval, TimeUnit.MILLISECONDS);
                 return store;
             }
@@ -148,7 +169,7 @@
                     return (T) stores.get(name);
                 }
                 monObjectStore = new MonitoredObjectStoreWrapper((ListableObjectStore) store, maxEntries,
-                    entryTTL, expirationInterval);
+                                                                 entryTTL, expirationInterval);
                 monObjectStore.setMuleContext(muleContext);
                 try
                 {
@@ -193,7 +214,7 @@
     {
         scheduler = new ScheduledThreadPoolExecutor(1);
         scheduler.setThreadFactory(new DaemonThreadFactory("ObjectStoreManager-Monitor", this.getClass()
-            .getClassLoader()));
+                .getClassLoader()));
     }
 
     class Monitor implements Runnable
@@ -226,7 +247,7 @@
                 catch (Exception e)
                 {
                     MuleObjectStoreManager.logger.warn("Running expirty on partition " + partitionName + " of "
-                        + store + " threw " + e + ":" + e.getMessage());
+                                                       + store + " threw " + e + ":" + e.getMessage());
                 }
             }
         }

Modified: branches/mule-3.x/core/src/main/java/org/mule/util/store/PartitionedPersistentObjectStore.java (25316 => 25317)


--- branches/mule-3.x/core/src/main/java/org/mule/util/store/PartitionedPersistentObjectStore.java	2013-02-27 20:45:18 UTC (rev 25316)
+++ branches/mule-3.x/core/src/main/java/org/mule/util/store/PartitionedPersistentObjectStore.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -33,7 +33,6 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.lang.SerializationException;
 
@@ -119,7 +118,6 @@
         File storeFile = createStoreFile(key, partitionName);
         T result = deserialize(storeFile);
         deleteStoreFile(storeFile);
-
         return result;
     }
 
@@ -140,7 +138,6 @@
         {
             List<Serializable> keys = new ArrayList<Serializable>();
             listStoredFiles(createStorePartition(partitionName), keys);
-
             return keys;
         }
         catch (ClassNotFoundException e)
@@ -293,9 +290,10 @@
 
     protected void serialize(T value, File outputFile) throws ObjectStoreException
     {
+        FileOutputStream out = null;
         try
         {
-            FileOutputStream out = new FileOutputStream(outputFile);
+            out = new FileOutputStream(outputFile);
             SerializationUtils.serialize(value, out);
         }
         catch (SerializationException se)
@@ -306,14 +304,29 @@
         {
             throw new ObjectStoreException(fnfe);
         }
+        finally
+        {
+            if (out != null)
+            {
+                try
+                {
+                    out.close();
+                }
+                catch (Exception e)
+                {
+                    logger.warn("error closing file " + outputFile.getAbsolutePath());
+                }
+            }
+        }
     }
 
     @SuppressWarnings("unchecked")
     protected T deserialize(File file) throws ObjectStoreException
     {
+        FileInputStream in = null;
         try
         {
-            FileInputStream in = new FileInputStream(file);
+            in = new FileInputStream(file);
             return (T) SerializationUtils.deserialize(in, muleContext);
         }
         catch (SerializationException se)
@@ -324,6 +337,20 @@
         {
             throw new ObjectDoesNotExistException(fnfe);
         }
+        finally
+        {
+            if (in != null)
+            {
+                try
+                {
+                    in.close();
+                }
+                catch (Exception e)
+                {
+                    logger.warn("error closing opened file " + file.getAbsolutePath());
+                }
+            }
+        }
     }
 
     protected void deleteStoreFile(File file) throws ObjectStoreException

Copied: branches/mule-3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java (from rev 25316, branches/mule-3.3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java) (0 => 25317)


--- branches/mule-3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java	                        (rev 0)
+++ branches/mule-3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -0,0 +1,149 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+package org.mule.util.store;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import org.mule.api.config.MuleProperties;
+import org.mule.api.store.ListableObjectStore;
+import org.mule.api.store.ObjectStore;
+import org.mule.api.store.ObjectStoreException;
+import org.mule.tck.junit4.AbstractMuleContextTestCase;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MuleObjectStoreManagerTestCase extends AbstractMuleContextTestCase
+{
+
+    public static final String OBJECT_KEY = "key";
+    public static final String OBJECT_KEY_VALUE_1 = "value";
+    public static final String OBJECT_KEY_VALUE_2 = "anotherValue";
+    private final ObjectStoreFactory objectStoreFactory;
+
+    public MuleObjectStoreManagerTestCase(ObjectStoreFactory objectStoreFactory)
+    {
+        this.objectStoreFactory = objectStoreFactory;
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> parameters()
+    {
+        return Arrays.asList(new Object[][] {
+                {new ObjectStoreFactory(false)},
+                {new ObjectStoreFactory(true)}
+        });
+    }
+
+    @Before
+    public void injectMuleContext()
+    {
+        objectStoreFactory.setMuleObjectStoreManager(muleContext.getRegistry().<MuleObjectStoreManager>get(MuleProperties.OBJECT_STORE_MANAGER));
+    }
+
+    @Test
+    public void partitionObjectStoreDoesNotCollide() throws Exception
+    {
+        ObjectStore os = objectStoreFactory.createObjectStore("myOs");
+        ObjectStore os2 = objectStoreFactory.createObjectStore("myOs2");
+        os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1);
+        os2.store(OBJECT_KEY, OBJECT_KEY_VALUE_2);
+        assertThat(os.contains(OBJECT_KEY), is(true));
+        assertThat((String) os.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_1));
+        assertThat(os2.contains(OBJECT_KEY),is(true));
+        assertThat((String) os2.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_2));
+        assertThat((String) os.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_1));
+        assertThat((String) os2.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_2));
+    }
+
+    @Ignore //Behavior must be reviewed. It allows more than maxEntries objects.
+    @Test(expected = ObjectStoreException.class)
+    public void maxEntriesIshonored() throws Exception
+    {
+        ObjectStore os = objectStoreFactory.createObjectStore("myOs", 5,0,60000);
+        int maxEntries = 5;
+        for (int i = 0; i < maxEntries; i++)
+        {
+            os.store(i, i);
+        }
+        os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1);
+    }
+
+    @Test
+    public void expirationIntervalWithLowTTL() throws Exception
+    {
+        int maxEntries = 5;
+        int entryTTL = 10;
+        ListableObjectStore os = objectStoreFactory.createObjectStore("myOs", maxEntries, entryTTL,100);
+        for (int i = 0; i < maxEntries; i++)
+        {
+            os.store(i,i);
+        }
+        os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1);
+        Thread.sleep(entryTTL*30);
+        assertThat(os.allKeys().isEmpty(), is(true));
+    }
+
+    @Test
+    public void expirationIntervalWithHighTTLPersistentObjectStore() throws Exception
+    {
+        int maxEntries = 5;
+        int entryTTL = 10000;
+        ListableObjectStore os = objectStoreFactory.createObjectStore("myOs", maxEntries, entryTTL,100);
+        for (int i = 0; i < maxEntries; i++)
+        {
+            os.store(i,i);
+        }
+        os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1);
+        Thread.sleep(entryTTL/5);
+        assertThat(os.allKeys().size(),is(maxEntries));
+        for (int i = 1; i < maxEntries; i++)
+        {
+            assertThat(os.contains(i),is(true));
+        }
+        assertThat(os.contains(OBJECT_KEY), is(true));
+    }
+
+    private static class ObjectStoreFactory
+    {
+        private final boolean isPersistent;
+        private MuleObjectStoreManager muleObjectStoreManager;
+
+        public ObjectStoreFactory(boolean isPersistent)
+        {
+            this.isPersistent = isPersistent;
+        }
+
+        public void setMuleObjectStoreManager(MuleObjectStoreManager muleObjectStoreManager)
+        {
+            this.muleObjectStoreManager = muleObjectStoreManager;
+        }
+
+        public <T extends ObjectStore<? extends Serializable>> T createObjectStore(String name)
+        {
+            return muleObjectStoreManager.getObjectStore(name,isPersistent);
+        }
+
+        public <T extends ObjectStore<? extends Serializable>> T createObjectStore(String name, int maxEntries, int entryTTL, int expirationInterval)
+        {
+            return muleObjectStoreManager.getObjectStore(name,isPersistent, maxEntries, entryTTL, expirationInterval);
+        }
+    }
+
+}

Copied: branches/mule-3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java (from rev 25316, branches/mule-3.3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java) (0 => 25317)


--- branches/mule-3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java	                        (rev 0)
+++ branches/mule-3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java	2013-02-27 20:55:12 UTC (rev 25317)
@@ -0,0 +1,154 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+package org.mule.util.store;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+import org.mule.api.MuleContext;
+import org.mule.api.store.ObjectAlreadyExistsException;
+import org.mule.api.store.ObjectStoreException;
+import org.mule.tck.junit4.AbstractMuleTestCase;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Answers;
+import org.mockito.Mockito;
+
+/**
+ *
+ */
+public class PartitionedPersistentObjectStoreTestCase extends AbstractMuleTestCase
+{
+
+    public static final String OBJECT_KEY = "key";
+    public static final String OBJECT_BASE_VALUE = "value";
+    private MuleContext mockMuleContext = Mockito.mock(MuleContext.class, Answers.RETURNS_DEEP_STUBS.get());
+    private PartitionedPersistentObjectStore<Serializable> os;
+    private int numberOfPartitions = 3;
+
+    @Before
+    public void setUpMockMuleContext() throws IOException
+    {
+        numberOfPartitions = 3;
+        when(mockMuleContext.getConfiguration().getWorkingDirectory()).thenReturn(".");
+        when(mockMuleContext.getExecutionClassLoader()).thenReturn(Thread.currentThread().getContextClassLoader());
+        os = new PartitionedPersistentObjectStore<Serializable>(mockMuleContext);
+        File objectStorePersistDir = new File(PartitionedPersistentObjectStore.DEFAULT_OBJECT_STORE);
+        if (objectStorePersistDir.exists())
+        {
+            FileUtils.deleteDirectory(objectStorePersistDir);
+        }
+    }
+
+    @Test
+    public void defaultPartitionAndNamedPartitionsDoNotCollide() throws Exception
+    {
+        openPartitions();
+        storeInPartitions(OBJECT_KEY, OBJECT_BASE_VALUE);
+        assertAllValuesExistsInPartitionAreUnique(OBJECT_KEY, OBJECT_BASE_VALUE);
+    }
+
+    @Test
+    public void removeEntries() throws Exception
+    {
+        openPartitions();
+        storeInPartitions(OBJECT_KEY, OBJECT_BASE_VALUE);
+        removeEntriesInPartitions();
+        assertAllPartitionsAreEmpty();
+    }
+
+    @Test(expected = ObjectAlreadyExistsException.class)
+    public void storeSameKeyThrowsException() throws Exception
+    {
+        numberOfPartitions = 0;
+        openPartitions();
+        storeInPartitions(OBJECT_KEY, OBJECT_BASE_VALUE);
+        storeInPartitions(OBJECT_KEY, OBJECT_BASE_VALUE);
+    }
+
+    @Test
+    public void objectStorePersistDataBetweenOpenAndClose() throws ObjectStoreException
+    {
+        openPartitions();
+        storeInPartitions(OBJECT_KEY, OBJECT_BASE_VALUE);
+        closePartitions();
+        openPartitions();
+        assertAllValuesExistsInPartitionAreUnique(OBJECT_KEY, OBJECT_BASE_VALUE);
+    }
+
+    private void closePartitions() throws ObjectStoreException
+    {
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            os.close(getPartitionName(i));
+        }
+        os.close();
+    }
+
+    private void assertAllPartitionsAreEmpty() throws ObjectStoreException
+    {
+        assertThat(os.contains(OBJECT_KEY), is(false));
+        assertThat(os.allKeys().size(),is(0));
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            assertThat(os.contains(OBJECT_KEY,getPartitionName(i)), is(false));
+            assertThat(os.allKeys(getPartitionName(i)).size(),is(0));
+        }
+    }
+
+    private void removeEntriesInPartitions() throws ObjectStoreException
+    {
+        os.remove(OBJECT_KEY);
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            os.remove(OBJECT_KEY, getPartitionName(i));
+        }
+    }
+
+    private void assertAllValuesExistsInPartitionAreUnique(String key, String value) throws ObjectStoreException
+    {
+        assertThat((String) os.retrieve(key), is(value));
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            assertThat((String) os.retrieve(key,getPartitionName(i)), is(value + i));
+        }
+    }
+
+    private void storeInPartitions(String key, String value) throws ObjectStoreException
+    {
+        os.store(key,value);
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            os.store(key, value + i, getPartitionName(i));
+        }
+    }
+
+    private void openPartitions() throws ObjectStoreException
+    {
+        os.open();
+        for (int i = 0; i < numberOfPartitions; i++)
+        {
+            os.open(getPartitionName(i));
+        }
+    }
+
+    private String getPartitionName(int i)
+    {
+        return "partition" + i;
+    }
+
+}

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email