1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47 public final class EnforcerTestUtils {
48
49
50
51
52
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
74
75
76
77
78
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 }