-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISPN-1330 - Add tests for @CacheKeyParam, @CachePut and @CacheValue.
- Loading branch information
1 parent
28e80ce
commit f058693
Showing
7 changed files
with
245 additions
and
40 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
cdi/src/test/java/org/infinispan/cdi/test/interceptor/CachePutInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
cdi/src/test/java/org/infinispan/cdi/test/interceptor/service/CachePutService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 16 additions & 35 deletions
51
cdi/src/test/java/org/infinispan/cdi/test/interceptor/service/CacheResultService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters