11 Replies Latest reply on Dec 15, 2011 3:18 PM by Aslak Knutsen

    @ArquillianResource Deployer gets injected only for the first test

    George Vagenas Newbie

      Working with unmanaged deployment and container in manualy mode , the injected Deployer is null on the second and the next tests in the class.

       


      @ArquillianResource

      private Deployer deployer;

      ....


      @Deployment(name="simple", managed=false)

      public static WebArchive createTestArchive()

      ....

       

      Any idea here?

       

      Regards

      George

        • 1. Re: @ArquillianResource Deployer gets injected only for the first test
          Radoslav Husar Master

          George,

           

          very very similar to my thread, what a coincidence, see http://community.jboss.org/thread/176096?tstart=0

           

          The summary being that the deployments need to be already deployed in order for @ArquillianResource to work. See the thread for workaround from Aslak.

           

          HTH,

          Rado

          • 2. Re: @ArquillianResource Deployer gets injected only for the first test
            George Vagenas Newbie

            Rado,

             

            I saw your thread before but i thought it was only related to the @OperateOnDeployement. I am working with latest Core master branch and the issue is still there.

             

            Regards

            George

            • 3. Re: @ArquillianResource Deployer gets injected only for the first test
              Radoslav Husar Master

              Ah, I should have been paid more attention to reading. Strage, the deployer itself is null? I though its only injected at the beginning of the class, so that would sound like a bug..

              • 5. Re: @ArquillianResource Deployer gets injected only for the first test
                George Vagenas Newbie

                Aslak,

                 

                See below:

                 

                 

                @RunWith(Arquillian.class)
                public class ShootistSipServletTest extends SipTestCase
                {
                   @ArquillianResource
                   private Deployer deployer;
                
                
                   private SipStack receiver;
                
                
                   private SipCall sipCall;
                
                
                   private SipPhone sipPhone;
                
                
                   private final int timeout = 10000;
                
                
                   private final Logger logger = Logger.getLogger(ShootistSipServletTest.class.getName());
                
                
                   private SipStackTool sipStackTool;
                
                
                   @Before
                   public void setUp() throws Exception
                   {
                   }
                
                
                   @After
                   public void tearDown() throws Exception
                   {
                   }
                
                
                   @Deployment(name = "simple", managed = false)
                   public static WebArchive createTestArchive()
                   {
                      WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "shootistsipservlet.war");
                      webArchive.addClasses(ShootistSipServlet.class);
                      webArchive.addAsWebInfResource("in-container-sip.xml", "sip.xml");
                
                
                      return webArchive;
                   }
                
                
                   @Test
                   public void testShootist() throws Exception
                   {
                      String testArchive = "simple";
                      SipStackTool sipStackTool = new SipStackTool();
                
                
                      //First create the sipCall and start listening for messages
                      receiver = sipStackTool.initializeSipStack(SipStack.PROTOCOL_UDP, "127.0.0.1", "5080", "127.0.0.1:5070");
                      sipPhone = receiver.createSipPhone("127.0.0.1", SipStack.PROTOCOL_UDP, 5070, "sip:LittleGuy@there.com");
                      sipCall = sipPhone.createSipCall();
                      sipCall.listenForIncomingCall();
                
                
                      //Deploy the first test archive
                      deployer.deploy(testArchive);
                
                
                      assertTrue(sipCall.waitForIncomingCall(timeout));
                      assertTrue(sipCall.sendIncomingCallResponse(Response.TRYING, "Trying", timeout));
                
                
                      Thread.sleep(100);
                
                
                      assertTrue(sipCall.sendIncomingCallResponse(Response.RINGING, "RINGING", timeout));
                
                
                      Thread.sleep(100);
                
                
                      assertTrue(sipCall.sendIncomingCallResponse(Response.OK, "OK", timeout));
                
                
                      Thread.sleep(100);
                
                
                      if (sipCall.getLastReceivedResponse() != null)
                
                
                         logger.info("sipCallB lastReceivedResponse: " + sipCall.getLastReceivedResponse().toString());
                      sipCall.listenForDisconnect();
                
                
                      assertTrue(sipCall.waitForDisconnect(timeout));
                      sipCall.respondToDisconnect();
                
                
                      Thread.sleep(500);
                
                
                      deployer.undeploy(testArchive);
                
                
                      sipStackTool.tearDown();
                
                
                      Thread.sleep(timeout);
                   }
                
                
                   @Test @ContextParam(name = "cancel", value = "true")
                   public void testShootistCancel() throws Exception
                   {
                      String testArchive = "simple";
                      SipStackTool sipStackTool = new SipStackTool();
                      receiver = sipStackTool.initializeSipStack(SipStack.PROTOCOL_UDP, "127.0.0.1", "5080", "127.0.0.1:5070");
                
                
                      sipPhone = receiver.createSipPhone("127.0.0.1", SipStack.PROTOCOL_UDP, 5070, "sip:LittleGuy@there.com");
                
                
                      Address addr = sipPhone.getParent().getAddressFactory()
                            .createAddress("Shootme <sip:127.0.0.1:5080;transport=udp>");
                
                
                      ContactHeader contactHeader = sipPhone.getParent().getHeaderFactory().createContactHeader(addr);
                
                
                      ArrayList<Header> replacedHeaders = new ArrayList<Header>();
                
                
                      replacedHeaders.add(contactHeader);
                
                
                      sipCall = sipPhone.createSipCall();
                
                
                      sipCall.listenForIncomingCall();
                
                
                      //Deploy the test archive
                
                
                      logger.info("About to deploy the application");
                
                
                      deployer.deploy(testArchive);
                
                
                      sipCall.waitForIncomingCall(60000);
                
                
                      sipCall.listenForCancel();
                
                
                      assertTrue(sipCall.sendIncomingCallResponse(Response.RINGING, "RINGING", timeout, null, replacedHeaders, null));
                
                
                      SipTransaction trans1 = sipCall.waitForCancel(timeout);
                
                
                      assertNotNull(trans1);
                
                
                      assertRequestReceived("CANCEL NOT RECEIVED", SipRequest.CANCEL, sipCall);
                
                
                      assertTrue(sipCall.respondToCancel(trans1, 200, "0K", -1));
                
                
                      // close the INVITE transaction on the called leg
                
                
                      assertTrue("487 NOT SENT",
                            sipCall.sendIncomingCallResponse(SipResponse.REQUEST_TERMINATED, "Request Terminated", 0));
                
                
                      assertTrue(sipCall.waitForMessage(20000));
                
                
                      assertTrue(sipCall.waitForMessage(20000));
                
                
                      List<String> allMessagesContent = sipCall.getAllReceivedMessagesContent();
                
                
                      assertTrue(allMessagesContent.size() >= 2);
                
                
                      assertTrue("sipSessionReadyToInvalidate", allMessagesContent.contains("sipSessionReadyToInvalidate"));
                
                
                      assertTrue("sipAppSessionReadyToInvalidate", allMessagesContent.contains("sipAppSessionReadyToInvalidate"));
                
                
                      deployer.undeploy(testArchive);
                
                
                      sipStackTool.tearDown();
                
                
                      Thread.sleep(timeout);
                
                
                   }
                }
                

                 

                Bare in mind that i am using an extension to control container and provide extra configuration if needed before startup (for example with @ContextParam). Here is a snapshot of the extension:

                 

                   @Inject
                   private Instance<ArquillianDescriptor> descriptor;
                
                
                   @Inject
                   private Instance<ContainerController> containerController;
                
                
                   private Annotation contextParam;
                
                
                   private Annotation contextParamMap;
                
                
                   private Annotation concurencyControl;
                
                
                   public void executeBeforeTest(@Observes Before event, TestClass testClass) throws InterruptedException
                   {
                      Map<String, String> parameters = new HashMap<String, String>();
                      List<ContainerDef> containerDefs = descriptor.get().getContainers();
                      Iterator<ContainerDef> iter = containerDefs.iterator();
                      if (checkForAnnotations(event))
                         parameters = getParameters(event);
                
                
                      while (iter.hasNext())
                      {
                         ContainerDef containerDef = (ContainerDef) iter.next();
                         String containerName = containerDef.getContainerName();
                         if (parameters.isEmpty())
                         {
                            containerController.get().start(containerName);
                         }
                         else
                         {
                            containerController.get().start(containerName, parameters);
                         }
                      }
                   }
                
                
                   public void executeAfterTest(@Observes After event, TestClass testClass)
                   {
                      List<ContainerDef> containerDefs = descriptor.get().getContainers();
                      Iterator<ContainerDef> iter = containerDefs.iterator();
                      while (iter.hasNext())
                      {
                         ContainerDef containerDef = (ContainerDef) iter.next();
                         String containerName = containerDef.getContainerName();
                         containerController.get().stop(containerName);
                      }
                   }
                
                

                 

                Thanks

                George

                • 6. Re: @ArquillianResource Deployer gets injected only for the first test
                  Aslak Knutsen Master

                  Need to setup a 'simplified' test scenario for this and debug. Not quite sure why it would behave like that.

                   

                  Does it always inject null on the second+ @Test method, or only if the second method has a @ContextParam ?

                  • 7. Re: @ArquillianResource Deployer gets injected only for the first test
                    George Vagenas Newbie

                    Aslak,

                     

                    Using the @InSequence annotation, i changed the execution order of the tests and i still have the same issue, that is for the next tests the deployer is null. Tested also without the @ContextParam and deployer is again null for the second test.

                     

                    Note that when each of the tests run individually the deployer is working fine.

                     

                    Since now i am working on Mobicents Sip-Servlets arquillian container, I will setup a similar test scenarion on arquillian tomcat-6 embedded and try to reproduce this issue.

                     

                    Thanks

                    • 8. Re: @ArquillianResource Deployer gets injected only for the first test
                      George Vagenas Newbie

                      Aslak,

                       

                      Well i have a simple test case with tomcat-embedded-6 and three test methods and here are my findings.

                       

                      - Everything GREEN when testing with test archive managed=true and container in auto mode.

                       

                      - When the test archive is unmanaged and the container in manual mode then i've got  the following exception for the second and third test :

                       

                      java.lang.IllegalStateException: Error launching request at http://localhost:8888/test2/ArquillianServletRunner?outputMode=serializedObject&className=org.mobicents.servlet.sip.arq.testsuite.TomcatEmbeddedInContainerTestCase&methodName=testMethod2. No result returned
                      

                       

                      As you will see below from the deployment archive and test methods, i am running the tests in "in-container" mode. When i change to "as-client" mode everything works perfect. The "as-client" mode also solves the issue i have with the null deployer in my test case.

                       

                      I am not sure if the container control and deployer should behave like this in the "in-container" mode, is that a bug?

                       

                      Note that i am working with the updated core master branch.

                      Find attached the project.

                       

                      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                      Code Snippet

                      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                      The deployment archive:

                       

                      @Deployment(name="first", managed=false)
                      public static WebArchive createTestArchive()
                      {
                      
                      
                      WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test2.war");
                      webArchive.addClasses(MyServlet.class);
                      webArchive.addAsWebInfResource("in-container-web.xml", "web.xml");
                      
                      
                      return webArchive;
                      }
                      
                      
                      

                       

                      The tests are exactly the same (the same test as the one from the arquillian-tomcat-embedded-6 project):

                       

                      @Test
                      public void testMethod1() throws Exception
                      {
                      containerController.start("tomcat");
                      deployer.deploy("first");
                      
                      
                      // Define the input and expected outcome
                      final String expected = "hello";
                      
                      
                      URL url = new URL(HELLO_WORLD_URL);
                      InputStream in = url.openConnection().getInputStream();
                      
                      
                      
                      byte[] buffer = new byte[10000];
                      int len = in.read(buffer);
                      String httpResponse = "";
                      for (int q = 0; q < len; q++)
                      {
                      httpResponse += (char) buffer[q];
                      }
                      
                      
                      // Test
                      Assert.assertEquals("Expected output was not equal by value", expected, httpResponse);
                      log.info("Got expected result from Http Servlet: " + httpResponse);
                      
                      
                      
                      deployer.undeploy("first");
                      containerController.stop("tomcat");
                      Thread.sleep(5000);
                      }
                      
                      
                      

                       

                      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                       

                       

                      Regards,

                      George

                      • 9. Re: @ArquillianResource Deployer gets injected only for the first test
                        Aslak Knutsen Master

                        George, you mind giving this Arq-Core branch a test, see if that helps..

                         

                        https://github.com/arquillian/arquillian-core/tree/ARQ-699

                         

                        Not sure how close your Mobicens Tomcat container impl resembles the Arquillian Tomcat ones, but keep in mind these:

                        https://issues.jboss.org/browse/ARQ-701

                        https://issues.jboss.org/browse/ARQ-702

                        • 10. Re: @ArquillianResource Deployer gets injected only for the first test
                          George Vagenas Newbie

                          Aslak, with ARQ-699 branch the test is working perfect either in "in-container" or "as-client" mode. I test both the tomcat-embedded-6 and the mobicents-tomcat-emebedded-6 containers with this branch and everything are ok.

                           

                          I didn't run into similar issues such as ARQ-701 & ARQ-702.

                           

                          I had an issue though in between start/stop/start related with JMX mbeanserver to register MBean on the initialize of the embedded tomcat (javax.management.InstanceAlreadyExistsException: mss-tomcat-embedded-6:type=SipApplicationDispatcher) after the first test that is, start/stop/START->EXCEPTION.

                          But since i am not working on tomcat embedded directly (instead i have MobicentsSipServletsEmbedded ) i stop and remove the StandardService in the stop() method  like this:

                           

                          if(service != null) {
                          
                          
                          
                          
                          
                          
                          try {
                          service.stop();
                          } catch (LifecycleException e) {
                          log.error("service already stopped ", e);
                          }
                          
                          
                          
                          
                          ServerFactory.getServer().removeService(service);
                          }
                          
                          
                          

                           

                          As far as the ARQ-699, i guess it will be included in the CR7 or the master branch, right?

                           

                          Thanks a lot for the help and the tips!

                          George