Merge branch 'bug/ZEST-107' into develop

Closes #2 Github Pull-Request
diff --git a/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONEntityState.java b/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONEntityState.java
index 435acde..ae60809 100644
--- a/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONEntityState.java
+++ b/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONEntityState.java
@@ -308,14 +308,9 @@
         }
     }
 
-    boolean isStateNotCloned()
-    {
-        return status == EntityStatus.LOADED;
-    }
-
     void cloneStateIfGlobalStateLoaded()
     {
-        if( isStateNotCloned() )
+        if( status != EntityStatus.LOADED )
         {
             return;
         }
diff --git a/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONMapEntityStoreMixin.java b/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONMapEntityStoreMixin.java
index 47bf17f..cac8ec9 100644
--- a/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONMapEntityStoreMixin.java
+++ b/core/spi/src/main/java/org/qi4j/spi/entitystore/helpers/JSONMapEntityStoreMixin.java
@@ -479,7 +479,8 @@
             {
                 String type = data.getString( JSONKeys.TYPE );
                 EntityDescriptor entityDescriptor = module.entityDescriptor( type );
-                return new JSONEntityState( currentTime, valueSerialization, identity, entityDescriptor, data );
+//                return new JSONEntityState( currentTime, valueSerialization, identity, entityDescriptor, data );
+                return new JSONEntityState( valueSerialization, data.getString( JSONKeys.VERSION ), data.getLong( JSONKeys.MODIFIED ), identity, EntityStatus.LOADED, entityDescriptor, data );
             }
             catch( JSONException e )
             {
diff --git a/core/testsupport/build.gradle b/core/testsupport/build.gradle
index 7fc088a..2f0af58 100644
--- a/core/testsupport/build.gradle
+++ b/core/testsupport/build.gradle
@@ -24,4 +24,6 @@
   compile project( ':org.qi4j.core:org.qi4j.core.bootstrap' )
   compile libraries.junit
 
-}
\ No newline at end of file
+  testRuntime project( ':org.qi4j.core:org.qi4j.core.runtime' )
+
+}
diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/AbstractEntityStoreWithCacheTest.java b/core/testsupport/src/main/java/org/qi4j/test/cache/AbstractEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..0d3de54
--- /dev/null
+++ b/core/testsupport/src/main/java/org/qi4j/test/cache/AbstractEntityStoreWithCacheTest.java
@@ -0,0 +1,165 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.test.cache;
+
+import org.junit.Test;
+import org.qi4j.api.common.Optional;
+import org.qi4j.api.injection.scope.Service;
+import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.entity.AbstractEntityStoreTest;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Assert Cache behaviour when used by an EntityStore.
+ * <p>
+ * Use an in-memory CachePool by default, implement the <code>assembleCachePool</code> method to override.
+ */
+public abstract class AbstractEntityStoreWithCacheTest
+    extends AbstractEntityStoreTest
+{
+    @Optional @Service MemoryCachePoolService cachePool;
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        assembleCachePool( module );
+    }
+
+    protected void assembleCachePool( ModuleAssembly module )
+        throws AssemblyException
+    {
+        module.services( MemoryCachePoolService.class );
+    }
+
+    @Test
+    public void whenNewEntityThenCanFindEntityAndCorrectValues()
+        throws Exception
+    {
+        super.whenNewEntityThenCanFindEntityAndCorrectValues();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 1 ) );
+            assertThat( cache.puts(), is( 1 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void whenRemovedEntityThenCannotFindEntity()
+        throws Exception
+    {
+        super.whenRemovedEntityThenCannotFindEntity();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 0 ) );
+            assertThat( cache.gets(), is( 2 ) );
+            assertThat( cache.puts(), is( 1 ) );
+            assertThat( cache.removes(), is( 1 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void givenEntityIsNotModifiedWhenUnitOfWorkCompletesThenDontStoreState()
+        throws UnitOfWorkCompletionException
+    {
+        super.givenEntityIsNotModifiedWhenUnitOfWorkCompletesThenDontStoreState();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 2 ) );
+            assertThat( cache.puts(), is( 1 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void givenPropertyIsModifiedWhenUnitOfWorkCompletesThenStoreState()
+        throws UnitOfWorkCompletionException
+    {
+        super.givenPropertyIsModifiedWhenUnitOfWorkCompletesThenStoreState();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 2 ) );
+            assertThat( cache.puts(), is( 2 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void givenManyAssociationIsModifiedWhenUnitOfWorkCompletesThenStoreState()
+        throws UnitOfWorkCompletionException
+    {
+        super.givenManyAssociationIsModifiedWhenUnitOfWorkCompletesThenStoreState();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 2 ) );
+            assertThat( cache.puts(), is( 2 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification()
+        throws UnitOfWorkCompletionException
+    {
+        super.givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 4 ) );
+            assertThat( cache.puts(), is( 2 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+
+    @Test
+    public void givenEntityStoredLoadedChangedWhenUnitOfWorkDiscardsThenDontStoreState()
+        throws UnitOfWorkCompletionException
+    {
+        super.givenEntityStoredLoadedChangedWhenUnitOfWorkDiscardsThenDontStoreState();
+        if( cachePool != null )
+        {
+            MemoryCacheImpl<?> cache = cachePool.singleCache();
+            assertThat( cache.size(), is( 1 ) );
+            assertThat( cache.gets(), is( 2 ) );
+            assertThat( cache.puts(), is( 1 ) );
+            assertThat( cache.removes(), is( 0 ) );
+            assertThat( cache.exists(), is( 0 ) );
+        }
+    }
+}
diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java
new file mode 100644
index 0000000..18a99c8
--- /dev/null
+++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java
@@ -0,0 +1,142 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.test.cache;
+
+import java.util.concurrent.ConcurrentHashMap;
+import org.qi4j.spi.cache.Cache;
+
+/**
+ * In-Memory Cache implementation based on ConcurrentHashMap.
+ */
+public class MemoryCacheImpl<T>
+    implements Cache<T>
+{
+    private int refCount;
+
+    private final ConcurrentHashMap<String, Object> backingCache;
+    private final Class<T> valueType;
+    private final String id;
+
+    private int gets;
+    private int removes;
+    private int puts;
+    private int exists;
+
+    public MemoryCacheImpl( String cacheId, ConcurrentHashMap<String, Object> cache, Class<T> valueType )
+    {
+        this.id = cacheId;
+        this.backingCache = cache;
+        this.valueType = valueType;
+    }
+
+    @Override
+    public T get( String key )
+    {
+        try
+        {
+            return valueType.cast( backingCache.get( key ) );
+        }
+        finally
+        {
+            gets++;
+        }
+    }
+
+    @Override
+    public T remove( String key )
+    {
+        try
+        {
+            return valueType.cast( backingCache.remove( key ) );
+        }
+        finally
+        {
+            removes++;
+        }
+    }
+
+    @Override
+    public void put( String key, T value )
+    {
+        try
+        {
+            backingCache.put( key, value );
+        }
+        finally
+        {
+            puts++;
+        }
+    }
+
+    @Override
+    public boolean exists( String key )
+    {
+        try
+        {
+            return backingCache.containsKey( key );
+        }
+        finally
+        {
+            exists++;
+        }
+    }
+
+    synchronized void decRefCount()
+    {
+        refCount--;
+    }
+
+    synchronized void incRefCount()
+    {
+        refCount++;
+    }
+
+    synchronized boolean isNotUsed()
+    {
+        return refCount == 0;
+    }
+
+    public String cacheId()
+    {
+        return id;
+    }
+
+    public int size()
+    {
+        return backingCache.size();
+    }
+
+    public int gets()
+    {
+        return gets;
+    }
+
+    public int removes()
+    {
+        return removes;
+    }
+
+    public int puts()
+    {
+        return puts;
+    }
+
+    public int exists()
+    {
+        return exists;
+    }
+}
diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java
new file mode 100644
index 0000000..cc9e014
--- /dev/null
+++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java
@@ -0,0 +1,82 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.test.cache;
+
+import java.util.concurrent.ConcurrentHashMap;
+import org.qi4j.api.util.NullArgumentException;
+import org.qi4j.spi.cache.Cache;
+
+import static org.qi4j.functional.Iterables.single;
+
+/**
+ * In-Memory CachePool Mixin based on ConcurrentHashMap.
+ */
+public abstract class MemoryCachePoolMixin
+    implements MemoryCachePoolService
+{
+    private final ConcurrentHashMap<String, MemoryCacheImpl<?>> caches = new ConcurrentHashMap<>();
+
+    @Override
+    public <T> Cache<T> fetchCache( String cacheId, Class<T> valueType )
+    {
+        NullArgumentException.validateNotEmpty( "cacheId", cacheId );
+        MemoryCacheImpl<?> cache = caches.get( cacheId );
+        if( cache == null )
+        {
+            cache = createNewCache( cacheId, valueType );
+            caches.put( cacheId, cache );
+        }
+        cache.incRefCount();
+        return (Cache<T>) cache;
+    }
+
+    private <T> MemoryCacheImpl<T> createNewCache( String cacheId, Class<T> valueType )
+    {
+        return new MemoryCacheImpl<>( cacheId, new ConcurrentHashMap<String, Object>(), valueType );
+    }
+
+    @Override
+    public void returnCache( Cache<?> cache )
+    {
+        MemoryCacheImpl<?> memory = (MemoryCacheImpl<?>) cache;
+        memory.decRefCount();
+        if( memory.isNotUsed() )
+        {
+            caches.remove( memory.cacheId() );
+        }
+    }
+
+    @Override
+    public void activateService()
+        throws Exception
+    {
+        caches.clear();
+    }
+
+    @Override
+    public void passivateService()
+        throws Exception
+    {
+        caches.clear();
+    }
+
+    @Override
+    public MemoryCacheImpl<?> singleCache()
+    {
+        return single( caches.values() );
+    }
+}
diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java
new file mode 100644
index 0000000..66b5c94
--- /dev/null
+++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java
@@ -0,0 +1,37 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.test.cache;
+
+import org.qi4j.api.mixin.Mixins;
+import org.qi4j.api.service.ServiceActivation;
+import org.qi4j.spi.cache.CachePool;
+
+/**
+ * In-Memory CachePool Service.
+ */
+@Mixins( MemoryCachePoolMixin.class )
+public interface MemoryCachePoolService
+    extends CachePool, ServiceActivation
+{
+    /**
+     * Get the single Cache of this CachePool.
+     *
+     * @return The single Cache of this CachePool
+     * @throws IllegalArgumentException if no or more than one Cache is present in the CachePool
+     */
+    MemoryCacheImpl<?> singleCache();
+}
diff --git a/core/testsupport/src/main/java/org/qi4j/test/entity/AbstractEntityStoreTest.java b/core/testsupport/src/main/java/org/qi4j/test/entity/AbstractEntityStoreTest.java
index 4549826..f78f932 100644
--- a/core/testsupport/src/main/java/org/qi4j/test/entity/AbstractEntityStoreTest.java
+++ b/core/testsupport/src/main/java/org/qi4j/test/entity/AbstractEntityStoreTest.java
@@ -53,6 +53,7 @@
 import org.qi4j.test.AbstractQi4jTest;
 
 import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.not;
 import static org.joda.time.DateTimeZone.UTC;
 import static org.junit.Assert.assertThat;
@@ -455,6 +456,32 @@
         }
     }
 
+    @Test
+    public void givenEntityStoredLoadedChangedWhenUnitOfWorkDiscardsThenDontStoreState()
+        throws UnitOfWorkCompletionException
+    {
+        UnitOfWork unitOfWork = module.newUnitOfWork();
+        try
+        {
+            String identity = createEntity( unitOfWork ).identity().get();
+            unitOfWork.complete();
+
+            unitOfWork = module.newUnitOfWork();
+            TestEntity entity = unitOfWork.get( TestEntity.class, identity );
+            assertThat( entity.intValue().get(), is( 42 ) );
+            entity.intValue().set( 23 );
+            unitOfWork.discard();
+
+            unitOfWork = module.newUnitOfWork();
+            entity = unitOfWork.get( TestEntity.class, identity );
+            assertThat( entity.intValue().get(), is( 42 ) );
+        }
+        finally
+        {
+            unitOfWork.discard();
+        }
+    }
+
     public interface TestEntity
         extends EntityComposite
     {
diff --git a/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java b/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java
new file mode 100644
index 0000000..1bbd6cf
--- /dev/null
+++ b/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java
@@ -0,0 +1,32 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.test.cache;
+
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.cache.AbstractCachePoolTest;
+
+public class MemoryCacheTest
+    extends AbstractCachePoolTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        module.services( MemoryCachePoolService.class );
+    }
+}
diff --git a/extensions/entitystore-file/src/test/java/org/qi4j/entitystore/file/FileEntityStoreWithCacheTest.java b/extensions/entitystore-file/src/test/java/org/qi4j/entitystore/file/FileEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..4fd31a5
--- /dev/null
+++ b/extensions/entitystore-file/src/test/java/org/qi4j/entitystore/file/FileEntityStoreWithCacheTest.java
@@ -0,0 +1,42 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.file;
+
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.entitystore.file.assembly.FileEntityStoreAssembler;
+import org.qi4j.library.fileconfig.FileConfigurationService;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class FileEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        module.services( FileConfigurationService.class );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new FileEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+}
diff --git a/extensions/entitystore-hazelcast/src/test/java/org/qi4j/entitystore/hazelcast/HazelcastEntityStoreWithCacheTest.java b/extensions/entitystore-hazelcast/src/test/java/org/qi4j/entitystore/hazelcast/HazelcastEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..8b8f032
--- /dev/null
+++ b/extensions/entitystore-hazelcast/src/test/java/org/qi4j/entitystore/hazelcast/HazelcastEntityStoreWithCacheTest.java
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.hazelcast;
+
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.entitystore.hazelcast.assembly.HazelcastEntityStoreAssembler;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class HazelcastEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new HazelcastEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+}
diff --git a/extensions/entitystore-jclouds/src/test/java/org/qi4j/entitystore/jclouds/JCloudsWithCacheTest.java b/extensions/entitystore-jclouds/src/test/java/org/qi4j/entitystore/jclouds/JCloudsWithCacheTest.java
new file mode 100644
index 0000000..ab473c7
--- /dev/null
+++ b/extensions/entitystore-jclouds/src/test/java/org/qi4j/entitystore/jclouds/JCloudsWithCacheTest.java
@@ -0,0 +1,39 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.jclouds;
+
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class JCloudsWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new JCloudsMapEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+}
diff --git a/extensions/entitystore-jdbm/src/test/java/org/qi4j/entitystore/jdbm/JdbmEntityStoreWithCacheTest.java b/extensions/entitystore-jdbm/src/test/java/org/qi4j/entitystore/jdbm/JdbmEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..f5ded45
--- /dev/null
+++ b/extensions/entitystore-jdbm/src/test/java/org/qi4j/entitystore/jdbm/JdbmEntityStoreWithCacheTest.java
@@ -0,0 +1,54 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.jdbm;
+
+import org.junit.Before;
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.entitystore.jdbm.assembly.JdbmEntityStoreAssembler;
+import org.qi4j.library.fileconfig.FileConfiguration;
+import org.qi4j.library.fileconfig.FileConfigurationDataWiper;
+import org.qi4j.library.fileconfig.FileConfigurationService;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class JdbmEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Before
+    public void testDataCleanup()
+    {
+        FileConfiguration fileConfig = module.findService( FileConfiguration.class ).get();
+        FileConfigurationDataWiper.registerApplicationPassivationDataWiper( fileConfig, application );
+    }
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+
+        ModuleAssembly config = module.layer().module( "config" );
+        config.services( FileConfigurationService.class ).visibleIn( Visibility.layer ).instantiateOnStartup();
+        new EntityTestAssembler().assemble( config );
+
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new JdbmEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+}
diff --git a/extensions/entitystore-leveldb/src/test/java/org/qi4j/entitystore/leveldb/LevelDBEntityStoreWithCacheTest.java b/extensions/entitystore-leveldb/src/test/java/org/qi4j/entitystore/leveldb/LevelDBEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..71b7bdf
--- /dev/null
+++ b/extensions/entitystore-leveldb/src/test/java/org/qi4j/entitystore/leveldb/LevelDBEntityStoreWithCacheTest.java
@@ -0,0 +1,46 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.leveldb;
+
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.library.fileconfig.FileConfigurationService;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class LevelDBEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().visibleIn( Visibility.module ).assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+
+        module.services( FileConfigurationService.class );
+
+        new LevelDBEntityStoreAssembler().
+            withConfig( config, Visibility.layer ).
+            identifiedBy( "java-leveldb-entitystore" ).
+            assemble( module );
+    }
+}
diff --git a/extensions/entitystore-memory/src/test/java/org/qi4j/entitystore/memory/MemoryEntityStoreWithCacheTest.java b/extensions/entitystore-memory/src/test/java/org/qi4j/entitystore/memory/MemoryEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..3a1c72c
--- /dev/null
+++ b/extensions/entitystore-memory/src/test/java/org/qi4j/entitystore/memory/MemoryEntityStoreWithCacheTest.java
@@ -0,0 +1,35 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.memory;
+
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+public class MemoryEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new MemoryEntityStoreAssembler().assemble( module );
+    }
+}
diff --git a/extensions/entitystore-mongodb/src/test/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java b/extensions/entitystore-mongodb/src/test/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..7daec24
--- /dev/null
+++ b/extensions/entitystore-mongodb/src/test/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
@@ -0,0 +1,83 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.mongodb;
+
+import com.mongodb.Mongo;
+import org.junit.BeforeClass;
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+import static org.qi4j.test.util.Assume.assumeConnectivity;
+
+/**
+ * Test the MongoMapEntityStoreService usage with a CachePool.
+ * <p>Installing mongodb and starting it should suffice as the test use mongodb defaults: 127.0.0.1:27017</p>
+ */
+public class MongoMapEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @BeforeClass
+    public static void beforeRedisMapEntityStoreTests()
+    {
+        assumeConnectivity( "localhost", 27017 );
+    }
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+
+        new OrgJsonValueSerializationAssembler().assemble( module );
+
+        new MongoMapEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+
+        MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults();
+        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE );
+        mongoConfig.database().set( "qi4j:test" );
+        mongoConfig.collection().set( "qi4j:test:entities" );
+    }
+
+    private Mongo mongo;
+    private String dbName;
+
+    @Override
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        MongoMapEntityStoreService es = module.findService( MongoMapEntityStoreService.class ).get();
+        mongo = es.mongoInstanceUsed();
+        dbName = es.dbInstanceUsed().getName();
+
+    }
+
+    @Override
+    public void tearDown()
+        throws Exception
+    {
+        mongo.dropDatabase( dbName );
+        super.tearDown();
+    }
+}
diff --git a/extensions/entitystore-redis/src/test/java/org/qi4j/entitystore/redis/RedisMapEntityStoreWithCacheTest.java b/extensions/entitystore-redis/src/test/java/org/qi4j/entitystore/redis/RedisMapEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..ab15743
--- /dev/null
+++ b/extensions/entitystore-redis/src/test/java/org/qi4j/entitystore/redis/RedisMapEntityStoreWithCacheTest.java
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.redis;
+
+import org.junit.BeforeClass;
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+
+import static org.qi4j.test.util.Assume.assumeConnectivity;
+
+public class RedisMapEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @BeforeClass
+    public static void beforeRedisMapEntityStoreTests()
+    {
+        assumeConnectivity( "localhost", 6379 );
+    }
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new RedisMapEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+
+    private JedisPool jedisPool;
+
+    @Override
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        RedisMapEntityStoreService es = module.findService( RedisMapEntityStoreService.class ).get();
+        jedisPool = es.jedisPool();
+
+    }
+
+    @Override
+    public void tearDown()
+        throws Exception
+    {
+        Jedis jedis = jedisPool.getResource();
+        try
+        {
+            jedis.flushDB();
+        }
+        finally
+        {
+            jedisPool.returnResource( jedis );
+        }
+        super.tearDown();
+    }
+}
diff --git a/extensions/entitystore-riak/src/test/java/org/qi4j/entitystore/riak/RiakMapEntityStoreWithCacheTest.java b/extensions/entitystore-riak/src/test/java/org/qi4j/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
new file mode 100644
index 0000000..5a0e6b3
--- /dev/null
+++ b/extensions/entitystore-riak/src/test/java/org/qi4j/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
@@ -0,0 +1,76 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.qi4j.entitystore.riak;
+
+import com.basho.riak.client.IRiakClient;
+import com.basho.riak.client.bucket.Bucket;
+import org.junit.BeforeClass;
+import org.qi4j.api.common.Visibility;
+import org.qi4j.bootstrap.AssemblyException;
+import org.qi4j.bootstrap.ModuleAssembly;
+import org.qi4j.test.EntityTestAssembler;
+import org.qi4j.test.cache.AbstractEntityStoreWithCacheTest;
+import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+
+import static org.qi4j.test.util.Assume.assumeConnectivity;
+
+public class RiakMapEntityStoreWithCacheTest
+    extends AbstractEntityStoreWithCacheTest
+{
+    @BeforeClass
+    public static void beforeRiakProtobufMapEntityStoreTests()
+    {
+        assumeConnectivity( "localhost", 8087 );
+    }
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+        new OrgJsonValueSerializationAssembler().assemble( module );
+        new RiakProtobufMapEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+    }
+
+    private IRiakClient riakClient;
+    private String bucketKey;
+
+    @Override
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        RiakMapEntityStoreService es = module.findService( RiakMapEntityStoreService.class ).get();
+        riakClient = es.riakClient();
+        bucketKey = es.bucket();
+    }
+
+    @Override
+    public void tearDown()
+        throws Exception
+    {
+        // Riak don't expose bucket deletion in its API so we empty the Zest Entities bucket.
+        Bucket bucket = riakClient.fetchBucket( bucketKey ).execute();
+        for( String key : bucket.keys() )
+        {
+            bucket.delete( key ).execute();
+        }
+        super.tearDown();
+    }
+}