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 java.io.IOException;
23  import java.util.Calendar;
24  import org.apache.maven.it.VerificationException;
25  import org.apache.maven.surefire.its.fixture.OutputValidator;
26  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
27  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
28  import org.junit.Test;
29  
30  /**
31   * Verifies the runOrder setting and its effect
32   *
33   * @author Kristian Rosenvold
34   */
35  public class RunOrderIT
36      extends SurefireJUnit4IntegrationTestCase
37  {
38      private static final String[] TESTS_IN_ALPHABETICAL_ORDER = { "TA", "TB", "TC" };
39  
40      private static final String[] TESTS_IN_REVERSE_ALPHABETICAL_ORDER = { "TC", "TB", "TA" };
41  
42      // testing random is left as an exercise to the reader. Patches welcome
43  
44      @Test
45      public void testAlphabetical()
46          throws Exception
47      {
48          OutputValidator validator = executeWithRunOrder( "alphabetical" );
49          assertTestnamesAppearInSpecificOrder( validator, TESTS_IN_ALPHABETICAL_ORDER );
50      }
51  
52      @Test
53      public void testReverseAlphabetical()
54          throws Exception
55      {
56          OutputValidator validator = executeWithRunOrder( "reversealphabetical" );
57          assertTestnamesAppearInSpecificOrder( validator, TESTS_IN_REVERSE_ALPHABETICAL_ORDER );
58      }
59  
60      @Test
61      public void testHourly()
62          throws Exception
63      {
64          int startHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
65          OutputValidator validator = executeWithRunOrder( "hourly" );
66          int endHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
67          if ( startHour != endHour )
68          {
69              return; // Race condition, cannot test when hour changed mid-run
70          }
71  
72          String[] testnames =
73              ( ( startHour % 2 ) == 0 ) ? TESTS_IN_ALPHABETICAL_ORDER : TESTS_IN_REVERSE_ALPHABETICAL_ORDER;
74          assertTestnamesAppearInSpecificOrder( validator, testnames );
75      }
76  
77      @Test
78      public void testNonExistingRunOrder()
79          throws Exception
80      {
81          unpack().forkMode( getForkMode() ).runOrder( "nonExistingRunOrder" ).maven().withFailure().executeTest().verifyTextInLog(
82              "There's no RunOrder with the name nonExistingRunOrder." );
83      }
84  
85      private OutputValidator executeWithRunOrder( String runOrder )
86          throws IOException, VerificationException
87      {
88          return unpack().forkMode( getForkMode() ).runOrder( runOrder ).executeTest().verifyErrorFree( 3 );
89      }
90  
91      protected String getForkMode()
92      {
93          return "once";
94      }
95  
96      private SurefireLauncher unpack()
97      {
98          return unpack( "runOrder" );
99      }
100 
101     private void assertTestnamesAppearInSpecificOrder( OutputValidator validator, String[] testnames )
102         throws VerificationException
103     {
104         if ( !validator.stringsAppearInSpecificOrderInLog( testnames ) )
105         {
106             throw new VerificationException( "Response does not contain expected item" );
107         }
108     }
109 }