-
Notifications
You must be signed in to change notification settings - Fork 25
/
ArchUnitMojoTest.java
318 lines (244 loc) · 12.6 KB
/
ArchUnitMojoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.societegenerale.commons.plugin.maven;
import static com.tngtech.junit.dataprovider.DataProviders.testForEach;
import static java.util.Arrays.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableSet;
import com.societegenerale.aut.test.TestClassWithPowerMock;
import com.societegenerale.commons.plugin.rules.MyCustomAndDummyRules;
import com.societegenerale.commons.plugin.rules.NoPowerMockRuleTest;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
import java.io.StringReader;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.Condition;
import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@RunWith(DataProviderRunner.class)
public class ArchUnitMojoTest {
@Rule
public final MojoRule mojoRule = new MojoRule();
@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();
@InjectMocks
private ArchUnitMojo archUnitMojo;
@Mock
private MavenProject mavenProject;
private PlexusConfiguration pluginConfiguration;
// @formatter:off
private static final String pomWithNoRule =
"<project>" +
"<build>" +
"<plugins>" +
"<plugin>" +
"<artifactId>arch-unit-maven-plugin</artifactId>" +
"<configuration>" +
"<rules>" +
"<preConfiguredRules></preConfiguredRules>" +
"<configurableRules></configurableRules>" +
"</rules>" +
"</configuration>" +
"</plugin>" +
"</plugins>" +
"</build>" +
"</project>";
// @formatter:on
@Before
public void setUp() throws Exception {
pluginConfiguration = mojoRule.extractPluginConfiguration("arch-unit-maven-plugin", Xpp3DomBuilder.build(new StringReader(pomWithNoRule)));
}
@Test
public void shouldFailWhenNoRuleConfigured() throws Exception {
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
assertThatExceptionOfType(MojoFailureException.class)
.isThrownBy(mojo::execute)
.withMessageContaining("Arch unit Plugin should have at least one preconfigured/configurable rule");
}
@Test
public void shouldExecuteSinglePreconfiguredRule() throws Exception {
pluginConfiguration.getChild("projectPath").setValue("./target/aut-target/test-classes/com/societegenerale/aut/test");
// add single rule
PlexusConfiguration preConfiguredRules = pluginConfiguration.getChild("rules").getChild("preConfiguredRules");
preConfiguredRules.addChild("rule", NoPowerMockRuleTest.class.getName());
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
executeAndExpectViolations(mojo,
expectRuleFailure("classes should not use Powermock")
.withDetails("Favor Mockito and proper dependency injection - " + TestClassWithPowerMock.class.getName()));
}
@Test
public void shouldFailWronglyDefinedConfigurableRule() throws Exception {
PlexusConfiguration configurableRule = new DefaultPlexusConfiguration("configurableRule");
String missingCheck = "notThere";
String ruleClass = MyCustomAndDummyRules.class.getName();
configurableRule.addChild("rule", ruleClass);
configurableRule.addChild(buildChecksBlock(missingCheck));
pluginConfiguration.getChild("rules").getChild("configurableRules").addChild(configurableRule);
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
assertThatExceptionOfType(MojoFailureException.class)
.isThrownBy(mojo::execute)
.withMessageContaining(String.format("The following configured checks are not present within %s: [%s]", ruleClass, missingCheck));
}
@DataProvider
public static Object[][] configurableRuleChecks() {
return testForEach("annotatedWithTest_asField", "annotatedWithTest_asMethod");
}
@Test
@UseDataProvider("configurableRuleChecks")
public void shouldExecuteSingleConfigurableRuleCheck(String checkName) throws Exception {
PlexusConfiguration configurableRule = new DefaultPlexusConfiguration("configurableRule");
configurableRule.addChild("rule", MyCustomAndDummyRules.class.getName());
configurableRule.addChild(buildChecksBlock(checkName));
configurableRule.addChild(buildApplyOnBlock("com.societegenerale.aut.test.specificCase", "test"));
PlexusConfiguration configurableRules = pluginConfiguration.getChild("rules").getChild("configurableRules");
configurableRules.addChild(configurableRule);
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
executeAndExpectViolations(mojo,
expectRuleFailure("classes should be annotated with @Test").ofAnyKind());
}
@Test
public void shouldExecuteAllConfigurableRuleChecksIfUnconfigured() throws Exception {
PlexusConfiguration configurableRule = new DefaultPlexusConfiguration("configurableRule");
configurableRule.addChild("rule", MyCustomAndDummyRules.class.getName());
configurableRule.addChild(buildApplyOnBlock("com.societegenerale.aut.test.specificCase", "test"));
PlexusConfiguration configurableRules = pluginConfiguration.getChild("rules").getChild("configurableRules");
configurableRules.addChild(configurableRule);
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
executeAndExpectViolations(mojo,
expectRuleFailure("classes should be annotated with @Test").ofAnyKind(),
expectRuleFailure("classes should be annotated with @Test").ofAnyKind(),
expectRuleFailure("classes should reside in a package 'myPackage'").ofAnyKind(),
expectRuleFailure("classes should reside in a package 'myPackage'").ofAnyKind()
);
}
@Test
public void shouldExecuteBothConfigurableRule_and_PreConfiguredRule() throws Exception {
PlexusConfiguration configurableRule = new DefaultPlexusConfiguration("configurableRule");
configurableRule.addChild("rule", MyCustomAndDummyRules.class.getName());
configurableRule.addChild(buildChecksBlock("annotatedWithTest_asField"));
configurableRule.addChild(buildApplyOnBlock("com.societegenerale.aut.test.specificCase", "test"));
PlexusConfiguration configurableRules = pluginConfiguration.getChild("rules").getChild("configurableRules");
configurableRules.addChild(configurableRule);
PlexusConfiguration preConfiguredRules = pluginConfiguration.getChild("rules").getChild("preConfiguredRules");
preConfiguredRules.addChild("rule", NoPowerMockRuleTest.class.getName());
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
executeAndExpectViolations(mojo,
expectRuleFailure("classes should be annotated with @Test").ofAnyKind(),
expectRuleFailure("classes should not use Powermock").ofAnyKind());
}
@Test
public void shouldNotExecuteConfigurableRule_and_PreConfiguredRule_IfSkipIsTrue() throws Exception {
PlexusConfiguration configurableRule = new DefaultPlexusConfiguration("configurableRule");
configurableRule.addChild("rule", MyCustomAndDummyRules.class.getName());
configurableRule.addChild(buildChecksBlock("annotatedWithTest_asField"));
configurableRule.addChild(buildApplyOnBlock("com.societegenerale.commons.plugin.rules.classesForTests.specificCase", "test"));
PlexusConfiguration configurableRules = pluginConfiguration.getChild("rules").getChild("configurableRules");
configurableRules.addChild(configurableRule);
PlexusConfiguration preConfiguredRules = pluginConfiguration.getChild("rules").getChild("preConfiguredRules");
preConfiguredRules.addChild("rule", NoPowerMockRuleTest.class.getName());
pluginConfiguration.getChild("skip").setValue("true");
ArchUnitMojo mojo = (ArchUnitMojo) mojoRule.configureMojo(archUnitMojo, pluginConfiguration);
assertThatCode(() -> {
mojo.execute();
}).doesNotThrowAnyException();
}
private String getBasedir() {
String basedir = System.getProperty("basedir");
if (basedir == null) {
basedir = new File("").getAbsolutePath();
}
return basedir;
}
@Test
public void shouldSkipIfPackagingIsPom() throws Exception {
InterceptingLog interceptingLogger = new InterceptingLog(
mojoRule.getContainer().lookup(LoggerManager.class).getLoggerForComponent(Mojo.ROLE));
File testPom = new File(getBasedir(), "target/test-classes/unit/plugin-config.xml");
ArchUnitMojo archUnitMojo = (ArchUnitMojo) mojoRule.lookupMojo("arch-test", testPom);
MavenProject mavenProject = mock(MavenProject.class);
when(mavenProject.getPackaging()).thenReturn("pom");
mojoRule.setVariableValueToObject(archUnitMojo, "mavenProject", mavenProject);
archUnitMojo.setLog(interceptingLogger);
assertThatCode(() -> archUnitMojo.execute()).doesNotThrowAnyException();
assertThat(interceptingLogger.debugLogs).containsExactly("module packaging is 'pom', so skipping execution");
}
private void executeAndExpectViolations( ArchUnitMojo mojo, ExpectedRuleFailure... expectedRuleFailures) {
AbstractThrowableAssert<?, ? extends Throwable> throwableAssert = assertThatThrownBy(mojo::execute);
stream(expectedRuleFailures).forEach(expectedFailure -> {
throwableAssert.hasMessageContaining(String.format("Rule '%s' was violated", expectedFailure.ruleDescription));
expectedFailure.details.forEach(throwableAssert::hasMessageContaining);
});
throwableAssert.has(exactNumberOfViolatedRules(expectedRuleFailures.length));
}
private Condition<Throwable> exactNumberOfViolatedRules(final int number) {
return new Condition<Throwable>("exactly " + number + " violated rules") {
@Override
public boolean matches(Throwable throwable) {
Matcher matcher = Pattern.compile("Rule '.*' was violated").matcher(throwable.getMessage());
int numberOfOccurrences = 0;
while (matcher.find()) {
numberOfOccurrences++;
}
return numberOfOccurrences == number;
}
};
}
private PlexusConfiguration buildApplyOnBlock(String packageName, String scope) {
PlexusConfiguration packageNameElement = new DefaultPlexusConfiguration("packageName", packageName);
PlexusConfiguration scopeElement = new DefaultPlexusConfiguration("scope", scope);
PlexusConfiguration applyOnElement = new DefaultPlexusConfiguration("applyOn");
applyOnElement.addChild(packageNameElement);
applyOnElement.addChild(scopeElement);
return applyOnElement;
}
private PlexusConfiguration buildChecksBlock(String... checks) {
PlexusConfiguration checksElement = new DefaultPlexusConfiguration("checks");
stream(checks).map(c -> new DefaultPlexusConfiguration("check", c)).forEach(checksElement::addChild);
return checksElement;
}
private static ExpectedRuleFailure.Creator expectRuleFailure(String ruleDescription) {
return new ExpectedRuleFailure.Creator(ruleDescription);
}
private static class ExpectedRuleFailure {
private final String ruleDescription;
private final Set<String> details;
private ExpectedRuleFailure(String ruleDescription, Set<String> details) {
this.ruleDescription = ruleDescription;
this.details = details;
}
private static class Creator {
private final String ruleDescription;
Creator(String ruleDescription) {
this.ruleDescription = ruleDescription;
}
ExpectedRuleFailure ofAnyKind() {
return withDetails();
}
ExpectedRuleFailure withDetails(String... detals) {
return new ExpectedRuleFailure(ruleDescription, ImmutableSet.copyOf(detals));
}
}
}
}