View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.junit.runner.Runner;
23  import org.junit.runner.notification.RunNotifier;
24  import org.junit.runners.BlockJUnit4ClassRunner;
25  import org.junit.runners.Suite;
26  import org.junit.runners.model.FrameworkMethod;
27  import org.junit.runners.model.InitializationError;
28  import org.junit.runners.model.Statement;
29  import org.junit.runners.model.TestClass;
30  
31  import java.lang.annotation.ElementType;
32  import java.lang.annotation.Retention;
33  import java.lang.annotation.RetentionPolicy;
34  import java.lang.annotation.Target;
35  import java.lang.reflect.Modifier;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * XXX remove when JUnit implemented this feature
41   * This class will be obsolete if this feature request for JUnit is resolved:
42   * http://github.com/KentBeck/junit/issues/#issue/44
43   */
44  public class Parameterized extends Suite {
45  
46    @Retention(RetentionPolicy.RUNTIME)
47    @Target(ElementType.METHOD)
48    public static @interface Parameters {
49    }
50  
51    public Parameterized(final Class<?> clazz) throws Exception {
52      super(clazz, new ArrayList<Runner>());
53  
54      final FrameworkMethod method = findMethod(getTestClass());
55      List<Object[]> parametersList = null;
56      try {
57        parametersList = (List<Object[]>) method.invokeExplosively(null);
58      } catch (final Throwable throwable) {
59        throw new Exception(throwable);
60      }
61      for (final Object[] aParametersList : parametersList) {
62        getChildren().add(new ClassRunnerForParameters(getTestClass().getJavaClass(), aParametersList));
63      }
64    }
65  
66    private FrameworkMethod findMethod(final TestClass clazz) throws Exception {
67      final List<FrameworkMethod> methods = clazz.getAnnotatedMethods(Parameters.class);
68      for (final FrameworkMethod method : methods) {
69        final int modifiers = method.getMethod().getModifiers();
70        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
71          return method;
72        }
73      }
74      throw new Exception(
75          "Need a public static method with @Parameters annotation in class " + getName());
76    }
77  
78    public static class ClassRunnerForParameters extends BlockJUnit4ClassRunner {
79  
80      private Object[] parameters;
81  
82      ClassRunnerForParameters(final Class<?> clazz, final Object[] parameters) throws InitializationError {
83        super(clazz);
84        this.parameters = parameters;
85      }
86  
87      @Override
88      protected Object createTest() throws Exception {
89        return getTestClass().getOnlyConstructor().newInstance(parameters);
90      }
91  
92      @Override
93      protected String getName() {
94        return "[" + parameters[0].toString() + "]";
95      }
96  
97      @Override
98      protected String testName(final FrameworkMethod method) {
99        return method.getName() + getName();
100     }
101 
102     @Override
103     protected Statement classBlock(final RunNotifier notifier) {
104       return childrenInvoker(notifier);
105     }
106 
107     @Override
108     protected void validateZeroArgConstructor(final List<Throwable> errors) {
109       // In this case there should be a constructor with arguments.
110     }
111   }
112 
113 }