It was released as Maven artifacts in JBoss Maven Repository. It is located at http://repository.jboss.org/nexus/content/groups/public-jboss/ . More information on how to set up and use the repo can be found at https://community.jboss.org/wiki/MavenGettingStarted-Users
What does this Arquillian extension offer to you? Let me describe all aspects of this extension one by one.
Developing tests with standalone Infinispan server
When testing, you might want to automatically start the Infinispan server before the test and stop it afterwards. This can be achieved by configuring infinispan-arquillian-container via Arquillian's configuration file. The following is a subset of attributes that can be specified and thus passed to the Infinispan server during startup: masterThreads, workerThreads, cacheConfig, jmxPort, ... The complete list can be found in bit.ly/R7j4d1 (all private fields).
NOTE: Examples are not a part of the release, only libraries are. In order to check out examples provided with the project, one has to clone project's repository: https://github.com/mgencur/infinispan-arquillian-container Examples are located in the respective sub-directory.
The configuration file then looks similar to the following:
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
<arquillian xmlns="http://www.jboss.org/arquillian-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.jboss.org/arquillian-1.0 http://jboss.org/schema/arquillian/arquillian-1.0.xsd"> | |
<group qualifier="Grid"> | |
<container qualifier="container1"> | |
<configuration> | |
<property name="ispnHome">/path/to/infinispan/server1</property> | |
<property name="cacheConfig">/path/to/xml/cache/config/file/</property> | |
<property name="protocol">hotrod</property> | |
</configuration> | |
</container> | |
<container qualifier="container2"> | |
<configuration> | |
<property name="ispnHome">/path/to/infinispan/server2</property> | |
<property name="protocol">hotrod</property> | |
<property name="cacheConfig">/path/to/xml/cache/config/file/</property> | |
<!-- use port-offset for second server running on localhost --> | |
<property name="port">11322</property> | |
<property name="jmxPort">1190</property> | |
</configuration> | |
</container> | |
</group> | |
</arquillian> |
Complete example: bit.ly/RkrpEE
When we tell Arquillian to work with Infinispan server, we can inject RemoteInfinispanServer object into our test. Such an object provides various information about the running Infinispan server. For example, we can retrieve a hostname and HotRod port and use these pieces of information to create a RemoteCacheManager instance. Besides that users are allowed to retrieve information available via JMX from the server like cluster size, number of entries in the cache, number of cache hits and many more.
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
@RunWith(Arquillian.class) | |
public class ServerInfoTest | |
{ | |
// container1 and container2 are labels for servers defined in arquillian.xml | |
@InfinispanResource("container1") | |
RemoteInfinispanServer server1; | |
@InfinispanResource("container2") | |
RemoteInfinispanServer server2; | |
@Test | |
public void test() { | |
RemoteCacheManager m = | |
new RemoteCacheManager( | |
server1.getHotrodEndpoint().getInetAddress().getHostName(), | |
server1.getHotrodEndpoint().getPort()); | |
RemoteCache<String, Integer> cache = m.getCache(); | |
System.out.println("Cluster size:" + | |
server1.getDefaultCacheManager().getClusterSize()); | |
System.out.println("Hits:" + | |
server2.getDefaultCacheManager().getDefaultCache().getHits()); | |
} | |
@Test | |
public void test2(@InfinispanResource("container2") RemoteInfinispanServer server3) | |
{ | |
//yes, RemoteInfinispanServer can be injected also into a method's parameter | |
System.out.println("Average read time: " + server3.getDefaultCacheManager() | |
.getDefaultCache().getStatistics(CacheStatistics.AVERAGE_READ_TIME)); | |
} | |
} |
Vital dependencies required for the test to run are:
org.infinispan.arquillian.container:infinispan-arquillian-container-managed:jar:1.0.0.CR1:test
org.infinispan.arquillian.container:infinispan-arquillian-impl:jar:1.0.0.CR1:test
Not only with standalone Infinispan server can Infinispan Arquillian extension work.
Developing tests with JBoss Data Grid (JDG) server
This time, the properties in Arquillian's configuration file are different and correspond to properties of JBoss Application Server 7. The most important property is again the path to the server (jbossHome).
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
<arquillian xmlns="http://www.jboss.org/arquillian-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.jboss.org/arquillian-1.0 http://jboss.org/schema/arquillian/arquillian-1.0.xsd"> | |
<group qualifier="Grid"> | |
<container qualifier="container1"> | |
<configuration> | |
<property name="jbossHome">/path/to/jboss-datagrid-server1</property> | |
<property name="managementPort">9999</property> | |
... | |
</configuration> | |
</container> | |
<container qualifier="container2"> | |
<configuration> | |
<property name="jbossHome">/path/to/jboss-datagrid-server2</property> | |
<property name="managementPort">10099</property> | |
... | |
</configuration> | |
</container> | |
</group> | |
</arquillian> |
As a result, you can call the following methods with JDG in one single test.
server1.getMemcachedEndpoint().getPort();
server1.getRESTEndpoint().getContextPath();
server1.getHotRodEndpoint().getPort();
The difference is, of course in dependencies. Instead of a handler for standalone Infinispan server, one has to use a handler for JBoss AS 7. The dependencies then look like this:
org.jboss.as:jboss-as-arquillian-container-managed:jar:7.1.2.Final:test
org.infinispan.arquillian.container:infinispan-arquillian-impl:jar:1.0.0.CR1:test
Testing Infinispan libraries
Sometimes we don't want to use a standalone server. Sometimes we want to test just Infinispan in its basic form - Java libraries. Infinispan has been under development for years and during that time, lots of tests were developed. With tests come utility methods. Infinispan Arquillian Container enables you to leverage these utility methods and call them via an instance of DatagridManager. This instance can be easily injected into a test, no matter which test framework (TestNG, JUnit) you use.
DatagridManager class can be found at http://bit.ly/Q0a7ki
Can you see the advantage? No? Let me point out some useful methods available in the manager.
List<Cache<K, V>> createClusteredCaches(int numMembersInCluster, String cacheName, ConfigurationBuilder builder)
- creates a cluster of caches with certain name and pre-defined configuration
void waitForClusterToForm()
- helps to wait until the cluster is up and running
Cache<A, B> cache(int index)
- retrieves a cache from certain node according to the index
Cache<A, B> cache(int managerIndex, String cacheName)
- retrieves a cache with that name
void killMember(int cacheIndex)
- kills a cache with cacheIndex index
AdvancedCache advancedCache(int i)
- retrieves an advanced cache from node i
Trancation tx(int i)
- retrieves a transaction from node i
TransactionManager tm(int i)
- retrieves a transaction manager from node i
...and much more.
The following test can be found among other examples in the GIT repository.
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
public class ReplicatedExpiryTest extends Arquillian | |
{ | |
@InfinispanResource | |
DatagridManager dm; | |
@BeforeMethod | |
public void setUp() throws Exception | |
{ | |
Configuration cfg = | |
dm.getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC); | |
dm.createClusteredCaches(2, "cache", cfg); | |
} | |
@Test | |
public void testLifespanExpiryReplicates() | |
{ | |
Cache c1 = dm.cache(0, "cache"); | |
Cache c2 = dm.cache(1, "cache"); | |
long lifespan = 3000; | |
c1.put("k", "v", lifespan, MILLISECONDS); | |
InternalCacheEntry ice = c2.getAdvancedCache().getDataContainer().get("k"); | |
assert ice instanceof MortalCacheEntry; | |
assert ice.getLifespan() == lifespan; | |
assert ice.getMaxIdle() == -1; | |
} | |
} |
org.infinispan:infinispan-core:jar:5.1.5.FINAL:test - users should replace this version with the one they want to test
org.infinispan.arquillian.container:infinispan-arquillian-impl:jar:1.0.0.CR1:test
Infinispan Arquillian Container was tested with Infinispan 5.1.5.FINAL and JDG 6.0.0.GA. Nevertheless, it should work smoothly also with other not-very-distinct versions. I'll be updating the project to work with newer versions of both Infinispan and JBoss Data Grid.
Very informative and very well written. Thanks!
OdpovědětVymazat