View Javadoc
1   package org.apache.maven.plugin.surefire;
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 org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugin.surefire.AbstractSurefireMojoTest.Mojo;
24  import org.apache.maven.surefire.api.suite.RunResult;
25  import org.junit.Test;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import static java.util.Collections.addAll;
32  import static java.util.Collections.singleton;
33  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
34  import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath;
35  import static org.apache.maven.plugin.surefire.SurefireHelper.reportExecution;
36  import static org.fest.assertions.Assertions.assertThat;
37  import static org.junit.Assert.fail;
38  import static org.junit.Assume.assumeTrue;
39  
40  /**
41   * Test of {@link SurefireHelper}.
42   */
43  public class SurefireHelperTest
44  {
45      @Test
46      public void shouldReplaceForkNumberPath()
47      {
48          File root = new File( System.getProperty( "user.dir", "" ) );
49          File pathWithPlaceholder = new File( root, "${surefire.forkNumber}" );
50          File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
51          assertThat( changed.getPath() )
52                  .isEqualTo( new File( root, "5" ).getPath() );
53      }
54  
55      @Test
56      public void shouldReplaceLongForkNumberPath()
57      {
58          File root = new File( System.getProperty( "user.dir", "" ) );
59          File subDir = new File( root, "reports-${surefire.forkNumber}" );
60          File pathWithPlaceholder = new File( subDir, "subfolder" );
61          File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
62          assertThat( changed.getPath() )
63                  .isEqualTo( new File( new File( root, "reports-5" ), "subfolder" ).getPath() );
64      }
65  
66      @Test
67      public void shouldBeThreeDumpFiles()
68      {
69          String[] dumps = SurefireHelper.getDumpFilesToPrint();
70          assertThat( dumps ).hasSize( 4 );
71          assertThat( dumps ).doesNotHaveDuplicates();
72          List<String> onlyStrings = new ArrayList<>();
73          addAll( onlyStrings, dumps );
74          onlyStrings.removeAll( singleton( (String) null ) );
75          assertThat( onlyStrings ).hasSize( 4 );
76      }
77  
78      @Test
79      public void shouldCloneDumpFiles()
80      {
81          String[] dumps1 = SurefireHelper.getDumpFilesToPrint();
82          String[] dumps2 = SurefireHelper.getDumpFilesToPrint();
83          assertThat( dumps1 ).isNotSameAs( dumps2 );
84      }
85  
86      @Test
87      public void testConstants()
88      {
89          assertThat( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER )
90                  .isEqualTo( SurefireHelper.DUMP_FILE_PREFIX + "%d.dumpstream" );
91  
92          assertThat( String.format( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER, 5 ) )
93                  .endsWith( "-jvmRun5.dumpstream" );
94      }
95  
96      @Test
97      public void shouldEscapeWindowsPath()
98      {
99          assumeTrue( IS_OS_WINDOWS );
100         String root = "X:\\path\\to\\project\\";
101         String pathToJar = "target\\surefire\\surefirebooter4942721306300108667.jar";
102         @SuppressWarnings( "checkstyle:magicnumber" )
103         int projectNameLength = 247 - root.length() - pathToJar.length();
104         StringBuilder projectFolder = new StringBuilder();
105         for ( int i = 0; i < projectNameLength; i++ )
106         {
107             projectFolder.append( 'x' );
108         }
109         String path = root + projectFolder + "\\" + pathToJar;
110         String escaped = escapeToPlatformPath( path );
111         assertThat( escaped ).isEqualTo( "\\\\?\\" + path );
112 
113         path = root + "\\" + pathToJar;
114         escaped = escapeToPlatformPath( path );
115         assertThat( escaped ).isEqualTo( root + "\\" + pathToJar );
116     }
117 
118     @Test
119     public void shouldHandleFailIfNoTests() throws Exception
120     {
121         RunResult summary = new RunResult( 0, 0, 0, 0 );
122         try
123         {
124             Mojo plugin = new Mojo();
125             plugin.setFailIfNoTests( true );
126             reportExecution( plugin, summary, null, null );
127         }
128         catch ( MojoFailureException e )
129         {
130             assertThat( e.getLocalizedMessage() )
131                     .isEqualTo( "No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)" );
132             return;
133         }
134         fail( "Expected MojoFailureException with message "
135                 + "'No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)'" );
136     }
137 
138     @Test
139     public void shouldHandleTestFailure() throws Exception
140     {
141         RunResult summary = new RunResult( 1, 0, 1, 0 );
142         try
143         {
144             reportExecution( new Mojo(), summary, null, null );
145         }
146         catch ( MojoFailureException e )
147         {
148             assertThat( e.getLocalizedMessage() )
149                     .isEqualTo( "There are test failures.\n\nPlease refer to null "
150                             + "for the individual test results.\nPlease refer to dump files (if any exist) "
151                             + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream." );
152             return;
153         }
154         fail( "Expected MojoFailureException with message "
155                 + "'There are test failures.\n\nPlease refer to null "
156                 + "for the individual test results.\nPlease refer to dump files (if any exist) "
157                 + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream.'" );
158     }
159 }