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