forked from mojohaus/license-maven-plugin
/
LicenseMojoUtilsTest.java
165 lines (144 loc) · 5.8 KB
/
LicenseMojoUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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\n"
, 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\n"
, 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\n", 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\n"
, 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.\n"
, 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;
}
}