View Javadoc
1   package org.apache.maven.plugins.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.repository.ArtifactRepository;
25  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
26  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
27  import org.apache.maven.plugin.LegacySupport;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
31  import org.sonatype.aether.util.DefaultRepositorySystemSession;
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  
50          LegacySupport legacySupport = lookup( LegacySupport.class );
51          legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
52          DefaultRepositorySystemSession repoSession =
53              (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
54          repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( testDir.getAbsolutePath() ) );
55  
56          setVariableValueToObject( mojo, "session", legacySupport.getSession() );
57      }
58  
59      /**
60       * Test transitive parameter
61       * 
62       * @throws Exception in case of errors
63       */
64      public void testTransitive()
65          throws Exception
66      {
67          // Set properties, transitive = default value = true
68          setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
69          setVariableValueToObject( mojo, "remoteRepositories",
70                                    "central::default::http://repo1.maven.apache.org/maven2" );
71          mojo.setGroupId( "org.apache.maven" );
72          mojo.setArtifactId( "maven-model" );
73          mojo.setVersion( "2.0.9" );
74  
75          mojo.execute();
76  
77          // Set properties, transitive = false
78          setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
79          mojo.execute();
80      }
81  
82      /**
83       * Test remote repositories parameter
84       * 
85       * @throws Exception in case of errors
86       */
87      public void testRemoteRepositories()
88          throws Exception
89      {
90          setVariableValueToObject( mojo, "remoteRepositories", "central::default::http://repo1.maven.apache.org/maven2,"
91              + "central::::http://repo1.maven.apache.org/maven2," + "http://repo1.maven.apache.org/maven2" );
92          mojo.setGroupId( "org.apache.maven" );
93          mojo.setArtifactId( "maven-model" );
94          mojo.setVersion( "2.0.9" );
95  
96          mojo.execute();
97      }
98  
99      /**
100      * Test parsing of the remote repositories parameter
101      * 
102      * @throws Exception in case of errors
103      */
104     public void testParseRepository()
105         throws Exception
106     {
107         ArtifactRepository repo;
108         ArtifactRepositoryPolicy policy = null;
109         repo = mojo.parseRepository( "central::default::http://repo1.maven.apache.org/maven2", policy );
110         assertEquals( "central", repo.getId() );
111         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
112         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
113 
114         try
115         {
116             repo = mojo.parseRepository( "central::legacy::http://repo1.maven.apache.org/maven2", policy );
117             fail( "Exception expected: legacy repository not supported anymore" );
118         }
119         catch ( MojoFailureException e )
120         {
121         }
122 
123         repo = mojo.parseRepository( "central::::http://repo1.maven.apache.org/maven2", policy );
124         assertEquals( "central", repo.getId() );
125         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
126         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
127 
128         repo = mojo.parseRepository( "http://repo1.maven.apache.org/maven2", policy );
129         assertEquals( "temp", repo.getId() );
130         assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
131         assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
132 
133         try
134         {
135             mojo.parseRepository( "::::http://repo1.maven.apache.org/maven2", policy );
136             fail( "Exception expected" );
137         }
138         catch ( MojoFailureException e )
139         {
140             // expected
141         }
142 
143         try
144         {
145             mojo.parseRepository( "central::http://repo1.maven.apache.org/maven2", policy );
146             fail( "Exception expected" );
147         }
148         catch ( MojoFailureException e )
149         {
150             // expected
151         }
152     }
153 }