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.testing.AbstractMojoTestCase;
25
26
27
28
29
30 public class CpdViolationCheckMojoTest
31 extends AbstractMojoTestCase
32 {
33
34 @Override
35 protected void setUp()
36 throws Exception
37 {
38 super.setUp();
39 CapturingPrintStream.init( true );
40 }
41
42 public void testDefaultConfiguration()
43 throws Exception
44 {
45 File testPom =
46 new File( getBasedir(),
47 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
48 final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
49 mojo.execute();
50
51
52 CapturingPrintStream.init( true );
53
54 try
55 {
56 testPom =
57 new File( getBasedir(),
58 "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
59 final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
60 cpdViolationMojo.execute();
61
62 fail( "MojoFailureException should be thrown." );
63 }
64 catch ( final Exception e )
65 {
66
67 String output = CapturingPrintStream.getOutput();
68 assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
69
70 assertTrue( e.getMessage().startsWith( "You have 1 CPD duplication." ) );
71 }
72 }
73
74 public void testNotFailOnViolation()
75 throws Exception
76 {
77
78 File testPom =
79 new File( getBasedir(),
80 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
81 final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
82 mojo.execute();
83
84 testPom =
85 new File( getBasedir(),
86 "src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml" );
87 final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
88 cpdViolationMojo.execute();
89
90 assertTrue( true );
91 }
92
93 public void testException()
94 throws Exception
95 {
96 try
97 {
98 final File testPom =
99 new File( getBasedir(),
100 "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
101 final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
102 mojo.execute();
103
104 fail( "MojoFailureException should be thrown." );
105 }
106 catch ( final Exception e )
107 {
108 assertTrue( true );
109 }
110 }
111
112 public void testExclusionsConfiguration()
113 throws Exception
114 {
115 File testPom =
116 new File( getBasedir(),
117 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
118 final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
119 mojo.execute();
120
121 testPom =
122 new File( getBasedir(),
123 "src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml" );
124 final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
125
126
127 cpdViolationMojo.execute();
128 }
129 }