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.junit.Test;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import static java.util.Collections.addAll;
29  import static java.util.Collections.singleton;
30  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
31  import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath;
32  import static org.fest.assertions.Assertions.assertThat;
33  import static org.junit.Assume.assumeTrue;
34  
35  /**
36   * Test of {@link SurefireHelper}.
37   */
38  public class SurefireHelperTest
39  {
40      @Test
41      public void shouldReplaceForkNumberPath()
42      {
43          File root = new File( System.getProperty( "user.dir", "" ) );
44          File pathWithPlaceholder = new File( root, "${surefire.forkNumber}" );
45          File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
46          assertThat( changed.getPath() )
47                  .isEqualTo( new File( root, "5" ).getPath() );
48      }
49  
50      @Test
51      public void shouldReplaceLongForkNumberPath()
52      {
53          File root = new File( System.getProperty( "user.dir", "" ) );
54          File subDir = new File( root, "reports-${surefire.forkNumber}" );
55          File pathWithPlaceholder = new File( subDir, "subfolder" );
56          File changed = SurefireHelper.replaceForkThreadsInPath( pathWithPlaceholder, 5 );
57          assertThat( changed.getPath() )
58                  .isEqualTo( new File( new File( root, "reports-5" ), "subfolder" ).getPath() );
59      }
60  
61      @Test
62      public void shouldBeThreeDumpFiles()
63      {
64          String[] dumps = SurefireHelper.getDumpFilesToPrint();
65          assertThat( dumps ).hasSize( 4 );
66          assertThat( dumps ).doesNotHaveDuplicates();
67          List<String> onlyStrings = new ArrayList<>();
68          addAll( onlyStrings, dumps );
69          onlyStrings.removeAll( singleton( (String) null ) );
70          assertThat( onlyStrings ).hasSize( 4 );
71      }
72  
73      @Test
74      public void shouldCloneDumpFiles()
75      {
76          String[] dumps1 = SurefireHelper.getDumpFilesToPrint();
77          String[] dumps2 = SurefireHelper.getDumpFilesToPrint();
78          assertThat( dumps1 ).isNotSameAs( dumps2 );
79      }
80  
81      @Test
82      public void testConstants()
83      {
84          assertThat( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER )
85                  .isEqualTo( SurefireHelper.DUMP_FILE_PREFIX + "%d.dumpstream" );
86  
87          assertThat( String.format( SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER, 5) )
88                  .endsWith( "-jvmRun5.dumpstream" );
89      }
90  
91      @Test
92      public void shouldEscapeWindowsPath()
93      {
94          assumeTrue( IS_OS_WINDOWS );
95          String root = "X:\\path\\to\\project\\";
96          String pathToJar = "target\\surefire\\surefirebooter4942721306300108667.jar";
97          int projectNameLength = 247 - root.length() - pathToJar.length();
98          StringBuilder projectFolder = new StringBuilder();
99          for ( int i = 0; i < projectNameLength; i++ )
100         {
101             projectFolder.append( 'x' );
102         }
103         String path = root + projectFolder + "\\" + pathToJar;
104         String escaped = escapeToPlatformPath( path );
105         assertThat( escaped ).isEqualTo( "\\\\?\\" + path );
106 
107         path = root + "\\" + pathToJar;
108         escaped = escapeToPlatformPath( path );
109         assertThat( escaped ).isEqualTo( root + "\\" + pathToJar );
110     }
111 }