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 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.util.Locale;
26
27 import org.apache.maven.plugin.LegacySupport;
28 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29 import org.apache.maven.plugin.testing.ArtifactStubFactory;
30 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31 import org.apache.maven.project.DefaultProjectBuildingRequest;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.project.ProjectBuilder;
34 import org.apache.maven.project.ProjectBuildingRequest;
35 import org.eclipse.aether.DefaultRepositorySystemSession;
36 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
37 import org.eclipse.aether.repository.LocalRepository;
38
39
40
41
42
43
44 public abstract class AbstractPmdReportTestCase
45 extends AbstractMojoTestCase
46 {
47 private ArtifactStubFactory artifactStubFactory;
48
49
50
51
52 private MavenProject testMavenProject;
53
54 @Override
55 protected void setUp()
56 throws Exception
57 {
58 super.setUp();
59 CapturingPrintStream.init( true );
60
61 artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
62 artifactStubFactory.getWorkingDir().mkdirs();
63 }
64
65
66
67
68
69
70 protected MavenProject getTestMavenProject()
71 {
72 return testMavenProject;
73 }
74
75
76
77
78
79
80
81
82 protected File getGeneratedReport( String name )
83 throws IOException
84 {
85 String outputDirectory = getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();
86
87 File report = new File( outputDirectory, name );
88 if ( !report.exists() )
89 {
90 throw new IOException( "File not found. Attempted: " + report );
91 }
92
93 return report;
94 }
95
96
97
98
99
100
101
102
103
104 protected File generateReport( String goal, String pluginXml )
105 throws Exception
106 {
107 File pluginXmlFile = new File( getBasedir(), "src/test/resources/unit/" + pluginXml );
108 AbstractPmdReport mojo = createReportMojo( goal, pluginXmlFile );
109 return generateReport( mojo, pluginXmlFile );
110 }
111
112 protected AbstractPmdReport createReportMojo( String goal, File pluginXmlFile )
113 throws Exception
114 {
115 AbstractPmdReport mojo = (AbstractPmdReport) lookupMojo( goal, pluginXmlFile );
116 assertNotNull( "Mojo not found.", mojo );
117
118 LegacySupport legacySupport = lookup( LegacySupport.class );
119 legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
120 DefaultRepositorySystemSession repoSession =
121 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
122 repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) );
123
124 setVariableValueToObject( mojo, "session", legacySupport.getSession() );
125 setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
126 return mojo;
127 }
128
129 protected File generateReport( AbstractPmdReport mojo, File pluginXmlFile )
130 throws Exception
131 {
132 mojo.execute();
133
134 ProjectBuilder builder = lookup( ProjectBuilder.class );
135
136 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
137 buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );
138
139 testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();
140
141 File outputDir = mojo.getReportOutputDirectory();
142 String filename = mojo.getOutputName() + ".html";
143
144 return new File( outputDir, filename );
145 }
146
147
148
149
150 protected String readFile( File pmdTestDir, String fileName ) throws IOException
151 {
152 return new String( Files.readAllBytes( pmdTestDir.toPath().resolve( fileName ) ) );
153 }
154
155
156
157
158
159
160
161
162
163
164 public static boolean lowerCaseContains( String text, String contains )
165 {
166 return text.toLowerCase( Locale.ROOT ).contains( contains.toLowerCase( Locale.ROOT ) );
167 }
168 }