Skip to content

Commit c4015c1

Browse files
authored
Merge pull request #333 from digulla/bugfix/331-overrideUrl_doesnt_work
Fix #331 overrideUrl doesn't work
2 parents c0ff2d7 + cd63928 commit c4015c1

File tree

4 files changed

+375
-9
lines changed

4 files changed

+375
-9
lines changed

src/main/java/org/codehaus/mojo/license/LicenseMojoUtils.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File
7373
{
7474
log.warn( "'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead." );
7575
}
76-
return prepareUrl( resolvedUrl, deprecatedFile, url, basedir, DEFAULT_OVERRIDE_THIRD_PARTY );
76+
return prepareUrl( resolvedUrl, deprecatedFile, url, basedir, DEFAULT_OVERRIDE_THIRD_PARTY, log );
7777
}
7878

7979
private static String prepareUrl( final String resolvedUrl, final File deprecatedFile, final String url,
80-
File basedir, String defaultFilePath )
80+
File basedir, String defaultFilePath, Log log )
8181
{
8282
if ( resolvedUrl != null && !NO_URL.equals( resolvedUrl ) )
8383
{
@@ -89,25 +89,47 @@ private static String prepareUrl( final String resolvedUrl, final File deprecate
8989
throw new IllegalArgumentException( "You can't use both overrideFile and overrideUrl" );
9090
}
9191

92-
if ( deprecatedFile != null && deprecatedFile.exists() )
92+
if ( deprecatedFile != null )
9393
{
94-
return deprecatedFile.toURI().toString();
94+
if ( deprecatedFile.exists() )
95+
{
96+
String result = deprecatedFile.toURI().toString();
97+
log.debug( "Loading overrides from file " + result );
98+
return result;
99+
}
100+
else
101+
{
102+
log.warn( "overrideFile [" + deprecatedFile.getAbsolutePath() + "] was configured but doesn't exist" );
103+
}
95104
}
96105

97-
final Path basedirPath = basedir.toPath();
98-
99-
if ( url != null && UrlRequester.isStringUrl( url ) )
106+
if ( url != null )
100107
{
101-
return basedirPath.toUri().toString();
108+
if ( UrlRequester.isStringUrl( url ) )
109+
{
110+
log.debug( "Loading overrides from URL " + url );
111+
return url;
112+
}
113+
else
114+
{
115+
log.warn( "Unsupported or invalid URL [" + url + "] found in overrideUrl; "
116+
+ "supported are 'classpath:' URLs and anything your JVM supports "
117+
+ "(file:, http: and https: should always work)" );
118+
}
102119
}
103120

121+
final Path basedirPath = basedir.toPath();
104122
final Path defaultPath = basedirPath.resolve( defaultFilePath );
105123

106124
if ( Files.exists( defaultPath ) )
107125
{
108-
return defaultPath.toUri().toString();
126+
String result = defaultPath.toUri().toString();
127+
log.debug( "Loading overrides from file " + result );
128+
return result;
109129
}
110130

131+
log.debug( "No (valid) URL and no file [" + defaultPath.toAbsolutePath()
132+
+ "] found; not loading any overrides" );
111133
return NO_URL;
112134
}
113135

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package org.codehaus.mojo.license;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
7+
import org.junit.Test;
8+
9+
public class LicenseMojoUtilsTest
10+
{
11+
private String resolvedUrl;
12+
private File deprecatedFile;
13+
private String url;
14+
private File basedir = new File( "" );
15+
private MockLogger log = new MockLogger();
16+
17+
@Test
18+
public void testIsValidNull()
19+
{
20+
assertFalse( LicenseMojoUtils.isValid( null ) );
21+
}
22+
23+
@Test
24+
public void testIsValidEmpty()
25+
{
26+
// This might be wrong; feel free to change the test when it starts to fail
27+
assertTrue( LicenseMojoUtils.isValid( "" ) );
28+
}
29+
30+
@Test
31+
public void testIsValidBlank()
32+
{
33+
// This might be wrong; feel free to change the test when it starts to fail
34+
assertTrue( LicenseMojoUtils.isValid( " " ) );
35+
}
36+
37+
@Test
38+
public void testIsValidNonexistingClasspathResource()
39+
{
40+
assertTrue( LicenseMojoUtils.isValid( "classpath:noSuchResource" ) );
41+
}
42+
43+
@Test
44+
public void testIsValidClasspathResource()
45+
{
46+
assertTrue( LicenseMojoUtils.isValid( "classpath:log4j.properties" ) );
47+
}
48+
49+
@Test
50+
public void testIsValidHttpResource()
51+
{
52+
assertTrue( LicenseMojoUtils.isValid( "http://foo/bar/baz" ) );
53+
}
54+
55+
@Test
56+
public void testPrepareThirdPartyOverrideUrlNull()
57+
{
58+
String actual = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log );
59+
assertEquals( LicenseMojoUtils.NO_URL, actual );
60+
}
61+
62+
@Test
63+
public void testPrepareThirdPartyOverrideUrlBothOverrides()
64+
{
65+
deprecatedFile = new File( "src/test/resources/overrides.properties" );
66+
url = "classpath:overrides.properties";
67+
try
68+
{
69+
LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log );
70+
71+
fail( "Missing exception" );
72+
}
73+
catch( IllegalArgumentException e )
74+
{
75+
assertEquals( "You can't use both overrideFile and overrideUrl", e.getMessage() );
76+
}
77+
}
78+
79+
@Test
80+
public void testPrepareThirdPartyOverrideUrlNonExistingDeprecatedFile()
81+
{
82+
deprecatedFile = new File( "foo" );
83+
String actual = runTestAndJoinResults();
84+
assertEquals(
85+
"resolved=file:///inexistent\n"
86+
+ "valid=false\n"
87+
+ "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead.\n"
88+
+ "WARN overrideFile [.../foo] was configured but doesn't exist\n"
89+
+ "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides"
90+
, actual );
91+
}
92+
93+
@Test
94+
public void testPrepareThirdPartyOverrideUrlDeprecatedFile()
95+
{
96+
deprecatedFile = new File( "src/test/resources/overrides.properties" );
97+
String actual = runTestAndJoinResults();
98+
assertEquals(
99+
"resolved=file:/.../overrides.properties\n"
100+
+ "valid=true\n"
101+
+ "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead.\n"
102+
+ "DEBUG Loading overrides from file file:/.../overrides.properties"
103+
, actual );
104+
}
105+
106+
@Test
107+
public void testPrepareThirdPartyOverrideClasspathResource()
108+
{
109+
url = "classpath:overrides.properties";
110+
String actual = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log );
111+
assertEquals( url, actual );
112+
assertTrue( LicenseMojoUtils.isValid(actual) );
113+
assertEquals( "DEBUG Loading overrides from URL classpath:overrides.properties", log.dump() );
114+
}
115+
116+
@Test
117+
public void testPrepareThirdPartyOverrideInvalidUrl()
118+
{
119+
url = "foo://localhost/bar";
120+
String actual = runTestAndJoinResults();
121+
assertEquals(
122+
"resolved=file:///inexistent\n"
123+
+ "valid=false\n"
124+
+ "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"
125+
+ "DEBUG No (valid) URL and no file [.../override-THIRD-PARTY.properties] found; not loading any overrides"
126+
, actual );
127+
}
128+
129+
@Test
130+
public void testPrepareThirdPartyOverridePreventReinit()
131+
{
132+
resolvedUrl = "classpath:overrides.properties";
133+
deprecatedFile = new File( "foo" );
134+
url = "classpath:bar";
135+
String actual = runTestAndJoinResults();
136+
assertEquals(
137+
"resolved=classpath:overrides.properties\n"
138+
+ "valid=true\n"
139+
+ "WARN 'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead."
140+
, actual );
141+
}
142+
143+
/** Allow to validate several test results in one assert */
144+
private String runTestAndJoinResults()
145+
{
146+
String result = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedUrl, deprecatedFile, url, basedir, log );
147+
File defaultOverride = new File ( LicenseMojoUtils.DEFAULT_OVERRIDE_THIRD_PARTY );
148+
String dump = log.dump()
149+
.replace( defaultOverride.getAbsolutePath(), ".../" + defaultOverride.getName() );
150+
151+
if ( deprecatedFile != null )
152+
{
153+
dump = dump
154+
.replace( deprecatedFile.toURI().toString(), "file:/.../" + deprecatedFile.getName() )
155+
.replace( deprecatedFile.getAbsolutePath(), ".../" + deprecatedFile.getName() );
156+
result = result
157+
.replace( deprecatedFile.toURI().toString(), "file:/.../" + deprecatedFile.getName() );
158+
}
159+
160+
String actual = "resolved=" + result + "\n"
161+
+ "valid=" + LicenseMojoUtils.isValid( result ) + "\n"
162+
+ dump;
163+
return actual;
164+
}
165+
}

0 commit comments

Comments
 (0)