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