1 package org.apache.maven.plugin.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
24
25 import java.io.File;
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 }
43
44 public void testDefaultConfiguration()
45 throws Exception
46 {
47 try
48 {
49 final File testPom = 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 assertTrue( true );
59 }
60 }
61
62 public void testNotFailOnViolation()
63 throws Exception
64 {
65 File testPom = new File( getBasedir(),
66 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
67 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
68 mojo.execute();
69
70 testPom = new File( getBasedir(),
71 "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" );
72 final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
73 pmdViolationMojo.execute();
74
75 assertTrue( true );
76 }
77
78 public void testFailurePriority()
79 throws Exception
80 {
81 File testPom = 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 = new File( getBasedir(),
87 "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" );
88 PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
89 pmdViolationMojo.execute();
90
91 testPom = new File( getBasedir(),
92 "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml" );
93 pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
94 try
95 {
96 pmdViolationMojo.execute();
97 fail( "Exception Expected" );
98 }
99 catch ( final MojoFailureException e )
100 {
101 String message = e.getMessage();
102 if ( message.contains( "You have 5 PMD violations and 3 warnings." ) ) {
103 System.out.println( "Caught Expected Message: " + e.getMessage() );
104 }
105 else
106 {
107 throw new AssertionError( "Expected: '" + message + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
108 }
109 }
110
111 }
112
113 public void testException()
114 throws Exception
115 {
116 try
117 {
118 final File testPom = new File( getBasedir(),
119 "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
120 final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
121 mojo.execute();
122
123 fail( "MojoFailureException should be thrown." );
124 }
125 catch ( final Exception e )
126 {
127 assertTrue( true );
128 }
129
130 }
131
132 public void testViolationExclusion()
133 throws Exception
134 {
135 File testPom = new File( getBasedir(),
136 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
137 final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
138 mojo.execute();
139
140 testPom = new File( getBasedir(),
141 "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" );
142 final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
143 pmdViolationMojo.execute();
144
145 assertTrue( true );
146 }
147 }