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 java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.building.TransformerContext;
29  import org.apache.maven.model.v4.MavenStaxReader;
30  import org.apache.maven.project.MavenProject;
31  import org.eclipse.aether.RepositorySystemSession;
32  import org.eclipse.aether.SessionData;
33  import org.junit.jupiter.api.Test;
34  import org.mockito.Mockito;
35  import org.xmlunit.assertj.XmlAssert;
36  
37  import static org.assertj.core.api.Assertions.assertThat;
38  import static org.mockito.ArgumentMatchers.any;
39  import static org.mockito.Mockito.when;
40  
41  class ConsumerPomArtifactTransformerTest {
42  
43      @Test
44      void transform() throws Exception {
45          RepositorySystemSession systemSessionMock = Mockito.mock(RepositorySystemSession.class);
46          SessionData sessionDataMock = Mockito.mock(SessionData.class);
47          when(systemSessionMock.getData()).thenReturn(sessionDataMock);
48          when(sessionDataMock.get(any())).thenReturn(new NoTransformerContext());
49  
50          Path beforePomFile =
51                  Paths.get("src/test/resources/projects/transform/before.pom").toAbsolutePath();
52          Path afterPomFile =
53                  Paths.get("src/test/resources/projects/transform/after.pom").toAbsolutePath();
54          Path tempFile = Files.createTempFile("", ".pom");
55          Files.delete(tempFile);
56          try (InputStream expected = Files.newInputStream(beforePomFile)) {
57              Model model = new Model(new MavenStaxReader().read(expected));
58              MavenProject project = new MavenProject(model);
59              project.setOriginalModel(model);
60              DefaultConsumerPomArtifactTransformer t = new DefaultConsumerPomArtifactTransformer((s, p, f) -> {
61                  try (InputStream is = Files.newInputStream(f)) {
62                      return DefaultConsumerPomBuilder.transform(new MavenStaxReader().read(is), project);
63                  }
64              });
65  
66              t.transform(project, systemSessionMock, beforePomFile, tempFile);
67          }
68          XmlAssert.assertThat(afterPomFile.toFile()).and(tempFile.toFile()).areIdentical();
69      }
70  
71      @Test
72      void injectTransformedArtifactsWithoutPomShouldNotInjectAnyArtifacts() throws IOException {
73          MavenProject emptyProject = new MavenProject();
74  
75          RepositorySystemSession systemSessionMock = Mockito.mock(RepositorySystemSession.class);
76          SessionData sessionDataMock = Mockito.mock(SessionData.class);
77          when(systemSessionMock.getData()).thenReturn(sessionDataMock);
78          when(sessionDataMock.get(any())).thenReturn(new NoTransformerContext());
79  
80          new DefaultConsumerPomArtifactTransformer((session, project, src) -> null)
81                  .injectTransformedArtifacts(systemSessionMock, emptyProject);
82  
83          assertThat(emptyProject.getAttachedArtifacts()).isEmpty();
84      }
85  
86      private static class NoTransformerContext implements TransformerContext {
87          @Override
88          public String getUserProperty(String key) {
89              return null;
90          }
91  
92          @Override
93          public Model getRawModel(Path from, String groupId, String artifactId) throws IllegalStateException {
94              throw new UnsupportedOperationException();
95          }
96  
97          @Override
98          public Model getRawModel(Path from, Path p) {
99              throw new UnsupportedOperationException();
100         }
101 
102         @Override
103         public Path locate(Path path) {
104             throw new UnsupportedOperationException();
105         }
106     }
107 }