Advertisement
aslak

Arquillian - Suite

Jan 12th, 2011
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 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 org.jboss.arquillian.junit;
  18.  
  19. import static org.jboss.arquillian.api.RunModeType.AS_CLIENT;
  20.  
  21. import java.net.URL;
  22.  
  23. import javax.naming.InitialContext;
  24.  
  25. import org.jboss.arquillian.api.ArquillianResource;
  26. import org.jboss.arquillian.api.Deployer;
  27. import org.jboss.arquillian.api.Deployment;
  28. import org.jboss.arquillian.api.DeploymentTarget;
  29. import org.jboss.arquillian.api.Protocol;
  30. import org.jboss.arquillian.api.Run;
  31. import org.jboss.arquillian.api.Target;
  32. import org.jboss.arquillian.spi.core.annotation.Inject;
  33. import org.jboss.shrinkwrap.api.Archive;
  34. import org.jboss.shrinkwrap.api.ShrinkWrap;
  35. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  36. import org.jboss.shrinkwrap.descriptor.api.Descriptor;
  37. import org.jboss.shrinkwrap.descriptor.api.Descriptors;
  38. import org.junit.Before;
  39. import org.junit.BeforeClass;
  40. import org.junit.Test;
  41. import org.junit.runner.RunWith;
  42.  
  43. /**
  44.  * SuperAdvancedExampleTestCase
  45.  *
  46.  * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
  47.  * @version $Revision: $
  48.  */
  49. @RunWith(ArquillianSuite.class)
  50. @SuiteClasses(ChildTestOne.class, ChildTestTwo.class)
  51. public class SuperAdvancedSuiteExampleTestCase
  52. {
  53.    // auto deployed on startup
  54.    @Deployment(order = 1) @Target("server-1")
  55.    public static Descriptor startupEnvironmentServer1()
  56.    {
  57.       return Descriptors.create(JMSDescriptor.class);
  58.    }
  59.    
  60.    // auto deployed on startup, enriched for in_container testing using HTTP Servlet 3.0 as test execution protocol
  61.    @Deployment(order = 2) @Target("server-1") @Protocol("Servlet 3.0")
  62.    public static Archive<?> startupTestableServer1()
  63.    {
  64.       return ShrinkWrap.create(JavaArchive.class);
  65.    }
  66.  
  67.    // needs to be manually deployed, enriched for in_container testing using EJB 3.0 as test execution protocol
  68.    @Deployment(name = "dep-1-server-2", startup = false) @Target("server-2") @Protocol("EJB 3.0")
  69.    public static Archive<?> manualTestableServer2()
  70.    {
  71.       return ShrinkWrap.create(JavaArchive.class);
  72.    }
  73.  
  74.    // needs to be manually deployed
  75.    @Deployment(name = "dep-1-server-3", startup = false, testable = false) @Target("server-3")
  76.    public static Archive<?> manualNonTestableServer3()
  77.    {
  78.       return ShrinkWrap.create(JavaArchive.class);
  79.    }
  80.  
  81.    // These should really be stand alone classes, added as inner just for the example
  82.    
  83.    public static class ChildTestOne
  84.    {
  85.       /*
  86.        * No 'new' default deployment target defined in this Class, follow the rules from Suite class
  87.        */
  88.       @Before // @DeploymentTarget("DEFAULT")
  89.       public void beforeOnServer1(MyCDIBean bean)
  90.       {
  91.          // do some setup ..
  92.       }
  93.      
  94.       // same as in stand alone
  95.       @Before @Run(AS_CLIENT) // @DeploymentTarget("DEFAULT")
  96.       public void beforeOnServer1(@ArquillianResource(MyServlet.class) URL servletUrl)
  97.       {
  98.          // do some setup ..
  99.       }
  100.      
  101.  
  102.       // defaults to Deployment with no defined name
  103.       @Test // @DeploymentTarget("DEFAULT")
  104.       public void executeOnServer1(@ArquillianResource Deployer deployer)
  105.       {
  106.          // do something, then manual deploy archives via callback to client. Will be auto
  107.          deployer.deploy("dep-1-server-2");
  108.          deployer.deploy("dep-1-server-3");
  109.       }
  110.      
  111.       /*
  112.        *  default run mode for a testable targeted deployment is IN_CONTIANER, override to run on client side.
  113.        *  communicates with container using Http Servlet 3.0 protocol.  
  114.        */
  115.       @Test @Run(AS_CLIENT) // @DeploymentTarget("DEFAULT")
  116.       public void executeWithContextSetAgainstServer1(@ArquillianResource InitialContext context)
  117.       {
  118.          // do something..
  119.       }
  120.      
  121.       /*
  122.        * defined target deployment, will execute on the deployments @Target server.
  123.        * communicates with container using RMI EJB 3.0 protocol.
  124.        */
  125.       @Test @DeploymentTarget("dep-1-server-2")
  126.       public void executeOnServer2(MyCDIBean bean)
  127.       {
  128.          // do something..
  129.       }
  130.  
  131.    }
  132.    
  133.    public static class ChildTestTwo
  134.    {
  135.  
  136.       // Only runs when @Test targets the "dep-1-server-3" deployment, will run with Run mode AS_CLIENT since deployment is not testable
  137.       @BeforeClass @DeploymentTarget("dep-1-server-3")
  138.       public void beforeAgainstServere3()
  139.       {
  140.          // do something..
  141.       }
  142.      
  143.       /*
  144.        * Will always be attempted injected in all run modes and on all servers
  145.        * (could support @Optional or use method argument injection when mixing run modes / multi containers)
  146.        */
  147.       @Inject
  148.       private MyCommonResource resource;
  149.      
  150.      
  151.       // since DeploymentTarget is defined as testable = false, incontainer is not possible. Run mode becomes AS_CLIENT
  152.       @Test @DeploymentTarget("dep-1-server-3")
  153.       public void executeWithContextSetAgainstServer3(@ArquillianResource(MyOtherSerlvet.class) URL servletUrl, @ArquillianResource Deployer deployer)
  154.       {
  155.          deployer.undeploy("dep-1-server-2"); // manual undeploy is possible, but deployments are registered for auto undeploy on AfterClass
  156.          // do something..
  157.       }
  158.    }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement