1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.maven;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.project.MavenProject;
27
28 import de.smartics.maven.io.MojoIoUtils;
29 import de.smartics.maven.util.LoggingUtils;
30 import de.smartics.maven.util.PathUtils;
31 import de.smartics.testdoc.core.doc.OutputManager;
32 import de.smartics.testdoc.core.doc.UnitTestDocIndex;
33 import de.smartics.testdoc.core.export.FileOutputManager;
34 import de.smartics.testdoc.core.export.UnitTestDocIndexExporter;
35 import de.smartics.testdoc.core.export.XmlExporter;
36 import de.smartics.testdoc.report.export.doc.IndexProvider;
37 import de.smartics.testdoc.report.export.doc.TestDocHelper;
38
39
40
41
42
43
44
45
46
47
48 public class TestStoriesXmlExportMojo extends AbstractMojo
49 {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 protected MavenProject project;
65
66
67
68
69
70
71
72
73 private boolean skip;
74
75
76
77
78
79
80
81
82
83
84
85 private String logLevel;
86
87
88
89
90
91
92
93 private String outputDirectory;
94
95
96
97
98
99
100
101 protected File outputDir;
102
103
104
105
106
107
108
109 private String serOutputDirectory;
110
111
112
113
114 private TestDocHelper testDocHelper;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 @Override
134 public void execute() throws MojoExecutionException, MojoFailureException
135 {
136 if (!skip)
137 {
138 if (canBuild())
139 {
140 final IndexProvider indexChecker =
141 new IndexProvider(serOutputDirectory);
142 testDocHelper = indexChecker.createHelper(null);
143 if (testDocHelper.isIndexProvided())
144 {
145 runExecution();
146 }
147 else
148 {
149 getLog().info(
150 "No test doc information found, no report will be generated.");
151 }
152 }
153 else
154 {
155 getLog()
156 .debug(
157 "Skipping testdoc since no test classes are provided by this project.");
158 }
159 }
160 else
161 {
162 getLog().info("Skipping testdoc XML generation since skip=true.");
163 }
164 }
165
166 private void runExecution() throws MojoExecutionException
167 {
168 init();
169
170 try
171 {
172 final XmlExporter exporter = new XmlExporter(true);
173 final OutputManager manager = new FileOutputManager(outputDir, exporter);
174 final UnitTestDocIndexExporter indexExporter =
175 new UnitTestDocIndexExporter(manager);
176 final UnitTestDocIndex index = testDocHelper.getIndex();
177 indexExporter.export(index);
178 }
179 catch (final ParserConfigurationException e)
180 {
181 throw new MojoExecutionException("Cannot generate testdoc XML files.", e);
182 }
183 catch (final IOException e)
184 {
185 throw new MojoExecutionException("Cannot generate testdoc XML files.", e);
186 }
187 }
188
189 @SuppressWarnings("unchecked")
190 private boolean canBuild()
191 {
192 final String packaging = project.getPackaging();
193 return !("pom".equals(packaging) || !PathUtils.exists(project
194 .getTestCompileSourceRoots()));
195 }
196
197 private void init() throws MojoExecutionException
198 {
199 LoggingUtils.configureLogger(getLog(), logLevel);
200 this.outputDir = MojoIoUtils.provideMojoDirectory(outputDirectory);
201 }
202
203
204
205 }