1   package org.apache.maven.changelog;
2   
3   /*
4    * Copyright 2001-2006 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.changelog.stubs.ScmManagerStub;
20  import org.apache.maven.changelog.stubs.FailedScmManagerStub;
21  import org.apache.maven.changelog.stubs.ScmManagerWithHostStub;
22  import org.apache.maven.plugin.Mojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25  import org.apache.maven.scm.manager.ScmManager;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  import java.io.File;
29  
30  /**
31   * @author Edwin Punzalan
32   */
33  public class ChangeLogReportTest
34      extends AbstractMojoTestCase
35  {
36      private ScmManager scmManager;
37  
38      public void testNoSource()
39          throws Exception
40      {
41          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/changelog/no-source-plugin-config.xml" );
42  
43          Mojo mojo = lookupMojo( "changelog", pluginXmlFile );
44  
45          assertNotNull( "Mojo found.", mojo );
46  
47          this.setVariableValueToObject( mojo, "manager", scmManager );
48  
49          mojo.execute();
50  
51          File outputDir = (File) getVariableValueFromObject( mojo, "outputDirectory" );
52  
53          File outputHtml = new File( outputDir, "changelog.html" );
54  
55          assertTrue( "Test html generated", outputHtml.exists() );
56      }
57  
58      public void testMinConfig()
59          throws Exception
60      {
61          executeMojo( "min-plugin-config.xml" );
62      }
63  
64      public void testFailedChangelog()
65          throws Exception
66      {
67          scmManager = new FailedScmManagerStub();
68  
69          try
70          {
71              executeMojo( "min-plugin-config.xml" );
72          }
73          catch ( MojoExecutionException e )
74          {
75              assertEquals( "Test thrown exception", "Command failed.", e.getCause().getCause().getMessage() );
76          }
77      }
78  
79      public void testUsageOfCachedXml()
80          throws Exception
81      {
82          File cacheFile = new File( getBasedir(), "src/test/changelog-xml/min-changelog.xml" );
83          cacheFile.setLastModified( System.currentTimeMillis() );
84  
85          executeMojo( "cached-plugin-config.xml" );
86      }
87  
88      public void testTypeException()
89          throws Exception
90      {
91          try
92          {
93              executeMojo( "inv-type-plugin-config.xml" );
94  
95              fail( "Test exception on invalid type" );
96          }
97          catch ( MojoExecutionException e )
98          {
99              assertTrue( "Test thrown exception", e.getCause().getMessage().startsWith( "The type parameter has an invalid value: invalid." ) );
100         }
101     }
102 
103     public void testTagType()
104         throws Exception
105     {
106         executeMojo( "tag-plugin-config.xml" );
107     }
108 
109     public void testTagsType()
110         throws Exception
111     {
112         executeMojo( "tags-plugin-config.xml" );
113     }
114 
115     public void testDateException()
116         throws Exception
117     {
118         try
119         {
120             executeMojo( "inv-date-plugin-config.xml" );
121         }
122         catch ( MojoExecutionException e )
123         {
124             assertTrue( "Test thrown exception",
125                         e.getCause().getCause().getMessage().startsWith( "Please use this date pattern: ") );
126         }
127     }
128 
129     public void testDateType()
130         throws Exception
131     {
132         executeMojo( "date-plugin-config.xml" );
133     }
134 
135     public void testDatesType()
136         throws Exception
137     {
138         executeMojo( "dates-plugin-config.xml" );
139     }
140 
141     public void testScmRepositoryWithHost()
142         throws Exception
143     {
144         scmManager = new ScmManagerWithHostStub();
145 
146         executeMojo( "hosted-plugin-config.xml" );
147     }
148 
149     public void testScmRepositoryWithHostFromSettings()
150         throws Exception
151     {
152         scmManager = new ScmManagerWithHostStub();
153 
154         executeMojo( "hosted-with-settings-plugin-config.xml" );
155     }
156 
157     public void testNoScmConnection()
158         throws Exception
159     {
160         try
161         {
162             executeMojo( "no-scm-plugin-config.xml" );
163         }
164         catch ( MojoExecutionException e )
165         {
166             assertEquals( "Test thrown exception", "SCM Connection is not set.",
167                           e.getCause().getCause().getCause().getMessage() );
168         }
169     }
170 
171     private void executeMojo( String pluginXml )
172         throws Exception
173     {
174         File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/changelog/" + pluginXml );
175 
176         Mojo mojo = lookupMojo( "changelog", pluginXmlFile );
177 
178         assertNotNull( "Mojo found.", mojo );
179 
180         this.setVariableValueToObject( mojo, "manager", scmManager );
181 
182         mojo.execute();
183 
184         File outputXML = (File) getVariableValueFromObject( mojo, "outputXML" );
185 
186         String encoding = (String) getVariableValueFromObject( mojo, "outputEncoding" );
187 
188         assertTrue( "Test if changelog.xml is created", outputXML.exists() );
189 
190         String changelogXml = FileUtils.fileRead( outputXML );
191 
192         assertTrue( "Test for xml header", changelogXml.startsWith( "<?xml version=\"1.0\" encoding=\"" +
193                     encoding + "\"?>" ) );
194 
195         assertTrue( "Test for xml footer", changelogXml.endsWith( "</changelog>" ) );
196 
197         File outputDir = (File) getVariableValueFromObject( mojo, "outputDirectory" );
198 
199         File outputHtml = new File( outputDir, "changelog.html" );
200 
201         assertTrue( "Test html generated", outputHtml.exists() );
202     }
203 
204     protected void setUp()
205         throws Exception
206     {
207         super.setUp();
208 
209         scmManager = new ScmManagerStub();
210     }
211 
212     protected void tearDown()
213         throws Exception
214     {
215         super.tearDown();
216     }
217 }