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