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