Skip to content

Commit

Permalink
second shot
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Aug 12, 2022
1 parent fe605b4 commit 2b46001
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 466 deletions.
49 changes: 44 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ under the License.
<javaVersion>8</javaVersion><!-- Because PMD 6.35.0+ requires Java 8 -->
<pmdVersion>6.48.0</pmdVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<sitePluginVersion>3.12.1</sitePluginVersion>
<projectInfoReportsPluginVersion>3.4.1</projectInfoReportsPluginVersion>
<project.build.outputTimestamp>2022-05-31T17:37:31Z</project.build.outputTimestamp>
Expand Down Expand Up @@ -203,6 +204,12 @@ under the License.
doxia-site-renderer requires transitively dom4j 1.1 which provides a very old version of jaxen (same jar).
PMD requires a newer version of jaxen, which is not compatible.
-->
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- shared -->
Expand Down Expand Up @@ -243,11 +250,43 @@ under the License.
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<!-- that's a very old version, but it's the newest one that brings in
a old guava version, that is compatible with maven 3.1.1.
maven 3.1.1 uses guava 11, wiremock 1.49 uses guava 13.0.1
-->
<version>1.49</version>
<version>1.58</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-wagon</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-lightweight</artifactId>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
113 changes: 0 additions & 113 deletions src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package org.apache.maven.plugins.pmd;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;

import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.ArtifactStubFactory;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;

/**
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
* @version $Id$
* @since 2.5
*/
public abstract class AbstractPmdReportTestCase
extends AbstractMojoTestCase
{
private ArtifactStubFactory artifactStubFactory;

/**
* The current project to be test.
*/
private MavenProject testMavenProject;

@Override
protected void setUp()
throws Exception
{
super.setUp();
CapturingPrintStream.init( true );

artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
artifactStubFactory.getWorkingDir().mkdirs();
}

/**
* Get the current Maven project
*
* @return the maven project
*/
protected MavenProject getTestMavenProject()
{
return testMavenProject;
}

/**
* Get the generated report as file in the test maven project.
*
* @param name the name of the report.
* @return the generated report as file
* @throws IOException if the return file doesnt exist
*/
protected File getGeneratedReport( String name )
throws IOException
{
String outputDirectory = getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();

File report = new File( outputDirectory, name );
if ( !report.exists() )
{
throw new IOException( "File not found. Attempted: " + report );
}

return report;
}

/**
* Generate the report and return the generated file
*
* @param goal the mojo goal.
* @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
* @return the generated HTML file
* @throws Exception if any
*/
protected File generateReport( String goal, String pluginXml )
throws Exception
{
File pluginXmlFile = new File( getBasedir(), "src/test/resources/unit/" + pluginXml );
AbstractPmdReport mojo = createReportMojo( goal, pluginXmlFile );
return generateReport( mojo, pluginXmlFile );
}

protected AbstractPmdReport createReportMojo( String goal, File pluginXmlFile )
throws Exception
{
AbstractPmdReport mojo = (AbstractPmdReport) lookupMojo( goal, pluginXmlFile );
assertNotNull( "Mojo not found.", mojo );

LegacySupport legacySupport = lookup( LegacySupport.class );
legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
DefaultRepositorySystemSession repoSession =
(DefaultRepositorySystemSession) legacySupport.getRepositorySession();
repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) );

setVariableValueToObject( mojo, "session", legacySupport.getSession() );
setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
return mojo;
}

protected File generateReport( AbstractPmdReport mojo, File pluginXmlFile )
throws Exception
{
mojo.execute();

ProjectBuilder builder = lookup( ProjectBuilder.class );

ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );

testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();

File outputDir = mojo.getReportOutputDirectory();
String filename = mojo.getOutputName() + ".html";

return new File( outputDir, filename );
}

/**
* Read the contents of the specified file object into a string
*/
protected String readFile( File pmdTestDir, String fileName ) throws IOException
{
return new String( Files.readAllBytes( pmdTestDir.toPath().resolve( fileName ) ) );
}


/**
* Checks, whether the string <code>contained</code> is contained in
* the given <code>text</code> ignoring case.
*
* @param text the string in which the search is executed
* @param contains the string, the should be searched
* @return <code>true</code> if the string is contained, otherwise <code>false</code>.
*/
public static boolean lowerCaseContains( String text, String contains )
{
return text.toLowerCase( Locale.ROOT ).contains( contains.toLowerCase( Locale.ROOT ) );
}
}

0 comments on commit 2b46001

Please sign in to comment.