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<?> list = Arrays.asList( new Class[]{ A.class, B.class, C.class } );
43          final List<?> prioritizedTestsClassRunTime = runEntryStatisticsMap.getPrioritizedTestsClassRunTime( list, 2 );
44          assertEquals( C.class, prioritizedTestsClassRunTime.get( 0 ) );
45          assertEquals( B.class, prioritizedTestsClassRunTime.get( 1 ) );
46          assertEquals( A.class, prioritizedTestsClassRunTime.get( 2 ) );
47      }
48  
49      public void testPrioritizedFailureFirst()
50          throws IOException
51      {
52          final RunEntryStatisticsMap runEntryStatisticsMap = RunEntryStatisticsMap.fromReader( getStatisticsFile() );
53          final List<?> list = Arrays.asList( A.class, B.class, NewClass.class, C.class );
54          final List<?> prioritizedTestsClassRunTime = runEntryStatisticsMap.getPrioritizedTestsByFailureFirst( list );
55          assertEquals( A.class, prioritizedTestsClassRunTime.get( 0 ) );
56          assertEquals( NewClass.class, prioritizedTestsClassRunTime.get( 1 ) );
57          assertEquals( C.class, prioritizedTestsClassRunTime.get( 2 ) );
58          assertEquals( B.class, prioritizedTestsClassRunTime.get( 3 ) );
59      }
60  
61      private StringReader getStatisticsFile()
62      {
63          String content = "0,17,testA(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$A)\n" +
64              "2,42,testB(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$B)\n" +
65              "1,100,testC(org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMapTest$C)\n";
66          return new StringReader( content );
67      }
68  
69      public void testSerialize()
70          throws Exception
71      {
72          File data = File.createTempFile( "surefire-unit", "test" );
73          RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
74          RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
75  
76          ReportEntry reportEntry1 = new SimpleReportEntry( "abc", "method1", new Integer( 42 ) );
77          ReportEntry reportEntry2 = new SimpleReportEntry( "abc", "willFail", new Integer( 17 ) );
78          ReportEntry reportEntry3 = new SimpleReportEntry( "abc", "method3", new Integer( 100 ) );
79  
80          newResults.add( existingEntries.createNextGeneration( reportEntry1 ) );
81          newResults.add( existingEntries.createNextGeneration( reportEntry2 ) );
82          newResults.add( existingEntries.createNextGeneration( reportEntry3 ) );
83  
84          newResults.serialize( data );
85  
86          RunEntryStatisticsMap nextRun = RunEntryStatisticsMap.fromFile( data );
87          newResults = new RunEntryStatisticsMap();
88  
89          newResults.add( existingEntries.createNextGeneration( reportEntry1 ) );
90          newResults.add( existingEntries.createNextGenerationFailure( reportEntry2 ) );
91          newResults.add( existingEntries.createNextGeneration( reportEntry3 ) );
92  
93          newResults.serialize( data );
94      }
95  
96      class A
97      {
98      }
99  
100     class B
101     {
102     }
103 
104     class C
105     {
106     }
107 
108     class NewClass
109     {
110     }
111 }