Skip to content

Commit

Permalink
ISPN-1330 - Add tests for @CacheKeyParam, @CachePut and @CacheValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpollet authored and maniksurtani committed Sep 21, 2011
1 parent 28e80ce commit f058693
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.infinispan.cdi.test.interceptor;

import org.infinispan.Cache;
import org.infinispan.cdi.interceptor.DefaultCacheKey;
import org.infinispan.cdi.test.interceptor.config.Config;
import org.infinispan.cdi.test.interceptor.config.Custom;
import org.infinispan.cdi.test.interceptor.service.CachePutService;
import org.infinispan.cdi.test.interceptor.service.CustomCacheKey;
import org.infinispan.manager.CacheContainer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.cache.interceptor.CacheKey;
import javax.inject.Inject;
import java.lang.reflect.Method;

import static org.infinispan.cdi.test.testutil.Deployments.baseDeployment;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

/**
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
* @see javax.cache.interceptor.CachePut
*/
@Test(groups = "functional", testName = "cdi.test.interceptor.CachePutInterceptorTest")
public class CachePutInterceptorTest extends Arquillian {

@Deployment
public static Archive<?> deployment() {
return baseDeployment()
.addClass(CachePutInterceptorTest.class)
.addPackage(CachePutService.class.getPackage())
.addPackage(Config.class.getPackage());
}

@Inject
private CachePutService service;

@Inject
private CacheContainer cacheContainer;

@Custom
@Inject
private Cache<CacheKey, String> customCache;

@BeforeMethod
public void beforeMethod() {
customCache.clear();
assertTrue(customCache.isEmpty());
}

public void testCachePut() {
final StringBuilder cacheName = new StringBuilder()
.append(CachePutService.class.getName())
.append(".put(long,java.lang.String)");

final Cache<CacheKey, String> cache = cacheContainer.getCache(cacheName.toString());

service.put(0l, "Manik");
service.put(0l, "Kevin");
service.put(1l, "Pete");

assertEquals(cache.size(), 2);
assertTrue(cache.containsKey(new DefaultCacheKey(new Object[]{0l})));
assertTrue(cache.containsKey(new DefaultCacheKey(new Object[]{1l})));
}

public void testCachePutWithCacheName() {
service.putWithCacheName(0l, "Manik");
service.putWithCacheName(0l, "Kevin");
service.putWithCacheName(1l, "Pete");

assertEquals(customCache.size(), 2);
assertTrue(customCache.containsKey(new DefaultCacheKey(new Object[]{0l})));
assertTrue(customCache.containsKey(new DefaultCacheKey(new Object[]{1l})));
}

public void testCachePutCacheKeyParam() {
service.putWithCacheKeyParam(0l, 1l, "Manik");
service.putWithCacheKeyParam(0l, 1l, "Kevin");
service.putWithCacheKeyParam(1l, 2l, "Pete");

assertEquals(customCache.size(), 2);
assertTrue(customCache.containsKey(new DefaultCacheKey(new Object[]{0l})));
assertTrue(customCache.containsKey(new DefaultCacheKey(new Object[]{1l})));
}

public void testCachePutBeforeInvocation() {
try {

service.putBeforeInvocation(0l, "Manik");

} catch (RuntimeException e) {
assertEquals(customCache.size(), 1);
}
}

public void putWithCacheKeyGenerator() throws Exception {
final Method method = CachePutService.class.getMethod("putWithCacheKeyGenerator", Long.TYPE, String.class);

service.putWithCacheKeyGenerator(0l, "Manik");
service.putWithCacheKeyGenerator(0l, "Kevin");
service.putWithCacheKeyGenerator(1l, "Pete");

assertEquals(customCache.size(), 2);
assertTrue(customCache.containsKey(new CustomCacheKey(method, 0l)));
assertTrue(customCache.containsKey(new CustomCacheKey(method, 0l)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ public void testCacheRemoveEntryWithCacheName() {
assertEquals(customCache.size(), 0);
}

public void testCacheRemoveEntryWithCacheKeyParam() {
final CacheKey cacheKey = new DefaultCacheKey(new Object[]{"Kevin"});

customCache.put(cacheKey, "Hello Kevin");

assertEquals(customCache.size(), 1);
assertTrue(customCache.containsKey(cacheKey));

service.removeEntryWithCacheKeyParam("Kevin", "foo");

assertEquals(customCache.size(), 0);
}

public void testCacheRemoveEntryAfterInvocationWithException() {
final CacheKey cacheKey = new DefaultCacheKey(new Object[]{"Kevin"});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testCacheResult() throws NoSuchMethodException {
assertEquals("Morning Foo", message);
assertEquals(cache.size(), 1);

assertEquals(service.getCacheResult(), 1);
assertEquals(service.getNbCall(), 1);
}

public void testCacheResultWithCacheName() {
Expand All @@ -90,7 +90,23 @@ public void testCacheResultWithCacheName() {
assertEquals("Hi Pete", message);
assertEquals(customCache.size(), 1);

assertEquals(service.getCacheResultWithCacheName(), 1);
assertEquals(service.getNbCall(), 1);
}

public void testCacheResultWithCacheKeyParam() {
String message = service.cacheResultWithCacheKeyParam("Pete", "foo");

assertNotNull(message);
assertEquals("Hola Pete", message);
assertEquals(customCache.size(), 1);

message = service.cacheResultWithCacheKeyParam("Pete", "foo2");

assertNotNull(message);
assertEquals("Hola Pete", message);
assertEquals(customCache.size(), 1);

assertEquals(service.getNbCall(), 1);
}

public void testCacheResultWithCustomCacheKeyGenerator() throws NoSuchMethodException {
Expand All @@ -111,7 +127,7 @@ public void testCacheResultWithCustomCacheKeyGenerator() throws NoSuchMethodExce
assertEquals("Hello Kevin", message);
assertEquals(customCache.size(), 1);

assertEquals(service.getCacheResultWithCacheKeyGenerator(), 1);
assertEquals(service.getNbCall(), 1);
}

public void testCacheResultWithSkipGet() throws NoSuchMethodException {
Expand All @@ -127,7 +143,7 @@ public void testCacheResultWithSkipGet() throws NoSuchMethodException {
assertEquals("Hey Manik", message);
assertEquals(customCache.size(), 1);

assertEquals(service.getCacheResultSkipGet(), 2);
assertEquals(service.getNbCall(), 2);
}

public void testCacheResultWithSpecificCacheManager() {
Expand All @@ -143,7 +159,7 @@ public void testCacheResultWithSpecificCacheManager() {
assertEquals("Bonjour Pete", message);
assertEquals(smallCache.size(), 1);

assertEquals(service.getCacheResultWithSpecificCacheManager(), 1);
assertEquals(service.getNbCall(), 1);
assertEquals(smallCache.size(), 1);
assertEquals(smallCache.getConfiguration().getEvictionMaxEntries(), 4);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.infinispan.cdi.test.interceptor.service;

import javax.cache.interceptor.CacheKeyParam;
import javax.cache.interceptor.CachePut;
import javax.cache.interceptor.CacheValue;

/**
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
public class CachePutService {

@CachePut
public void put(long id, @CacheValue String name) {
}

@CachePut(cacheName = "custom")
public void putWithCacheName(long id, @CacheValue String name) {
}

@CachePut(cacheName = "custom")
public void putWithCacheKeyParam(@CacheKeyParam long id, long id2, @CacheValue String name) {
}

@CachePut(cacheName = "custom", afterInvocation = false)
public void putBeforeInvocation(long id, @CacheValue String name) {
throw new RuntimeException();
}

@CachePut(cacheName = "custom", cacheKeyGenerator = CustomCacheKeyGenerator.class)
public void putWithCacheKeyGenerator(long id, @CacheValue String name) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package org.infinispan.cdi.test.interceptor.service;

import javax.cache.interceptor.CacheKeyParam;
import javax.cache.interceptor.CacheRemoveEntry;

import static org.infinispan.cdi.util.Contracts.assertNotNull;
Expand All @@ -41,6 +42,11 @@ public void removeEntryWithCacheName(String login) {
assertNotNull(login, "login parameter cannot be null");
}

@CacheRemoveEntry(cacheName = "custom")
public void removeEntryWithCacheKeyParam(@CacheKeyParam String login, String unused) {
assertNotNull(login, "login parameter cannot be null");
}

@CacheRemoveEntry(cacheName = "custom", afterInvocation = false)
public void removeEntryBeforeInvocationWithException(String login) {
assertNotNull(login, "login parameter cannot be null");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,56 @@
package org.infinispan.cdi.test.interceptor.service;

import javax.cache.interceptor.CacheKeyParam;
import javax.cache.interceptor.CacheResult;
import javax.enterprise.context.ApplicationScoped;

/**
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
@ApplicationScoped
public class CacheResultService {

private int cacheResult;
private int cacheResultWithCacheKeyGenerator;
private int cacheResultSkipGet;
private int cacheResultWithCacheName;
private int cacheResultWithSpecificCacheManager;
private int nbCall;

public CacheResultService() {
this.cacheResult = 0;
this.cacheResultWithCacheKeyGenerator = 0;
this.cacheResultSkipGet = 0;
this.cacheResultWithCacheName = 0;
this.cacheResultWithSpecificCacheManager = 0;
this.nbCall = 0;
}

@CacheResult
public String cacheResult(String user) {
cacheResult++;
nbCall++;
return "Morning " + user;
}

@CacheResult(cacheName = "custom")
public String cacheResultWithCacheName(String user) {
cacheResultWithCacheName++;
nbCall++;
return "Hi " + user;
}

@CacheResult(cacheName = "custom")
public String cacheResultWithCacheKeyParam(@CacheKeyParam String user, String unused) {
nbCall++;
return "Hola " + user;
}

@CacheResult(cacheName = "custom", cacheKeyGenerator = CustomCacheKeyGenerator.class)
public String cacheResultWithCacheKeyGenerator(String user) {
cacheResultWithCacheKeyGenerator++;
nbCall++;
return "Hello " + user;
}

@CacheResult(cacheName = "custom", skipGet = true)
public String cacheResultSkipGet(String user) {
cacheResultSkipGet++;
nbCall++;
return "Hey " + user;
}

@CacheResult(cacheName = "small")
public String cacheResultWithSpecificCacheManager(String user) {
cacheResultWithSpecificCacheManager++;
nbCall++;
return "Bonjour " + user;
}

public int getCacheResultWithCacheKeyGenerator() {
return cacheResultWithCacheKeyGenerator;
}

public int getCacheResultSkipGet() {
return cacheResultSkipGet;
}

public int getCacheResultWithCacheName() {
return cacheResultWithCacheName;
}

public int getCacheResult() {
return cacheResult;
}

public int getCacheResultWithSpecificCacheManager() {
return cacheResultWithSpecificCacheManager;
public int getNbCall() {
return nbCall;
}
}
1 change: 1 addition & 0 deletions cdi/src/test/resources/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<interceptors>
<class>org.infinispan.cdi.interceptor.CacheResultInterceptor</class>
<class>org.infinispan.cdi.interceptor.CachePutInterceptor</class>
<class>org.infinispan.cdi.interceptor.CacheRemoveEntryInterceptor</class>
<class>org.infinispan.cdi.interceptor.CacheRemoveAllInterceptor</class>
</interceptors>
Expand Down

0 comments on commit f058693

Please sign in to comment.