1 package org.apache.maven.surefire.its.jiras;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.it.VerificationException;
23 import org.apache.maven.surefire.its.fixture.OutputValidator;
24 import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25 import org.apache.maven.surefire.its.fixture.SurefireLauncher;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 import java.text.Format;
30 import java.text.NumberFormat;
31 import java.util.Iterator;
32 import java.util.Set;
33 import java.util.TreeSet;
34
35 import static java.lang.String.format;
36 import static java.math.RoundingMode.DOWN;
37 import static java.util.Locale.ROOT;
38 import static org.hamcrest.CoreMatchers.anyOf;
39 import static org.hamcrest.CoreMatchers.containsString;
40 import static org.hamcrest.CoreMatchers.endsWith;
41 import static org.hamcrest.core.Is.is;
42 import static org.junit.Assert.assertThat;
43 import static org.junit.Assert.assertTrue;
44
45
46
47
48 public class Surefire747MethodParallelWithSuiteCountIT
49 extends SurefireJUnit4IntegrationTestCase
50 {
51
52 private static final int PERFORMANCE_TEST_MULTIPLICATION_FACTOR = 4;
53
54 private Format lowerScaleFormatter, noFractionalDigitsFormatter;
55
56 private static Set<String> printTestLines( OutputValidator validator, String pattern )
57 throws VerificationException
58 {
59 Set<String> log = new TreeSet<>( validator.loadLogLines() );
60 for ( Iterator<String> it = log.iterator(); it.hasNext(); )
61 {
62 String line = it.next();
63 if ( !line.contains( pattern ) )
64 {
65 it.remove();
66 }
67 }
68 return log;
69 }
70
71 private static long duration( String logLine )
72 {
73 return Integer.decode( logLine.split( "=" )[1] );
74 }
75
76 @Before
77 public void init()
78 {
79 NumberFormat lowScaleFormatter = NumberFormat.getInstance( ROOT );
80 lowScaleFormatter.setRoundingMode( DOWN );
81 lowScaleFormatter.setMinimumFractionDigits( 1 );
82 lowScaleFormatter.setMaximumFractionDigits( 1 );
83 this.lowerScaleFormatter = lowScaleFormatter;
84
85 NumberFormat noFractionalDigitsFormatter = NumberFormat.getInstance( ROOT );
86 noFractionalDigitsFormatter.setRoundingMode( DOWN );
87 noFractionalDigitsFormatter.setMinimumFractionDigits( 0 );
88 noFractionalDigitsFormatter.setMaximumFractionDigits( 0 );
89 this.noFractionalDigitsFormatter = noFractionalDigitsFormatter;
90 }
91
92 @Test
93 public void testMethodsParallelWithSuite()
94 throws VerificationException
95 {
96 OutputValidator validator = unpack().executeTest().verifyErrorFree( 6 );
97 Set<String> testLines = printTestLines( validator, "test finished after duration=" );
98 assertThat( testLines.size(), is( 2 ) );
99 for ( String testLine : testLines )
100 {
101 long duration = duration( testLine );
102 long min = 250 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
103 long max = 750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
104 assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
105 duration > min && duration < max
106 );
107 }
108 Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
109 assertThat( suiteLines.size(), is( 1 ) );
110 long duration = duration( suiteLines.iterator().next() );
111 long min = 750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
112 long max = 1250 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
113 assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
114 duration > min && duration < max
115 );
116
117 String delayMin = lowerScaleFormatter.format( 0.98 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR * 0.5 );
118 String delayMax = noFractionalDigitsFormatter.format( PERFORMANCE_TEST_MULTIPLICATION_FACTOR * 0.5 ) + ".";
119
120 for ( String line : validator.loadLogLines() )
121 {
122 if ( line.startsWith( "Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:" ) )
123 {
124 assertThat( line, anyOf(
125 containsString( "Time elapsed: " + delayMin ),
126 containsString( "Time elapsed: " + delayMax )
127 )
128 );
129 assertThat( line, anyOf(
130 endsWith( " s - in surefire747.SuiteTest1" ),
131 endsWith( " s - in surefire747.SuiteTest2" )
132 )
133 );
134 }
135 }
136 }
137
138 @Test
139 public void testClassesParallelWithSuite()
140 throws VerificationException
141 {
142 OutputValidator validator = unpack().parallelClasses().executeTest().verifyErrorFree( 6 );
143 Set<String> testLines = printTestLines( validator, "test finished after duration=" );
144 assertThat( testLines.size(), is( 2 ) );
145 for ( String testLine : testLines )
146 {
147 long duration = duration( testLine );
148 long min = 1450 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
149 long max = 1750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
150 assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
151 duration > min && duration < max
152 );
153 }
154 Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
155 assertThat( suiteLines.size(), is( 1 ) );
156 long duration = duration( suiteLines.iterator().next() );
157 long min = 1450 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
158 long max = 1750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
159 assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
160 duration > min && duration < max
161 );
162 }
163
164 public SurefireLauncher unpack()
165 {
166 return unpack( "junit47-parallel-with-suite" );
167 }
168 }