View Javadoc
1   package org.apache.maven.plugin.surefire.runorder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.StringReader;
25  import java.util.Arrays;
26  import java.util.List;
27  import org.apache.maven.surefire.report.ReportEntry;
28  import org.apache.maven.surefire.report.SimpleReportEntry;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * @author Kristian Rosenvold
34   */
35  public class RunEntryStatisticsMapTest
36      extends TestCase
37  {
38      public void testPrioritizedClassRuntime()
39          throws IOException
40      {
41          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromReader( getStatisticsFile() );
42          final List<Class<?>> list = Arrays.<Class<?>>asList( A.class, B.class, C.class );
43          final List<Class<?>> prioritizedTestsClassRunTime =
44              runEntryStatisticsMap.getPrioritizedTestsClassRunTime( list, 2 );
45          assertEquals( C.class, prioritizedTestsClassRunTime.get( 0 ) );
46          assertEquals( B.class, prioritizedTestsClassRunTime.get( 1 ) );
47          assertEquals( A.class, prioritizedTestsClassRunTime.get( 2 ) );
48      }
49  
50      public void testPrioritizedFailureFirst()
51          throws IOException
52      {
53          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromReader( getStatisticsFile() );
54          final List<Class<?>> list = Arrays.<Class<?>>asList( A.class, B.class, NewClass.class, C.class );
55          final List<Class<?>> prioritizedTestsClassRunTime =
56              runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( list );
57          assertEquals( A.class, prioritizedTestsClassRunTime.get( 0 ) );
58          assertEquals( NewClass.class, prioritizedTestsClassRunTime.get( 1 ) );
59          assertEquals( C.class, prioritizedTestsClassRunTime.get( 2 ) );
60          assertEquals( B.class, prioritizedTestsClassRunTime.get( 3 ) );
61      }
62  
63      private StringReader getStatisticsFile()
64      {
65          String content = "0,17,testA(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$A)\n" +
66              "2,42,testB(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$B)\n" +
67              "1,100,testC(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$C)\n";
68          return new StringReader( content );
69      }
70  
71      public void testSerialize()
72          throws Exception
73      {
74          File data = File.createTempFile( "surefire-unit", "test" );
75          RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
76          RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
77  
78          ReportEntry reportEntry1 = new SimpleReportEntry( "abc", "method1", 42 );
79          ReportEntry reportEntry2 = new SimpleReportEntry( "abc", "willFail", 17 );
80          ReportEntry reportEntry3 = new SimpleReportEntry( "abc", "method3", 100 );
81  
82          newResults.add( existingEntries.createNextGeneration( reportEntry1 ) );
83          newResults.add( existingEntries.createNextGeneration( reportEntry2 ) );
84          newResults.add( existingEntries.createNextGeneration( reportEntry3 ) );
85  
86          newResults.serialize( data );
87  
88          RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile( data );
89          newResults = new RunEntryStatisticsMap();
90  
91          newResults.add( existingEntries.createNextGeneration( reportEntry1 ) );
92          newResults.add( existingEntries.createNextGenerationFailure( reportEntry2 ) );
93          newResults.add( existingEntries.createNextGeneration( reportEntry3 ) );
94  
95          newResults.serialize( data );
96      }
97  
98      class A
99      {
100     }
101 
102     class B
103     {
104     }
105 
106     class C
107     {
108     }
109 
110     class NewClass
111     {
112     }
113 }