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  package testng.utils;
20  
21  import java.lang.reflect.Method;
22  
23  import junit.framework.TestCase;
24  import org.apache.maven.surefire.api.testset.TestListResolver;
25  import org.apache.maven.surefire.testng.utils.MethodSelector;
26  import org.testng.IClass;
27  import org.testng.IRetryAnalyzer;
28  import org.testng.ITestClass;
29  import org.testng.ITestNGMethod;
30  import org.testng.internal.DefaultMethodSelectorContext;
31  
32  /**
33   *
34   */
35  public class MethodSelectorTest extends TestCase {
36      public void testInclusionOfMethodFromBaseClass() {
37          MethodSelector selector = new MethodSelector();
38          DefaultMethodSelectorContext context = new DefaultMethodSelectorContext();
39          ITestNGMethod testngMethod = new FakeTestNGMethod(ChildClassSample.class, "baseClassMethodToBeIncluded");
40          TestListResolver resolver =
41                  new TestListResolver(BaseClassSample.class.getName() + "#baseClassMethodToBeIncluded");
42          MethodSelector.setTestListResolver(resolver);
43          boolean include = selector.includeMethod(context, testngMethod, true);
44          assertTrue(include);
45      }
46  
47      public void testNoInclusionOfMethodFromBaseClass() {
48          MethodSelector selector = new MethodSelector();
49          DefaultMethodSelectorContext context = new DefaultMethodSelectorContext();
50          ITestNGMethod testngMethod = new FakeTestNGMethod(ChildClassSample.class, "baseClassMethodToBeIncluded");
51          TestListResolver resolver = new TestListResolver(BaseClassSample.class.getName() + "#nonExistedMethod");
52          MethodSelector.setTestListResolver(resolver);
53          boolean include = selector.includeMethod(context, testngMethod, true);
54          assertFalse(include);
55      }
56  
57      public void testInclusionOfMethodFromSubClass() {
58          MethodSelector selector = new MethodSelector();
59          DefaultMethodSelectorContext context = new DefaultMethodSelectorContext();
60          ITestNGMethod testngMethod = new FakeTestNGMethod(ChildClassSample.class, "subClassMethod");
61          TestListResolver resolver = new TestListResolver(ChildClassSample.class.getName() + "#sub*");
62          MethodSelector.setTestListResolver(resolver);
63          boolean include = selector.includeMethod(context, testngMethod, true);
64          assertTrue(include);
65      }
66  
67      private static class FakeTestNGMethod implements ITestNGMethod {
68          private final Class<?> clazz;
69          private final String methodName;
70  
71          FakeTestNGMethod(Class<?> clazz, String methodName) {
72              this.clazz = clazz;
73              this.methodName = methodName;
74          }
75  
76          @Override
77          public Class getRealClass() {
78              return clazz;
79          }
80  
81          @Override
82          public ITestClass getTestClass() {
83              return null;
84          }
85  
86          @Override
87          public void setTestClass(ITestClass iTestClass) {}
88  
89          @Override
90          public Method getMethod() {
91              return null;
92          }
93  
94          @Override
95          public String getMethodName() {
96              return methodName;
97          }
98  
99          @Override
100         public Object[] getInstances() {
101             return new Object[0];
102         }
103 
104         @Override
105         public long[] getInstanceHashCodes() {
106             return new long[0];
107         }
108 
109         @Override
110         public String[] getGroups() {
111             return new String[0];
112         }
113 
114         @Override
115         public String[] getGroupsDependedUpon() {
116             return new String[0];
117         }
118 
119         @Override
120         public String getMissingGroup() {
121             return null;
122         }
123 
124         @Override
125         public void setMissingGroup(String s) {}
126 
127         @Override
128         public String[] getBeforeGroups() {
129             return new String[0];
130         }
131 
132         @Override
133         public String[] getAfterGroups() {
134             return new String[0];
135         }
136 
137         @Override
138         public String[] getMethodsDependedUpon() {
139             return new String[0];
140         }
141 
142         @Override
143         public void addMethodDependedUpon(String s) {}
144 
145         @Override
146         public boolean isTest() {
147             return false;
148         }
149 
150         @Override
151         public boolean isBeforeMethodConfiguration() {
152             return false;
153         }
154 
155         @Override
156         public boolean isAfterMethodConfiguration() {
157             return false;
158         }
159 
160         @Override
161         public boolean isBeforeClassConfiguration() {
162             return false;
163         }
164 
165         @Override
166         public boolean isAfterClassConfiguration() {
167             return false;
168         }
169 
170         @Override
171         public boolean isBeforeSuiteConfiguration() {
172             return false;
173         }
174 
175         @Override
176         public boolean isAfterSuiteConfiguration() {
177             return false;
178         }
179 
180         @Override
181         public boolean isBeforeTestConfiguration() {
182             return false;
183         }
184 
185         @Override
186         public boolean isAfterTestConfiguration() {
187             return false;
188         }
189 
190         @Override
191         public boolean isBeforeGroupsConfiguration() {
192             return false;
193         }
194 
195         @Override
196         public boolean isAfterGroupsConfiguration() {
197             return false;
198         }
199 
200         @Override
201         public long getTimeOut() {
202             return 0;
203         }
204 
205         @Override
206         public int getInvocationCount() {
207             return 0;
208         }
209 
210         @Override
211         public void setInvocationCount(int i) {}
212 
213         @Override
214         public int getSuccessPercentage() {
215             return 0;
216         }
217 
218         @Override
219         public String getId() {
220             return null;
221         }
222 
223         @Override
224         public void setId(String s) {}
225 
226         @Override
227         public long getDate() {
228             return 0;
229         }
230 
231         @Override
232         public void setDate(long l) {}
233 
234         @Override
235         public boolean canRunFromClass(IClass iClass) {
236             return false;
237         }
238 
239         @Override
240         public boolean isAlwaysRun() {
241             return false;
242         }
243 
244         @Override
245         public int getThreadPoolSize() {
246             return 0;
247         }
248 
249         @Override
250         public void setThreadPoolSize(int i) {}
251 
252         @Override
253         public String getDescription() {
254             return null;
255         }
256 
257         @Override
258         public void incrementCurrentInvocationCount() {}
259 
260         @Override
261         public int getCurrentInvocationCount() {
262             return 0;
263         }
264 
265         @Override
266         public void setParameterInvocationCount(int i) {}
267 
268         @Override
269         public int getParameterInvocationCount() {
270             return 0;
271         }
272 
273         @Override
274         public ITestNGMethod clone() {
275             try {
276                 return (ITestNGMethod) super.clone();
277             } catch (CloneNotSupportedException e) {
278                 throw new RuntimeException(e);
279             }
280         }
281 
282         @Override
283         public IRetryAnalyzer getRetryAnalyzer() {
284             return null;
285         }
286 
287         @Override
288         public void setRetryAnalyzer(IRetryAnalyzer iRetryAnalyzer) {}
289 
290         @Override
291         public boolean skipFailedInvocations() {
292             return false;
293         }
294 
295         @Override
296         public void setSkipFailedInvocations(boolean b) {}
297 
298         @Override
299         public long getInvocationTimeOut() {
300             return 0;
301         }
302 
303         @Override
304         public boolean ignoreMissingDependencies() {
305             return false;
306         }
307 
308         @Override
309         public void setIgnoreMissingDependencies(boolean b) {}
310 
311         @Override
312         public int compareTo(Object o) {
313             return 0;
314         }
315     }
316 }