1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.github;
31
32 import com.google.common.collect.Iterables;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.util.Arrays;
36 import org.junit.rules.TestRule;
37 import org.junit.runner.Description;
38 import org.junit.runners.model.Statement;
39 import org.reflections.Reflections;
40 import org.reflections.scanners.ResourcesScanner;
41 import org.reflections.scanners.SubTypesScanner;
42 import org.reflections.util.ClasspathHelper;
43 import org.reflections.util.ConfigurationBuilder;
44 import org.reflections.util.FilterBuilder;
45
46
47
48
49
50
51
52 public final class ClasspathRule implements TestRule {
53
54
55
56
57
58 public Iterable<Class<?>> allTypes() {
59 return Iterables.filter(
60 new Reflections(
61 new ConfigurationBuilder()
62 .setScanners(
63 new SubTypesScanner(false),
64 new ResourcesScanner()
65 )
66 .setUrls(
67 ClasspathHelper.forClassLoader(
68 ClasspathHelper.contextClassLoader(),
69 ClasspathHelper.staticClassLoader()
70 )
71 ).filterInputsBy(
72 new FilterBuilder().include(
73 FilterBuilder.prefix("com.jcabi.github")
74 )
75 )
76 ).getSubTypesOf(Object.class),
77 input -> {
78 final String name = input.getName();
79
80 return !name.endsWith("Test")
81 && !name.endsWith("ITCase")
82 && !name.endsWith("ClasspathRule")
83 && !name.endsWith("RepoRule")
84 && (input.getEnclosingClass() == null
85 || name.endsWith("Smart"));
86 }
87 );
88 }
89
90 @Override
91 public Statement apply(final Statement statement,
92 final Description description) {
93 return new Statement() {
94 @Override
95
96 public void evaluate() throws Throwable {
97 statement.evaluate();
98 }
99 };
100 }
101
102
103
104
105
106 public Iterable<Method> allPublicMethods() {
107 return Iterables.concat(
108 Iterables.transform(
109 this.allTypes(),
110 input -> Iterables.filter(
111 Arrays.asList(input.getDeclaredMethods()),
112 method -> Modifier.isPublic(
113 method.getModifiers()
114 )
115 )
116 )
117 );
118 }
119 }