View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.util.Arrays;
26  
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29  import org.apache.maven.model.building.ModelBuildingException;
30  import org.apache.maven.model.building.ModelProblem;
31  import org.apache.maven.repository.RepositorySystem;
32  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
33  import org.codehaus.plexus.ContainerConfiguration;
34  import org.codehaus.plexus.PlexusConstants;
35  import org.codehaus.plexus.PlexusTestCase;
36  import org.eclipse.aether.DefaultRepositorySystemSession;
37  
38  /**
39   * @author Jason van Zyl
40   */
41  public abstract class AbstractMavenProjectTestCase extends PlexusTestCase {
42      protected ProjectBuilder projectBuilder;
43  
44      protected RepositorySystem repositorySystem;
45  
46      @Override
47      protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
48          super.customizeContainerConfiguration(containerConfiguration);
49          containerConfiguration.setAutoWiring(true);
50          containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
51      }
52  
53      protected void setUp() throws Exception {
54          super.setUp();
55  
56          if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
57              projectBuilder = lookup(ProjectBuilder.class, "test");
58          } else {
59              // default over to the main project builder...
60              projectBuilder = lookup(ProjectBuilder.class);
61          }
62  
63          repositorySystem = lookup(RepositorySystem.class);
64      }
65  
66      @Override
67      protected void tearDown() throws Exception {
68          projectBuilder = null;
69  
70          super.tearDown();
71      }
72  
73      protected ProjectBuilder getProjectBuilder() {
74          return projectBuilder;
75      }
76  
77      @Override
78      protected String getCustomConfigurationName() {
79          return AbstractMavenProjectTestCase.class.getName().replace('.', '/') + ".xml";
80      }
81  
82      // ----------------------------------------------------------------------
83      // Local repository
84      // ----------------------------------------------------------------------
85  
86      protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
87          File markerFile = getFileForClasspathResource("local-repo/marker.txt");
88  
89          return markerFile.getAbsoluteFile().getParentFile();
90      }
91  
92      protected static File getFileForClasspathResource(String resource)
93              throws FileNotFoundException, URISyntaxException {
94          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
95  
96          URL resourceUrl = cloader.getResource(resource);
97  
98          if (resourceUrl == null) {
99              throw new FileNotFoundException("Unable to find: " + resource);
100         }
101 
102         return new File(resourceUrl.toURI());
103     }
104 
105     protected ArtifactRepository getLocalRepository() throws Exception {
106         ArtifactRepositoryLayout repoLayout = lookup(ArtifactRepositoryLayout.class, "legacy");
107 
108         ArtifactRepository r = repositorySystem.createArtifactRepository(
109                 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
110 
111         return r;
112     }
113 
114     // ----------------------------------------------------------------------
115     // Project building
116     // ----------------------------------------------------------------------
117 
118     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
119         ProjectBuildingRequest configuration = newBuildingRequest();
120         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
121         configuration.setProcessPlugins(false);
122         configuration.setResolveDependencies(true);
123 
124         try {
125             return projectBuilder.build(pom, configuration).getProject();
126         } catch (Exception e) {
127             Throwable cause = e.getCause();
128             if (cause instanceof ModelBuildingException) {
129                 String message = "In: " + pom + "\n\n";
130                 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
131                     message += problem + "\n";
132                 }
133                 System.out.println(message);
134             }
135 
136             throw e;
137         }
138     }
139 
140     protected MavenProject getProject(File pom) throws Exception {
141         ProjectBuildingRequest configuration = newBuildingRequest();
142 
143         return projectBuilder.build(pom, configuration).getProject();
144     }
145 
146     protected MavenProject getProjectFromRemoteRepository(final File pom) throws Exception {
147         final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
148         configuration.setLocalRepository(this.getLocalRepository());
149         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
150         initRepoSession(configuration);
151 
152         return projectBuilder.build(pom, configuration).getProject();
153     }
154 
155     protected ProjectBuildingRequest newBuildingRequest() throws Exception {
156         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
157         configuration.setLocalRepository(getLocalRepository());
158         initRepoSession(configuration);
159         return configuration;
160     }
161 
162     protected void initRepoSession(ProjectBuildingRequest request) {
163         File localRepo = new File(request.getLocalRepository().getBasedir());
164         DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
165         repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
166         request.setRepositorySession(repoSession);
167     }
168 }