File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\test\java\hudson\maven\reporters\SurefireArchiverUnitTest.java.html

1 package hudson.maven.reporters;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Mockito.doReturn;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.spy;
7 import static org.mockito.Mockito.when;
8 import hudson.FilePath;
9 import hudson.console.ConsoleNote;
10 import hudson.maven.ExecutedMojo;
11 import hudson.maven.MavenBuild;
12 import hudson.maven.MavenBuildInformation;
13 import hudson.maven.MavenBuildProxy;
14 import hudson.maven.MavenProjectActionBuilder;
15 import hudson.maven.MavenReporter;
16 import hudson.maven.MojoInfo;
17 import hudson.maven.MojoInfoBuilder;
18 import hudson.model.BuildListener;
19 import hudson.model.Cause;
20 import hudson.model.Result;
21 import hudson.tasks.junit.TestResult;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.io.PrintWriter;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import java.util.Calendar;
30 import java.util.List;
31
32 import org.apache.commons.io.output.NullOutputStream;
33 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Matchers;
38
39 /**
40 * Unit test for the JUnit result parsing in {@link SurefireArchiver}.
41 *
42 * @author kutzi
43 */
44 public class SurefireArchiverUnitTest {
45
46 private SurefireArchiver archiver;
47 private MavenBuild build;
48 private TestBuildProxy buildProxy;
49 private MojoInfo mojoInfo;
50
51 @Before
52 @SuppressWarnings("unchecked")
53 public void before() throws ComponentConfigurationException, URISyntaxException {
54 this.archiver = new SurefireArchiver();
55 this.build = mock(MavenBuild.class);
56 when(build.getAction(Matchers.any(Class.class))).thenCallRealMethod();
57 when(build.getActions()).thenCallRealMethod();
58 when(build.getRootDir()).thenReturn(new File("target"));
59
60 this.buildProxy = new TestBuildProxy(build);
61
62 MojoInfo spy = createMojoInfo();
63
64 this.mojoInfo = spy;
65 }
66
67 private MojoInfo createMojoInfo() throws ComponentConfigurationException {
68 MojoInfo info = MojoInfoBuilder.mojoBuilder("org.apache.maven.plugins", "maven-surefire-plugin", "test")
69 .version("2.9").build();
70
71 MojoInfo spy = spy(info);
72
73 doReturn(Boolean.FALSE).when(spy).getConfigurationValue(Matchers.anyString(), Matchers.eq(Boolean.class));
74 return spy;
75 }
76
77 @Test
78 public void testNotArchivingEmptyResults() throws InterruptedException, IOException, URISyntaxException, ComponentConfigurationException {
79 URL resource = SurefireArchiverUnitTest.class.getResource("/surefire-archiver-test1");
80 File reportsDir = new File(resource.toURI().getPath());
81 doReturn(reportsDir).when(this.mojoInfo).getConfigurationValue("reportsDirectory", File.class);
82
83 this.archiver.postExecute(buildProxy, null, this.mojoInfo, new NullBuildListener(), null);
84
85 SurefireReport action = this.build.getAction(SurefireReport.class);
86 Assert.assertNull(action);
87 }
88
89 @Test
90 public void testArchiveResults() throws InterruptedException, IOException, URISyntaxException, ComponentConfigurationException {
91 URL resource = SurefireArchiverUnitTest.class.getResource("/surefire-archiver-test2");
92 File reportsDir = new File(resource.toURI().getPath());
93
94 doReturn(reportsDir).when(this.mojoInfo).getConfigurationValue("reportsDirectory", File.class);
95 touchReportFiles(reportsDir);
96
97 this.archiver.postExecute(buildProxy, null, this.mojoInfo, new NullBuildListener(), null);
98
99 SurefireReport action = this.build.getAction(SurefireReport.class);
100 Assert.assertNotNull(action);
101 TestResult result = action.getResult();
102 Assert.assertNotNull(result);
103 Assert.assertEquals(2658, result.getTotalCount());
104
105
106 resource = SurefireArchiverUnitTest.class.getResource("/surefire-archiver-test3");
107 reportsDir = new File(resource.toURI().getPath());
108 doReturn(reportsDir).when(this.mojoInfo).getConfigurationValue("reportsDirectory", File.class);
109 touchReportFiles(reportsDir);
110
111 this.archiver.postExecute(buildProxy, null, this.mojoInfo, new NullBuildListener(), null);
112
113 action = this.build.getAction(SurefireReport.class);
114 Assert.assertNotNull(action);
115 result = action.getResult();
116 Assert.assertNotNull(result);
117 Assert.assertEquals(2670, result.getTotalCount());
118 }
119
120 @Test
121 public void testResultsAreNotCountedTwice() throws InterruptedException, IOException, URISyntaxException, ComponentConfigurationException {
122 URL resource = SurefireArchiverUnitTest.class.getResource("/surefire-archiver-test2");
123 File reportsDir = new File(resource.toURI().getPath());
124 doReturn(reportsDir).when(this.mojoInfo).getConfigurationValue("reportsDirectory", File.class);
125 touchReportFiles(reportsDir);
126
127 this.archiver.postExecute(buildProxy, null, this.mojoInfo, new NullBuildListener(), null);
128 SurefireReport action = this.build.getAction(SurefireReport.class);
129 TestResult result = action.getResult();
130 assertEquals(2658, result.getTotalCount());
131
132 // result count shouldn't increase if mojo is called again
133 this.archiver.postExecute(buildProxy, null, this.mojoInfo, new NullBuildListener(), null);
134 action = this.build.getAction(SurefireReport.class);
135 result = action.getResult();
136 assertEquals(2658, result.getTotalCount());
137 }
138
139 @Test
140 public void testMultiThreaded() throws InterruptedException, IOException, URISyntaxException, ComponentConfigurationException {
141 File reportsDir2 = new File(SurefireArchiverUnitTest.class.getResource("/surefire-archiver-test2").toURI().getPath());
142 doReturn(reportsDir2).when(this.mojoInfo).getConfigurationValue("reportsDirectory", File.class);
143 touchReportFiles(reportsDir2);
144
145 final MojoInfo mojoInfo2 = createMojoInfo();
146 doReturn(reportsDir2).when(mojoInfo2).getConfigurationValue("reportsDirectory", File.class);
147
148 int count = 20;
149 ArchiverThread t1 = new ArchiverThread(this.mojoInfo, count);
150 ArchiverThread t2 = new ArchiverThread(mojoInfo2, count);
151 t1.start();
152 t2.start();
153
154 t1.join();
155 t2.join();
156
157 if (t1.exception != null) {
158 t1.exception.printStackTrace(System.out);
159 Assert.fail(t1.exception.toString());
160 }
161
162 if (t2.exception != null) {
163 t2.exception.printStackTrace(System.out);
164 Assert.fail(t2.exception.toString());
165 }
166
167 SurefireReport action = this.build.getAction(SurefireReport.class);
168 Assert.assertNotNull(action);
169 TestResult result = action.getResult();
170 Assert.assertNotNull(result);
171 Assert.assertEquals(2658, result.getTotalCount());
172 }
173
174 private class ArchiverThread extends Thread {
175
176 private MojoInfo info;
177 private Throwable exception;
178 private int count;
179
180 public ArchiverThread(MojoInfo info, int count) {
181 this.info = info;
182 this.count = count;
183 }
184
185 public void run() {
186 try {
187 for (int i=0; i < count; i++) {
188 archiver.postExecute(buildProxy, null, this.info, new NullBuildListener(), null);
189 }
190 } catch (Throwable e) {
191 this.exception = e;
192 }
193 }
194 }
195
196 private void touchReportFiles(File reportsDir) {
197 File[] files = reportsDir.listFiles();
198 for(File f : files) {
199 f.setLastModified(this.mojoInfo.getStartTime());
200 }
201 }
202
203 private static class TestBuildProxy implements MavenBuildProxy {
204
205 private final MavenBuild build;
206
207 public TestBuildProxy(MavenBuild build) {
208 this.build = build;
209 }
210
211 @Override
212 public <V, T extends Throwable> V execute(BuildCallable<V, T> program)
213 throws T, IOException, InterruptedException {
214 return program.call(build);
215 }
216
217 @Override
218 public void executeAsync(BuildCallable<?, ?> program)
219 throws IOException {
220 try {
221 program.call(this.build);
222 } catch(Throwable e) {
223 throw new IOException(e);
224 }
225 }
226
227 @Override
228 public FilePath getRootDir() {
229 return null;
230 }
231
232 @Override
233 public FilePath getProjectRootDir() {
234 return null;
235 }
236
237 @Override
238 public FilePath getModuleSetRootDir() {
239 return null;
240 }
241
242 @Override
243 public FilePath getArtifactsDir() {
244 return null;
245 }
246
247 @Override
248 public void setResult(Result result) {
249 }
250
251 @Override
252 public Calendar getTimestamp() {
253 return null;
254 }
255
256 @Override
257 public long getMilliSecsSinceBuildStart() {
258 return 0;
259 }
260
261 @Override
262 public boolean isArchivingDisabled() {
263 return false;
264 }
265
266 @Override
267 public void registerAsProjectAction(MavenReporter reporter) {
268 }
269
270 @Override
271 public void registerAsProjectAction(MavenProjectActionBuilder builder) {
272 }
273
274 @Override
275 public void registerAsAggregatedProjectAction(MavenReporter reporter) {
276 }
277
278 @Override
279 public void setExecutedMojos(List<ExecutedMojo> executedMojos) {
280 }
281
282 @Override
283 public MavenBuildInformation getMavenBuildInformation() {
284 return null;
285 }
286 }
287
288 private static class NullBuildListener implements BuildListener {
289
290 private static final long serialVersionUID = 1L;
291
292 @Override
293 public PrintStream getLogger() {
294 return new PrintStream(new NullOutputStream());
295 }
296
297 @SuppressWarnings("rawtypes")
298 @Override
299 public void annotate(ConsoleNote ann) throws IOException {
300 }
301
302 @Override
303 public void hyperlink(String url, String text) throws IOException {
304 }
305
306 @Override
307 public PrintWriter error(String msg) {
308 return null;
309 }
310
311 @Override
312 public PrintWriter error(String format, Object... args) {
313 return null;
314 }
315
316 @Override
317 public PrintWriter fatalError(String msg) {
318 return null;
319 }
320
321 @Override
322 public PrintWriter fatalError(String format, Object... args) {
323 return null;
324 }
325
326 @Override
327 public void started(List<Cause> causes) {
328 }
329
330 @Override
331 public void finished(Result result) {
332 }
333
334 }
335 }
336