Skip to content

Commit

Permalink
Merge pull request #333 from digulla/bugfix/331-overrideUrl_doesnt_work
Browse files Browse the repository at this point in the history
Fix #331 overrideUrl doesn't work
  • Loading branch information
ppalaga committed May 12, 2019
2 parents c0ff2d7 + cd63928 commit c4015c1
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java
Expand Up @@ -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 ) )
{
Expand All @@ -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;
}

Expand Down
165 changes: 165 additions & 0 deletions 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;
}
}

0 comments on commit c4015c1

Please sign in to comment.