View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.junit;
16  
17  import java.lang.reflect.Field;
18  import java.lang.reflect.Method;
19  import java.util.List;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.junit.runners.Parameterized.Parameter;
23  import org.junit.runners.model.FrameworkField;
24  import org.junit.runners.model.FrameworkMethod;
25  import org.junit.runners.model.TestClass;
26  
27  /**
28   * A {@link FrameworkMethod} with parameters.
29   *
30   * @author Ahmed Ashour
31   */
32  public class FrameworkMethodWithParameters extends FrameworkMethod {
33  
34      private final TestClass testClass_;
35      private final List<Object> parameters_;
36  
37      /**
38       * The constructor.
39       *
40       * @param testClass the test class
41       * @param method the method
42       * @param parameters the parameters
43       */
44      public FrameworkMethodWithParameters(final TestClass testClass, final Method method,
45              final List<Object> parameters) {
46          super(method);
47          testClass_ = testClass;
48          parameters_ = parameters;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public Object invokeExplosively(final Object target, final Object... params)
56          throws Throwable {
57          if (!parameters_.isEmpty()) {
58              final List<FrameworkField> annotatedFieldsByParameter = testClass_.getAnnotatedFields(Parameter.class);
59              if (annotatedFieldsByParameter.size() != parameters_.size()) {
60                  throw new Exception(
61                          "Wrong number of parameters and @Parameter fields."
62                                  + " @Parameter fields counted: "
63                                  + annotatedFieldsByParameter.size()
64                                  + ", available parameters: " + parameters_.size()
65                                  + ".");
66              }
67              for (final FrameworkField each : annotatedFieldsByParameter) {
68                  final Field field = each.getField();
69                  final Parameter annotation = field.getAnnotation(Parameter.class);
70                  final int index = annotation.value();
71                  try {
72                      field.set(target, parameters_.get(index));
73                  }
74                  catch (final IllegalArgumentException ex) {
75                      throw new Exception(testClass_.getName()
76                              + ": Trying to set " + field.getName()
77                              + " with the value " + parameters_.get(index)
78                              + " that is not the right type ("
79                              + parameters_.get(index).getClass().getSimpleName()
80                              + " instead of " + field.getType().getSimpleName()
81                              + ").", ex);
82                  }
83              }
84          }
85          return super.invokeExplosively(target, params);
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public String getName() {
93          if (!parameters_.isEmpty()) {
94              return '_' + StringUtils.join(parameters_, '_');
95          }
96          return super.getName();
97      }
98  }