1 package org.apache.maven.plugins.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
26
27
28
29
30
31 public class PmdViolationCheckMojoTest
32 extends AbstractMojoTestCase
33 {
34
35
36
37 @Override
38 protected void setUp()
39 throws Exception
40 {
41 super.setUp();
42 CapturingPrintStream.init( true );
43 }
44
45 public void testDefaultConfiguration()
46 throws Exception
47 {
48 File testPomPmd =
49 new File( getBasedir(),
50 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
51 final PmdReport pmdMojo = (PmdReport) lookupMojo( "pmd", testPomPmd );
52 pmdMojo.execute();
53
54
55 CapturingPrintStream.init( true );
56
57 try
58 {
59 final File testPom =
60 new File( getBasedir(),
61 "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
62 final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
63 mojo.execute();
64
65 fail( "MojoFailureException should be thrown." );
66 }
67 catch ( final Exception e )
68 {
69
70 String output = CapturingPrintStream.getOutput();
71 assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
72
73 assertTrue( e.getMessage().startsWith( "You have 8 PMD violations." ) );
74 }
75 }
76
77 public void testNotFailOnViolation()
78 throws Exception
79 {
80 File testPom =
81 new File( getBasedir(),
82 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
83 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
84 mojo.execute();
85
86 testPom =
87 new File( getBasedir(),
88 "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" );
89 final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
90 pmdViolationMojo.execute();
91
92 assertTrue( true );
93 }
94
95 public void testMaxAllowedViolations()
96 throws Exception
97 {
98 File testPom =
99 new File( getBasedir(),
100 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
101 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
102 mojo.execute();
103
104 testPom =
105 new File( getBasedir(),
106 "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml" );
107 final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
108 pmdViolationMojo.execute();
109
110 testPom =
111 new File( getBasedir(),
112 "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml" );
113 final PmdViolationCheckMojo pmdViolationMojoFail = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
114
115 try
116 {
117 pmdViolationMojoFail.execute();
118 fail( "Exception Expected" );
119 }
120 catch ( final MojoFailureException e )
121 {
122 String message = e.getMessage();
123 if ( message.contains( "You have 5 PMD violations and 3 warnings." ) )
124 {
125 System.out.println( "Caught expected message: " + e.getMessage() );
126 }
127 else
128 {
129 throw new AssertionError( "Expected: '" + message
130 + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
131 }
132 }
133
134 }
135
136 public void testFailurePriority()
137 throws Exception
138 {
139 File testPom =
140 new File( getBasedir(),
141 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
142 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
143 mojo.execute();
144
145 testPom =
146 new File( getBasedir(),
147 "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" );
148 PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
149 pmdViolationMojo.execute();
150
151 testPom =
152 new File( getBasedir(),
153 "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml" );
154 pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
155 try
156 {
157 pmdViolationMojo.execute();
158 fail( "Exception Expected" );
159 }
160 catch ( final MojoFailureException e )
161 {
162 String message = e.getMessage();
163 if ( message.contains( "You have 5 PMD violations and 3 warnings." ) )
164 {
165 System.out.println( "Caught expected message: " + e.getMessage() );
166 }
167 else
168 {
169 throw new AssertionError( "Expected: '" + message
170 + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
171 }
172 }
173
174 }
175
176 public void testException()
177 throws Exception
178 {
179 try
180 {
181 final File testPom =
182 new File( getBasedir(),
183 "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
184 final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
185 mojo.execute();
186
187 fail( "MojoFailureException should be thrown." );
188 }
189 catch ( final Exception e )
190 {
191 assertTrue( true );
192 }
193
194 }
195
196 public void testViolationExclusion()
197 throws Exception
198 {
199 File testPom =
200 new File( getBasedir(),
201 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
202 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
203 mojo.execute();
204
205 testPom =
206 new File( getBasedir(),
207 "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" );
208 final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
209
210
211 pmdViolationMojo.execute();
212 }
213 }