| 1 |
/* |
| 2 |
* Copyright (c) 2021 Goldman Sachs. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* and Eclipse Distribution License v. 1.0 which accompany this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
*/ |
| 10 |
|
| 11 |
package org.eclipse.collections.impl.test.junit; |
| 12 |
|
| 13 |
import java.lang.annotation.Annotation; |
| 14 |
import java.lang.reflect.Method; |
| 15 |
import java.lang.reflect.Modifier; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.HashSet; |
| 18 |
import java.util.LinkedList; |
| 19 |
import java.util.List; |
| 20 |
import java.util.Map; |
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
import org.junit.internal.MethodSorter; |
| 24 |
import org.junit.runners.model.FrameworkField; |
| 25 |
import org.junit.runners.model.FrameworkMethod; |
| 26 |
import org.junit.runners.model.TestClass; |
| 27 |
|
| 28 |
public class Java8TestClass extends TestClass |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Creates a {@code TestClass} wrapping {@code klass}. Each time this |
| 32 |
* constructor executes, the class is scanned for annotations, which can be |
| 33 |
* an expensive process (we hope in future JDK's it will not be.) Therefore, |
| 34 |
* try to share instances of {@code TestClass} where possible. |
| 35 |
*/ |
| 36 |
public Java8TestClass(Class<?> klass) |
| 37 |
{ |
| 38 |
super(klass); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) |
| 43 |
{ |
| 44 |
super.scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations); |
| 45 |
|
| 46 |
this.getInterfaceMethodsForAnnotations(methodsForAnnotations, this.getJavaClass()); |
| 47 |
} |
| 48 |
|
| 49 |
private void getInterfaceMethodsForAnnotations(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Class<?> clazz) |
| 50 |
{ |
| 51 |
List<Class<?>> interfaces = Java8TestClass.getInterfaces(this.getJavaClass()); |
| 52 |
for (Class<?> eachInterface : interfaces) |
| 53 |
{ |
| 54 |
for (Method eachMethod : MethodSorter.getDeclaredMethods(eachInterface)) |
| 55 |
{ |
| 56 |
if (!Modifier.isAbstract(eachMethod.getModifiers())) |
| 57 |
{ |
| 58 |
TestClass.addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private static List<Class<?>> getInterfaces(Class<?> testClass) |
| 65 |
{ |
| 66 |
LinkedList<Class<?>> queue = new LinkedList<>(); |
| 67 |
queue.add(testClass); |
| 68 |
|
| 69 |
Set<Class<?>> visited = new HashSet<>(); |
| 70 |
visited.add(testClass); |
| 71 |
|
| 72 |
List<Class<?>> results = new ArrayList<>(); |
| 73 |
while (!queue.isEmpty()) |
| 74 |
{ |
| 75 |
Class<?> anInterface = queue.poll(); |
| 76 |
results.add(anInterface); |
| 77 |
|
| 78 |
Class<?>[] parentInterfaces = anInterface.getInterfaces(); |
| 79 |
for (Class<?> parentInterface : parentInterfaces) |
| 80 |
{ |
| 81 |
if (!visited.contains(parentInterface)) |
| 82 |
{ |
| 83 |
visited.add(parentInterface); |
| 84 |
queue.add(parentInterface); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return results; |
| 89 |
} |
| 90 |
} |