View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.its;
20  
21  import java.util.ArrayList;
22  
23  import org.apache.maven.surefire.its.fixture.OutputValidator;
24  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
26  import org.apache.maven.surefire.its.fixture.TestFile;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.junit.runners.Parameterized;
30  import org.junit.runners.Parameterized.Parameter;
31  import org.junit.runners.Parameterized.Parameters;
32  
33  import static java.nio.charset.StandardCharsets.UTF_8;
34  import static org.hamcrest.Matchers.containsString;
35  import static org.hamcrest.Matchers.equalTo;
36  
37  /**
38   * Basic suite test using all known versions of JUnit 4.x
39   *
40   * @author Kristian Rosenvold
41   */
42  @RunWith(Parameterized.class)
43  public class ConsoleOutputIT extends SurefireJUnit4IntegrationTestCase {
44      private static final String LEGACY_FORK_NODE = "org.apache.maven.plugin.surefire.extensions.LegacyForkNodeFactory";
45  
46      private static final String SUREFIRE_FORK_NODE =
47              "org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory";
48  
49      @Parameters
50      public static Iterable<Object[]> data() {
51          ArrayList<Object[]> args = new ArrayList<>();
52          args.add(new Object[] {"tcp"});
53          args.add(new Object[] {null});
54          return args;
55      }
56  
57      @Parameter
58      @SuppressWarnings("checkstyle:visibilitymodifier")
59      public String profileId;
60  
61      @Test
62      public void properNewlinesAndEncodingWithDefaultEncodings() throws Exception {
63          OutputValidator outputValidator = unpack().forkOnce().executeTest();
64          validate(outputValidator, true);
65      }
66  
67      @Test
68      public void properNewlinesAndEncodingWithDifferentEncoding() throws Exception {
69          OutputValidator outputValidator =
70                  unpack().forkOnce().argLine("-Dfile.encoding=UTF-16").executeTest();
71          validate(outputValidator, true);
72      }
73  
74      @Test
75      public void properNewlinesAndEncodingWithoutFork() throws Exception {
76          OutputValidator outputValidator = unpack().forkNever().executeTest();
77          validate(outputValidator, false);
78      }
79  
80      private SurefireLauncher unpack() {
81          SurefireLauncher launcher = unpack("/consoleOutput", profileId == null ? "" : "-" + profileId)
82                  .debugLogging();
83  
84          if (profileId != null) {
85              launcher.activateProfile(profileId);
86          }
87  
88          return launcher;
89      }
90  
91      private void validate(final OutputValidator outputValidator, boolean canFork) throws Exception {
92          TestFile xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-consoleOutput.Test1.xml");
93          xmlReportFile.assertContainsText("SoutLine");
94          xmlReportFile.assertContainsText("äöüß");
95          xmlReportFile.assertContainsText("failing with ü");
96  
97          TestFile outputFile = outputValidator.getSurefireReportsFile("consoleOutput.Test1-output.txt", UTF_8);
98          outputFile.assertContainsText("SoutAgain");
99          outputFile.assertContainsText("SoutLine");
100         outputFile.assertContainsText("äöüß");
101 
102         String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
103 
104         if (canFork) {
105             outputValidator.assertThatLogLine(
106                     containsString("Found implementation of fork node factory: " + cls), equalTo(1));
107         }
108     }
109 
110     @Test
111     public void largerSoutThanMemory() throws Exception {
112         SurefireLauncher launcher = unpackNoisy().setMavenOpts("-Xmx64m").sysProp("thousand", "32000");
113 
114         String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
115 
116         launcher.executeTest()
117                 .verifyErrorFreeLog()
118                 .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
119     }
120 
121     private SurefireLauncher unpackNoisy() {
122         SurefireLauncher launcher = unpack("consoleoutput-noisy", profileId == null ? "" : "-" + profileId)
123                 .debugLogging();
124 
125         if (profileId != null) {
126             launcher.activateProfile(profileId);
127         }
128 
129         return launcher;
130     }
131 }