View Javadoc

1   package org.apache.maven.plugin.dependency;
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  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
27  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
28  import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  public class TestGetMojo
34      extends AbstractDependencyMojoTestCase
35  {
36      GetMojo mojo;
37  
38      protected void setUp()
39          throws Exception
40      {
41          // required for mojo lookups to work
42          super.setUp( "markers", false );
43  
44          File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
45          assert testPom.exists();
46          mojo = (GetMojo) lookupMojo( "get", testPom );
47  
48          assertNotNull( mojo );
49          setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ){
50              @Override
51              public String pathOf( Artifact artifact )
52              {
53                  StringBuilder pathOf = new StringBuilder();
54                  pathOf.append( artifact.getGroupId() )
55                      .append( '_' )
56                      .append( artifact.getArtifactId() )
57                      .append( '-' )
58                      .append( artifact.getVersion() );
59                  if ( artifact.getClassifier() != null )
60                  {
61                      pathOf.append( '-' )
62                      .append( artifact.getClassifier() );
63                  }
64                  pathOf.append( '.' ).append( artifact.getArtifactHandler().getExtension() );
65                  return pathOf.toString();
66              }
67          } );
68      }
69  
70      /**
71       * Test transitive parameter
72       * 
73       * @throws Exception
74       */
75      public void testTransitive()
76          throws Exception
77      {
78          // Set properties, transitive = default value = true
79          setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
80          setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.apache.org/maven2" );
81          setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
82          setVariableValueToObject( mojo, "artifactId", "maven-model" );
83          setVariableValueToObject( mojo, "version", "2.0.9" );
84  
85          mojo.execute();
86  
87          // Set properties, transitive = false
88          setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
89          mojo.execute();
90      }
91      
92      /**
93       * Test destination parameter
94       * 
95       * @throws Exception
96       */
97      public void testDestination()
98          throws Exception
99      {
100         // Set properties, transitive = default value = true
101         setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
102         setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.apache.org/maven2" );
103         setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
104         setVariableValueToObject( mojo, "artifactId", "maven-model" );
105         setVariableValueToObject( mojo, "version", "2.0.9" );
106         File output = new File( getBasedir(), "target/unit-tests/get-test/destination-file/maven-model-2.0.9.jar" );
107         output.delete();
108         setVariableValueToObject( mojo, "destination", output.getAbsolutePath() );
109 
110         mojo.execute();
111         assertTrue( output.exists() );
112 
113         // Test directory
114         output = new File( getBasedir(), "target/unit-tests/get-test/destination-dir" );
115         output.mkdirs();
116         FileUtils.cleanDirectory( output );
117         setVariableValueToObject( mojo, "destination", output.getAbsolutePath() );
118 
119         mojo.execute();
120         assertTrue( new File( output, "org.apache.maven_maven-model-2.0.9.jar" ).exists() );
121     }
122     
123     
124 
125     /**
126      * Test remote repositories parameter
127      * 
128      * @throws Exception
129      */
130     public void testRemoteRepositories()
131         throws Exception
132     {
133         setVariableValueToObject( mojo, "remoteRepositories", "central::default::http://repo1.maven.apache.org/maven2,"
134             + "central::::http://repo1.maven.apache.org/maven2," + "http://repo1.maven.apache.org/maven2" );
135         setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
136         setVariableValueToObject( mojo, "artifactId", "maven-model" );
137         setVariableValueToObject( mojo, "version", "2.0.9" );
138 
139         mojo.execute();
140     }
141 
142     /**
143      * Test parsing of the remote repositories parameter
144      * 
145      * @throws Exception
146      */
147     public void testParseRepository()
148         throws Exception
149     {
150         ArtifactRepository repo;
151         ArtifactRepositoryPolicy policy = null;
152         repo = mojo.parseRepository( "central::default::http://repo1.maven.apache.org/maven2", policy );
153         assertEquals( "central", repo.getId() );
154         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
155         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
156 
157         repo = mojo.parseRepository( "central::legacy::http://repo1.maven.apache.org/maven2", policy );
158         assertEquals( "central", repo.getId() );
159         assertEquals( LegacyRepositoryLayout.class, repo.getLayout().getClass() );
160         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
161 
162         repo = mojo.parseRepository( "central::::http://repo1.maven.apache.org/maven2", policy );
163         assertEquals( "central", repo.getId() );
164         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
165         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
166 
167         repo = mojo.parseRepository( "http://repo1.maven.apache.org/maven2", policy );
168         assertEquals( "temp", repo.getId() );
169         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
170         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
171 
172         try
173         {
174             repo = mojo.parseRepository( "::::http://repo1.maven.apache.org/maven2", policy );
175             fail( "Exception expected" );
176         }
177         catch ( MojoFailureException e )
178         {
179             // expected
180         }
181 
182         try
183         {
184             repo = mojo.parseRepository( "central::http://repo1.maven.apache.org/maven2", policy );
185             fail( "Exception expected" );
186         }
187         catch ( MojoFailureException e )
188         {
189             // expected
190         }
191     }
192 }