View Javadoc

1   package org.apache.maven.surefire.its;
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 org.apache.maven.it.VerificationException;
23  import org.apache.maven.it.Verifier;
24  import org.apache.maven.it.util.ResourceExtractor;
25  import org.apache.maven.surefire.its.misc.HelperAssertions;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Calendar;
30  import java.util.List;
31  
32  /**
33   * Verifies the runOrder setting and its effect
34   * 
35   * @author Kristian Rosenvold
36   */
37  public class RunOrderIT
38      extends AbstractSurefireIntegrationTestClass
39  {
40      private static final String[] TESTS_IN_ALPHABETICAL_ORDER = { "TA", "TB", "TC" };
41  
42      private static final String[] TESTS_IN_REVERSE_ALPHABETICAL_ORDER = { "TC", "TB", "TA" };
43  
44      // testing random is left as an exercise to the reader. Patches welcome
45  
46      private File testDir;
47  
48      private Verifier verifier;
49  
50      public void setUp()
51          throws IOException, VerificationException
52      {
53          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/runOrder" );
54          verifier = new Verifier( testDir.getAbsolutePath() );
55      }
56  
57      public void tearDown()
58          throws Exception
59      {
60          verifier.resetStreams();
61      }
62  
63      public void testAlphabetical()
64          throws Exception
65      {
66          executeWithRunOrder( "alphabetical" );
67          assertTestnamesAppearInSpecificOrder( TESTS_IN_ALPHABETICAL_ORDER );
68      }
69  
70      public void testReverseAlphabetical()
71          throws Exception
72      {
73          executeWithRunOrder( "reversealphabetical" );
74          assertTestnamesAppearInSpecificOrder( TESTS_IN_REVERSE_ALPHABETICAL_ORDER );
75      }
76  
77      public void testHourly()
78          throws Exception
79      {
80          int startHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
81          executeWithRunOrder( "hourly" );
82          int endHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
83          if ( startHour != endHour )
84          {
85              return; // Race condition, cannot test when hour changed mid-run
86          }
87  
88          String[] testnames =
89              ( ( startHour % 2 ) == 0 ) ? TESTS_IN_ALPHABETICAL_ORDER : TESTS_IN_REVERSE_ALPHABETICAL_ORDER;
90          assertTestnamesAppearInSpecificOrder( testnames );
91      }
92  
93      public void testNonExistingRunOrder()
94          throws Exception
95      {
96          try
97          {
98              executeTestsWithRunOrder( "nonExistingRunOrder" );
99          }
100         catch ( VerificationException e )
101         {
102         }
103         verifier.verifyTextInLog( "There's no RunOrder with the name nonExistingRunOrder." );
104     }
105 
106     private void executeWithRunOrder( String runOrder )
107         throws IOException, VerificationException
108     {
109         executeTestsWithRunOrder( runOrder );
110         verifier.verifyErrorFreeLog();
111         HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, testDir );
112     }
113 
114     private void executeTestsWithRunOrder( String runOrder )
115         throws VerificationException
116     {
117         List<String> goals = getInitialGoals();
118         goals.add( "-DrunOrder=" + runOrder );
119         goals.add( "test" );
120         executeGoals( verifier, goals );
121     }
122 
123     private void assertTestnamesAppearInSpecificOrder( String[] testnames )
124         throws VerificationException
125     {
126         if ( !testnamesAppearInSpecificOrder( testnames ) )
127         {
128             throw new VerificationException( "Response does not contain expected item" );
129         }
130     }
131 
132     private boolean testnamesAppearInSpecificOrder( String[] testnames ) throws VerificationException
133     {
134         int i = 0;
135         for ( String line : getLog() )
136         {
137             if ( line.startsWith( testnames[i] ) )
138             {
139                 if ( i == testnames.length - 1 )
140                 {
141                     return true;
142                 }
143                 ++i;
144             }
145         }
146         return false;
147     }
148 
149     private List<String> getLog()
150         throws VerificationException
151     {
152         return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false );
153     }
154 }