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  import org.junit.runner.RunWith;
24  import org.mockito.Mock;
25  import org.mockito.runners.MockitoJUnitRunner;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.nio.file.Path;
30  
31  import static org.apache.commons.lang3.JavaVersion.JAVA_1_7;
32  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
33  import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast;
34  import static org.fest.assertions.Assertions.assertThat;
35  import static org.junit.Assume.assumeTrue;
36  import static org.mockito.Mockito.when;
37  
38  /**
39   * Test for {@link AbstractSurefireMojo}.
40   */
41  @RunWith( MockitoJUnitRunner.class )
42  public class AbstractSurefireMojoTest
43  {
44      @Mock
45      private AbstractSurefireMojo mojo;
46  
47      @Test
48      public void shouldHaveTmpDirectory() throws IOException
49      {
50          assumeTrue( isJavaVersionAtLeast( JAVA_1_7 ) );
51  
52          Path path = (Path) AbstractSurefireMojo.createTmpDirectoryWithJava7( "surefire" );
53  
54          assertThat( path )
55                  .isNotNull();
56  
57          assertThat( path.startsWith( System.getProperty( "java.io.tmpdir" ) ) )
58                  .isTrue();
59  
60          String dir = path.getName( path.getNameCount() - 1 ).toString();
61  
62          assertThat( dir )
63                  .startsWith( "surefire" );
64  
65          assertThat( dir )
66                  .matches( "^surefire[\\d]+$" );
67      }
68  
69      @Test
70      public void shouldHaveTmpDirectoryName() throws IOException
71      {
72          assumeTrue( isJavaVersionAtLeast( JAVA_1_7 ) );
73  
74          String dir = AbstractSurefireMojo.createTmpDirectoryNameWithJava7( "surefire" );
75  
76          assertThat( dir )
77                  .isNotNull();
78  
79          assertThat( dir )
80                  .startsWith( "surefire" );
81  
82          assertThat( dir )
83                  .matches( "^surefire[\\d]+$" );
84      }
85  
86      @Test
87      public void shouldExistTmpDirectory()
88      {
89          when( mojo.getTempDir() ).thenReturn( "surefireX" );
90          when( mojo.getProjectBuildDirectory() ).thenReturn( new File( System.getProperty( "user.dir" ), "target" ) );
91          when( mojo.createSurefireBootDirectoryInTemp() ).thenCallRealMethod();
92          when( mojo.createSurefireBootDirectoryInBuild() ).thenCallRealMethod();
93          when( mojo.getSurefireTempDir() ).thenCallRealMethod();
94  
95          File tmp = mojo.createSurefireBootDirectoryInTemp();
96          assertThat( tmp ).isNotNull();
97          assertThat( tmp ).exists();
98          assertThat( tmp.getAbsolutePath() )
99                  .startsWith( System.getProperty( "java.io.tmpdir" ) );
100         assertThat( tmp.getName() )
101                 .startsWith( "surefireX" );
102 
103         tmp = mojo.createSurefireBootDirectoryInBuild();
104         assertThat( tmp ).isNotNull();
105         assertThat( tmp ).exists();
106         assertThat( tmp.getAbsolutePath() ).startsWith( System.getProperty( "user.dir" ) );
107         assertThat( tmp.getName() ).isEqualTo( "surefireX" );
108 
109         tmp = mojo.getSurefireTempDir();
110         assertThat( tmp ).isNotNull();
111         assertThat( tmp ).exists();
112         assertThat( tmp.getAbsolutePath() )
113               .startsWith( IS_OS_WINDOWS ? System.getProperty( "java.io.tmpdir" ) : System.getProperty( "user.dir" ) );
114         if ( IS_OS_WINDOWS )
115         {
116             assertThat( tmp.getName() )
117                     .startsWith( "surefireX" );
118         }
119         else
120         {
121             assertThat( tmp.getName() )
122                     .isEqualTo( "surefireX" );
123         }
124     }
125 }