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          String name = AbstractMavenProjectTestCase.class.getName().replace('.', '/') + ".xml";
80          System.out.println(name);
81          return name;
82      }
83  
84      // ----------------------------------------------------------------------
85      // Local repository
86      // ----------------------------------------------------------------------
87  
88      protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
89          File markerFile = getFileForClasspathResource("local-repo/marker.txt");
90  
91          return markerFile.getAbsoluteFile().getParentFile();
92      }
93  
94      protected static File getFileForClasspathResource(String resource)
95              throws FileNotFoundException, URISyntaxException {
96          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
97  
98          URL resourceUrl = cloader.getResource(resource);
99  
100         if (resourceUrl == null) {
101             throw new FileNotFoundException("Unable to find: " + resource);
102         }
103 
104         return new File(resourceUrl.toURI());
105     }
106 
107     protected ArtifactRepository getLocalRepository() throws Exception {
108         ArtifactRepositoryLayout repoLayout = lookup(ArtifactRepositoryLayout.class, "legacy");
109 
110         ArtifactRepository r = repositorySystem.createArtifactRepository(
111                 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
112 
113         return r;
114     }
115 
116     // ----------------------------------------------------------------------
117     // Project building
118     // ----------------------------------------------------------------------
119 
120     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
121         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
122         configuration.setLocalRepository(getLocalRepository());
123         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
124         configuration.setProcessPlugins(false);
125         configuration.setResolveDependencies(true);
126         initRepoSession(configuration);
127 
128         try {
129             return projectBuilder.build(pom, configuration).getProject();
130         } catch (Exception e) {
131             Throwable cause = e.getCause();
132             if (cause instanceof ModelBuildingException) {
133                 String message = "In: " + pom + "\n\n";
134                 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
135                     message += problem + "\n";
136                 }
137                 System.out.println(message);
138                 fail(message);
139             }
140 
141             throw e;
142         }
143     }
144 
145     protected MavenProject getProject(File pom) throws Exception {
146         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
147         configuration.setLocalRepository(getLocalRepository());
148         initRepoSession(configuration);
149 
150         return projectBuilder.build(pom, configuration).getProject();
151     }
152 
153     protected void initRepoSession(ProjectBuildingRequest request) {
154         File localRepo = new File(request.getLocalRepository().getBasedir());
155         DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
156         session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
157         request.setRepositorySession(session);
158     }
159 }