View Javadoc

1   package org.apache.maven.surefire.its;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.maven.it.VerificationException;
22  import org.apache.maven.it.Verifier;
23  import org.apache.maven.it.util.ResourceExtractor;
24  import org.apache.maven.surefire.its.misc.HelperAssertions;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.Arrays;
29  import java.util.Calendar;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * Verifies the runOrder setting and its effect
35   *
36   * @author Kristian Rosenvold
37   */
38  public class RunOrderIT
39      extends AbstractSurefireIntegrationTestClass
40  {
41      // testing random is left as an exercise to the reader. Patches welcome
42  
43      public void testAlphabetical()
44          throws Exception
45      {
46          checkOrder( "alphabetical", getAlphabetical() );
47      }
48  
49  
50      public void testReverseAlphabetical()
51          throws Exception
52      {
53          checkOrder( "reversealphabetical", getReverseAlphabetical() );
54      }
55  
56  
57      public void testHourly()
58          throws Exception
59      {
60          int startHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
61          final List<String> actual = executeWithRunOrder( "hourly" );
62          int endHour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
63          if ( startHour != endHour )
64          {
65              return; // Race condition, cannot test when hour changed mid-run
66          }
67  
68          List<String> expected = ( ( startHour % 2 ) == 0 ) ? getAlphabetical() : getReverseAlphabetical();
69          if ( !contains( actual, expected ) )
70          {
71              throw new VerificationException( "Response does not contain expected item" );
72          }
73      }
74  
75      private boolean contains( List<String> items, List<String> expected )
76      {
77          Iterator<String> expectedIterator = expected.iterator();
78          String next = (String) expectedIterator.next();
79          Iterator<String> content = items.iterator();
80          while ( content.hasNext() )
81          {
82              String line = content.next();
83              if ( line.startsWith( next ) )
84              {
85                  if ( !expectedIterator.hasNext() )
86                  {
87                      return true;
88                  }
89                  next = expectedIterator.next();
90              }
91          }
92          return content.hasNext();
93      }
94  
95      private void checkOrder( String alphabetical, List<String> expected )
96          throws VerificationException, IOException
97      {
98          final List<String> list = executeWithRunOrder( alphabetical );
99          if ( !contains( list, expected ) )
100         {
101             throw new VerificationException( "Response does not contain expected item" );
102         }
103     }
104 
105     private List<String> executeWithRunOrder( String runOrder )
106         throws IOException, VerificationException
107     {
108         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/runOrder" );
109         Verifier verifier = new Verifier( testDir.getAbsolutePath() );
110 
111         List<String> goals = getInitialGoals();
112         goals.add( "-DrunOrder=" + runOrder );
113         goals.add( "test" );
114         this.executeGoals( verifier, goals );
115         verifier.verifyErrorFreeLog();
116         verifier.resetStreams();
117         HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, testDir );
118         return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false );
119     }
120 
121     private List<String> getAlphabetical()
122     {
123         return Arrays.asList( new String[]{ "TA", "TB", "TC" } );
124     }
125 
126     private List<String> getReverseAlphabetical()
127     {
128         return Arrays.asList( new String[]{ "TC", "TB", "TA" } );
129     }
130 
131 }