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.IOException;
24  import java.io.InputStream;
25  import java.nio.file.Files;
26  import java.util.Arrays;
27  import java.util.List;
28  import java.util.Map;
29  
30  import junit.framework.TestCase;
31  import org.apache.maven.surefire.api.report.ReportEntry;
32  import org.apache.maven.surefire.api.report.SimpleReportEntry;
33  import org.apache.maven.surefire.api.runorder.RunEntryStatistics;
34  import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
35  import org.apache.maven.surefire.api.util.SureFireFileManager;
36  import org.apache.maven.surefire.api.util.internal.ClassMethod;
37  
38  import static java.nio.charset.StandardCharsets.UTF_8;
39  import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
40  import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
41  import static org.assertj.core.api.Assertions.assertThat;
42  import static org.powermock.reflect.Whitebox.getInternalState;
43  
44  /**
45   * @author Kristian Rosenvold
46   */
47  public class RunEntryStatisticsMapTest extends TestCase {
48      public void testPrioritizedClassRuntime() {
49          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream(getStatisticsFile());
50          final List<Class<?>> list = Arrays.asList(A.class, B.class, C.class);
51          final List<Class<?>> prioritizedTestsClassRunTime =
52                  runEntryStatisticsMap.getPrioritizedTestsClassRunTime(list, 2);
53          assertEquals(C.class, prioritizedTestsClassRunTime.get(0));
54          assertEquals(B.class, prioritizedTestsClassRunTime.get(1));
55          assertEquals(A.class, prioritizedTestsClassRunTime.get(2));
56      }
57  
58      public void testPrioritizedFailureFirst() {
59          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromStream(getStatisticsFile());
60          final List<Class<?>> list = Arrays.asList(A.class, B.class, NewClass.class, C.class);
61          final List<Class<?>> prioritizedTestsClassRunTime =
62                  runEntryStatisticsMap.getPrioritizedTestsByFailureFirst(list);
63          assertEquals(A.class, prioritizedTestsClassRunTime.get(0));
64          assertEquals(NewClass.class, prioritizedTestsClassRunTime.get(1));
65          assertEquals(C.class, prioritizedTestsClassRunTime.get(2));
66          assertEquals(B.class, prioritizedTestsClassRunTime.get(3));
67      }
68  
69      private InputStream getStatisticsFile() {
70          String content = "0,17,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$A,testA\n"
71                  + "2,42,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$B,testB\n"
72                  + "1,100,org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$C,testC\n";
73          return new ByteArrayInputStream(content.getBytes(UTF_8));
74      }
75  
76      @SuppressWarnings("checkstyle:magicnumber")
77      public void testSerializeClass() throws Exception {
78          File data = SureFireFileManager.createTempFile("surefire-unit", "test");
79          RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
80          ReportEntry reportEntry = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, null, null, 42);
81          newResults.add(newResults.createNextGeneration(reportEntry));
82          newResults.serialize(data);
83  
84          List<String> lines = Files.readAllLines(data.toPath(), UTF_8);
85          assertThat(lines).hasSize(1);
86          assertThat(lines).containsSequence("1,42,abc,");
87      }
88  
89      @SuppressWarnings("checkstyle:magicnumber")
90      public void testDeserializeClass() throws Exception {
91          File data = SureFireFileManager.createTempFile("surefire-unit", "test");
92          Files.write(data.toPath(), "1,42,abc".getBytes(UTF_8));
93          RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile(data);
94          Map<?, ?> runEntryStatistics = getInternalState(existingEntries, "runEntryStatistics");
95          assertThat(runEntryStatistics).hasSize(1);
96          ClassMethod cm = (ClassMethod) runEntryStatistics.keySet().iterator().next();
97          assertThat(cm.getClazz()).isEqualTo("abc");
98          assertThat(cm.getMethod()).isNull();
99          RunEntryStatistics statistics =
100                 (RunEntryStatistics) runEntryStatistics.values().iterator().next();
101         assertThat(statistics.getRunTime()).isEqualTo(42);
102         assertThat(statistics.getSuccessfulBuilds()).isEqualTo(1);
103     }
104 
105     @SuppressWarnings("checkstyle:magicnumber")
106     public void testSerialize() throws Exception {
107         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
108         RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile(data);
109         RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
110 
111         ReportEntry reportEntry1 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method1", null, 42);
112         ReportEntry reportEntry2 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "willFail", null, 17);
113         ReportEntry reportEntry3 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method3", null, 100);
114 
115         newResults.add(existingEntries.createNextGeneration(reportEntry1));
116         newResults.add(existingEntries.createNextGeneration(reportEntry2));
117         newResults.add(existingEntries.createNextGeneration(reportEntry3));
118 
119         newResults.serialize(data);
120         List<String> lines = Files.readAllLines(data.toPath(), UTF_8);
121         assertThat(lines).hasSize(3);
122         assertThat(lines).containsSequence("1,17,abc,willFail", "1,42,abc,method1", "1,100,abc,method3");
123 
124         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
125         newResults = new RunEntryStatisticsMap();
126 
127         ReportEntry newRunReportEntry1 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method1", null, 52);
128         ReportEntry newRunReportEntry2 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "willFail", null, 27);
129         ReportEntry newRunReportEntry3 = new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "method3", null, 110);
130 
131         newResults.add(nextRun.createNextGeneration(newRunReportEntry1));
132         newResults.add(nextRun.createNextGenerationFailure(newRunReportEntry2));
133         newResults.add(nextRun.createNextGeneration(newRunReportEntry3));
134 
135         newResults.serialize(data);
136         lines = Files.readAllLines(data.toPath(), UTF_8);
137         assertThat(lines).hasSize(3);
138         assertThat(lines).containsSequence("0,27,abc,willFail", "2,52,abc,method1", "2,110,abc,method3");
139     }
140 
141     @SuppressWarnings("checkstyle:magicnumber")
142     public void testMultiLineTestMethodName() throws IOException {
143         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
144         RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile(data);
145         ReportEntry reportEntry =
146                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "line1\nline2" + NL + " line3", null, 42);
147         reportEntries.add(reportEntries.createNextGeneration(reportEntry));
148 
149         reportEntries.serialize(data);
150         List<String> lines = Files.readAllLines(data.toPath(), UTF_8);
151         assertThat(lines).hasSize(3);
152         assertThat(lines).containsSequence("1,42,abc,line1", " line2", "  line3");
153 
154         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
155         assertThat(data.delete()).isTrue();
156         nextRun.serialize(data);
157 
158         lines = Files.readAllLines(data.toPath(), UTF_8);
159         assertThat(lines).hasSize(3);
160         assertThat(lines).containsSequence("1,42,abc,line1", " line2", "  line3");
161     }
162 
163     @SuppressWarnings("checkstyle:magicnumber")
164     public void testCombinedMethodNames() throws IOException {
165         File data = SureFireFileManager.createTempFile("surefire-unit", "test");
166         RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile(data);
167         reportEntries.add(reportEntries.createNextGeneration(
168                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "line1\nline2", null, 42)));
169         reportEntries.add(reportEntries.createNextGeneration(
170                 new SimpleReportEntry(NORMAL_RUN, 0L, "abc", null, "test", null, 10)));
171 
172         reportEntries.serialize(data);
173         List<String> lines = Files.readAllLines(data.toPath(), UTF_8);
174         assertThat(lines).hasSize(3);
175         assertThat(lines).containsSequence("1,10,abc,test", "1,42,abc,line1", " line2");
176 
177         RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile(data);
178         assertThat(data.delete()).isTrue();
179         nextRun.serialize(data);
180         lines = Files.readAllLines(data.toPath(), UTF_8);
181         assertThat(lines).hasSize(3);
182         assertThat(lines).containsSequence("1,10,abc,test", "1,42,abc,line1", " line2");
183     }
184 
185     class A {}
186 
187     class B {}
188 
189     class C {}
190 
191     class NewClass {}
192 }