1 package org.apache.maven.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43 public void testFileNameWithoutSuffix() throws IOException
44 {
45 File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp1" );
46
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
64 expectedReportFile.delete();
65 }
66
67
68
69
70 public void testFileNameWithSuffix() throws IOException
71 {
72 File reportDir = new File( new File( System.getProperty( "user.dir" ), "target" ), "tmp2" );
73
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
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
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
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
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
160 expectedReportFile.delete();
161 }
162 }