From e53303a77b1ac33b9ab11533c1542859bc5ae5f4 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Sat, 22 Jun 2019 14:39:57 +0200 Subject: [PATCH] Fixup #337 Get rid of org.apache.maven.plugin.logging.Log in LicenseMojoUtils --- .../license/AbstractAddThirdPartyMojo.java | 2 +- .../license/AbstractThirdPartyReportMojo.java | 2 +- .../mojo/license/LicenseMojoUtils.java | 101 +++++++++-- .../mojo/license/LicenseMojoUtilsTest.java | 10 +- .../org/codehaus/mojo/license/MockLogger.java | 166 ++---------------- 5 files changed, 112 insertions(+), 169 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java index b914ac704..88a3d66a2 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java @@ -776,7 +776,7 @@ else if ( licenseMergesUrl != null ) } resolvedOverrideUrl = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedOverrideUrl, overrideFile, - overrideUrl, project.getBasedir(), getLog() ); + overrideUrl, project.getBasedir() ); } void consolidate() throws IOException, ArtifactNotFoundException, ArtifactResolutionException, MojoFailureException, diff --git a/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java index dccdbcd0a..61c529c90 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java @@ -410,7 +410,7 @@ protected void executeReport( Locale locale ) throws MavenReportException { resolvedOverrideUrl = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedOverrideUrl, overrideFile, - overrideUrl, project.getBasedir(), getLog() ); + overrideUrl, project.getBasedir() ); Collection details; diff --git a/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java b/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java index 9112bdf52..e5962d6d0 100644 --- a/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java +++ b/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java @@ -26,8 +26,9 @@ import java.nio.file.Files; import java.nio.file.Path; -import org.apache.maven.plugin.logging.Log; import org.codehaus.mojo.license.utils.UrlRequester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Utility methods common to various mojos. @@ -36,8 +37,11 @@ */ public final class LicenseMojoUtils { + private static final LoggerFacade LOG = new Slf4jLoggerFacade( LoggerFactory.getLogger( LicenseMojoUtils.class ) ); + /** A special singleton to pass the information that the URL was not set. */ static final String NO_URL = "file:///inexistent"; + static final String DEFAULT_OVERRIDE_THIRD_PARTY = "src/license/override-THIRD-PARTY.properties"; /** @@ -66,8 +70,24 @@ public static boolean isValid( String url ) * @param basedir {@code basedir} to resolve {@value #DEFAULT_OVERRIDE_THIRD_PARTY} against * @return a valid URL or {@link #NO_URL}, never {@code null} */ + public static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File deprecatedFile, + final String url, File basedir ) + { + return prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, LOG ); + } + + /** + * An overload with explicit {@code log} param for testing purposes. + * + * @param resolvedUrl returns this one if it is not {@code null} and not equal to {@link #NO_URL} + * @param deprecatedFile the deprecated mojo parameter + * @param url the newer variant of the mojo parameter + * @param basedir {@code basedir} to resolve {@value #DEFAULT_OVERRIDE_THIRD_PARTY} against + * @param log the log to write to + * @return a valid URL or {@link #NO_URL}, never {@code null} + */ static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File deprecatedFile, final String url, - File basedir, Log log ) + File basedir, LoggerFacade log ) { if ( deprecatedFile != null ) { @@ -77,7 +97,7 @@ static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File } private static String prepareUrl( final String resolvedUrl, final File deprecatedFile, final String url, - File basedir, String defaultFilePath, Log log ) + File basedir, String defaultFilePath, LoggerFacade log ) { if ( resolvedUrl != null && !NO_URL.equals( resolvedUrl ) ) { @@ -94,12 +114,12 @@ private static String prepareUrl( final String resolvedUrl, final File deprecate if ( deprecatedFile.exists() ) { String result = deprecatedFile.toURI().toString(); - log.debug( "Loading overrides from file " + result ); + log.debug( "Loading overrides from file {}", result ); return result; } else { - log.warn( "overrideFile [" + deprecatedFile.getAbsolutePath() + "] was configured but doesn't exist" ); + log.warn( "overrideFile [{}] was configured but doesn't exist", deprecatedFile.getAbsolutePath() ); } } @@ -107,14 +127,14 @@ private static String prepareUrl( final String resolvedUrl, final File deprecate { if ( UrlRequester.isStringUrl( url ) ) { - log.debug( "Loading overrides from URL " + url ); + log.debug( "Loading overrides from URL {}", url ); return url; } else { - log.warn( "Unsupported or invalid URL [" + url + "] found in overrideUrl; " - + "supported are 'classpath:' URLs and anything your JVM supports " - + "(file:, http: and https: should always work)" ); + log.warn( "Unsupported or invalid URL [{}] found in overrideUrl; " + + "supported are 'classpath:' URLs and anything your JVM supports " + + "(file:, http: and https: should always work)", url ); } } @@ -124,13 +144,70 @@ private static String prepareUrl( final String resolvedUrl, final File deprecate if ( Files.exists( defaultPath ) ) { String result = defaultPath.toUri().toString(); - log.debug( "Loading overrides from file " + result ); + log.debug( "Loading overrides from file {}", result ); return result; } - log.debug( "No (valid) URL and no file [" + defaultPath.toAbsolutePath() - + "] found; not loading any overrides" ); + log.debug( "No (valid) URL and no file [{}] found; not loading any overrides", defaultPath.toAbsolutePath() ); return NO_URL; } + /** + * A {@link LoggerFacade} implementation backed by a SLF4J {@link Logger}. + */ + static class Slf4jLoggerFacade + implements LoggerFacade + { + + private final Logger delegate; + + public Slf4jLoggerFacade( Logger delegate ) + { + super(); + this.delegate = delegate; + } + + @Override + public void warn( String message ) + { + delegate.warn( message ); + } + + @Override + public void warn( String template, Object param ) + { + delegate.warn( template, param ); + } + + @Override + public void debug( String template, Object param ) + { + delegate.debug( template, param ); + } + + } + + /** + * A simplified facade of {@link Logger} to be able to implement a test logger easily. + */ + interface LoggerFacade + { + + /** + * @see Logger#warn(String) + */ + void warn( String message ); + + /** + * @see Logger#warn(String, Object) + */ + void warn( String template, Object param ); + + /** + * @see Logger#debug(String, Object) + */ + void debug( String template, Object param ); + + } + } diff --git a/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java b/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java index 1ae02db3a..b36d67c23 100644 --- a/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java +++ b/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java @@ -86,7 +86,7 @@ public void testPrepareThirdPartyOverrideUrlNonExistingDeprecatedFile() + "valid=false\n" + "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead.\n" + "WARN overrideFile [.../foo] was configured but doesn't exist\n" - + "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides" + + "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides\n" , actual ); } @@ -99,7 +99,7 @@ public void testPrepareThirdPartyOverrideUrlDeprecatedFile() "resolved=file:/.../overrides.properties\n" + "valid=true\n" + "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead.\n" - + "DEBUG Loading overrides from file file:/.../overrides.properties" + + "DEBUG Loading overrides from file file:/.../overrides.properties\n" , actual ); } @@ -110,7 +110,7 @@ public void testPrepareThirdPartyOverrideClasspathResource() String actual = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log ); assertEquals( url, actual ); assertTrue( LicenseMojoUtils.isValid(actual) ); - assertEquals( "DEBUG Loading overrides from URL classpath:overrides.properties", log.dump() ); + assertEquals( "DEBUG Loading overrides from URL classpath:overrides.properties\n", log.dump() ); } @Test @@ -122,7 +122,7 @@ public void testPrepareThirdPartyOverrideInvalidUrl() "resolved=file:///inexistent\n" + "valid=false\n" + "WARN Unsupported or invalid URL [foo://localhost/bar] found in overrideUrl; supported are 'classpath:' URLs and anything your JVM supports (file:, http: and https: should always work)\n" - + "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides" + + "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides\n" , actual ); } @@ -136,7 +136,7 @@ public void testPrepareThirdPartyOverridePreventReinit() assertEquals( "resolved=classpath:overrides.properties\n" + "valid=true\n" - + "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead." + + "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead.\n" , actual ); } diff --git a/src/test/java/org/codehaus/mojo/license/MockLogger.java b/src/test/java/org/codehaus/mojo/license/MockLogger.java index 4fe977cd0..4e6afe588 100644 --- a/src/test/java/org/codehaus/mojo/license/MockLogger.java +++ b/src/test/java/org/codehaus/mojo/license/MockLogger.java @@ -1,178 +1,44 @@ package org.codehaus.mojo.license; -import java.util.ArrayList; -import java.util.List; +import java.util.regex.Matcher; -import org.apache.maven.plugin.logging.Log; -import org.codehaus.plexus.util.StringUtils; +import org.codehaus.mojo.license.LicenseMojoUtils.LoggerFacade; -public class MockLogger implements Log +public class MockLogger + implements LoggerFacade { - private boolean debugEnabled = true; - private boolean infoEnabled = true; - private boolean warnEnabled = true; - private boolean errorEnabled = true; - private List messages = new ArrayList<>(); - - @Override - public boolean isDebugEnabled() - { - return debugEnabled; - } - - @Override - public void debug( CharSequence content ) - { - add( debugEnabled, "DEBUG", content ); - } - - @Override - public void debug( CharSequence content, Throwable error ) - { - add( debugEnabled, "DEBUG", content, error ); - } - - @Override - public void debug( Throwable error ) - { - add( debugEnabled, "DEBUG", error ); - } - - @Override - public boolean isInfoEnabled() - { - return infoEnabled; - } - - @Override - public void info( CharSequence content ) - { - add( infoEnabled, "INFO", content ); - } - - @Override - public void info( CharSequence content, Throwable error ) - { - add( infoEnabled, "INFO", content, error ); - } - - @Override - public void info( Throwable error ) - { - add( infoEnabled, "INFO", error ); - } - - @Override - public boolean isWarnEnabled() - { - return warnEnabled; - } - - @Override - public void warn( CharSequence content ) - { - add( warnEnabled, "WARN", content ); - } - - @Override - public void warn( CharSequence content, Throwable error ) - { - add( warnEnabled, "WARN", content, error ); + private static final String format(String template, Object param) { + return template.replaceFirst( "\\Q{}\\E", Matcher.quoteReplacement( String.valueOf( param ) ) ); } - @Override - public void warn( Throwable error ) - { - add( warnEnabled, "WARN", error ); - } + private final StringBuilder log = new StringBuilder(); @Override - public boolean isErrorEnabled() + public void warn( String string ) { - return errorEnabled; + log.append( "WARN " ).append( string ).append( "\n" ); } @Override - public void error( CharSequence content ) + public void warn( String template, Object param ) { - add( errorEnabled, "ERROR", content ); + log.append( "WARN " ).append( format( template, param ) ).append( "\n" ); } @Override - public void error( CharSequence content, Throwable error ) - { - add( errorEnabled, "ERROR", content, error ); - } - - @Override - public void error( Throwable error ) - { - add( errorEnabled, "ERROR", error ); - } - - // Builder pattern initialization - - public MockLogger enableDebug( boolean enable ) - { - debugEnabled = enable; - return this; - } - - public MockLogger enableInfo( boolean enable ) + public void debug( String template, Object param ) { - infoEnabled = enable; - return this; - } - - public MockLogger enableWarn( boolean enable ) - { - warnEnabled = enable; - return this; - } - - public MockLogger enableError( boolean enable ) - { - errorEnabled = enable; - return this; - } - - private void add(boolean enabled, String level, Throwable error) { - add(enabled, level, null, error); - } - - private void add(boolean enabled, String level, CharSequence content) { - add(enabled, level, content, null); - } - - private void add(boolean enabled, String level, CharSequence content, Throwable error) { - if (!enabled) { - return; - } - - StringBuilder buffer = new StringBuilder(level); - if (content != null) { - buffer.append( ' ' ).append(content); - } - - if (error != null) { - buffer.append( "\n" ).append(error.toString()); - } - - messages.add( buffer.toString() ); - } - - public List getMessages() - { - return messages; + log.append( "DEBUG " ).append( format( template, param ) ).append( "\n" ); } public String dump() { - return StringUtils.join( messages.iterator(), "\n" ); + return log.toString(); } public void reset() { - messages.clear(); + log.setLength( 0 ); } + }