View Javadoc
1   package org.apache.maven.surefire.booter;
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.Rule;
23  import org.junit.Test;
24  import org.junit.rules.ExpectedException;
25  
26  import java.io.File;
27  import java.lang.management.ManagementFactory;
28  import java.util.regex.Matcher;
29  
30  import static org.apache.commons.lang3.SystemUtils.IS_OS_UNIX;
31  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
32  import static org.fest.assertions.Assertions.assertThat;
33  import static org.hamcrest.CoreMatchers.is;
34  import static org.hamcrest.CoreMatchers.not;
35  import static org.hamcrest.CoreMatchers.notNullValue;
36  import static org.junit.Assert.fail;
37  import static org.junit.Assume.assumeThat;
38  import static org.junit.Assume.assumeTrue;
39  import static org.powermock.reflect.Whitebox.invokeMethod;
40  
41  /**
42   * Testing {@link PpidChecker} on a platform.
43   *
44   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
45   * @since 2.20.1
46   */
47  public class PpidCheckerTest
48  {
49      @Rule
50      public final ExpectedException exceptions = ExpectedException.none();
51  
52      @Test
53      public void shouldHavePpidAsWindows()
54      {
55          assumeTrue( IS_OS_WINDOWS );
56  
57          long expectedPid = Long.parseLong( ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim() );
58  
59          PpidChecker checker = new PpidChecker( expectedPid );
60          ProcessInfo processInfo = checker.windows();
61  
62          assertThat( processInfo )
63                  .isNotNull();
64  
65          assertThat( checker.canUse() )
66                  .isTrue();
67  
68          assertThat( checker.isProcessAlive() )
69                  .isTrue();
70  
71          assertThat( processInfo.getPID() )
72                  .isEqualTo( expectedPid );
73  
74          assertThat( processInfo.getTime() )
75                  .isNotNull();
76      }
77  
78      @Test
79      public void shouldHavePpidAsUnix()
80      {
81          assumeTrue( IS_OS_UNIX );
82  
83          assertThat( PpidChecker.canExecuteUnixPs() )
84                  .as( "Surefire should be tested on real box OS, e.g. Ubuntu or FreeBSD." )
85                  .isTrue();
86  
87          long expectedPid = Long.parseLong( ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim() );
88  
89          PpidChecker checker = new PpidChecker( expectedPid );
90          ProcessInfo processInfo = checker.unix();
91  
92          assertThat( processInfo )
93                  .isNotNull();
94  
95          assertThat( checker.canUse() )
96                  .isTrue();
97  
98          assertThat( checker.isProcessAlive() )
99                  .isTrue();
100 
101         assertThat( processInfo.getPID() )
102                 .isEqualTo( expectedPid );
103 
104         assertThat( processInfo.getTime() )
105                 .isNotNull();
106     }
107 
108     @Test
109     public void shouldNotFindSuchPID()
110     {
111         long ppid = 1000000L;
112 
113         PpidChecker checker = new PpidChecker( ppid );
114 
115         assertThat( checker.canUse() )
116                 .isTrue();
117 
118         exceptions.expect( IllegalStateException.class );
119         exceptions.expectMessage( "Cannot use PPID " + ppid + " process information. Going to use NOOP events." );
120 
121         checker.isProcessAlive();
122 
123         fail( "this test should throw exception" );
124     }
125 
126     @Test
127     public void shouldParseEtime()
128     {
129         Matcher m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "38" );
130         assertThat( m.matches() )
131                 .isFalse();
132 
133         m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "05:38" );
134         assertThat( m.matches() )
135                 .isTrue();
136         assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
137         assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 0L );
138         assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
139         assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
140 
141         m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "00:05:38" );
142         assertThat( m.matches() )
143                 .isTrue();
144         assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
145         assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 0L );
146         assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
147         assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
148 
149         m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "01:05:38" );
150         assertThat( m.matches() )
151                 .isTrue();
152         assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
153         assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 3600L );
154         assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
155         assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
156 
157         m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "02-01:05:38" );
158         assertThat( m.matches() )
159                 .isTrue();
160         assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 2 * 24 * 3600L );
161         assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 3600L );
162         assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
163         assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
164 
165         m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "02-1:5:3" );
166         assertThat( m.matches() )
167                 .isTrue();
168         assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 2 * 24 * 3600L );
169         assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 3600L );
170         assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
171         assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 3L );
172     }
173 
174     @Test
175     public void shouldHaveSystemPathToWmicOnWindows() throws Exception
176     {
177         assumeTrue( IS_OS_WINDOWS );
178         assumeThat( System.getenv( "SystemRoot" ), is( notNullValue() ) );
179         assumeThat( System.getenv( "SystemRoot" ), is( not( "" ) ) );
180         assumeTrue( new File( System.getenv( "SystemRoot" ), "System32\\Wbem" ).isDirectory() );
181         assumeTrue( new File( System.getenv( "SystemRoot" ), "System32\\Wbem\\wmic.exe" ).isFile() );
182         assertThat( (Boolean) invokeMethod( PpidChecker.class, "hasWmicStandardSystemPath" ) ).isTrue();
183         assertThat( new File( System.getenv( "SystemRoot" ), "System32\\Wbem\\wmic.exe" ) ).isFile();
184     }
185 }