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.internal.transformation.impl;
20  
21  import javax.annotation.Priority;
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.io.InputStream;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.util.Collections;
31  import java.util.function.Consumer;
32  
33  import org.apache.maven.api.Session;
34  import org.apache.maven.api.model.Model;
35  import org.apache.maven.api.services.ModelResolver;
36  import org.apache.maven.api.services.ModelResolverException;
37  import org.apache.maven.api.services.ModelSource;
38  import org.apache.maven.artifact.repository.MavenArtifactRepository;
39  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
40  import org.apache.maven.internal.impl.InternalMavenSession;
41  import org.apache.maven.internal.impl.InternalSession;
42  import org.apache.maven.internal.transformation.AbstractRepositoryTestCase;
43  import org.apache.maven.model.v4.MavenStaxReader;
44  import org.apache.maven.project.MavenProject;
45  import org.eclipse.aether.DefaultRepositorySystemSession;
46  import org.junit.jupiter.api.Test;
47  
48  import static org.junit.jupiter.api.Assertions.assertNotNull;
49  import static org.junit.jupiter.api.Assertions.assertTrue;
50  
51  public class ConsumerPomBuilderTest extends AbstractRepositoryTestCase {
52  
53      @Inject
54      ConsumerPomBuilder builder;
55  
56      @Test
57      void testTrivialConsumer() throws Exception {
58          MavenProject project;
59          Path file = Paths.get("src/test/resources/consumer/trivial/child/pom.xml");
60          try (InputStream inputStream = Files.newInputStream(file)) {
61              org.apache.maven.model.Model model =
62                      new org.apache.maven.model.Model(new MavenStaxReader().read(inputStream));
63              project = new MavenProject(model);
64              project.setRootDirectory(Paths.get("src/test/resources/consumer/trivial"));
65              project.setOriginalModel(model);
66              project.setRemoteArtifactRepositories(Collections.singletonList(new MavenArtifactRepository(
67                      "central", "http://repo.maven.apache.org/", new DefaultRepositoryLayout(), null, null)));
68          }
69          Model model = builder.build(session, project, file);
70  
71          assertNotNull(model);
72      }
73  
74      @Test
75      void testSimpleConsumer() throws Exception {
76          MavenProject project;
77          Path file = Paths.get("src/test/resources/consumer/simple/simple-parent/simple-weather/pom.xml");
78          ((DefaultRepositorySystemSession) session).setUserProperty("changelist", "MNG6957");
79          try (InputStream inputStream = Files.newInputStream(file)) {
80              org.apache.maven.model.Model model =
81                      new org.apache.maven.model.Model(new MavenStaxReader().read(inputStream));
82              project = new MavenProject(model);
83              project.setRootDirectory(Paths.get("src/test/resources/consumer/simple"));
84              project.setRemoteArtifactRepositories(Collections.singletonList(new MavenArtifactRepository(
85                      "central", "http://repo.maven.apache.org/", new DefaultRepositoryLayout(), null, null)));
86              project.setOriginalModel(model);
87          }
88          InternalMavenSession.from(InternalSession.from(session))
89                  .getMavenSession()
90                  .getRequest()
91                  .setRootDirectory(Paths.get("src/test/resources/consumer/simple"));
92          Model model = builder.build(session, project, file);
93  
94          assertNotNull(model);
95          assertTrue(model.getProfiles().isEmpty());
96      }
97  
98      @Named
99      @Singleton
100     @Priority(10)
101     public static class MyModelResolver implements ModelResolver {
102         @Override
103         public ModelSource resolveModel(
104                 Session session, String groupId, String artifactId, String version, Consumer<String> resolvedVersion)
105                 throws ModelResolverException {
106             String id = groupId + ":" + artifactId + ":" + version;
107             if (id.startsWith("org.sonatype.mavenbook.multi:parent:")) {
108                 return ModelSource.fromPath(Paths.get("src/test/resources/consumer/simple/pom.xml"));
109             } else if (id.startsWith("org.sonatype.mavenbook.multi:simple-parent:")) {
110                 return ModelSource.fromPath(Paths.get("src/test/resources/consumer/simple/simple-parent/pom.xml"));
111             } else if (id.startsWith("org.my.group:parent:")) {
112                 return ModelSource.fromPath(Paths.get("src/test/resources/consumer/trivial/pom.xml"));
113             }
114             return null;
115         }
116     }
117 }