|
| 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