Skip to content

Commit

Permalink
[MPIR-422] Stop overriding AbstractMavenReport#execute()
Browse files Browse the repository at this point in the history
This closes #41
  • Loading branch information
michael-o committed Jul 7, 2022
1 parent 91d0efe commit 90f65c2
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -36,26 +33,15 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.doxia.site.decoration.Body;
import org.apache.maven.doxia.site.decoration.DecorationModel;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.doxia.siterenderer.RendererException;
import org.apache.maven.doxia.siterenderer.RenderingContext;
import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
import org.apache.maven.doxia.tools.SiteTool;
import org.apache.maven.doxia.tools.SiteToolException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
Expand All @@ -65,7 +51,6 @@
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;

Expand Down Expand Up @@ -200,63 +185,6 @@ public boolean canGenerateReport()
return !skip;
}

@Override
public void execute()
throws MojoExecutionException
{
if ( !canGenerateReport() )
{
return;
}

// TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
Writer writer = null;
try
{
String filename = getOutputName() + ".html";

DecorationModel model = new DecorationModel();
model.setBody( new Body() );

Map<String, Object> attributes = new HashMap<>();
attributes.put( "outputEncoding", "UTF-8" );
attributes.put( "project", project );

Locale locale = Locale.getDefault();
Artifact defaultSkin =
siteTool.getDefaultSkinArtifact( localRepository, project.getRemoteArtifactRepositories() );

SiteRenderingContext siteContext = siteRenderer.createContextForSkin( defaultSkin, attributes,
model, getName( locale ), locale );

RenderingContext context = new RenderingContext( outputDirectory, filename, null );

SiteRendererSink sink = new SiteRendererSink( context );

generate( sink, null, locale );

outputDirectory.mkdirs();

writer = new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ), "UTF-8" );

siteRenderer.mergeDocumentIntoSite( writer, sink, siteContext );

siteRenderer.copyResources( siteContext, outputDirectory );

writer.close();
writer = null;
}
catch ( RendererException | IOException | SiteToolException | MavenReportException e )
{
throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
+ " report generation.", e );
}
finally
{
IOUtil.close( writer );
}
}

@Override
public String getCategoryName()
{
Expand Down Expand Up @@ -374,15 +302,6 @@ protected String getPluginParameter( String pluginId, String param )
return null;
}

/**
* {@inheritDoc}
*/
@Override
protected Renderer getSiteRenderer()
{
return siteRenderer;
}

/**
* @param locale The locale
* @param key The key to search for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected File generateReport( String goal, String pluginXml )
{
File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
AbstractProjectInfoReport mojo = createReportMojo( goal, pluginXmlFile );
return generateReport( mojo, pluginXmlFile);
return generateReport( mojo, pluginXmlFile );
}

protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.ci-management.name" ),
getString( "report.ci-management.title" ) );
String expectedTitle = getString( "report.ci-management.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.dependencies.name" ),
getString( "report.dependencies.title" ) );
String expectedTitle = getString( "report.dependencies.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the tables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.dependency-convergence.name" ),
getString( "report.dependency-convergence.reactor.title" ) );
String expectedTitle = getString( "report.dependency-convergence.reactor.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.dependency-management.name" ),
getString( "report.dependency-management.title" ) );
String expectedTitle = getString( "report.dependency-management.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the tables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()

// Test the Page title
// Index does not have a 'name' but 'title' only
String expectedTitle = prepareTitle( getString( "report.index.title" ),
getString( "report.index.title" ) );
String expectedTitle = getString( "report.index.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.issue-management.name" ),
getString( "report.issue-management.title" ) );
String expectedTitle = getString( "report.issue-management.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.licenses.name" ),
getString( "report.licenses.title" ) );
String expectedTitle = getString( "report.licenses.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down Expand Up @@ -99,8 +98,7 @@ public void testReportLinksOnly()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.licenses.name" ),
getString( "report.licenses.title" ) );
String expectedTitle = getString( "report.licenses.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.mailing-lists.name" ),
getString( "report.mailing-lists.title" ) );
String expectedTitle = getString( "report.mailing-lists.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public class ModulesReportTest
* WebConversation object
*/
private static final WebConversation WEB_CONVERSATION = new WebConversation();

@Override
protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
throws Exception
{
AbstractProjectInfoReport mojo = super.createReportMojo( goal, pluginXmlFile );

mojo.setLog( new SilentLog() );

return mojo;
}

Expand All @@ -81,7 +81,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.modules.name" ), getString( "report.modules.title" ) );
String expectedTitle = getString( "report.modules.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,31 @@ public class PluginManagementReportTest
*/
private static final WebConversation WEB_CONVERSATION = new WebConversation();



@Override
protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
throws Exception
{
AbstractProjectInfoReport mojo = super.createReportMojo( goal, pluginXmlFile );

ProjectBuilder builder = mock( ProjectBuilder.class );

when( builder.build( isA( Artifact.class ),
isA( ProjectBuildingRequest.class ) ) ).thenAnswer( new Answer<ProjectBuildingResult>()
{
@Override
public ProjectBuildingResult answer( InvocationOnMock invocation )
throws Throwable
{
return createProjectBuildingResult( (Artifact) invocation.getArgument( 0 ),
return createProjectBuildingResult( (Artifact) invocation.getArgument( 0 ),
"http://m.a.o/" );
}
} );

setVariableValueToObject( mojo, "projectBuilder", builder );

return mojo;
}

/**
* Test report
*
Expand All @@ -103,8 +101,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.plugin-management.name" ),
getString( "report.plugin-management.title" ) );
String expectedTitle = getString( "report.plugin-management.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the tables
Expand Down Expand Up @@ -142,8 +139,7 @@ public void testReportEclipseM2EPluginLifecycleMapping()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.plugin-management.name" ),
getString( "report.plugin-management.title" ) );
String expectedTitle = getString( "report.plugin-management.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the tables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.scm.name" ),
getString( "report.scm.title" ) );
String expectedTitle = getString( "report.scm.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
TextBlock[] textBlocks = response.getTextBlocks();

assertEquals( textBlocks.length, 6 );

assertEquals( 6, textBlocks.length );
assertEquals( getString( "report.scm.overview.title" ), textBlocks[0].getText() );
assertEquals( getString( "report.scm.general.intro" ), textBlocks[1].getText() );
assertEquals( getString( "report.scm.webaccess.title" ), textBlocks[2].getText() );
Expand All @@ -93,8 +90,7 @@ public void testReportWithWrongUrl()
{
File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/"
+ "scm-wrong-url-plugin-config.xml" );
Mojo mojo = lookupMojo( "scm", pluginXmlFile );
assertNotNull( "Mojo found.", mojo );
Mojo mojo = createReportMojo( "scm", pluginXmlFile );

setVariableValueToObject( mojo, "anonymousConnection", "scm:svn" );
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.summary.name" ),
getString( "report.summary.title" ) );
String expectedTitle = getString( "report.summary.title" );
assertEquals( expectedTitle, response.getTitle() );

// Test the texts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,14 @@ public void testReport()
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.team.name" ),
getString( "report.team.title" ) );
String expectedTitle = getString( "report.team.title" );
assertEquals( expectedTitle, response.getTitle() );

assertTrue( response.getText().contains( "gravatar" ));

// Test the texts
TextBlock[] textBlocks = response.getTextBlocks();

assertEquals( textBlocks.length, 7 );

assertEquals( 7, textBlocks.length );
assertEquals( getString( "report.team.intro.title" ), textBlocks[0].getText() );
assertEquals( getString( "report.team.intro.description1" ), textBlocks[1].getText() );
assertEquals( getString( "report.team.intro.description2" ), textBlocks[2].getText() );
Expand Down

0 comments on commit 90f65c2

Please sign in to comment.