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.plugin.testing.AbstractMojoTestCase;
23 import org.apache.maven.project.MavenProject;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.Properties;
28
29 /**
30 * Class to test Ant plugin
31 *
32 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
33 * @version $Id: AntMojoTest.java 1517969 2013-08-27 20:14:02Z krosenvold $
34 */
35 public class AntMojoTest
36 extends AbstractMojoTestCase
37 {
38 /**
39 * @see junit.framework.TestCase#setUp()
40 */
41 protected void setUp()
42 throws Exception
43 {
44 // required for mojo lookups to work
45 super.setUp();
46 }
47
48 /**
49 * @see junit.framework.TestCase#tearDown()
50 */
51 protected void tearDown()
52 throws Exception
53 {
54 // nop
55 }
56
57 /**
58 * Method to test Default Ant generation
59 *
60 * @throws Exception
61 */
62 public void testDefaultProject()
63 throws Exception
64 {
65 invokeAntMojo( "ant-test" );
66 }
67
68 /**
69 * Method to test Project with no dependencies
70 *
71 * @throws Exception
72 */
73 public void testProjectWithNoDep()
74 throws Exception
75 {
76 invokeAntMojo( "ant-nodep-test" );
77 }
78
79 /**
80 * Method to test Project with no dependencies
81 *
82 * @throws Exception
83 */
84 public void testProjectWithJavadoc()
85 throws Exception
86 {
87 invokeAntMojo( "ant-javadoc-test" );
88 }
89
90 /**
91 * Invoke Ant mojo.
92 * <br/>
93 * The Maven test project should be in a directory called <code>testProject</code> in "src/test/resources/unit/" directory.
94 * The Maven test project should be called <code>"testProject"-plugin-config.xml</code> and should produced
95 * <code>ant-plugin-test.jar</code> as artefact.
96 *
97 * @param testProject
98 * @throws Exception
99 */
100 private void invokeAntMojo( String testProject )
101 throws Exception
102 {
103 File testPom = new File( getBasedir(), "src/test/resources/unit/" + testProject + "/pom.xml" );
104 AntMojo mojo = (AntMojo) lookupMojo( "ant", testPom );
105 mojo.execute();
106
107 MavenProject currentProject = (MavenProject) getVariableValueFromObject( mojo, "project" );
108
109 File antBasedir = new File( getBasedir(), "target/test/unit/" + testProject + "/" );
110 File antBuild = new File( antBasedir, AntBuildWriter.DEFAULT_BUILD_FILENAME );
111 assertTrue( antBuild.exists() );
112 if ( !currentProject.getPackaging().toLowerCase().equals( "pom" ) )
113 {
114 File antProperties = new File( antBasedir, AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME );
115 assertTrue( antProperties.exists() );
116 }
117
118 AntWrapper.invoke( antBuild );
119
120 if ( !currentProject.getPackaging().toLowerCase().equals( "pom" ) )
121 {
122 assertTrue( new File( antBasedir, "target" ).exists() );
123 assertTrue( new File( antBasedir, "target/classes" ).exists() );
124 assertTrue(
125 new File( antBasedir, "target/" + currentProject.getBuild().getFinalName() + ".jar" ).exists() );
126
127 Properties properties = new Properties();
128 properties.load(
129 new FileInputStream( new File( antBasedir, AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME ) ) );
130 String repo = properties.getProperty( "maven.repo.local" );
131 assertTrue( repo.equals( new File( getBasedir(), "target/local-repo" ).getAbsolutePath() ) );
132 }
133 }
134 }