1 package org.apache.maven.plugins.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.io.UnsupportedEncodingException;
25 import java.io.Writer;
26 import java.net.URL;
27 import java.net.URLDecoder;
28 import java.util.Locale;
29 import org.apache.maven.doxia.site.decoration.DecorationModel;
30 import org.apache.maven.doxia.siterenderer.RendererException;
31 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
32 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34 import org.apache.maven.shared.utils.WriterFactory;
35 import org.apache.maven.shared.utils.io.FileUtils;
36 import org.apache.maven.shared.utils.io.IOUtil;
37
38
39
40
41 public class SurefireReportMojoTest
42 extends AbstractMojoTestCase
43 {
44 public void testBasicSurefireReport()
45 throws Exception
46 {
47 File testPom = new File( getUnitBaseDir(), "basic-surefire-report-test/plugin-config.xml" );
48
49 SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
50
51 assertNotNull( mojo );
52
53 File outputDir = (File) getVariableValueFromObject( mojo, "outputDirectory" );
54
55 boolean showSuccess = ( (Boolean) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
56
57 File reportsDir = (File) getVariableValueFromObject( mojo, "reportsDirectory" );
58
59 String outputName = (String) getVariableValueFromObject( mojo, "outputName" );
60
61 File xrefLocation = (File) getVariableValueFromObject( mojo, "xrefLocation" );
62
63 boolean linkXRef = ( (Boolean) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
64
65 assertEquals( new File( getBasedir() + "/target/site/unit/basic-surefire-report-test" ), outputDir );
66
67 assertTrue( showSuccess );
68
69 assertEquals( new File(
70 getBasedir() + "/src/test/resources/unit/basic-surefire-report-test/surefire-reports" ).getAbsolutePath(),
71 reportsDir.getAbsolutePath() );
72
73 assertEquals( "surefire-report", outputName );
74 assertEquals(
75 new File( getBasedir() + "/target/site/unit/basic-surefire-report-test/xref-test" ).getAbsolutePath(),
76 xrefLocation.getAbsolutePath() );
77
78 assertTrue( linkXRef );
79
80 mojo.execute();
81
82 File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-test/surefire-report.html" );
83
84 renderer( mojo, report );
85
86 assertTrue( report.exists() );
87
88 String htmlContent = FileUtils.fileRead( report );
89
90 int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
91
92 assertTrue( idx >= 0 );
93 }
94
95 private File getUnitBaseDir()
96 throws UnsupportedEncodingException
97 {
98 URL resource = getClass().getResource( "/unit" );
99
100 return new File( URLDecoder.decode( resource.getPath(), "UTF-8" ) ).getAbsoluteFile();
101 }
102
103 public void testBasicSurefireReportIfShowSuccessIsFalse()
104 throws Exception
105 {
106 File testPom = new File( getUnitBaseDir(), "basic-surefire-report-success-false/plugin-config.xml" );
107
108 SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
109
110 assertNotNull( mojo );
111
112 boolean showSuccess = ( (Boolean) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
113
114 assertFalse( showSuccess );
115
116 mojo.execute();
117
118 File report =
119 new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
120
121 renderer( mojo, report );
122
123 assertTrue( report.exists() );
124
125 String htmlContent = FileUtils.fileRead( report );
126
127 int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
128
129 assertTrue( idx < 0 );
130 }
131
132 public void testBasicSurefireReportIfLinkXrefIsFalse()
133 throws Exception
134 {
135 File testPom = new File( getUnitBaseDir(), "basic-surefire-report-linkxref-false/plugin-config.xml" );
136
137 SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
138
139 assertNotNull( mojo );
140
141 boolean linkXRef = ( (Boolean) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
142
143 assertFalse( linkXRef );
144
145 mojo.execute();
146
147 File report =
148 new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
149
150 renderer( mojo, report );
151
152 assertTrue( report.exists() );
153
154 String htmlContent = FileUtils.fileRead( report );
155
156 int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
157
158 assertTrue( idx == -1 );
159 }
160
161 public void testBasicSurefireReportIfReportingIsNull()
162 throws Exception
163 {
164 File testPom = new File( getUnitBaseDir(), "basic-surefire-report-reporting-null/plugin-config.xml" );
165
166 SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
167
168 assertNotNull( mojo );
169
170 mojo.execute();
171
172 File report =
173 new File( getBasedir(), "target/site/unit/basic-surefire-report-reporting-null/surefire-report.html" );
174
175 renderer( mojo, report );
176
177 assertTrue( report.exists() );
178
179 String htmlContent = FileUtils.fileRead( report );
180
181 int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
182
183 assertTrue( idx < 0 );
184 }
185
186
187
188
189
190
191
192
193
194 private void renderer( SurefireReportMojo mojo, File outputHtml )
195 throws RendererException, IOException
196 {
197 Writer writer = null;
198 SiteRenderingContext context = new SiteRenderingContext();
199 context.setDecoration( new DecorationModel() );
200 context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
201 context.setLocale( Locale.ENGLISH );
202
203 try
204 {
205 outputHtml.getParentFile().mkdirs();
206 writer = WriterFactory.newXmlWriter( outputHtml );
207
208 mojo.getSiteRenderer().generateDocument( writer, (SiteRendererSink) mojo.getSink(), context );
209 }
210 finally
211 {
212 IOUtil.close( writer );
213 }
214 }
215 }