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 junit.framework.TestCase;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.project.ProjectBuildingRequest;
26  
27  import java.lang.reflect.Field;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import static org.apache.maven.plugins.dependency.AbstractDependencyMojoTest.ConcreteDependencyMojo.createConcreteDependencyMojoWithArtifactRepositories;
32  import static org.apache.maven.plugins.dependency.AbstractDependencyMojoTest.ConcreteDependencyMojo.createConcreteDependencyMojoWithPluginRepositories;
33  import static org.mockito.Mockito.mock;
34  import static org.mockito.Mockito.when;
35  
36  public class AbstractDependencyMojoTest extends TestCase
37  {
38      private MavenSession session = mock( MavenSession.class );
39  
40      private ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
41  
42      private ArrayList<ArtifactRepository> artifactRepos = new ArrayList<>();
43  
44      private ArrayList<ArtifactRepository> pluginRepos = new ArrayList<>();
45  
46      static class ConcreteDependencyMojo extends AbstractDependencyMojo
47      {
48          static ConcreteDependencyMojo createConcreteDependencyMojoWithArtifactRepositories(
49                  MavenSession mavenSession, List<ArtifactRepository> artifactRepos )
50                  throws NoSuchFieldException, IllegalAccessException
51          {
52              ConcreteDependencyMojo cdm = new ConcreteDependencyMojo();
53              cdm.session = mavenSession;
54  
55              Field par = AbstractDependencyMojo.class.getDeclaredField( "remoteRepositories" );
56              par.setAccessible( true );
57              par.set( cdm, artifactRepos );
58  
59              return cdm;
60          }
61  
62          static ConcreteDependencyMojo createConcreteDependencyMojoWithPluginRepositories(
63                  MavenSession mavenSession, List<ArtifactRepository> pluginRepos )
64                  throws NoSuchFieldException, IllegalAccessException
65          {
66              ConcreteDependencyMojo cdm = new ConcreteDependencyMojo();
67              cdm.session = mavenSession;
68  
69              Field par = AbstractDependencyMojo.class.getDeclaredField( "remotePluginRepositories" );
70              par.setAccessible( true );
71              par.set( cdm, pluginRepos );
72  
73              return cdm;
74          }
75  
76          @Override
77          protected void doExecute()
78          {
79          }
80      }
81  
82      @Override
83      protected void setUp() throws Exception
84      {
85          pluginRepos.add( newRepositoryWithId( "pr-central" ) );
86          pluginRepos.add( newRepositoryWithId( "pr-plugins" ) );
87  
88          artifactRepos.add( newRepositoryWithId( "ar-central" ) );
89          artifactRepos.add( newRepositoryWithId( "ar-snapshots" ) );
90          artifactRepos.add( newRepositoryWithId( "ar-staging" ) );
91  
92          when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
93      }
94  
95      private static ArtifactRepository newRepositoryWithId( String id )
96      {
97          ArtifactRepository repo = mock( ArtifactRepository.class );
98          when( repo.getId() ).thenReturn( id );
99          return repo;
100     }
101 
102     public void testNewResolveArtifactProjectBuildingRequestRemoteRepositoriesSize()
103             throws NoSuchFieldException, IllegalAccessException
104     {
105         AbstractDependencyMojo mojo = createConcreteDependencyMojoWithArtifactRepositories( session, artifactRepos );
106 
107         ProjectBuildingRequest pbr = mojo.newResolveArtifactProjectBuildingRequest();
108         List<ArtifactRepository> rrepos = pbr.getRemoteRepositories();
109 
110         assertEquals( 3, rrepos.size() );
111     }
112 
113     public void testNewResolveArtifactProjectBuildingRequestRemoteRepositoriesContents()
114             throws NoSuchFieldException, IllegalAccessException
115     {
116         AbstractDependencyMojo mojo = createConcreteDependencyMojoWithArtifactRepositories( session, artifactRepos );
117 
118         ProjectBuildingRequest pbr = mojo.newResolveArtifactProjectBuildingRequest();
119         List<ArtifactRepository> rrepos = pbr.getRemoteRepositories();
120 
121         assertEquals( "ar-central", rrepos.get( 0 ).getId() );
122         assertEquals( "ar-snapshots", rrepos.get( 1 ).getId() );
123         assertEquals( "ar-staging", rrepos.get( 2 ).getId() );
124     }
125 
126     public void testNewResolvePluginProjectBuildingRequestRemoteRepositoriesSize()
127             throws NoSuchFieldException, IllegalAccessException
128     {
129         AbstractDependencyMojo mojo = createConcreteDependencyMojoWithPluginRepositories( session, pluginRepos );
130 
131         ProjectBuildingRequest pbr = mojo.newResolvePluginProjectBuildingRequest();
132         List<ArtifactRepository> rrepos = pbr.getRemoteRepositories();
133 
134         assertEquals( 2, rrepos.size() );
135     }
136 
137     public void testNewResolvePluginProjectBuildingRequestRemoteRepositoriesContents()
138             throws NoSuchFieldException, IllegalAccessException
139     {
140         AbstractDependencyMojo mojo = createConcreteDependencyMojoWithPluginRepositories( session, pluginRepos );
141 
142         ProjectBuildingRequest pbr = mojo.newResolvePluginProjectBuildingRequest();
143         List<ArtifactRepository> rrepos = pbr.getRemoteRepositories();
144 
145         assertEquals( "pr-central", rrepos.get( 0 ).getId() );
146         assertEquals( "pr-plugins", rrepos.get( 1 ).getId() );
147     }
148 }