aslak

Arquillian - Super Advanced Example

Jan 3rd, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 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(Arquillian.class)
  50. public class SuperAdvancedExampleTestCase
  51. {
  52.    // auto deployed on startup based on order
  53.    @Deployment(order = 1) @Target("server-1")
  54.    public static Descriptor startupEnvironmentServer1()
  55.    {
  56.       return Descriptors.create(JMSDescriptor.class);
  57.    }
  58.    
  59.    /*
  60.     *  auto deployed on startup (multiple startup deployments defined for same server, sorted by order),
  61.     *  enriched for in_container testing using HTTP Servlet 3.0 as test execution protocol
  62.     */
  63.    @Deployment(order = 2) @Target("server-1") @Protocol("Servlet 3.0")
  64.    public static Archive<?> startupTestableServer1()
  65.    {
  66.       return ShrinkWrap.create(JavaArchive.class);
  67.    }
  68.  
  69.    // needs to be manually deployed, enriched for in_container testing using EJB 3.0 as test execution protocol
  70.    @Deployment(name = "dep-1-server-2", startup = false) @Target("server-2") @Protocol("EJB 3.0")
  71.    public static Archive<?> manualTestableServer2()
  72.    {
  73.       return ShrinkWrap.create(JavaArchive.class);
  74.    }
  75.  
  76.    // needs to be manually deployed
  77.    @Deployment(name = "dep-1-server-3", startup = false, testable = false) @Target("server-3")
  78.    public static Archive<?> manualNonTestableServer3()
  79.    {
  80.       return ShrinkWrap.create(JavaArchive.class);
  81.    }
  82.  
  83.    /*
  84.     * Follow same default as @Test if no @DeploymentTarget defined. Target deployment is testable so it will only be executed when
  85.     * running against default deployment on server-1 and @Test is running in mode IN_CONTAINER.
  86.     * (AS_CLIENT mode never hit the server, so we can't just execute a Before IN_CONTAINER)
  87.     */
  88.    @Before // @DeploymentTarget("DEFAULT")
  89.    public void beforeOnServer1(MyCDIBean bean) // support for method argument injection in Before/After on same level as @Test
  90.    {
  91.       // do some setup ..
  92.    }
  93.    
  94.    // Follows default rules, but can always be executed even when @Test runs in mode AS_CLIENT.
  95.    @Before @Run(AS_CLIENT) // @DeploymentTarget("DEFAULT")
  96.    public void beforeOnServer1(@ArquillianResource(MyServlet.class) URL servletUrl)
  97.    {
  98.       // do some setup ..
  99.    }
  100.  
  101.    // Only runs when @Test targets the "dep-1-server-3" deployment, will run with Run mode AS_CLIENT since deployment is not testable
  102.    @BeforeClass @DeploymentTarget("dep-1-server-3")
  103.    public void beforeAgainstServere3()
  104.    {
  105.       // do something..
  106.    }
  107.    
  108.    /*
  109.     * Will always be attempted injected in all run modes and on all servers
  110.     * (could support @Optional or use method argument injection when mixing run modes / multi containers)
  111.     */
  112.    @Inject
  113.    private MyCommonResource resource;
  114.    
  115.    // defaults to Deployment with no defined name
  116.    @Test // @DeploymentTarget("DEFAULT")
  117.    public void executeOnServer1(@ArquillianResource Deployer deployer)
  118.    {
  119.       // do something, then manual deploy archives via callback to client
  120.       deployer.deploy("dep-1-server-2");
  121.       deployer.deploy("dep-1-server-3");
  122.    }
  123.    
  124.    /*
  125.     *  default run mode for a testable targeted deployment is IN_CONTIANER, override to run on client side.
  126.     *  communicates with container using Http Servlet 3.0 protocol.  
  127.     */
  128.    @Test @Run(AS_CLIENT) // @DeploymentTarget("DEFAULT")
  129.    public void executeWithContextSetAgainstServer1(@ArquillianResource InitialContext context)
  130.    {
  131.       // do something..
  132.    }
  133.    
  134.    /*
  135.     * defined target deployment, will execute on the deployments @Target server.
  136.     * communicates with container using RMI EJB 3.0 protocol.
  137.     */
  138.    @Test @DeploymentTarget("dep-1-server-2")
  139.    public void executeOnServer2(MyCDIBean bean)
  140.    {
  141.       // do something..
  142.    }
  143.    
  144.    // since DeploymentTarget is defined as testable = false, IN_CONTAINER is not possible. Run mode becomes AS_CLIENT
  145.    @Test @DeploymentTarget("dep-1-server-3")
  146.    public void executeWithContextSetAgainstServer3(@ArquillianResource(MyOtherSerlvet.class) URL servletUrl, @ArquillianResource Deployer deployer)
  147.    {
  148.       /*
  149.        * deployer API is available in both AS_CLIENT and IN_CONTIANER,
  150.        * deployments are registered for auto undeploy on AfterClass if not undeployed manually.
  151.        */
  152.       deployer.undeploy("dep-1-server-2");
  153.       // do something..
  154.    }
  155. }
Add Comment
Please, Sign In to add comment