aslak

Arquillian - Infinispan - 3 node demo

Nov 23rd, 2010
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2010, Red Hat Middleware LLC, and individual contributors
  4.  * by the @authors tag. See the copyright.txt in the distribution for a
  5.  * full listing of individual contributors.
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package drumport;
  18.  
  19. import javax.inject.Inject;
  20.  
  21. import junit.framework.Assert;
  22.  
  23. import org.infinispan.Cache;
  24. import org.jboss.arquillian.api.Deployment;
  25. import org.jboss.arquillian.api.DeploymentTarget;
  26. import org.jboss.arquillian.api.Target;
  27. import org.jboss.arquillian.junit.Arquillian;
  28. import org.jboss.shrinkwrap.api.ShrinkWrap;
  29. import org.jboss.shrinkwrap.api.asset.EmptyAsset;
  30. import org.jboss.shrinkwrap.api.spec.WebArchive;
  31. import org.jboss.shrinkwrap.dependencies.Dependencies;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34.  
  35. /**
  36.  * @author Galder Zamarreño
  37.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  38.  */
  39. @RunWith(Arquillian.class)
  40. public class ContainerTestCase {
  41.  
  42.    @Deployment(name = "standby-dep", testable = false) @Target("standby")
  43.    public static WebArchive createStandbyDeployment()
  44.    {
  45.       return createStandByArchive();
  46.    }
  47.    
  48.    @Deployment(name = "active-1-dep") @Target("active-1")
  49.    public static WebArchive createTestDeployment()
  50.    {
  51.       return createActiveArchive("active1");
  52.    }
  53.  
  54.    @Deployment(name = "active-2-dep") @Target("active-2")
  55.    public static WebArchive createTestDeployment2()
  56.    {
  57.       return createActiveArchive("active2");
  58.    }
  59.    
  60.    public static WebArchive createStandByArchive()
  61.    {
  62.       return ShrinkWrap.create(WebArchive.class, "standby.war")
  63.             .addPackage("drumport.standby")
  64.             .addLibraries(
  65.                   Dependencies.artifact("org.infinispan:infinispan-core:4.2.0.BETA1")
  66.                               .artifact("org.infinispan:infinispan-cachestore-remote:4.2.0.BETA1")
  67.                               .artifact("org.infinispan:infinispan-server-hotrod:4.2.0.BETA1")
  68.                               .artifact("org.jboss.weld.servlet:weld-servlet:1.1.0-SNAPSHOT")
  69.                               .artifact("log4j:log4j:1.2.16").resolve())
  70.             .addWebResource(EmptyAsset.INSTANCE, "beans.xml")
  71.             .addResource("standby-in-container-context.xml", "/META-INF/context.xml")
  72.             .addResource("standby-infinispan.xml", "/WEB-INF/classes/standby-infinispan.xml")
  73.             .setWebXML("standby-in-container-web.xml");
  74.    }
  75.  
  76.    public static WebArchive createActiveArchive(String name)
  77.    {
  78.       return ShrinkWrap.create(WebArchive.class, name + ".war")
  79.             .addPackage("drumport.client")
  80.             .addLibraries(
  81.                   Dependencies.artifact("org.infinispan:infinispan-core:4.2.0.BETA1")
  82.                               .artifact("org.infinispan:infinispan-cachestore-remote:4.2.0.BETA1")
  83.                               .artifact("org.jboss.weld.servlet:weld-servlet:1.1.0-SNAPSHOT").resolve())
  84.             .addWebResource(EmptyAsset.INSTANCE, "beans.xml")
  85.             .addResource("in-container-context.xml", "/META-INF/context.xml")
  86.             .addResource("infinispan.xml", "/WEB-INF/classes/infinispan.xml")
  87.             .addResource("hotrod-client.properties", "/WEB-INF/classes/hotrod-client.properties")
  88.             .setWebXML("in-container-web.xml");
  89.    }
  90.  
  91.    
  92.    @Inject
  93.    private Cache<String, Integer> cache;
  94.    
  95.    @Test @DeploymentTarget("active-1-dep")
  96.    public void callActive1() throws Exception
  97.    {
  98.       int count = incrementCache(cache);
  99.       Assert.assertEquals(1, count);
  100.    }
  101.    
  102.    @Test @DeploymentTarget("active-2-dep")
  103.    public void callActive2() throws Exception
  104.    {
  105.       int count = incrementCache(cache);
  106.       Assert.assertEquals(2, count);
  107.    }
  108.  
  109.    private Integer incrementCache(Cache<String, Integer> cache)
  110.    {
  111.       String key = "counter";
  112.       Integer counter = cache.get(key);
  113.       Integer newCounter;
  114.       if (counter != null)
  115.       {
  116.          newCounter = counter.intValue() + 1;
  117.       }
  118.       else
  119.       {
  120.          newCounter = 1;
  121.       }
  122.       cache.put(key, newCounter);
  123.       return newCounter;
  124.    }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment