View Javadoc
1   package org.apache.maven.surefire.its.jiras;
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.surefire.its.fixture.OutputValidator;
23  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
24  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.Parameterized;
28  import org.junit.runners.Parameterized.Parameter;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  import java.util.Iterator;
32  
33  import static java.util.Arrays.asList;
34  import static java.util.concurrent.TimeUnit.SECONDS;
35  import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
36  import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC_OSX;
37  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_7;
38  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8;
39  import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_10;
40  import static org.apache.maven.surefire.its.jiras.Surefire1295AttributeJvmCrashesToTestsIT.ForkMode.DEFAULT;
41  import static org.apache.maven.surefire.its.jiras.Surefire1295AttributeJvmCrashesToTestsIT.ForkMode.ONE_FORK_NO_REUSE;
42  import static org.apache.maven.surefire.its.jiras.Surefire1295AttributeJvmCrashesToTestsIT.ForkMode.ONE_FORK_REUSE;
43  import static org.fest.assertions.Assertions.assertThat;
44  import static org.junit.Assert.fail;
45  import static org.junit.Assume.assumeTrue;
46  
47  /**
48   * https://issues.apache.org/jira/browse/SUREFIRE-1295
49   * https://github.com/apache/maven-surefire/pull/136
50   *
51   * @author michaeltandy
52   * @since 2.20
53   */
54  @RunWith( Parameterized.class )
55  public class Surefire1295AttributeJvmCrashesToTestsIT
56          extends SurefireJUnit4IntegrationTestCase
57  {
58      public enum ForkMode
59      {
60          DEFAULT,
61          ONE_FORK_NO_REUSE,
62          ONE_FORK_REUSE
63      }
64  
65      @Parameters
66      public static Iterable<Object[]> parameters()
67      {
68          return asList(new Object[][] {
69  //                exit() does not stop all Threads immediately,
70  //                see https://github.com/michaeltandy/crashjvm/issues/1
71                  { "exit", DEFAULT },
72                  { "exit", ONE_FORK_NO_REUSE },
73                  { "exit", ONE_FORK_REUSE },
74                  { "abort", DEFAULT },
75                  { "abort", ONE_FORK_NO_REUSE },
76                  { "abort", ONE_FORK_REUSE },
77                  { "segfault", DEFAULT },
78                  { "segfault", ONE_FORK_NO_REUSE },
79                  { "segfault", ONE_FORK_REUSE }
80          });
81      }
82  
83      @Parameter( 0 )
84      public static String crashStyle;
85  
86      @Parameter( 1 )
87      public static ForkMode forkStyle;
88  
89      @Test
90      public void test()
91              throws Exception
92      {
93          assumeTrue( IS_OS_LINUX || IS_OS_MAC_OSX || IS_OS_WINDOWS_7 || IS_OS_WINDOWS_8 || IS_OS_WINDOWS_10 );
94  
95          SurefireLauncher launcher =
96                  unpack( "crash-during-test", "_" + crashStyle + "_" + forkStyle.ordinal() )
97                  .setForkJvm();
98  
99          switch ( forkStyle )
100         {
101             case DEFAULT:
102                 break;
103             case ONE_FORK_NO_REUSE:
104                 launcher.forkCount( 1 )
105                         .reuseForks( false );
106                 break;
107             case ONE_FORK_REUSE:
108                 launcher.forkPerThread()
109                         .reuseForks( true )
110                         .threadCount( 1 );
111                 break;
112             default:
113                 fail();
114         }
115 
116         checkCrash( launcher.addGoal( "-DcrashType=" + crashStyle ) );
117     }
118 
119     private static void checkCrash( SurefireLauncher launcher ) throws Exception
120     {
121         OutputValidator validator = launcher.maven()
122                 .withFailure()
123                 .executeTest()
124                 .verifyTextInLog( "The forked VM terminated without properly saying "
125                         + "goodbye. VM crash or System.exit called?" )
126                 .verifyTextInLog( "Crashed tests:" );
127 
128         // Cannot flush log.txt stream because it is consumed internally by Verifier.
129         // Waiting for the stream to become flushed on disk.
130         SECONDS.sleep( 1L );
131 
132         for ( Iterator<String> it = validator.loadLogLines().iterator(); it.hasNext(); )
133         {
134             String line = it.next();
135             if ( line.contains( "Crashed tests:" ) )
136             {
137                 line = it.next();
138                 if ( it.hasNext() )
139                 {
140                     assertThat( line )
141                             .contains( "junit44.environment.Test1CrashedTest" );
142                 }
143                 else
144                 {
145                     fail( "Could not find any line after 'Crashed tests:'." );
146                 }
147             }
148         }
149     }
150 }