View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.runorder;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.nio.file.Files;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.Map;
30  
31  import junit.framework.TestCase;
32  import org.apache.maven.surefire.api.report.ReportEntry;
33  import org.apache.maven.surefire.api.report.SimpleReportEntry;
34  import org.apache.maven.surefire.api.runorder.RunEntryStatistics;
35  import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
36  import org.apache.maven.surefire.api.util.SureFireFileManager;
37  import org.apache.maven.surefire.api.util.internal.ClassMethod;
38  
39  import static java.nio.charset.StandardCharsets.UTF_8;
40  import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
41  import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
42  import static org.apache.maven.surefire.shared.io.IOUtils.readLines;
43  import static org.assertj.core.api.Assertions.assertThat;
44  import static org.powermock.reflect.Whitebox.getInternalState;
45  
46  /**
47   * @author Kristian Rosenvold
48   */
49  public class RunEntryStatisticsMapTest extends TestCase {
50      public void testPrioritizedClassRuntime() {
51          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream(getStatisticsFile());
52          final List<Class<?>> list = Arrays.asList(A.class, B.class, C.class);
53          final List<Class<?>> prioritizedTestsClassRunTime =
54                  runEntryStatisticsMap.getPrioritizedTestsClassRunTime(list, 2);
55          assertEquals(C.class, prioritizedTestsClassRunTime.get(0));
56          assertEquals(B.class, prioritizedTestsClassRunTime.get(1));
57          assertEquals(A.class, prioritizedTestsClassRunTime.get(2));
58      }
59  
60      public void testPrioritizedFailureFirst() {
61          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream(getStatisticsFile());
62          final List<Class<?>> list = Arrays.asList(A.class, B.class, NewClass.class, C.class);
63          final List<Class<?>> prioritizedTestsClassRunTime =
64                  runEntryStatisticsMap.getPrioritizedTestsByFailureFirst(list);
65          assertEquals(A.class, prioritizedTestsClassRunTime.get(0));
66          assertEquals(NewClass.class, prioritizedTestsClassRunTime.get(1));
67          assertEquals(C.class, prioritizedTestsClassRunTime.get(2));
68          assertEquals(B.class, prioritizedTestsClassRunTime.get(3));
69      }
70  
71      private InputStream getStatisticsFile() {
72          String content = "0,17,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$A,testA\n"
73                  + "2,42,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$B,testB\n"
74                  + "1,100,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$C,testC\n";
75          return new ByteArrayInputStream(content.getBytes(UTF_8));
76      }
77  
78      @SuppressWarnings("checkstyle:magicnumber")
79      public void testSerializeClass() throws Exception {
80          File data = SureFireFileManager.createTempFile("surefire-unit", "test");
81          RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
82          ReportEntry reportEntry = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, null, null, 42);
83          newResults.add(newResults.createNextGeneration(reportEntry));
84          newResults.serialize(data);
85          try (InputStream io = new FileInputStream(data)) {
86              List<String> lines = readLines(io, UTF_8);
87  
88              assertThat(lines).hasSize(1);
89  
90              assertThat(lines).containsSequence("1,42,abc,");
91          }
92      }
93  
94      @SuppressWarnings("checkstyle:magicnumber")
95      public void testDeserializeClass() throws Exception {
96          File data = SureFireFileManager.createTempFile("surefire-unit", "test");
97          Files.write(data.toPath(), "1,42,abc".getBytes(UTF_8));
98          RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile(data);
99          Map<?, ?> runEntryStatistics = getInternalState(existingEntries, "runEntryStatistics");
100         assertThat(runEntryStatistics).hasSize(1);
101         ClassMethod cm = (ClassMethod) runEntryStatistics.keySet().iterator().next();
102         assertThat(cm.getClazz()).isEqualTo("abc");
103         assertThat(cm.getMethod()).isNull();
104         RunEntryStatistics statistics =
105                 (RunEntryStatistics) runEntryStatistics.values().iterator().next();
106         assertThat(statistics.getRunTime()).isEqualTo(42);
107         assertThat(statistics.getSuccessfulBuilds()).isEqualTo(1);
108     }
109 
110     @SuppressWarnings("checkstyle:magicnumber")
111     public void testSerialize() throws Exception {
112         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
113         RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile(data);
114         RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
115 
116         ReportEntry reportEntry1 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method1", null, 42);
117         ReportEntry reportEntry2 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "willFail", null, 17);
118         ReportEntry reportEntry3 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method3", null, 100);
119 
120         newResults.add(existingEntries.createNextGeneration(reportEntry1));
121         newResults.add(existingEntries.createNextGeneration(reportEntry2));
122         newResults.add(existingEntries.createNextGeneration(reportEntry3));
123 
124         newResults.serialize(data);
125         try (InputStream io = new FileInputStream(data)) {
126             List<String> lines = readLines(io, UTF_8);
127 
128             assertThat(lines).hasSize(3);
129 
130             assertThat(lines).containsSequence("1,17,abc,willFail", "1,42,abc,method1", "1,100,abc,method3");
131         }
132 
133         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
134         newResults = new RunEntryStatisticsMap();
135 
136         ReportEntry newRunReportEntry1 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method1", null, 52);
137         ReportEntry newRunReportEntry2 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "willFail", null, 27);
138         ReportEntry newRunReportEntry3 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method3", null, 110);
139 
140         newResults.add(nextRun.createNextGeneration(newRunReportEntry1));
141         newResults.add(nextRun.createNextGenerationFailure(newRunReportEntry2));
142         newResults.add(nextRun.createNextGeneration(newRunReportEntry3));
143 
144         newResults.serialize(data);
145         try (InputStream io = new FileInputStream(data)) {
146             List<String> lines = readLines(io, UTF_8);
147 
148             assertThat(lines).hasSize(3);
149 
150             assertThat(lines).containsSequence("0,27,abc,willFail", "2,52,abc,method1", "2,110,abc,method3");
151         }
152     }
153 
154     @SuppressWarnings("checkstyle:magicnumber")
155     public void testMultiLineTestMethodName() throws IOException {
156         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
157         RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile(data);
158         ReportEntry reportEntry =
159                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "line1\nline2" + NL + " line3", null, 42);
160         reportEntries.add(reportEntries.createNextGeneration(reportEntry));
161 
162         reportEntries.serialize(data);
163         try (InputStream io = new FileInputStream(data)) {
164             List<String> lines = readLines(io, UTF_8);
165 
166             assertThat(lines).hasSize(3);
167 
168             assertThat(lines).containsSequence("1,42,abc,line1", " line2", "  line3");
169         }
170 
171         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
172         assertThat(data.delete()).isTrue();
173         nextRun.serialize(data);
174         try (InputStream io = new FileInputStream(data)) {
175             List<String> lines = readLines(io, UTF_8);
176 
177             assertThat(lines).hasSize(3);
178 
179             assertThat(lines).containsSequence("1,42,abc,line1", " line2", "  line3");
180         }
181     }
182 
183     @SuppressWarnings("checkstyle:magicnumber")
184     public void testCombinedMethodNames() throws IOException {
185         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
186         RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile(data);
187         reportEntries.add(reportEntries.createNextGeneration(
188                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "line1\nline2", null, 42)));
189         reportEntries.add(reportEntries.createNextGeneration(
190                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "test", null, 10)));
191 
192         reportEntries.serialize(data);
193         try (InputStream io = new FileInputStream(data)) {
194             List<String> lines = readLines(io, UTF_8);
195 
196             assertThat(lines).hasSize(3);
197 
198             assertThat(lines).containsSequence("1,10,abc,test", "1,42,abc,line1", " line2");
199         }
200 
201         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
202         assertThat(data.delete()).isTrue();
203         nextRun.serialize(data);
204         try (InputStream io = new FileInputStream(data)) {
205             List<String> lines = readLines(io, UTF_8);
206 
207             assertThat(lines).hasSize(3);
208 
209             assertThat(lines).containsSequence("1,10,abc,test", "1,42,abc,line1", " line2");
210         }
211     }
212 
213     class A {}
214 
215     class B {}
216 
217     class C {}
218 
219     class NewClass {}
220 }