1 package org.apache.maven.plugin.surefire.runorder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.maven.surefire.api.runorder.RunEntryStatistics;
33 import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
34 import org.apache.maven.surefire.api.report.ReportEntry;
35 import org.apache.maven.surefire.api.report.SimpleReportEntry;
36
37 import junit.framework.TestCase;
38 import org.apache.maven.surefire.api.util.internal.ClassMethod;
39
40 import static java.nio.charset.StandardCharsets.UTF_8;
41 import static org.apache.maven.surefire.shared.io.IOUtils.readLines;
42 import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
43 import static org.fest.assertions.Assertions.assertThat;
44 import static org.powermock.reflect.Whitebox.getInternalState;
45
46
47
48
49 public class RunEntryStatisticsMapTest
50 extends TestCase
51 {
52 public void testPrioritizedClassRuntime()
53 {
54 final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream( getStatisticsFile() );
55 final List<Class<?>> list = Arrays.asList( A.class, B.class, C.class );
56 final List<Class<?>> prioritizedTestsClassRunTime =
57 runEntryStatisticsMap.getPrioritizedTestsClassRunTime( list, 2 );
58 assertEquals( C.class, prioritizedTestsClassRunTime.get( 0 ) );
59 assertEquals( B.class, prioritizedTestsClassRunTime.get( 1 ) );
60 assertEquals( A.class, prioritizedTestsClassRunTime.get( 2 ) );
61 }
62
63 public void testPrioritizedFailureFirst()
64 {
65 final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream( getStatisticsFile() );
66 final List<Class<?>> list = Arrays.asList( A.class, B.class, NewClass.class, C.class );
67 final List<Class<?>> prioritizedTestsClassRunTime =
68 runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( list );
69 assertEquals( A.class, prioritizedTestsClassRunTime.get( 0 ) );
70 assertEquals( NewClass.class, prioritizedTestsClassRunTime.get( 1 ) );
71 assertEquals( C.class, prioritizedTestsClassRunTime.get( 2 ) );
72 assertEquals( B.class, prioritizedTestsClassRunTime.get( 3 ) );
73 }
74
75 private InputStream getStatisticsFile()
76 {
77 String content = "0,17,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$A,testA\n"
78 + "2,42,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$B,testB\n"
79 + "1,100,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$C,testC\n";
80 return new ByteArrayInputStream( content.getBytes( UTF_8 ) );
81 }
82
83 @SuppressWarnings( "checkstyle:magicnumber" )
84 public void testSerializeClass()
85 throws Exception
86 {
87 File data = File.createTempFile( "surefire-unit", "test" );
88 RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
89 ReportEntry reportEntry = new SimpleReportEntry( "abc", null, null, null, 42 );
90 newResults.add( newResults.createNextGeneration( reportEntry ) );
91 newResults.serialize( data );
92 try ( InputStream io = new FileInputStream( data ) )
93 {
94 List<String> lines = readLines( io, UTF_8 );
95
96 assertThat( lines )
97 .hasSize( 1 );
98
99 assertThat( lines )
100 .containsSequence( "1,42,abc," );
101 }
102 }
103
104 @SuppressWarnings( "checkstyle:magicnumber" )
105 public void testDeserializeClass()
106 throws Exception
107 {
108 File data = File.createTempFile( "surefire-unit", "test" );
109 Files.write( data.toPath(), "1,42,abc".getBytes( UTF_8 ) );
110 RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
111 Map<?, ?> runEntryStatistics = getInternalState( existingEntries, "runEntryStatistics" );
112 assertThat( runEntryStatistics )
113 .hasSize( 1 );
114 ClassMethod cm = (ClassMethod) runEntryStatistics.keySet().iterator().next();
115 assertThat( cm.getClazz() )
116 .isEqualTo( "abc" );
117 assertThat( cm.getMethod() )
118 .isNull();
119 RunEntryStatistics statistics = (RunEntryStatistics) runEntryStatistics.values().iterator().next();
120 assertThat( statistics.getRunTime() )
121 .isEqualTo( 42 );
122 assertThat( statistics.getSuccessfulBuilds() )
123 .isEqualTo( 1 );
124 }
125
126 @SuppressWarnings( "checkstyle:magicnumber" )
127 public void testSerialize()
128 throws Exception
129 {
130 File data = File.createTempFile( "surefire-unit", "test" );
131 RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
132 RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
133
134 ReportEntry reportEntry1 = new SimpleReportEntry( "abc", null, "method1", null, 42 );
135 ReportEntry reportEntry2 = new SimpleReportEntry( "abc", null, "willFail", null, 17 );
136 ReportEntry reportEntry3 = new SimpleReportEntry( "abc", null, "method3", null, 100 );
137
138 newResults.add( existingEntries.createNextGeneration( reportEntry1 ) );
139 newResults.add( existingEntries.createNextGeneration( reportEntry2 ) );
140 newResults.add( existingEntries.createNextGeneration( reportEntry3 ) );
141
142 newResults.serialize( data );
143 try ( InputStream io = new FileInputStream( data ) )
144 {
145 List<String> lines = readLines( io, UTF_8 );
146
147 assertThat( lines )
148 .hasSize( 3 );
149
150 assertThat( lines )
151 .containsSequence( "1,17,abc,willFail", "1,42,abc,method1", "1,100,abc,method3" );
152 }
153
154 RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile( data );
155 newResults = new RunEntryStatisticsMap();
156
157 ReportEntry newRunReportEntry1 = new SimpleReportEntry( "abc", null, "method1", null, 52 );
158 ReportEntry newRunReportEntry2 = new SimpleReportEntry( "abc", null, "willFail", null, 27 );
159 ReportEntry newRunReportEntry3 = new SimpleReportEntry( "abc", null, "method3", null, 110 );
160
161 newResults.add( nextRun.createNextGeneration( newRunReportEntry1 ) );
162 newResults.add( nextRun.createNextGenerationFailure( newRunReportEntry2 ) );
163 newResults.add( nextRun.createNextGeneration( newRunReportEntry3 ) );
164
165 newResults.serialize( data );
166 try ( InputStream io = new FileInputStream( data ) )
167 {
168 List<String> lines = readLines( io, UTF_8 );
169
170 assertThat( lines )
171 .hasSize( 3 );
172
173 assertThat( lines )
174 .containsSequence( "0,27,abc,willFail", "2,52,abc,method1", "2,110,abc,method3" );
175 }
176 }
177
178 @SuppressWarnings( "checkstyle:magicnumber" )
179 public void testMultiLineTestMethodName() throws IOException
180 {
181 File data = File.createTempFile( "surefire-unit", "test" );
182 RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile( data );
183 ReportEntry reportEntry = new SimpleReportEntry( "abc", null, "line1\nline2" + NL + " line3", null, 42 );
184 reportEntries.add( reportEntries.createNextGeneration( reportEntry ) );
185
186 reportEntries.serialize( data );
187 try ( InputStream io = new FileInputStream( data ) )
188 {
189 List<String> lines = readLines( io, UTF_8 );
190
191 assertThat( lines )
192 .hasSize( 3 );
193
194 assertThat( lines )
195 .containsSequence( "1,42,abc,line1", " line2", " line3" );
196 }
197
198 RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile( data );
199 assertThat( data.delete() ).isTrue();
200 nextRun.serialize( data );
201 try ( InputStream io = new FileInputStream( data ) )
202 {
203 List<String> lines = readLines( io, UTF_8 );
204
205 assertThat( lines )
206 .hasSize( 3 );
207
208 assertThat( lines )
209 .containsSequence( "1,42,abc,line1", " line2", " line3" );
210 }
211 }
212
213 @SuppressWarnings( "checkstyle:magicnumber" )
214 public void testCombinedMethodNames() throws IOException
215 {
216 File data = File.createTempFile( "surefire-unit", "test" );
217 RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile( data );
218 reportEntries.add(
219 reportEntries.createNextGeneration( new SimpleReportEntry( "abc", null, "line1\nline2", null, 42 ) ) );
220 reportEntries.add(
221 reportEntries.createNextGeneration( new SimpleReportEntry( "abc", null, "test", null, 10 ) ) );
222
223 reportEntries.serialize( data );
224 try ( InputStream io = new FileInputStream( data ) )
225 {
226 List<String> lines = readLines( io, UTF_8 );
227
228 assertThat( lines )
229 .hasSize( 3 );
230
231 assertThat( lines )
232 .containsSequence( "1,10,abc,test",
233 "1,42,abc,line1",
234 " line2" );
235 }
236
237 RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile( data );
238 assertThat( data.delete() ).isTrue();
239 nextRun.serialize( data );
240 try ( InputStream io = new FileInputStream( data ) )
241 {
242 List<String> lines = readLines( io, UTF_8 );
243
244 assertThat( lines )
245 .hasSize( 3 );
246
247 assertThat( lines )
248 .containsSequence( "1,10,abc,test",
249 "1,42,abc,line1",
250 " line2" );
251 }
252 }
253
254 class A
255 {
256 }
257
258 class B
259 {
260 }
261
262 class C
263 {
264 }
265
266 class NewClass
267 {
268 }
269 }