View Javadoc
1   package org.apache.maven.surefire.report;
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.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.concurrent.Callable;
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  
29  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
30  
31  import junit.framework.TestCase;
32  import org.apache.maven.surefire.api.report.SimpleReportEntry;
33  import org.apache.maven.surefire.api.report.TestSetReportEntry;
34  import org.apache.maven.surefire.shared.utils.io.FileUtils;
35  
36  import static java.nio.charset.StandardCharsets.US_ASCII;
37  import static org.fest.assertions.Assertions.assertThat;
38  
39  /**
40   *
41   */
42  public class ConsoleOutputFileReporterTest
43      extends TestCase
44  {
45      /*
46       * Test method for 'org.codehaus.surefire.report.ConsoleOutputFileReporter.testSetCompleted(ReportEntry report)'
47       */
48      public void testFileNameWithoutSuffix() throws IOException
49      {
50          File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp1" );
51          //noinspection ResultOfMethodCallIgnored
52          reportDir.mkdirs();
53          TestSetReportEntry reportEntry =
54                  new SimpleReportEntry( getClass().getName(), null, getClass().getName(), null );
55          ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter( reportDir, null, false, null, "UTF-8" );
56          reporter.testSetStarting( reportEntry );
57          reporter.writeTestOutput( "some ", false, true );
58          reporter.testSetCompleted( reportEntry );
59          reporter.close();
60  
61          File expectedReportFile = new File( reportDir, getClass().getName() + "-output.txt" );
62  
63          assertTrue( "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
64                      expectedReportFile.exists() );
65  
66          assertThat( FileUtils.fileRead( expectedReportFile, US_ASCII.name() ) )
67                  .contains( "some " );
68  
69          //noinspection ResultOfMethodCallIgnored
70          expectedReportFile.delete();
71      }
72  
73      /*
74       * Test method for 'org.codehaus.surefire.report.ConsoleOutputFileReporter.testSetCompleted(ReportEntry report)'
75       */
76      public void testFileNameWithSuffix() throws IOException
77      {
78          File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp2" );
79          String suffixText = "sampleSuffixText";
80          TestSetReportEntry reportEntry =
81                  new SimpleReportEntry( getClass().getName(), null, getClass().getName(), null );
82          ConsoleOutputFileReporter reporter =
83                  new ConsoleOutputFileReporter( reportDir, suffixText, false, null, "UTF-8" );
84          reporter.testSetStarting( reportEntry );
85          reporter.writeTestOutput( null, true, true );
86          reporter.writeTestOutput( "some ", true, true );
87          reporter.testSetCompleted( reportEntry );
88          reporter.close();
89  
90          File expectedReportFile = new File( reportDir, getClass().getName() + "-" + suffixText + "-output.txt" );
91  
92          assertTrue( "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
93                      expectedReportFile.exists() );
94  
95          assertThat( FileUtils.fileRead( expectedReportFile, US_ASCII.name() ) )
96                  .contains( "some " );
97  
98          assertThat( expectedReportFile )
99                  .hasSize( 9 + 2 * System.lineSeparator().length() );
100 
101         //noinspection ResultOfMethodCallIgnored
102         expectedReportFile.delete();
103     }
104 
105     public void testNullReportFile() throws IOException
106     {
107         File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp3" );
108         ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter( reportDir, null, false, null, "UTF-8" );
109         reporter.writeTestOutput( "some text", false, true );
110         reporter.testSetCompleted( new SimpleReportEntry( getClass().getName(), null, getClass().getName(), null ) );
111         reporter.close();
112 
113         File expectedReportFile = new File( reportDir, "null-output.txt" );
114 
115         assertTrue( "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
116                 expectedReportFile.exists() );
117 
118         assertThat( FileUtils.fileRead( expectedReportFile, US_ASCII.name() ) )
119                 .contains( "some " );
120 
121         //noinspection ResultOfMethodCallIgnored
122         expectedReportFile.delete();
123     }
124 
125     public void testConcurrentAccessReportFile() throws Exception
126     {
127         File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp4" );
128         final ConsoleOutputFileReporter reporter =
129                 new ConsoleOutputFileReporter( reportDir, null, false, null, "UTF-8" );
130         reporter.testSetStarting( new SimpleReportEntry( getClass().getName(), null, getClass().getName(), null ) );
131         ExecutorService scheduler = Executors.newFixedThreadPool( 10 );
132         final ArrayList<Callable<Void>> jobs = new ArrayList<>();
133         for ( int i = 0; i < 10; i++ )
134         {
135             jobs.add( new Callable<Void>()
136             {
137                 @Override
138                 public Void call()
139                 {
140                     reporter.writeTestOutput( "some text\n", false, true );
141                     return null;
142                 }
143             } );
144         }
145         scheduler.invokeAll( jobs );
146         scheduler.shutdown();
147         reporter.close();
148 
149         File expectedReportFile = new File( reportDir, getClass().getName() + "-output.txt" );
150 
151         assertTrue( "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
152                 expectedReportFile.exists() );
153 
154         assertThat( FileUtils.fileRead( expectedReportFile, US_ASCII.name() ) )
155                 .contains( "some text" );
156 
157         StringBuilder expectedText = new StringBuilder();
158         for ( int i = 0; i < 10; i++ )
159         {
160             expectedText.append( "some text\n" );
161         }
162 
163         assertThat( FileUtils.fileRead( expectedReportFile, US_ASCII.name() ) )
164                 .isEqualTo( expectedText.toString() );
165 
166         //noinspection ResultOfMethodCallIgnored
167         expectedReportFile.delete();
168     }
169 }