1   package org.apache.maven.plugins.checkstyle;
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.internal.impl.SimpleLocalRepositoryManagerFactory;
36  import org.eclipse.aether.DefaultRepositorySystemSession;
37  import org.eclipse.aether.repository.LocalRepository;
38  
39  
40  
41  
42  public abstract class AbstractCheckstyleTestCase
43      extends AbstractMojoTestCase
44  {
45      private Locale oldLocale;
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          
59          super.setUp();
60  
61          oldLocale = Locale.getDefault();
62          Locale.setDefault( Locale.ENGLISH );
63  
64          artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
65          artifactStubFactory.getWorkingDir().mkdirs();
66      }
67  
68      @Override
69      protected void tearDown()
70          throws Exception
71      {
72          super.tearDown();
73  
74          Locale.setDefault( oldLocale );
75          oldLocale = null;
76      }
77  
78      
79  
80  
81  
82  
83      protected MavenProject getTestMavenProject()
84      {
85          return testMavenProject;
86      }
87  
88      
89  
90  
91  
92  
93  
94  
95      protected File getGeneratedReport( String name )
96          throws IOException
97      {
98          String outputDirectory = getBasedir() + "/target/test/test-harness/" + getTestMavenProject().getArtifactId();
99  
100         File report = new File( outputDirectory, name );
101         if ( !report.exists() )
102         {
103             throw new IOException( "File not found. Attempted: " + report );
104         }
105 
106         return report;
107     }
108 
109     
110 
111 
112 
113 
114 
115 
116 
117     protected File generateReport( String goal, String pluginXml )
118         throws Exception
119     {
120         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
121         CheckstyleReport mojo  = createReportMojo( goal, pluginXmlFile );
122         return generateReport( mojo, pluginXmlFile );
123     }
124 
125     protected CheckstyleReport createReportMojo( String goal, File pluginXmlFile )
126         throws Exception
127     {
128         CheckstyleReport mojo = (CheckstyleReport) lookupMojo( goal, pluginXmlFile );
129         assertNotNull( "Mojo not found.", mojo );
130 
131         LegacySupport legacySupport = lookup( LegacySupport.class );
132         legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
133         DefaultRepositorySystemSession repoSession =
134             (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
135         repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) );
136 
137         setVariableValueToObject( mojo, "session", legacySupport.getSession() );
138         setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
139         return mojo;
140     }
141 
142     protected File generateReport( CheckstyleReport mojo, File pluginXmlFile )
143         throws Exception
144     {
145         mojo.execute();
146 
147         ProjectBuilder builder = lookup( ProjectBuilder.class );
148 
149         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
150         buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );
151 
152         testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();
153 
154         File outputDir = mojo.getReportOutputDirectory();
155         String filename = mojo.getOutputName() + ".html";
156 
157         return new File( outputDir, filename );
158     }
159 
160     
161 
162 
163     protected String readFile( File checkstyleTestDir, String fileName ) throws IOException
164     {
165         return new String( Files.readAllBytes( checkstyleTestDir.toPath().resolve( fileName ) ) );
166     }
167 
168 }