1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.pmd;
20
21 import java.io.File;
22
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.apache.maven.project.MavenProject;
25
26
27
28
29
30 public class PmdViolationCheckMojoTest extends AbstractPmdReportTestCase {
31
32 public void testDefaultConfiguration() throws Exception {
33 generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
34
35 try {
36 final File testPom = new File(
37 getBasedir(),
38 "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
39 final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
40 mojo.execute();
41
42 fail("MojoFailureException should be thrown.");
43 } catch (final MojoFailureException e) {
44 assertTrue(
45 e.getMessage().startsWith("PMD " + AbstractPmdReport.getPmdVersion() + " has found 8 violations."));
46 }
47 }
48
49 public void testNotFailOnViolation() throws Exception {
50 generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
51
52 File testPom = new File(
53 getBasedir(),
54 "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml");
55 final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
56 pmdViolationMojo.execute();
57 }
58
59 public void testMaxAllowedViolations() throws Exception {
60 generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
61
62 File testPom = new File(
63 getBasedir(),
64 "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml");
65 final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
66 pmdViolationMojo.execute();
67
68 testPom = new File(
69 getBasedir(),
70 "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml");
71 final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo(getGoal(), testPom);
72
73 try {
74 pmdViolationMojoFail.execute();
75 fail("Exception Expected");
76 } catch (final MojoFailureException e) {
77 assertTrue(e.getMessage()
78 .startsWith("PMD " + AbstractPmdReport.getPmdVersion()
79 + " has found 5 violations and issued 3 warnings."));
80 }
81 }
82
83 public void testFailurePriority() throws Exception {
84 generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
85
86 File testPom = new File(
87 getBasedir(),
88 "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
89 PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
90 pmdViolationCheckMojo.execute();
91
92 testPom = new File(
93 getBasedir(),
94 "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
95 pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
96
97 try {
98 pmdViolationCheckMojo.execute();
99 fail("MojoFailureException expected");
100 } catch (final MojoFailureException e) {
101 assertTrue(e.getMessage()
102 .startsWith("PMD " + AbstractPmdReport.getPmdVersion()
103 + " has found 5 violations and issued 3 warnings."));
104 }
105 }
106
107 public void testException() throws Exception {
108 try {
109 final File testPom = new File(
110 getBasedir(),
111 "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
112 final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
113 mojo.project = new MavenProject();
114 mojo.execute();
115
116 fail("MojoFailureException should be thrown.");
117 } catch (final MojoFailureException e) {
118 assertNotNull(e.getMessage());
119 }
120 }
121
122 public void testViolationExclusion() throws Exception {
123 generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
124
125 File testPom = new File(
126 getBasedir(),
127 "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml");
128 final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
129
130
131 pmdViolationMojo.execute();
132 }
133
134 @Override
135 protected String getGoal() {
136 return "check";
137 }
138 }