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