diff --git a/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java b/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java index 35351601d..9112bdf52 100644 --- a/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java +++ b/src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java @@ -73,11 +73,11 @@ static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File { log.warn( "'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead." ); } - return prepareUrl( resolvedUrl, deprecatedFile, url, basedir, DEFAULT_OVERRIDE_THIRD_PARTY ); + return prepareUrl( resolvedUrl, deprecatedFile, url, basedir, DEFAULT_OVERRIDE_THIRD_PARTY, log ); } private static String prepareUrl( final String resolvedUrl, final File deprecatedFile, final String url, - File basedir, String defaultFilePath ) + File basedir, String defaultFilePath, Log log ) { if ( resolvedUrl != null && !NO_URL.equals( resolvedUrl ) ) { @@ -89,25 +89,47 @@ private static String prepareUrl( final String resolvedUrl, final File deprecate throw new IllegalArgumentException( "You can't use both overrideFile and overrideUrl" ); } - if ( deprecatedFile != null && deprecatedFile.exists() ) + if ( deprecatedFile != null ) { - return deprecatedFile.toURI().toString(); + if ( deprecatedFile.exists() ) + { + String result = deprecatedFile.toURI().toString(); + log.debug( "Loading overrides from file " + result ); + return result; + } + else + { + log.warn( "overrideFile [" + deprecatedFile.getAbsolutePath() + "] was configured but doesn't exist" ); + } } - final Path basedirPath = basedir.toPath(); - - if ( url != null && UrlRequester.isStringUrl( url ) ) + if ( url != null ) { - return basedirPath.toUri().toString(); + if ( UrlRequester.isStringUrl( 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)" ); + } } + final Path basedirPath = basedir.toPath(); final Path defaultPath = basedirPath.resolve( defaultFilePath ); if ( Files.exists( defaultPath ) ) { - return defaultPath.toUri().toString(); + String result = defaultPath.toUri().toString(); + log.debug( "Loading overrides from file " + result ); + return result; } + log.debug( "No (valid) URL and no file [" + defaultPath.toAbsolutePath() + + "] found; not loading any overrides" ); return NO_URL; } diff --git a/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java b/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java new file mode 100644 index 000000000..1ae02db3a --- /dev/null +++ b/src/test/java/org/codehaus/mojo/license/LicenseMojoUtilsTest.java @@ -0,0 +1,165 @@ +package org.codehaus.mojo.license; + +import static org.junit.Assert.*; + +import java.io.File; + +import org.junit.Test; + +public class LicenseMojoUtilsTest +{ + private String resolvedUrl; + private File deprecatedFile; + private String url; + private File basedir = new File( "" ); + private MockLogger log = new MockLogger(); + + @Test + public void testIsValidNull() + { + assertFalse( LicenseMojoUtils.isValid( null ) ); + } + + @Test + public void testIsValidEmpty() + { + // This might be wrong; feel free to change the test when it starts to fail + assertTrue( LicenseMojoUtils.isValid( "" ) ); + } + + @Test + public void testIsValidBlank() + { + // This might be wrong; feel free to change the test when it starts to fail + assertTrue( LicenseMojoUtils.isValid( " " ) ); + } + + @Test + public void testIsValidNonexistingClasspathResource() + { + assertTrue( LicenseMojoUtils.isValid( "classpath:noSuchResource" ) ); + } + + @Test + public void testIsValidClasspathResource() + { + assertTrue( LicenseMojoUtils.isValid( "classpath:log4j.properties" ) ); + } + + @Test + public void testIsValidHttpResource() + { + assertTrue( LicenseMojoUtils.isValid( "http://foo/bar/baz" ) ); + } + + @Test + public void testPrepareThirdPartyOverrideUrlNull() + { + String actual = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log ); + assertEquals( LicenseMojoUtils.NO_URL, actual ); + } + + @Test + public void testPrepareThirdPartyOverrideUrlBothOverrides() + { + deprecatedFile = new File( "src/test/resources/overrides.properties" ); + url = "classpath:overrides.properties"; + try + { + LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log ); + + fail( "Missing exception" ); + } + catch( IllegalArgumentException e ) + { + assertEquals( "You can't use both overrideFile and overrideUrl", e.getMessage() ); + } + } + + @Test + public void testPrepareThirdPartyOverrideUrlNonExistingDeprecatedFile() + { + deprecatedFile = new File( "foo" ); + String actual = runTestAndJoinResults(); + assertEquals( + "resolved=file:///inexistent\n" + + "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" + , actual ); + } + + @Test + public void testPrepareThirdPartyOverrideUrlDeprecatedFile() + { + deprecatedFile = new File( "src/test/resources/overrides.properties" ); + String actual = runTestAndJoinResults(); + assertEquals( + "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" + , actual ); + } + + @Test + public void testPrepareThirdPartyOverrideClasspathResource() + { + url = "classpath:overrides.properties"; + 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() ); + } + + @Test + public void testPrepareThirdPartyOverrideInvalidUrl() + { + url = "foo://localhost/bar"; + String actual = runTestAndJoinResults(); + assertEquals( + "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" + , actual ); + } + + @Test + public void testPrepareThirdPartyOverridePreventReinit() + { + resolvedUrl = "classpath:overrides.properties"; + deprecatedFile = new File( "foo" ); + url = "classpath:bar"; + String actual = runTestAndJoinResults(); + assertEquals( + "resolved=classpath:overrides.properties\n" + + "valid=true\n" + + "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead." + , actual ); + } + + /** Allow to validate several test results in one assert */ + private String runTestAndJoinResults() + { + String result = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log ); + File defaultOverride = new File ( LicenseMojoUtils.DEFAULT_OVERRIDE_THIRD_PARTY ); + String dump = log.dump() + .replace( defaultOverride.getAbsolutePath(), ".../" + defaultOverride.getName() ); + + if ( deprecatedFile != null ) + { + dump = dump + .replace( deprecatedFile.toURI().toString(), "file:/.../" + deprecatedFile.getName() ) + .replace( deprecatedFile.getAbsolutePath(), ".../" + deprecatedFile.getName() ); + result = result + .replace( deprecatedFile.toURI().toString(), "file:/.../" + deprecatedFile.getName() ); + } + + String actual = "resolved=" + result + "\n" + + "valid=" + LicenseMojoUtils.isValid( result ) + "\n" + + dump; + return actual; + } +} diff --git a/src/test/java/org/codehaus/mojo/license/MockLogger.java b/src/test/java/org/codehaus/mojo/license/MockLogger.java new file mode 100644 index 000000000..4fe977cd0 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/license/MockLogger.java @@ -0,0 +1,178 @@ +package org.codehaus.mojo.license; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.StringUtils; + +public class MockLogger implements Log +{ + 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 ); + } + + @Override + public void warn( Throwable error ) + { + add( warnEnabled, "WARN", error ); + } + + @Override + public boolean isErrorEnabled() + { + return errorEnabled; + } + + @Override + public void error( CharSequence content ) + { + add( errorEnabled, "ERROR", content ); + } + + @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 ) + { + 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; + } + + public String dump() + { + return StringUtils.join( messages.iterator(), "\n" ); + } + + public void reset() + { + messages.clear(); + } +} diff --git a/src/test/resources/overrides.properties b/src/test/resources/overrides.properties new file mode 100644 index 000000000..87fbfbb90 --- /dev/null +++ b/src/test/resources/overrides.properties @@ -0,0 +1 @@ +org.jboss.xnio--xnio-api--3.3.6.Final=The Apache Software License, Version 2.0 \ No newline at end of file