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