-
Notifications
You must be signed in to change notification settings - Fork 531
/
SurefireHelperTest.java
178 lines (159 loc) · 6.85 KB
/
SurefireHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.apache.maven.plugin.surefire;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.surefire.AbstractSurefireMojoTest.Mojo;
import org.apache.maven.surefire.api.suite.RunResult;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static java.util.Collections.addAll;
import static java.util.Collections.singleton;
import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath;
import static org.apache.maven.plugin.surefire.SurefireHelper.reportExecution;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
/**
* Test of {@link SurefireHelper}.
*/
public class SurefireHelperTest
{
@Rule
public ExpectedException e = ExpectedException.none();
@Test
public void shouldReplaceForkNumberPath()
{
File root = new File( System.getProperty( "user.dir", "" ) );
File pathWithPlaceholder = new File( root, "${surefire.forkNumber}" );
File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
assertThat( changed.getPath() )
.isEqualTo( new File( root, "5" ).getPath() );
}
@Test
public void shouldReplaceLongForkNumberPath()
{
File root = new File( System.getProperty( "user.dir", "" ) );
File subDir = new File( root, "reports-${surefire.forkNumber}" );
File pathWithPlaceholder = new File( subDir, "subfolder" );
File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
assertThat( changed.getPath() )
.isEqualTo( new File( new File( root, "reports-5" ), "subfolder" ).getPath() );
}
@Test
public void shouldBeThreeDumpFiles()
{
String[] dumps = SurefireHelper.getDumpFilesToPrint();
assertThat( dumps ).hasSize( 4 );
assertThat( dumps ).doesNotHaveDuplicates();
List<String> onlyStrings = new ArrayList<>();
addAll( onlyStrings, dumps );
onlyStrings.removeAll( singleton( (String) null ) );
assertThat( onlyStrings ).hasSize( 4 );
}
@Test
public void shouldCloneDumpFiles()
{
String[] dumps1 = SurefireHelper.getDumpFilesToPrint();
String[] dumps2 = SurefireHelper.getDumpFilesToPrint();
assertThat( dumps1 ).isNotSameAs( dumps2 );
}
@Test
public void testConstants()
{
assertThat( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER )
.isEqualTo( SurefireHelper.DUMP_FILE_PREFIX + "%d.dumpstream" );
assertThat( String.format( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER, 5 ) )
.endsWith( "-jvmRun5.dumpstream" );
}
@Test
public void shouldEscapeWindowsPath()
{
assumeTrue( IS_OS_WINDOWS );
String root = "X:\\path\\to\\project\\";
String pathToJar = "target\\surefire\\surefirebooter4942721306300108667.jar";
@SuppressWarnings( "checkstyle:magicnumber" )
int projectNameLength = 247 - root.length() - pathToJar.length();
StringBuilder projectFolder = new StringBuilder();
for ( int i = 0; i < projectNameLength; i++ )
{
projectFolder.append( 'x' );
}
String path = root + projectFolder + "\\" + pathToJar;
String escaped = escapeToPlatformPath( path );
assertThat( escaped ).isEqualTo( "\\\\?\\" + path );
path = root + "\\" + pathToJar;
escaped = escapeToPlatformPath( path );
assertThat( escaped ).isEqualTo( root + "\\" + pathToJar );
}
@Test
public void shouldHandleFailIfNoTests() throws Exception
{
RunResult summary = new RunResult( 0, 0, 0, 0 );
Mojo plugin = new Mojo();
plugin.setFailIfNoTests( true );
e.expect( MojoFailureException.class );
e.expectMessage( "No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)" );
reportExecution( plugin, summary, null, null );
}
@Test
public void shouldHandleTestFailure() throws Exception
{
RunResult summary = new RunResult( 1, 0, 1, 0 );
e.expect( MojoFailureException.class );
e.expectMessage( "There are test failures.\n\nPlease refer to null "
+ "for the individual test results.\nPlease refer to dump files (if any exist) "
+ "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream." );
reportExecution( new Mojo(), summary, null, null );
}
@Test
public void failsIfThereAreTooManyFlakes() throws Exception
{
RunResult summary = new RunResult( 1, 0, 0, 0, 1 );
Mojo reportParameters = new Mojo();
reportParameters.setFailOnFlakeCount( 1 );
e.expect( MojoFailureException.class );
e.expectMessage( "There is 1 flake and failOnFlakeCount is set to 1.\n\nPlease refer to null "
+ "for the individual test results.\nPlease refer to dump files (if any exist) "
+ "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream." );
reportExecution( reportParameters, summary, null, null );
}
@Test
public void reportsFailuresAndFlakes() throws Exception
{
RunResult summary = new RunResult( 1, 0, 1, 0, 2 );
Mojo reportParameters = new Mojo();
reportParameters.setFailOnFlakeCount( 1 );
e.expect( MojoFailureException.class );
e.expectMessage( "There are test failures.\nThere are 2 flakes and failOnFlakeCount is set to 1."
+ "\n\nPlease refer to null "
+ "for the individual test results.\nPlease refer to dump files (if any exist) "
+ "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream." );
reportExecution( reportParameters, summary, null, null );
}
@Test
public void passesIfFlakesAreWithinThreshold() throws Exception
{
RunResult summary = new RunResult( 1, 0, 0, 0 , 1 );
reportExecution( new Mojo(), summary, null, null );
}
}