Quantcast

[mule-scm] [mule][25316] branches/mule-3.3.x: Merged revisions 25313-25315 via svnmerge from

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

[mule-scm] [mule][25316] branches/mule-3.3.x: Merged revisions 25313-25315 via svnmerge from

pablo.lagreca
[mule][25316] branches/mule-3.3.x: Merged revisions 25313-25315 via svnmerge from

Diff

Property changes: branches/mule-3.3.x


Modified: svnmerge-integrated

+ /branches/mule-3.2.x:1-22786,22788-22794,22796-22822,22824-22832,22834-22855,22858-22905,22907,22909-22924,22926-22950,22952-22988,22990-22991,22993-23033,23035-23048,23050-23055,23057-23069,23071-23114,23116-23125,23127-23131,23133-23134,23136-23153,23155-23163,23165-23243,23245-23247,23249-23254,23256-23315,23317-23323,23325-23347,23349-23380,23382-23384,23386-23404,23406-23423,23425-23440,23442-23465,23467-23478,23480-23570,23572-23583,23585-23587,23589-23590,23592-23607,23609-23622,23625-23631,23634-23676,23678-23682,23684-23710,23712-23751,23753-23807,23809-23814,23816-23857,23859-23873,23875-23885,23887-23891,23893-23894,23896,23898-23958,23960-24069,24071-24086,24089-24120,24122-24136,24138-24251,24254-24284,24286-24425,24429,24434,24441,24467,24471,24477,24481,24483,24487,24500,24526,24530,24534,24539,24542,24545,24548,24571,24574,24578,24582,24587,24592,24596,24600,24603,24606,24613-24628,24635-24641,24650,24661,24666,24671,24674,24680-24681,246 84-24693,24695,24706,24712,24719-24721,24724,24740,24743,24752-24753,24756-24757,24763,24768,24785-24786,24796,24832,24835,24841-24842,24846,24852,24875,24892,24897,24910,24915,24930,24957,24962,24970,24983,24990,25015,25030,25043,25064,25068,25072,25075,25077,25096,25156,25158,25185,25196,25202,25208,25227,25233-25234,25236,25245,25298,25310,25313-25315

Modified: svn:mergeinfo

/branches/mule-3.2.x:23038,23057,23254,23267,23270-23272,23456,23460,23470,23472,23476,23480,23500,23508-23510,23518,23579,23594,23598,23602,23611,23620,23635,23637,23645,23649,23655,23657,23662,23665,23667,23669,23673-23675,23681,23686,23689,23691,23694,23696,23702,23707,23712,23714,23717,23719,23721-23722,23726,23731,23736,23738,23741,23747,23750,23754,23756,23762-23763,23775,23778-23780,23801,23819,23828,23834,23848,23865,23867,23869,23873,23876,23882-23883,23885,23896,23900,23908,23914,23937-23944,23951,24048-24050,24062,24073,24100,24117,24132-24133,24182-24187,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,24429,24434,24441,24467,24471,24477,24481,24483,24487,24500,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,2466 6,24680-24681,24684,24686,24693,24695,24706,24719-24721,24724,24740,24743,24752-24753,24756-24757,24763,24768,24785-24786,24796,24832,24835,24841-24842,24846,24852,24875,24892,24897,24910,24915,24930,24957,24962,24970,24983,24990,25015,25030,25043,25064,25068,25072,25075,25077,25096,25156,25158,25185,25196,25202,25208,25227,25233,25298,25310 + /branches/mule-3.1.x:23002,23163,23454-23455,23459,23471,23475,23499,23506,23509,23516,23596,23610,23618,23628-23631,23634,23644,23647-23648,23653-23654,23659-23661,23672,23674,23679-23680,23684,23693,23700,23713,23734-23735,23749,23753,23761,23777,23800,23818,23859-23860,23864,23872,23884,23899,23907,23935,24072,24097,24118,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,24959,24969,24980,24988-24989,25042,25063,25095,25195,25198,25225,25232,25294 /branches/mule-3.2.x:23038,23057,23254,23267,23270-23272,23456,23460,23470,23472,23476,23480,23500,23508-23510,23518,23579,23594,23598,23602,23611,23620,23635,23637,23645,23649,23655,23657,23662,23665,23667,23669,23673-23675,23681,23686,23689,23691,23694,23696,23702,23707,23712,23714,23717,23719,23721-23722,23726,23731,23736,23738,23741,23747,23750,23754,23756,23762-23763,23775,23778-23780,23801,23819,23828,23834,23848,23865,23867,23869,23873,23876,23882-23883,23885,23896,23900,23908,23914,23937-23944,23951,24048-24050,24062,24073,24100,24117,24132-24133,24182-24187,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,24429,24434,24441,24467,24471,24477,24481,24483,24487,24500,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,246 61,24666,24680-24681,24684,24686,24693,24695,24706,24719-24721,24724,24740,24743,24752-24753,24756-24757,24763,24768,24785-24786,24796,24832,24835,24841-24842,24846,24852,24875,24892,24897,24910,24915,24930,24957,24962,24970,24983,24990,25015,25030,25043,25064,25068,25072,25075,25077,25096,25156,25158,25185,25196,25202,25208,25227,25233,25298,25310,25313-25315

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


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

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


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

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


--- branches/mule-3.3.x/core/src/main/java/org/mule/util/store/MuleObjectStoreManager.java	2013-02-27 20:25:28 UTC (rev 25315)
+++ branches/mule-3.3.x/core/src/main/java/org/mule/util/store/MuleObjectStoreManager.java	2013-02-27 20:45:18 UTC (rev 25316)
@@ -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,39 +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);
@@ -88,34 +90,54 @@
             return getMonitorablePartition(name,baseStore,store,entryTTL,maxEntries,expirationInterval);
         }
     }
-    
+
+    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, (ListableObjectStore) baseStore);
+            PartitionedObjectStoreWrapper partitionedObjectStoreWrapper = new PartitionedObjectStoreWrapper(partitionName, muleContext, baseStore);
+            partitionedObjectStoreWrapper.open();
+            return (T) partitionedObjectStoreWrapper;
         }
     }
-    
+
     private <T extends ObjectStore<? extends Serializable>> T putInStoreMap(String name,T store)
     {
         @SuppressWarnings("unchecked")
         T previous = (T) stores.putIfAbsent(name, store);
         if (previous == null)
         {
-            return (T) store;
+            return store;
         }
         else
         {
             return previous;
         }
     }
-    
+
     @SuppressWarnings({"rawtypes", "unchecked"})
     private <T extends ObjectStore<? extends Serializable>> T getMonitorablePartition(String name,ListableObjectStore baseStore,T store, int entryTTL, int maxEntries, int expirationInterval)
     {
@@ -125,9 +147,9 @@
             if (previous == null)
             {
                 Monitor m = new Monitor(name, (PartitionableExpirableObjectStore) baseStore, entryTTL,
-                    maxEntries);
+                                        maxEntries);
                 scheduler.scheduleWithFixedDelay(m, 0, expirationInterval, TimeUnit.MILLISECONDS);
-                return (T) store;
+                return store;
             }
             else
             {
@@ -147,7 +169,7 @@
                     return (T) stores.get(name);
                 }
                 monObjectStore = new MonitoredObjectStoreWrapper((ListableObjectStore) store, maxEntries,
-                    entryTTL, expirationInterval);
+                                                                 entryTTL, expirationInterval);
                 monObjectStore.setMuleContext(muleContext);
                 try
                 {
@@ -178,6 +200,13 @@
     public void dispose()
     {
         scheduler.shutdown();
+        for (ObjectStore<?> objectStore : stores.values())
+        {
+            if (objectStore instanceof Disposable)
+            {
+                ((Disposable)objectStore).dispose();
+            }
+        }
     }
 
     @Override
@@ -185,7 +214,7 @@
     {
         scheduler = new ScheduledThreadPoolExecutor(1);
         scheduler.setThreadFactory(new DaemonThreadFactory("ObjectStoreManager-Monitor", this.getClass()
-            .getClassLoader()));
+                .getClassLoader()));
     }
 
     class Monitor implements Runnable
@@ -218,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());
                 }
             }
         }
@@ -255,7 +284,7 @@
             }
             else
             {
-                //there is nothing we can do                
+                //there is nothing we can do
             }
         }
     }

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


--- branches/mule-3.3.x/core/src/main/java/org/mule/util/store/PartitionedPersistentObjectStore.java	2013-02-27 20:25:28 UTC (rev 25315)
+++ branches/mule-3.3.x/core/src/main/java/org/mule/util/store/PartitionedPersistentObjectStore.java	2013-02-27 20:45:18 UTC (rev 25316)
@@ -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

Added: branches/mule-3.3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java (0 => 25316)


--- branches/mule-3.3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java	                        (rev 0)
+++ branches/mule-3.3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java	2013-02-27 20:45:18 UTC (rev 25316)
@@ -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);
+        }
+    }
+
+}
Property changes on: branches/mule-3.3.x/core/src/test/java/org/mule/util/store/MuleObjectStoreManagerTestCase.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Added: branches/mule-3.3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java (0 => 25316)


--- branches/mule-3.3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java	                        (rev 0)
+++ branches/mule-3.3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java	2013-02-27 20:45:18 UTC (rev 25316)
@@ -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;
+    }
+
+}
Property changes on: branches/mule-3.3.x/core/src/test/java/org/mule/util/store/PartitionedPersistentObjectStoreTestCase.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Loading...