View Javadoc
1   package org.apache.maven.plugin.ant;
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.embedder.MavenEmbedder;
23  import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.plexus.PlexusTestCase;
26  
27  import java.io.File;
28  import java.util.Map;
29  
30  /**
31   * Test cases for 'org.apache.maven.plugin.ant.AntBuildWriterUtil'
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @version $Id: AntBuildWriterUtilTest.java 1517969 2013-08-27 20:14:02Z krosenvold $
35   */
36  public class AntBuildWriterUtilTest
37      extends PlexusTestCase
38  {
39      /**
40       * Test method for 'org.apache.maven.plugin.ant.AntBuildWriterUtil.getMavenCompilerPluginConfiguration(MavenProject, String, String)'
41       *
42       * @throws Exception
43       */
44      public void testGetMavenCompilerPluginConfiguration()
45          throws Exception
46      {
47          File testPom = new File( getBasedir(), "src/test/resources/unit/ant-compiler-config-test/pom.xml" );
48  
49          MavenEmbedder maven = new MavenEmbedder();
50          maven.setClassLoader( Thread.currentThread().getContextClassLoader() );
51          maven.setLogger( new MavenEmbedderConsoleLogger() );
52          maven.setLocalRepositoryDirectory( getTestFile( "target/local-repo" ) );
53          maven.setOffline( true );
54          maven.start();
55  
56          MavenProject project = maven.readProject( testPom );
57  
58          assertEquals( AntBuildWriterUtil.getMavenCompilerPluginBasicOption( project, "debug", null ), "true" );
59  
60          assertNotNull( AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "includes", null ) );
61          assertEquals( AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "includes", null ).length, 2 );
62          assertNotNull( AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "excludes", null ) );
63          assertEquals( AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "excludes", null ).length, 1 );
64  
65          maven.stop();
66      }
67  
68      /**
69       * Test method for 'org.apache.maven.plugin.ant.AntBuildWriterUtil.getMavenWarPluginConfiguration(MavenProject, String, String)'
70       *
71       * @throws Exception
72       */
73      public void testGetMavenWarPluginConfiguration()
74          throws Exception
75      {
76          File testPom = new File( getBasedir(), "src/test/resources/unit/ant-war-config-test/pom.xml" );
77  
78          MavenEmbedder maven = new MavenEmbedder();
79          maven.setClassLoader( Thread.currentThread().getContextClassLoader() );
80          maven.setLogger( new MavenEmbedderConsoleLogger() );
81          maven.setLocalRepositoryDirectory( getTestFile( "target/local-repo" ) );
82          maven.setOffline( true );
83          maven.start();
84  
85          MavenProject project = maven.readProject( testPom );
86  
87          assertEquals( AntBuildWriterUtil.getMavenWarPluginBasicOption( project, "warName", null ), "mywebapp" );
88          assertTrue( AntBuildWriterUtil.getMavenWarPluginBasicOption( project, "webXml", null ).endsWith(
89              "/src/main/webapp/WEB-INF/web.xml" ) );
90  
91          maven.stop();
92      }
93  
94      /**
95       * Test method for 'org.apache.maven.plugin.ant.AntBuildWriterUtil.getMavenJavadocPluginConfiguration(MavenProject, String, String)'
96       *
97       * @throws Exception
98       */
99      public void testGetMavenJavadocPluginConfiguration()
100         throws Exception
101     {
102         File testPom = new File( getBasedir(), "src/test/resources/unit/ant-javadoc-test/pom.xml" );
103 
104         MavenEmbedder maven = new MavenEmbedder();
105         maven.setClassLoader( Thread.currentThread().getContextClassLoader() );
106         maven.setLogger( new MavenEmbedderConsoleLogger() );
107         maven.setLocalRepositoryDirectory( getTestFile( "target/local-repo" ) );
108         maven.setOffline( true );
109         maven.start();
110 
111         MavenProject project = maven.readProject( testPom );
112 
113         assertEquals( AntBuildWriterUtil.getMavenJavadocPluginBasicOption( project, "doclet", null ),
114                       "gr.spinellis.umlgraph.doclet.UmlGraphDoc" );
115 
116         assertNotNull( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "links", null ) );
117         assertEquals( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "links", null ).length, 2 );
118 
119         assertNotNull( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "docletArtifacts", null ) );
120         assertEquals( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "docletArtifacts", null ).length, 2 );
121 
122         Map[] options = AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "tags", null );
123         assertNotNull( options );
124         assertEquals( options.length, 1 );
125         assertEquals( 1, options[0].size() );
126         Map properties = (Map) options[0].get( "tag" );
127         assertNotNull( properties );
128         assertEquals( "requirement", properties.get( "name" ) );
129         assertEquals( "a", properties.get( "placement" ) );
130         assertEquals( "Software Requirement:", properties.get( "head" ) );
131 
132         maven.stop();
133     }
134 
135     /**
136      * Test method for <code>AntBuildWriterUtil.getSingularForm(String)}</code>.
137      *
138      * @throws Exception
139      */
140     public static void testGetSingularForm()
141         throws Exception
142     {
143         assertEquals( "property", AntBuildWriterUtil.getSingularForm( "properties" ) );
144         assertEquals( "branch", AntBuildWriterUtil.getSingularForm( "branches" ) );
145         assertEquals( "report", AntBuildWriterUtil.getSingularForm( "reports" ) );
146         assertEquals( "", AntBuildWriterUtil.getSingularForm( "singular" ) );
147         assertEquals( "", AntBuildWriterUtil.getSingularForm( null ) );
148     }
149 
150     /**
151      * Test method for <code>AntBuildWriterUtil.toRelative(File, String)</code>.
152      *
153      * @throws Exception
154      */
155     public static void testToRelative()
156         throws Exception
157     {
158         assertEquals( "relative", AntBuildWriterUtil.toRelative( new File( "/home" ), "relative" ) );
159         assertEquals( "dir",
160                       AntBuildWriterUtil.toRelative( new File( "home" ), new File( "home/dir" ).getAbsolutePath() ) );
161         assertEquals( "dir",
162                       AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home/dir" ).getAbsolutePath() ) );
163         assertEquals( "dir/", AntBuildWriterUtil.toRelative( new File( "/home" ),
164                                                              new File( "/home/dir" ).getAbsolutePath() + "/" ) );
165         assertEquals( "dir/sub", AntBuildWriterUtil.toRelative( new File( "/home" ),
166                                                                 new File( "/home/dir/sub" ).getAbsolutePath() ) );
167         assertEquals( ".",
168                       AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home" ).getAbsolutePath() ) );
169         assertEquals( "./", AntBuildWriterUtil.toRelative( new File( "/home" ),
170                                                            new File( "/home" ).getAbsolutePath() + "/" ) );
171     }
172 
173 }