Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
   * as indicated by the @authors tag. All rights reserved.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.arquillian.testng;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doAnswer;
 
 import java.util.List;
 import java.util.Map;
 
JUnitTestBaseClass

Author(s):
Aslak Knutsen
Version:
$Revision: $
 
 public class TestNGTestBaseClass
 {
    public static enum Cycle { BEFORE_SUITE, BEFORE_CLASS, BEFORE, TEST,  AFTER, AFTER_CLASS, AFTER_SUITE }
 
    /*
     * Setup / Clear the static callback info.
     */
    private static Map<CycleIntegercallbackCount = new HashMap<CycleInteger>();
    private static Map<CycleThrowablecallbackException = new HashMap<CycleThrowable>();
    static
    {
       for(Cycle tmp : Cycle.values())
       {
          .put(tmp, 0);
       }
    }
 
    public static void throwException(Cycle cycleThrowable exception)
    {
       .put(cycleexception);
    }
 
    public static void wasCalled(Cycle cyclethrows Throwable
    {
       ..println("called: " + cycle);
       if(.containsKey(cycle))
       {
          .put(cycle.get(cycle) + 1);
       }
       else
       {
          throw new RuntimeException("Unknown callback: " + cycle);
       }
       if(.containsKey(cycle))
       {
          throw .remove(cycle);
       }
    }
 
    @After
    public void clearCallbacks()
    {
       .clear();
       for(Cycle tmp : Cycle.values())
       {
          .put(tmp, 0);
       }
   }
   /*
    * Mockito Answers for invoking the LifeCycle callbacks.
    */
   public static class ExecuteLifecycle implements Answer<Object>
   {
      private Cycle cycle;
      public ExecuteLifecycle(Cycle cycle)
      {
         this. = cycle;
      }
      @Override
      public Object answer(org.mockito.invocation.InvocationOnMock invocationthrows Throwable
      {
         wasCalled();
         for(Object argument : invocation.getArguments())
         {
            if(argument instanceof LifecycleMethodExecutor)
            {
               ((LifecycleMethodExecutor)argument).invoke();
            }
            else if(argument instanceof TestMethodExecutor)
            {
               ((TestMethodExecutor)argument).invoke();
            }
         }
         return null;
      }
   }
   public static class TestExecuteLifecycle extends ExecuteLifecycle
   {
      private TestResult result;
      public TestExecuteLifecycle(TestResult result)
      {
         super(.);
         this. = result;
      }
      @Override
      public Object answer(InvocationOnMock invocationthrows Throwable
      {
         super.answer(invocation);
         return ;
      }
   }
   /*
    * Internal Helpers
    */
   protected void executeAllLifeCycles(TestRunnerAdaptor adaptorthrows Exception
   {
      doAnswer(new ExecuteLifecycle(.)).when(adaptor).before(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class));
      doAnswer(new ExecuteLifecycle(.)).when(adaptor).after(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class));
   }
   public void assertCycle(int countCycle... cycles)
   {
      for(Cycle cycle : cycles)
      {
         Assert.assertEquals("Verify " + cycle +  " called N times",
               count, (int).get(cycle));
      }
   }
   protected TestListenerAdapter run(TestRunnerAdaptor adaptorClass<?>... classes)
      throws Exception
   {
      return run(nulladaptorclasses);
   }
   protected TestListenerAdapter run(String[] groupsTestRunnerAdaptor adaptorClass<?>... classes)
      throws Exception
   {
      try
      {
         setAdaptor(adaptor);
         TestListenerAdapter listener = new TestListenerAdapter();
         TestNG runner = new TestNG(false);
         runner.addListener(listener);
         runner.setXmlSuites(Arrays.asList(createSuite(groupsclasses)));
         runner.run();
         return listener;
      }
      finally
      {
         setAdaptor(null);
      }
   }
   protected boolean wasSuccessful(TestListenerAdapter adapter)
   {
      return adapter.getFailedTests().size() == 0 && adapter.getSkippedTests().size() == 0;
   }
   private XmlSuite createSuite(String[] groupsClass<?>... classes)
   {
      XmlSuite suite = new XmlSuite();
      suite.setName("Arquillian - TEST");
      suite.setConfigFailurePolicy("continue");
      XmlTest test = new XmlTest(suite);
      if(groups != null)
      {
         test.setIncludedGroups(Arrays.asList(groups));
      }
      test.setName("Arquillian - TEST");
      List<XmlClasstestClasses = new ArrayList<XmlClass>();
      for(Class<?> clazz : classes)
      {
         XmlClass testClass = new XmlClass(clazz);
         testClasses.add(testClass);
      }
      test.setXmlClasses(testClasses);
      return suite;
   }
   // force set the TestRunnerAdaptor to use
   private void setAdaptor(TestRunnerAdaptor adaptorthrows Exception
   {
      Method method = TestRunnerAdaptorBuilder.class.getMethod("set"TestRunnerAdaptor.class);
      method.setAccessible(true);
      method.invoke(nulladaptor);
   }
New to GrepCode? Check out our FAQ X