1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
20  import org.codehaus.plexus.util.FileUtils;
21  
22  import java.io.File;
23  
24  /**
25   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
26   */
27  public class SurefireReportMojoTest
28      extends AbstractMojoTestCase
29  {
30      public void setUp()
31          throws Exception
32      {
33          super.setUp();
34      }
35  
36      public void testBasicSurefireReport()
37          throws Exception
38      {
39          File testPom = new File( getBasedir(),
40                                   "target/test-classes/unit/basic-surefire-report-test/plugin-config.xml" );
41  
42          SurefireReportMojo mojo = ( SurefireReportMojo ) lookupMojo( "report", testPom );
43  
44          assertNotNull( mojo );
45  
46          String outputDir = ( String ) getVariableValueFromObject( mojo, "outputDirectory" );
47  
48          boolean showSuccess = ( ( Boolean ) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
49  
50          File reportsDir = ( File ) getVariableValueFromObject( mojo, "reportsDirectory" );
51  
52          String outputName = ( String ) getVariableValueFromObject( mojo, "outputName" );
53  
54          File xrefLocation = ( File ) getVariableValueFromObject( mojo, "xrefLocation" );
55  
56          boolean linkXRef = ( ( Boolean ) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
57  
58          assertEquals( getBasedir() + "/target/site/unit/basic-surefire-report-test", outputDir );
59  
60          assertTrue( showSuccess );
61  
62          assertEquals( new File( getBasedir() + "/src/test/resources/unit/basic-surefire-report-test/surefire-reports" ).getAbsolutePath(), reportsDir.getAbsolutePath() );
63  
64          assertEquals( "surefire-report", outputName );
65  
66          assertEquals( new File( getBasedir() + "/target/site/unit/basic-surefire-report-test/xref-test" ).getAbsolutePath(), xrefLocation.getAbsolutePath() );
67  
68          assertTrue( linkXRef );
69  
70          mojo.execute();
71  
72          File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-test/surefire-report.html" );
73  
74          assertTrue( report.exists() );
75  
76          String htmlContent = FileUtils.fileRead( report );
77  
78          int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
79  
80          assertTrue( idx >= 0 );
81      }
82  
83      public void testBasicSurefireReportIfShowSuccessIsFalse()
84          throws Exception
85      {
86          File testPom = new File( getBasedir(),
87                                   "target/test-classes/unit/basic-surefire-report-success-false/plugin-config.xml" );
88  
89          SurefireReportMojo mojo = ( SurefireReportMojo ) lookupMojo( "report", testPom );
90  
91          assertNotNull( mojo );
92  
93          boolean showSuccess = ( ( Boolean ) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
94  
95          assertFalse( showSuccess );
96  
97          mojo.execute();
98  
99          File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
100 
101         assertTrue( report.exists() );
102 
103         String htmlContent = FileUtils.fileRead( report );
104 
105         int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
106 
107         assertTrue( idx < 0 );
108     }
109 
110     public void testBasicSurefireReportIfLinkXrefIsFalse()
111         throws Exception
112     {
113         File testPom = new File( getBasedir(),
114                                  "target/test-classes/unit/basic-surefire-report-linkxref-false/plugin-config.xml" );
115 
116         SurefireReportMojo mojo = ( SurefireReportMojo ) lookupMojo( "report", testPom );
117 
118         assertNotNull( mojo );
119 
120         boolean linkXRef = ( ( Boolean ) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
121 
122         assertFalse( linkXRef );
123 
124         mojo.execute();
125 
126         File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
127 
128         assertTrue( report.exists() );
129 
130         String htmlContent = FileUtils.fileRead( report );
131 
132         int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
133 
134         assertTrue( idx >= 0);
135     }
136 
137     public void testBasicSurefireReportIfReportingIsNull()
138         throws Exception
139     {
140         File testPom = new File( getBasedir(),
141                                  "target/test-classes/unit/basic-surefire-report-reporting-null/plugin-config.xml" );
142 
143         SurefireReportMojo mojo = ( SurefireReportMojo ) lookupMojo( "report", testPom );
144 
145         assertNotNull( mojo );
146 
147         mojo.execute();
148 
149         File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-reporting-null/surefire-report.html" );
150 
151         assertTrue( report.exists() );
152 
153         String htmlContent = FileUtils.fileRead( report );
154 
155         int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
156 
157         assertTrue( idx < 0);
158     }
159 }