polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [42/50] zest-java git commit: ZEST-107 TestSupport: add MemoryCachePoolService for testing purpose
Date Tue, 28 Jul 2015 12:55:39 GMT
ZEST-107 TestSupport: add MemoryCachePoolService for testing purpose


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/ef1dcd50
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/ef1dcd50
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/ef1dcd50

Branch: refs/heads/master
Commit: ef1dcd503ef666b306c283f2627b3728bb8459e6
Parents: 12622af
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon Jul 27 16:28:00 2015 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon Jul 27 17:09:02 2015 +0200

----------------------------------------------------------------------
 core/testsupport/build.gradle                   |   4 +-
 .../org/qi4j/test/cache/MemoryCacheImpl.java    | 142 +++++++++++++++++++
 .../qi4j/test/cache/MemoryCachePoolMixin.java   |  82 +++++++++++
 .../qi4j/test/cache/MemoryCachePoolService.java |  37 +++++
 .../org/qi4j/test/cache/MemoryCacheTest.java    |  32 +++++
 5 files changed, 296 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/build.gradle
----------------------------------------------------------------------
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 @@ dependencies {
   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' )
+
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java
----------------------------------------------------------------------
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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java
----------------------------------------------------------------------
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() );
+    }
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java
----------------------------------------------------------------------
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();
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java
----------------------------------------------------------------------
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 );
+    }
+}


Mime
View raw message