author | herrick |
Tue, 24 Sep 2019 13:57:28 -0400 | |
branch | JDK-8200758-branch |
changeset 58304 | 7a61351edad2 |
parent 58303 | 88453b906981 |
child 58416 | f09bf58c1f17 |
permissions | -rw-r--r-- |
58113 | 1 |
/* |
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.nio.file.Files; |
|
26 |
import java.nio.file.Path; |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
27 |
import java.util.List; |
58304 | 28 |
import java.util.ArrayList; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
29 |
import java.util.Arrays; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
30 |
import java.util.function.Function; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
31 |
import java.util.stream.Collectors; |
58113 | 32 |
import jdk.jpackage.test.JPackageCommand; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
33 |
import jdk.jpackage.test.PackageType; |
58113 | 34 |
import jdk.jpackage.test.PackageTest; |
35 |
import jdk.jpackage.test.LinuxHelper; |
|
36 |
import jdk.jpackage.test.Executor; |
|
37 |
import jdk.jpackage.test.Test; |
|
38 |
||
39 |
/** |
|
40 |
* Test --license-file parameter. Output of the test should be licensetest*.* |
|
41 |
* package bundle. The output package should provide the same functionality as |
|
42 |
* the default package and also incorporate license information from |
|
43 |
* test/jdk/tools/jpackage/resources/license.txt file from OpenJDK repo. |
|
44 |
* |
|
45 |
* deb: |
|
46 |
* |
|
47 |
* Package should install license file /usr/share/doc/licensetest/copyright |
|
48 |
* file. |
|
49 |
* |
|
50 |
* rpm: |
|
51 |
* |
|
52 |
* Package should install license file in |
|
53 |
* %{_defaultlicensedir}/licensetest-1.0/license.txt file. |
|
54 |
* |
|
55 |
* Mac: |
|
56 |
* |
|
57 |
* Windows |
|
58 |
* |
|
59 |
* Installer should display license text matching contents of the license file |
|
60 |
* during installation. |
|
61 |
*/ |
|
62 |
||
63 |
/* |
|
64 |
* @test |
|
65 |
* @summary jpackage with --license-file |
|
66 |
* @library ../helpers |
|
67 |
* @modules jdk.jpackage/jdk.jpackage.internal |
|
58304 | 68 |
* @run main/othervm/timeout=360 -Xmx512m LicenseTest testCommon |
58113 | 69 |
*/ |
58304 | 70 |
|
71 |
/* |
|
72 |
* @test |
|
73 |
* @summary jpackage with --license-file |
|
74 |
* @library ../helpers |
|
75 |
* @modules jdk.jpackage/jdk.jpackage.internal |
|
76 |
* @requires (os.family == "linux") |
|
77 |
* @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyright |
|
78 |
* @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyrightSubst |
|
79 |
*/ |
|
80 |
||
58113 | 81 |
public class LicenseTest { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
82 |
public static void main(String[] args) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
83 |
Test.run(args, () -> { |
58304 | 84 |
String testFuncName = args[0]; |
85 |
Test.trace(String.format("Running %s...", testFuncName)); |
|
86 |
Test.getTestClass().getDeclaredMethod(testFuncName).invoke(null); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
87 |
}); |
58304 | 88 |
} |
89 |
||
90 |
public static void testCommon() { |
|
91 |
new PackageTest().configureHelloApp() |
|
92 |
.addInitializer(cmd -> { |
|
93 |
cmd.addArguments("--license-file", LICENSE_FILE); |
|
94 |
}) |
|
95 |
.forTypes(PackageType.LINUX) |
|
96 |
.addBundleVerifier(cmd -> { |
|
97 |
verifyLicenseFileInLinuxPackage(cmd, linuxLicenseFile(cmd)); |
|
98 |
}) |
|
99 |
.addInstallVerifier(cmd -> { |
|
100 |
Test.assertReadableFileExists(linuxLicenseFile(cmd)); |
|
101 |
}) |
|
102 |
.addUninstallVerifier(cmd -> { |
|
103 |
verifyLicenseFileNotInstalledLinux(linuxLicenseFile(cmd)); |
|
104 |
}) |
|
105 |
.forTypes(PackageType.LINUX_DEB) |
|
106 |
.addInstallVerifier(cmd -> { |
|
107 |
verifyLicenseFileInstalledDebian(debLicenseFile(cmd)); |
|
108 |
}) |
|
109 |
.forTypes(PackageType.LINUX_RPM) |
|
110 |
.addInstallVerifier(cmd -> { |
|
111 |
verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd)); |
|
112 |
}) |
|
113 |
.run(); |
|
114 |
} |
|
115 |
||
116 |
public static void testCustomDebianCopyright() { |
|
117 |
new CustomDebianCopyrightTest().run(); |
|
118 |
} |
|
119 |
||
120 |
public static void testCustomDebianCopyrightSubst() { |
|
121 |
new CustomDebianCopyrightTest().withSubstitution(true).run(); |
|
122 |
} |
|
58113 | 123 |
|
124 |
private static Path rpmLicenseFile(JPackageCommand cmd) { |
|
125 |
final Path licenseRoot = Path.of( |
|
126 |
new Executor() |
|
127 |
.setExecutable("rpm") |
|
128 |
.addArguments("--eval", "%{_defaultlicensedir}") |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
129 |
.executeAndGetFirstLineOfOutput()); |
58113 | 130 |
final Path licensePath = licenseRoot.resolve(String.format("%s-%s", |
131 |
LinuxHelper.getPackageName(cmd), cmd.version())).resolve( |
|
132 |
LICENSE_FILE.getFileName()); |
|
133 |
return licensePath; |
|
134 |
} |
|
135 |
||
58304 | 136 |
private static Path linuxLicenseFile(JPackageCommand cmd) { |
137 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
138 |
switch (cmd.packageType()) { |
|
139 |
case LINUX_DEB: |
|
140 |
return debLicenseFile(cmd); |
|
141 |
||
142 |
case LINUX_RPM: |
|
143 |
return rpmLicenseFile(cmd); |
|
144 |
||
145 |
default: |
|
146 |
return null; |
|
147 |
} |
|
148 |
} |
|
149 |
||
58113 | 150 |
private static Path debLicenseFile(JPackageCommand cmd) { |
58303
88453b906981
8231279 : Change install location for copyright file for Debian package
herrick
parents:
58301
diff
changeset
|
151 |
return cmd.appInstallationDirectory().resolve("share/doc/copyright"); |
58113 | 152 |
} |
153 |
||
154 |
private static void verifyLicenseFileInLinuxPackage(JPackageCommand cmd, |
|
155 |
Path expectedLicensePath) { |
|
156 |
Test.assertTrue(LinuxHelper.getPackageFiles(cmd).filter(path -> path.equals( |
|
157 |
expectedLicensePath)).findFirst().orElse(null) != null, |
|
158 |
String.format("Check license file [%s] is in %s package", |
|
159 |
expectedLicensePath, LinuxHelper.getPackageName(cmd))); |
|
160 |
} |
|
161 |
||
58304 | 162 |
private static void verifyLicenseFileInstalledRpm(Path licenseFile) throws |
163 |
IOException { |
|
164 |
Test.assertStringListEquals(Files.readAllLines(LICENSE_FILE), |
|
165 |
Files.readAllLines(licenseFile), String.format( |
|
166 |
"Check contents of package license file [%s] are the same as contents of source license file [%s]", |
|
167 |
licenseFile, LICENSE_FILE)); |
|
58113 | 168 |
} |
169 |
||
58304 | 170 |
private static void verifyLicenseFileInstalledDebian(Path licenseFile) |
171 |
throws IOException { |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
172 |
|
58304 | 173 |
List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile( |
174 |
line -> !line.startsWith("License:")).collect( |
|
175 |
Collectors.toList()); |
|
176 |
// Remove leading `License:` followed by the whitespace from the first text line. |
|
177 |
actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
178 |
|
58304 | 179 |
actualLines = DEBIAN_COPYRIGT_FILE_STRIPPER.apply(actualLines); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
180 |
|
58304 | 181 |
Test.assertNotEquals(0, String.join("\n", actualLines).length(), |
182 |
"Check stripped license text is not empty"); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
183 |
|
58304 | 184 |
Test.assertStringListEquals(DEBIAN_COPYRIGT_FILE_STRIPPER.apply( |
185 |
Files.readAllLines(LICENSE_FILE)), actualLines, String.format( |
|
186 |
"Check subset of package license file [%s] is a match of the source license file [%s]", |
|
187 |
licenseFile, LICENSE_FILE)); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
188 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
189 |
|
58113 | 190 |
private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) { |
58304 | 191 |
Test.assertPathExists(licenseFile.getParent(), false); |
192 |
} |
|
193 |
||
194 |
private static class CustomDebianCopyrightTest { |
|
195 |
CustomDebianCopyrightTest() { |
|
196 |
withSubstitution(false); |
|
197 |
} |
|
198 |
||
199 |
private List<String> licenseFileText(String copyright, String licenseText) { |
|
200 |
List<String> lines = new ArrayList(List.of( |
|
201 |
String.format("Copyright=%s", copyright), |
|
202 |
"Foo", |
|
203 |
"Bar", |
|
204 |
"Buz")); |
|
205 |
lines.addAll(List.of(licenseText.split("\\R", -1))); |
|
206 |
return lines; |
|
207 |
} |
|
208 |
||
209 |
private List<String> licenseFileText() { |
|
210 |
if (withSubstitution) { |
|
211 |
return licenseFileText("APPLICATION_COPYRIGHT", |
|
212 |
"APPLICATION_LICENSE_TEXT"); |
|
213 |
} else { |
|
214 |
return expetedLicenseFileText(); |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
private List<String> expetedLicenseFileText() { |
|
219 |
return licenseFileText(copyright, licenseText); |
|
220 |
} |
|
221 |
||
222 |
CustomDebianCopyrightTest withSubstitution(boolean v) { |
|
223 |
withSubstitution = v; |
|
224 |
// Different values just to make easy to figure out from the test log which test was executed. |
|
225 |
if (v) { |
|
226 |
copyright = "Duke (C)"; |
|
227 |
licenseText = "The quick brown fox\n jumps over the lazy dog"; |
|
228 |
} else { |
|
229 |
copyright = "Java (C)"; |
|
230 |
licenseText = "How vexingly quick daft zebras jump!"; |
|
231 |
} |
|
232 |
return this; |
|
233 |
} |
|
234 |
||
235 |
void run() { |
|
236 |
final Path srcLicenseFile = Test.workDir().resolve("license"); |
|
237 |
new PackageTest().configureHelloApp().forTypes(PackageType.LINUX_DEB) |
|
238 |
.addInitializer(cmd -> { |
|
239 |
// Create source license file. |
|
240 |
Files.write(srcLicenseFile, List.of( |
|
241 |
licenseText.split("\\R", -1))); |
|
242 |
||
243 |
cmd.setFakeRuntime(); |
|
244 |
cmd.setArgumentValue("--name", String.format("%s%s", |
|
245 |
withSubstitution ? "CustomDebianCopyrightWithSubst" : "CustomDebianCopyright", |
|
246 |
cmd.name())); |
|
247 |
cmd.addArguments("--license-file", srcLicenseFile); |
|
248 |
cmd.addArguments("--copyright", copyright); |
|
249 |
cmd.addArguments("--resource-dir", RESOURCE_DIR); |
|
250 |
||
251 |
// Create copyright template file in a resource dir. |
|
252 |
Files.createDirectories(RESOURCE_DIR); |
|
253 |
Files.write(RESOURCE_DIR.resolve("copyright"), |
|
254 |
licenseFileText()); |
|
255 |
}) |
|
256 |
.addInstallVerifier(cmd -> { |
|
257 |
Path installedLicenseFile = debLicenseFile(cmd); |
|
258 |
Test.assertStringListEquals(expetedLicenseFileText(), |
|
259 |
DEBIAN_COPYRIGT_FILE_STRIPPER.apply(Files.readAllLines( |
|
260 |
installedLicenseFile)), String.format( |
|
261 |
"Check contents of package license file [%s] are the same as contents of source license file [%s]", |
|
262 |
installedLicenseFile, srcLicenseFile)); |
|
263 |
}) |
|
264 |
.run(); |
|
265 |
} |
|
266 |
||
267 |
private boolean withSubstitution; |
|
268 |
private String copyright; |
|
269 |
private String licenseText; |
|
270 |
||
271 |
private final Path RESOURCE_DIR = Test.workDir().resolve("resources"); |
|
58113 | 272 |
} |
273 |
||
274 |
private static final Path LICENSE_FILE = Test.TEST_SRC_ROOT.resolve( |
|
275 |
Path.of("resources", "license.txt")); |
|
58304 | 276 |
|
277 |
private static final Function<List<String>, List<String>> DEBIAN_COPYRIGT_FILE_STRIPPER = (lines) -> Arrays.asList( |
|
278 |
String.join("\n", lines).stripTrailing().split("\n")); |
|
58113 | 279 |
} |