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