aslak

Arquillian - TestNG - Phases

Sep 14th, 2010
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 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.testng;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22.  
  23. import org.testng.IConfigurable;
  24. import org.testng.IConfigureCallBack;
  25. import org.testng.IPhaseListener;
  26. import org.testng.ITestNGMethod;
  27. import org.testng.ITestResult;
  28. import org.testng.TestNG;
  29. import org.testng.annotations.AfterClass;
  30. import org.testng.annotations.AfterMethod;
  31. import org.testng.annotations.AfterSuite;
  32. import org.testng.annotations.BeforeClass;
  33. import org.testng.annotations.BeforeMethod;
  34. import org.testng.annotations.BeforeSuite;
  35. import org.testng.annotations.Listeners;
  36. import org.testng.annotations.Test;
  37. import org.testng.internal.AnnotationTypeEnum;
  38. import org.testng.phase.PhaseClassEvent;
  39. import org.testng.phase.PhaseGroupEvent;
  40. import org.testng.phase.PhaseMethodEvent;
  41. import org.testng.phase.PhaseSuiteEvent;
  42. import org.testng.phase.PhaseTestEvent;
  43. import org.testng.xml.XmlClass;
  44. import org.testng.xml.XmlSuite;
  45. import org.testng.xml.XmlTest;
  46.  
  47. /**
  48.  * ArquillianTestCase
  49.  *
  50.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  51.  * @version $Revision: $
  52.  */
  53. public class ArquillianTestCase
  54. {
  55.    @org.junit.Test
  56.    public void shouldVetoConfigurationMethods() throws Exception
  57.    {
  58.       XmlSuite suite = new XmlSuite();
  59.       suite.setName("Arquillian");
  60.       suite.setAnnotations(AnnotationTypeEnum.JDK.getName());
  61.  
  62.       XmlTest test = new XmlTest(suite);
  63.       test.setName("Arquillian");
  64.       List<XmlClass> testClasses = new ArrayList<XmlClass>();
  65.       testClasses.add(new XmlClass(TestClass1.class));
  66.       testClasses.add(new XmlClass(TestClass2.class));
  67.      
  68.       test.setXmlClasses(testClasses);
  69.  
  70.      
  71.       TestNG runner = new TestNG(true);
  72.       runner.setVerbose(0);
  73.       runner.setJUnit(false);
  74.       runner.setXmlSuites(
  75.             Arrays.asList(suite));
  76.       //runner.addListener(new DebugListener());
  77.       runner.run();
  78.    }
  79.  
  80.    public class TestClass
  81.    {
  82.       @BeforeSuite
  83.       public void beforeSuite() {
  84.          System.out.println(printTab(0) + this.getClass().getSimpleName() + " beforeSuite");
  85.       }
  86.      
  87.       @AfterSuite
  88.       public void afterSuite() {
  89.          System.out.println(printTab(0) + this.getClass().getSimpleName() + " afterSuite");
  90.       }
  91.  
  92.       @BeforeClass
  93.       public void beforeClass() {
  94.          System.out.println(printTab(1) + this.getClass().getSimpleName() + " beforeClass");
  95.       }
  96.      
  97.       @AfterClass
  98.       public void afterClass() {
  99.          System.out.println(printTab(1) + this.getClass().getSimpleName() + " afterClass");
  100.       }
  101.  
  102.       @BeforeMethod
  103.       public void beforeMethod() {
  104.          System.out.println(printTab(2) + this.getClass().getSimpleName() + " beforeMethod");
  105.       }
  106.      
  107.       @AfterMethod
  108.       public void afterMethod() {
  109.          System.out.println(printTab(2) + this.getClass().getSimpleName() + " afterMethod");
  110.       }
  111.  
  112.       @Test
  113.       public void test1() {
  114.          System.out.println(printTab(3) + this.getClass().getSimpleName() + " test1");
  115.       }
  116.  
  117.       @Test
  118.       public void test2() {
  119.          System.out.println(printTab(3) + this.getClass().getSimpleName() + " test2");
  120.       }
  121.    }
  122.    
  123.    @Listeners(DebugListener.class)
  124.    public class TestClass1 extends TestClass
  125.    {
  126.      
  127.    }
  128.  
  129.    @Listeners(DebugListener.class)
  130.    public class TestClass2 extends TestClass
  131.    {
  132.      
  133.    }
  134.  
  135.    
  136.    public static class DebugListener implements IPhaseListener, IConfigurable
  137.    {
  138.       public void run(IConfigureCallBack callBack, ITestResult testResult)
  139.       {
  140.          System.out.println(printTab(testResult) + "IConfigurable: " + testResult.getMethod().getMethod().getName());
  141.          callBack.runConfigurationMethod(testResult);
  142.       }
  143.      
  144.       public void onSuiteEvent(PhaseSuiteEvent event)
  145.       {
  146.          System.out.println(printTab(0) + "onSuiteEvent: " + event.isBefore());        
  147.       }
  148.  
  149.       public void onTestEvent(PhaseTestEvent event)
  150.       {
  151.          //System.out.println("onTestEvent: " + event.isBefore() + " " + event.getTestClass().getRealClass().getSimpleName());
  152.       }
  153.  
  154.       public void onGroupEvent(PhaseGroupEvent event)
  155.       {
  156.          //System.out.println("onGroupEvent: " + event.isBefore() + " " + event.getTestClass().getRealClass().getSimpleName());        
  157.       }
  158.  
  159.       public void onClassEvent(PhaseClassEvent event)
  160.       {
  161.          System.out.println(printTab(1) + "onClassEvent: " + event.isBefore() + " " + event.getTestClass().getRealClass().getSimpleName());        
  162.       }
  163.  
  164.       public void onMethodEvent(PhaseMethodEvent event)
  165.       {
  166.          System.out.println(printTab(2) + "onMethodEvent: " + event.isBefore() + " " + event.getMethod().getMethod().getName());
  167.       }
  168.    }
  169.  
  170.    private static String printTab(int count)
  171.    {
  172.       StringBuilder sb = new StringBuilder();
  173.       for(int i = 0; i < count; i++)
  174.       {
  175.          sb.append('\t');
  176.       }
  177.       return sb.toString();
  178.    }
  179.    
  180.    private static String printTab(ITestResult result)
  181.    {
  182.       ITestNGMethod method = result.getMethod();
  183.       if(method.isBeforeSuiteConfiguration() || method.isAfterSuiteConfiguration())
  184.          return printTab(0);
  185.       if(method.isBeforeClassConfiguration() || method.isAfterClassConfiguration())
  186.          return printTab(1);
  187.       if(method.isBeforeMethodConfiguration() || method.isAfterMethodConfiguration())
  188.          return printTab(2);
  189.      
  190.       return printTab(0);
  191.    }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment