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.util.ArrayList;
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
26 import de.smartics.testdoc.collect.extractor.TestDocAnnotationExtractor.UutInfo;
27 import de.smartics.testdoc.core.doc.ScenarioTestDoc;
28 import de.smartics.testdoc.core.doc.UnitTestDoc;
29
30
31
32
33
34
35
36 public final class TestDocMethodExtractor extends AbstractExtractor
37 {
38
39
40
41
42
43
44
45
46
47
48 private final ExecutableElement declaredTestCaseMethod;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public TestDocMethodExtractor(final ExtractorConfig config,
66 final ExecutableElement declaredTestCaseMethod)
67 throws IllegalArgumentException
68 {
69 super(config, initTypeElement(declaredTestCaseMethod));
70 this.declaredTestCaseMethod = declaredTestCaseMethod;
71 }
72
73
74
75
76
77
78
79 private static void checkArguments(final ExecutableElement declaredUutMethod)
80 {
81 if (null == declaredUutMethod)
82 {
83 throw new IllegalArgumentException(
84 "The method, declared in the test case, that points to the UUT is required.");
85 }
86 }
87
88 private static TypeElement initTypeElement(
89 final ExecutableElement declaredUutMethod)
90 throws IllegalArgumentException
91 {
92 checkArguments(declaredUutMethod);
93
94 Element current = declaredUutMethod;
95 while (current != null)
96 {
97 final ElementKind kind = current.getKind();
98 if (ElementKind.CLASS.equals(kind))
99 {
100 return (TypeElement) current;
101 }
102 current = current.getEnclosingElement();
103 }
104
105 throw new IllegalArgumentException("Declaration of UUT '"
106 + declaredUutMethod.asType()
107 + "' is not within a test case class.");
108 }
109
110
111
112
113
114
115
116
117
118
119
120 public UnitTestDoc readTestDoc()
121 {
122 final UutInfo info = uutExtractor.read(declaredTestCaseMethod);
123 final List<ScenarioTestDoc> scenarios = calcScenario(info);
124 final String type = info.getType();
125 if (type != null)
126 {
127 final UnitTestDoc testDoc = new UnitTestDoc(type, scenarios);
128 return testDoc;
129 }
130
131 return null;
132 }
133
134 private List<ScenarioTestDoc> calcScenario(final UutInfo uutInfo)
135 {
136 final List<ScenarioTestDoc> scenarios = new ArrayList<ScenarioTestDoc>();
137
138 final String testCaseTypeName = calcTestCaseType();
139 final ScenarioTestDoc scenario =
140 createScenarioIfElementIsTestMethod(uutInfo, testCaseTypeName,
141 declaredTestCaseMethod);
142
143 if (scenario != null)
144 {
145 scenarios.add(scenario);
146 }
147
148 return scenarios;
149 }
150
151 private String calcTestCaseType()
152 {
153 return testCaseType.asType().toString();
154 }
155
156
157
158 }