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 java.io.File;
23  import java.util.Map;
24  
25  import org.apache.maven.embedder.MavenEmbedder;
26  import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.PlexusTestCase;
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 683260 2008-08-06 13:30:51Z aheritier $
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 )
89              .endsWith( "/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 ), "gr.spinellis.umlgraph.doclet.UmlGraphDoc" );
114 
115         assertNotNull( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "links", null ) );
116         assertEquals( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "links", null ).length, 2 );
117 
118         assertNotNull( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "docletArtifacts", null ) );
119         assertEquals( AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "docletArtifacts", null ).length, 2 );
120 
121         Map[] options = AntBuildWriterUtil.getMavenJavadocPluginOptions( project, "tags", null );
122         assertNotNull( options );
123         assertEquals( options.length, 1 );
124         assertEquals( 1, options[0].size() );
125         Map properties = (Map) options[0].get( "tag" );
126         assertNotNull( properties );
127         assertEquals( "requirement", properties.get( "name" ) );
128         assertEquals( "a", properties.get( "placement" ) );
129         assertEquals( "Software Requirement:", properties.get( "head" ) );
130 
131         maven.stop();
132     }
133 
134     /**
135      * Test method for <code>AntBuildWriterUtil.getSingularForm(String)}</code>.
136      *
137      * @throws Exception
138      */
139     public static void testGetSingularForm()
140         throws Exception
141     {
142         assertEquals( "property", AntBuildWriterUtil.getSingularForm( "properties" ) );
143         assertEquals( "branch", AntBuildWriterUtil.getSingularForm( "branches" ) );
144         assertEquals( "report", AntBuildWriterUtil.getSingularForm( "reports" ) );
145         assertEquals( "", AntBuildWriterUtil.getSingularForm( "singular" ) );
146         assertEquals( "", AntBuildWriterUtil.getSingularForm( null ) );
147     }
148 
149     /**
150      * Test method for <code>AntBuildWriterUtil.toRelative(File, String)</code>.
151      * 
152      * @throws Exception
153      */
154     public static void testToRelative()
155         throws Exception
156     {
157         assertEquals( "relative", AntBuildWriterUtil.toRelative( new File( "/home" ), "relative" ) );
158         assertEquals( "dir", AntBuildWriterUtil.toRelative( new File( "home" ),
159                                                             new File( "home/dir" ).getAbsolutePath() ) );
160         assertEquals( "dir", AntBuildWriterUtil.toRelative( new File( "/home" ),
161                                                             new File( "/home/dir" ).getAbsolutePath() ) );
162         assertEquals( "dir/", AntBuildWriterUtil.toRelative( new File( "/home" ),
163                                                              new File( "/home/dir" ).getAbsolutePath() + "/" ) );
164         assertEquals( "dir/sub", AntBuildWriterUtil.toRelative( new File( "/home" ),
165                                                                 new File( "/home/dir/sub" ).getAbsolutePath() ) );
166         assertEquals( ".", AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home" ).getAbsolutePath() ) );
167         assertEquals( "./", AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home" ).getAbsolutePath()
168             + "/" ) );
169     }
170 
171 }