1 package org.apache.maven.surefire.its;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36
37
38 public class RunOrderIT
39 extends AbstractSurefireIntegrationTestClass
40 {
41
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;
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 }