1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.collect.extractor;
17
18 import java.lang.annotation.Annotation;
19 import java.util.List;
20
21 import javax.lang.model.element.Element;
22 import javax.lang.model.element.ElementKind;
23 import javax.lang.model.element.ExecutableElement;
24 import javax.lang.model.element.TypeElement;
25 import javax.lang.model.util.Elements;
26
27 import org.junit.Test;
28 import org.junit.experimental.theories.Theory;
29
30 import de.smartics.testdoc.annotations.Uut;
31 import de.smartics.testdoc.collect.extractor.TestDocHintsExtractor.Hints;
32 import de.smartics.testdoc.collect.extractor.TestDocAnnotationExtractor.UutInfo;
33 import de.smartics.testdoc.core.doc.ScenarioTestDoc;
34 import de.smartics.testdoc.core.doc.SortKey;
35 import de.smartics.testdoc.core.doc.TestMethodDoc;
36 import de.smartics.testdoc.core.doc.Type;
37 import de.smartics.testdoc.core.doc.UnitTestDoc;
38 import de.smartics.testdoc.core.doc.names.TestNameUtils;
39 import de.smartics.testdoc.core.source.SourceCodeHelper;
40 import de.smartics.util.source.SourceCodeLocation;
41
42
43
44
45
46
47
48 public abstract class AbstractExtractor
49 {
50
51
52
53
54
55
56
57
58
59 protected final Elements elementUtils;
60
61
62
63
64 protected final SourceCodeHelper sourceCodeHelper;
65
66
67
68
69 private final TestNameUtils testNameUtils;
70
71
72
73
74 protected final TypeElement testCaseType;
75
76
77
78
79 protected final CategoryExtractor categoryExtractor;
80
81
82
83
84 protected final TestDocHintsExtractor hintExtractor =
85 new TestDocHintsExtractor();
86
87
88
89
90 protected final TestDocAnnotationExtractor uutExtractor =
91 new TestDocAnnotationExtractor();
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 protected AbstractExtractor(final ExtractorConfig config,
108 final TypeElement testCaseType) throws IllegalArgumentException
109 {
110 checkArguments(config);
111 this.elementUtils = config.getElementUtils();
112 this.sourceCodeHelper = config.getSourceCodeHelper();
113 this.testNameUtils = config.getTestNameUtils();
114 this.testCaseType = testCaseType;
115 this.categoryExtractor = new CategoryExtractor(testCaseType);
116 }
117
118
119
120
121
122
123
124 private void checkArguments(final ExtractorConfig config)
125 {
126 if (config == null)
127 {
128 throw new IllegalArgumentException("The extractor config is required.");
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141
142 public abstract UnitTestDoc readTestDoc();
143
144 protected ScenarioTestDoc createScenarioIfElementIsTestMethod(
145 final UutInfo uutInfo, final String testCaseType,
146 final ExecutableElement testMethod)
147 {
148 final String uutMethod = uutInfo.getMethod();
149 final Hints methodHints = hintExtractor.readHints(testMethod);
150 final TestMethodDoc testMethodDoc =
151 createTestMethod(methodHints, testMethod);
152 final List<String> categories =
153 categoryExtractor.calcCategories(testMethod);
154
155 final Hints typeHints =
156 hintExtractor.readHints(testMethod.getEnclosingElement());
157 final SortKey sortKey = methodHints.createSortKey(typeHints);
158 final ScenarioTestDoc scenario =
159 new ScenarioTestDoc(uutMethod, new Type(testCaseType), testMethodDoc,
160 categories, sortKey);
161 return scenario;
162 }
163
164 private TestMethodDoc createTestMethod(final Hints methodHints,
165 final ExecutableElement testMethod)
166 {
167 final String testName = testMethod.getSimpleName().toString();
168 final String testSentence = calculateTestSentence(methodHints, testName);
169 final SourceCodeLocation location =
170 sourceCodeHelper.getSourceCodeLocation(testMethod);
171 final String comment = elementUtils.getDocComment(testMethod);
172 return new TestMethodDoc(testName, testSentence, location, comment);
173 }
174
175 protected String calculateTestSentence(final Hints hints,
176 final String testName)
177 {
178 if (hints.hasSentence())
179 {
180 return hints.getSentence();
181 }
182 else
183 {
184 return testNameUtils.calculateTestNameSentence(testName);
185 }
186 }
187
188 protected static boolean isTestMethodWithoutUutAnnotation(
189 final Element element)
190 {
191 final ElementKind kind = element.getKind();
192 if (ElementKind.METHOD.equals(kind))
193 {
194 final ExecutableElement method = (ExecutableElement) element;
195 return (hasNotAnnotation(method, Uut.class) && (hasAnnotation(method,
196 Test.class) || hasAnnotation(method, Theory.class)));
197 }
198 return false;
199 }
200
201 protected static boolean hasAnnotation(final Element method,
202 final Class<? extends Annotation> annotation)
203 {
204 return null != method.getAnnotation(annotation);
205 }
206
207 protected static boolean hasNotAnnotation(final Element method,
208 final Class<? extends Annotation> annotation)
209 {
210 return null == method.getAnnotation(annotation);
211 }
212
213
214
215 }