View Javadoc

1   /*
2    * $Id: MuleParameterized.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tck;
12  
13  import java.io.IOException;
14  import java.net.URI;
15  import java.net.URISyntaxException;
16  import java.net.URL;
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.junit.runner.Description;
23  import org.junit.runner.manipulation.Filter;
24  import org.junit.runner.manipulation.NoTestsRemainException;
25  import org.junit.runners.Parameterized;
26  import org.mule.util.FileUtils;
27  import org.mule.util.StringUtils;
28  
29  /**
30   * <code>MuleParameterized</code> adds test exclusions to the
31   * <code>Parameterized</code> class. This is used for running JUnit4 tests with
32   * parameters. The test exclusion logic in AbstractMuleTestCase does not work in
33   * JUnit4 most likely due to the fact the JUnit4 does not extend the TestCase class
34   * when using annotations, which are necessary for parameterized testing.
35   */
36  public class MuleParameterized extends Parameterized
37  {
38      /**
39       * The list of tests to exclude
40       */
41      private static ArrayList<String> excludedTests = new ArrayList<String>();
42  
43      protected static transient Log logger = LogFactory.getLog(org.mule.tck.MuleParameterized.class);    
44      
45      public MuleParameterized(Class<?> klass) throws Throwable
46      {
47          super(klass);
48          getExcluded();
49          try
50          {
51              filter(excludeFilter);
52          }
53          // we need to ignore this error since we do filtering against the test method
54          // names and not the test class, since the 'name' for the test class is
55          // always an index when running parameterized tests, i.e. [0]
56          catch (NoTestsRemainException e)
57          {
58              // ignore
59          }
60      }
61  
62      /**
63       * Read the test exclusions file and find the tests to be excluded from running.
64       */
65      public void getExcluded()
66      {      
67          try
68          {
69              URL fileUrl = this.getClass().getClassLoader().getResource("mule-test-exclusions.txt");
70  
71              if (fileUrl != null)
72              {
73                  // in case .txt is in jar
74                  URI fileUri = new URI(StringUtils.removeStart(fileUrl.toString(), "jar:"));
75  
76                  // this iterates over all lines in the exclusion file
77                  @SuppressWarnings("unchecked")
78                  Iterator<String> lines = FileUtils.lineIterator(FileUtils.newFile(fileUri));
79  
80                  ArrayList<String> s = new ArrayList<String>();
81                  String line;
82                  while (lines.hasNext())
83                  {
84                      line = StringUtils.trimToEmpty(lines.next());
85                      if (!(line.startsWith("#")) && !line.equals("") && line.length() > 0)
86                      {
87                          s.add(line);
88                          logger.info("adding test to the list of exclusions : " + line);
89                      }
90                  }
91                  excludedTests = s;
92              }
93              else
94              {
95                  logger.info("did not find test exclusions file");
96              }
97          }
98          catch (IOException ioex)
99          {
100             // ignore
101         }
102         catch (URISyntaxException e)
103         {
104             // ignore
105         }
106     }
107 
108     private static Filter excludeFilter = new Filter()
109     {
110         /**
111          * Checks the test description against the list of excluded tests. TODO: take
112          * this one step further and allow you to exclude specific tests in a test
113          * class. Currently, parameterized tests have a name like this:
114          * testMethod[index](TestClass)
115          */
116         @Override
117         public boolean shouldRun(Description description)
118         {
119             for (String excludedTest : excludedTests)
120             {
121                 // use contains instead of equals since parameterized tests list
122                 // their name as an index, i.e. [0] when filtering against the test
123                 // class name and methodName[index]ClassName when checking against
124                 // individual test methods. The test exclusions file contains the
125                 // full class name to exclude.
126                 if (description.getChildren().get(0).toString().contains(excludedTest))
127                 {
128                     logger.info("skipping test : " + description.getChildren().get(0).toString());
129                     return false;
130                 }
131             }
132             return true;
133         }
134 
135         @Override
136         public String describe()
137         {
138             return "excludes tests from mule-test-exclusions.txt";
139         }
140     };
141 }