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.enforcer.rules;
20  
21  import java.util.Properties;
22  
23  import org.apache.maven.enforcer.rules.utils.DependencyNodeBuilder;
24  import org.apache.maven.execution.MavenExecutionRequest;
25  import org.apache.maven.execution.MavenExecutionResult;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.model.InputLocation;
28  import org.apache.maven.model.InputSource;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.project.ProjectBuildingRequest;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.classworlds.ClassWorld;
33  import org.codehaus.plexus.classworlds.realm.ClassRealm;
34  import org.eclipse.aether.DefaultRepositorySystemSession;
35  import org.eclipse.aether.graph.DependencyNode;
36  
37  import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
38  import static org.mockito.Mockito.any;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.when;
41  
42  /**
43   * The Class EnforcerTestUtils.
44   *
45   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
46   */
47  public final class EnforcerTestUtils {
48  
49      /**
50       * Gets the maven session.
51       *
52       * @return the maven session
53       */
54      public static MavenSession getMavenSession() {
55          PlexusContainer mock = mock(PlexusContainer.class);
56  
57          MavenExecutionRequest mer = mock(MavenExecutionRequest.class);
58          ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
59          when(buildingRequest.setRepositorySession(any())).thenReturn(buildingRequest);
60          when(mer.getProjectBuildingRequest()).thenReturn(buildingRequest);
61  
62          Properties systemProperties = new Properties();
63          systemProperties.put("maven.version", "3.0");
64          when(mer.getUserProperties()).thenReturn(new Properties());
65          when(mer.getSystemProperties()).thenReturn(systemProperties);
66  
67          MavenExecutionResult meResult = mock(MavenExecutionResult.class);
68  
69          return new MavenSession(mock, new DefaultRepositorySystemSession(), mer, meResult);
70      }
71  
72      /**
73       * New plugin.
74       *
75       * @param groupId the group id
76       * @param artifactId the artifact id
77       * @param version the version
78       * @return the plugin
79       */
80      public static Plugin newPlugin(String groupId, String artifactId, String version) {
81          InputSource inputSource = new InputSource();
82          inputSource.setModelId("unit");
83  
84          Plugin plugin = new Plugin();
85          plugin.setArtifactId(artifactId);
86          plugin.setGroupId(groupId);
87          plugin.setVersion(version);
88          plugin.setLocation("version", new InputLocation(0, 0, inputSource));
89          return plugin;
90      }
91  
92      public static DependencyNode getDependencyNodeWithMultipleSnapshots() {
93          return new DependencyNodeBuilder()
94                  .withType(DependencyNodeBuilder.Type.POM)
95                  .withChildNode(new DependencyNodeBuilder()
96                          .withArtifactId("childA")
97                          .withVersion("1.0.0")
98                          .withChildNode(new DependencyNodeBuilder()
99                                  .withArtifactId("childAA")
100                                 .withVersion("1.0.0-SNAPSHOT")
101                                 .build())
102                         .build())
103                 .withChildNode(new DependencyNodeBuilder()
104                         .withArtifactId("childB")
105                         .withVersion("2.0.0-SNAPSHOT")
106                         .build())
107                 .build();
108     }
109 
110     public static DependencyNode getDependencyNodeWithMultipleTestSnapshots() {
111         return new DependencyNodeBuilder()
112                 .withType(DependencyNodeBuilder.Type.POM)
113                 .withChildNode(new DependencyNodeBuilder()
114                         .withArtifactId("childA")
115                         .withVersion("1.0.0")
116                         .withChildNode(new DependencyNodeBuilder()
117                                 .withArtifactId("childAA")
118                                 .withVersion("1.0.0-SNAPSHOT")
119                                 .withScope(SCOPE_TEST)
120                                 .build())
121                         .build())
122                 .withChildNode(new DependencyNodeBuilder()
123                         .withArtifactId("childB")
124                         .withVersion("2.0.0-SNAPSHOT")
125                         .withScope(SCOPE_TEST)
126                         .build())
127                 .build();
128     }
129 
130     public static ClassRealm getTestClassRealm() {
131         ClassWorld classWorld = new ClassWorld("test", EnforcerTestUtils.class.getClassLoader());
132         return classWorld.getClassRealm("test");
133     }
134 }