View Javadoc
1   package org.apache.maven.surefire.its;
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 java.nio.charset.Charset;
23  
24  import org.apache.maven.surefire.its.fixture.OutputValidator;
25  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
26  import org.apache.maven.surefire.its.fixture.TestFile;
27  import org.junit.Test;
28  
29  /**
30   * Basic suite test using all known versions of JUnit 4.x
31   *
32   * @author Kristian Rosenvold
33   */
34  public class ConsoleOutputIT
35      extends SurefireJUnit4IntegrationTestCase
36  {
37      @Test
38      public void properNewlinesAndEncodingWithDefaultEncodings()
39      {
40          final OutputValidator outputValidator =
41              unpack( "/consoleOutput" ).forkOnce().executeTest();
42  
43          validate( outputValidator, true );
44      }
45  
46      @Test
47      public void properNewlinesAndEncodingWithDifferentEncoding()
48      {
49          final OutputValidator outputValidator =
50              unpack( "/consoleOutput" ).forkOnce().argLine( "-Dfile.encoding=UTF-16" ).executeTest();
51  
52          validate( outputValidator, true );
53      }
54  
55      @Test
56      public void properNewlinesAndEncodingWithoutFork()
57      {
58          final OutputValidator outputValidator =
59              unpack( "/consoleOutput" ).forkNever().executeTest();
60  
61          validate( outputValidator, false );
62      }
63  
64      private void validate( final OutputValidator outputValidator, boolean includeShutdownHook )
65      {
66          TestFile xmlReportFile = outputValidator.getSurefireReportsXmlFile( "TEST-consoleOutput.Test1.xml" );
67          xmlReportFile.assertContainsText( "SoutLine" );
68          xmlReportFile.assertContainsText( normalizeToDefaultCharset( "äöüß" ) );
69          xmlReportFile.assertContainsText( normalizeToDefaultCharset( "failing with ü" ) );
70  
71          TestFile outputFile = outputValidator.getSurefireReportsFile( "consoleOutput.Test1-output.txt" );
72          outputFile.assertContainsText( "SoutAgain" );
73          outputFile.assertContainsText( "SoutLine" );
74          outputFile.assertContainsText( normalizeToDefaultCharset( "äöüß" ) );
75  
76          if ( includeShutdownHook )
77          {
78              outputFile.assertContainsText( "Printline in shutdown hook" );
79          }
80      }
81  
82      /**
83       * @param string the string to normalize
84       * @return the string with all characters not available in the current charset being replaced, e.g. for US-ASCII,
85       *         German umlauts would be replaced to ?
86       */
87      private String normalizeToDefaultCharset( String string )
88      {
89          Charset cs = Charset.defaultCharset();
90          if ( cs.canEncode() )
91          {
92              string = cs.decode( cs.encode( string ) ).toString();
93          }
94  
95          return string;
96      }
97  
98      @Test
99      public void largerSoutThanMemory()
100     {
101         unpack( "consoleoutput-noisy" )
102                 .setMavenOpts( "-Xmx64m" )
103                 .sysProp( "thousand", "32000" )
104                 .executeTest();
105     }
106 }