1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.deploy;
20
21 import java.io.File;
22
23 import org.apache.maven.model.Model;
24 import org.apache.maven.model.Parent;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
32
33
34
35
36 public class DeployFileMojoUnitTest {
37 MockDeployFileMojo mojo;
38 Parent parent;
39
40 @Before
41 public void setUp() {
42 Model pomModel = new Model();
43 pomModel.setPackaging(null);
44
45 parent = new Parent();
46 parent.setGroupId("parentGroup");
47 parent.setArtifactId("parentArtifact");
48 parent.setVersion("parentVersion");
49
50 mojo = new MockDeployFileMojo(pomModel);
51 }
52
53 @After
54 public void tearDown() {
55 mojo = null;
56 }
57
58 static class MockDeployFileMojo extends DeployFileMojo {
59 private Model model;
60
61 public MockDeployFileMojo(Model model) {
62 this.model = model;
63 }
64
65 public void setModel(Model model) {
66 this.model = model;
67 }
68
69 protected Model readModel(File pomFile) {
70 return model;
71 }
72 }
73
74 @Test
75 public void testProcessPomFromPomFileWithParent1() {
76 mojo.setPomFile(new File("foo.bar"));
77
78 setMojoModel(mojo.model, null, null, null, null, parent);
79
80 try {
81 mojo.initProperties();
82 } catch (MojoExecutionException expected) {
83 assertTrue(true);
84 }
85
86 checkMojoProperties("parentGroup", null, "parentVersion", null);
87 }
88
89 @Test
90 public void testProcessPomFromPomFileWithParent2() {
91 mojo.setPomFile(new File("foo.bar"));
92 setMojoModel(mojo.model, null, "artifact", null, null, parent);
93
94 try {
95 mojo.initProperties();
96 } catch (MojoExecutionException expected) {
97 assertTrue(true);
98 }
99
100 checkMojoProperties("parentGroup", "artifact", "parentVersion", null);
101 }
102
103 @Test
104 public void testProcessPomFromPomFileWithParent3() {
105 mojo.setPomFile(new File("foo.bar"));
106 setMojoModel(mojo.model, null, "artifact", "version", null, parent);
107
108 try {
109 mojo.initProperties();
110 } catch (MojoExecutionException expected) {
111 assertTrue(true);
112 }
113
114 checkMojoProperties("parentGroup", "artifact", "version", null);
115 }
116
117 @Test
118 public void testProcessPomFromPomFileWithParent4() throws MojoExecutionException {
119 mojo.setPomFile(new File("foo.bar"));
120 setMojoModel(mojo.model, null, "artifact", "version", "packaging", parent);
121
122 mojo.initProperties();
123
124 checkMojoProperties("parentGroup", "artifact", "version", "packaging");
125 }
126
127 @Test
128 public void testProcessPomFromPomFileWithParent5() throws MojoExecutionException {
129 mojo.setPomFile(new File("foo.bar"));
130 setMojoModel(mojo.model, "group", "artifact", "version", "packaging", parent);
131
132 mojo.initProperties();
133
134 checkMojoProperties("group", "artifact", "version", "packaging");
135 }
136
137 @Test
138 public void testProcessPomFromPomFileWithParent6() throws MojoExecutionException {
139 mojo.setPomFile(new File("foo.bar"));
140 setMojoModel(mojo.model, "group", "artifact", "version", "packaging", null);
141
142 mojo.initProperties();
143
144 checkMojoProperties("group", "artifact", "version", "packaging");
145 }
146
147 @Test
148 public void testProcessPomFromPomFileWithOverrides() throws MojoExecutionException {
149 mojo.setPomFile(new File("foo.bar"));
150 setMojoModel(mojo.model, "group", "artifact", "version", "packaging", null);
151
152 mojo.setGroupId("groupO");
153 mojo.setArtifactId("artifactO");
154 mojo.setVersion("versionO");
155 mojo.setPackaging("packagingO");
156
157 mojo.initProperties();
158
159 checkMojoProperties("groupO", "artifactO", "versionO", "packagingO");
160 }
161
162 private void checkMojoProperties(
163 final String expectedGroup,
164 final String expectedArtifact,
165 final String expectedVersion,
166 final String expectedPackaging) {
167 assertEquals(expectedGroup, mojo.getGroupId());
168 assertEquals(expectedArtifact, mojo.getArtifactId());
169 assertEquals(expectedVersion, mojo.getVersion());
170 assertEquals(expectedPackaging, mojo.getPackaging());
171 }
172
173 private void setMojoModel(
174 Model model, String group, String artifact, String version, String packaging, Parent parent) {
175 model.setGroupId(group);
176 model.setArtifactId(artifact);
177 model.setVersion(version);
178 model.setPackaging(packaging);
179 model.setParent(parent);
180 }
181 }