1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.checkstyle;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.util.Locale;
25
26 import org.apache.maven.plugin.LegacySupport;
27 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28 import org.apache.maven.plugin.testing.ArtifactStubFactory;
29 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30 import org.apache.maven.project.DefaultProjectBuildingRequest;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuilder;
33 import org.apache.maven.project.ProjectBuildingRequest;
34 import org.eclipse.aether.DefaultRepositorySystemSession;
35 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
36 import org.eclipse.aether.repository.LocalRepository;
37
38
39
40
41 public abstract class AbstractCheckstyleTestCase extends AbstractMojoTestCase {
42 private Locale oldLocale;
43
44 private ArtifactStubFactory artifactStubFactory;
45
46
47
48
49 private MavenProject testMavenProject;
50
51 @Override
52 protected void setUp() throws Exception {
53
54 super.setUp();
55
56 oldLocale = Locale.getDefault();
57 Locale.setDefault(Locale.ENGLISH);
58
59 artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
60 artifactStubFactory.getWorkingDir().mkdirs();
61 }
62
63 @Override
64 protected void tearDown() throws Exception {
65 super.tearDown();
66
67 Locale.setDefault(oldLocale);
68 oldLocale = null;
69 }
70
71
72
73
74
75
76 protected MavenProject getTestMavenProject() {
77 return testMavenProject;
78 }
79
80
81
82
83
84
85
86
87 protected File getGeneratedReport(String name) throws IOException {
88 String outputDirectory = getBasedir() + "/target/test/test-harness/"
89 + getTestMavenProject().getArtifactId();
90
91 File report = new File(outputDirectory, name);
92 if (!report.exists()) {
93 throw new IOException("File not found. Attempted: " + report);
94 }
95
96 return report;
97 }
98
99
100
101
102
103
104
105
106
107 protected File generateReport(String goal, String pluginXml) throws Exception {
108 File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
109 CheckstyleReport mojo = createReportMojo(goal, pluginXmlFile);
110 return generateReport(mojo, pluginXmlFile);
111 }
112
113 protected CheckstyleReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
114 CheckstyleReport mojo = (CheckstyleReport) lookupMojo(goal, pluginXmlFile);
115 assertNotNull("Mojo not found.", mojo);
116
117 LegacySupport legacySupport = lookup(LegacySupport.class);
118 legacySupport.setSession(newMavenSession(new MavenProjectStub()));
119 DefaultRepositorySystemSession repoSession =
120 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
121 repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
122 .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(CheckstyleReport mojo, File pluginXmlFile) throws Exception {
130 mojo.execute();
131
132 ProjectBuilder builder = lookup(ProjectBuilder.class);
133
134 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
135 buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
136
137 testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
138
139 File outputDir = mojo.getReportOutputDirectory();
140 String filename = mojo.getOutputName() + ".html";
141
142 return new File(outputDir, filename);
143 }
144
145
146
147
148 protected String readFile(File checkstyleTestDir, String fileName) throws IOException {
149 return new String(Files.readAllBytes(checkstyleTestDir.toPath().resolve(fileName)));
150 }
151 }