author | herrick |
Wed, 16 Oct 2019 10:32:08 -0400 | |
branch | JDK-8200758-branch |
changeset 58648 | 3bf53ffa9ae7 |
parent 58418 | 7cd20dbeee36 |
child 58761 | 88e2753a2334 |
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; |
|
58418
7cd20dbeee36
8230974: creating rpm, get error for relative path to the license.txt
herrick
parents:
58416
diff
changeset
|
27 |
import java.nio.file.StandardCopyOption; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
28 |
import java.util.List; |
58304 | 29 |
import java.util.ArrayList; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
30 |
import java.util.Arrays; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
31 |
import java.util.function.Function; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
32 |
import java.util.stream.Collectors; |
58113 | 33 |
import jdk.jpackage.test.JPackageCommand; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
34 |
import jdk.jpackage.test.PackageType; |
58113 | 35 |
import jdk.jpackage.test.PackageTest; |
36 |
import jdk.jpackage.test.LinuxHelper; |
|
37 |
import jdk.jpackage.test.Executor; |
|
58416 | 38 |
import jdk.jpackage.test.TKit; |
58113 | 39 |
|
40 |
/** |
|
58648 | 41 |
* Test --license-file parameter. Output of the test should be commonlicensetest*.* |
58113 | 42 |
* package bundle. The output package should provide the same functionality as |
43 |
* the default package and also incorporate license information from |
|
44 |
* test/jdk/tools/jpackage/resources/license.txt file from OpenJDK repo. |
|
45 |
* |
|
46 |
* deb: |
|
47 |
* |
|
58648 | 48 |
* Package should install license file /opt/commonlicensetest/share/doc/copyright |
58113 | 49 |
* file. |
50 |
* |
|
51 |
* rpm: |
|
52 |
* |
|
53 |
* Package should install license file in |
|
54 |
* %{_defaultlicensedir}/licensetest-1.0/license.txt file. |
|
55 |
* |
|
56 |
* Mac: |
|
57 |
* |
|
58 |
* Windows |
|
59 |
* |
|
60 |
* Installer should display license text matching contents of the license file |
|
61 |
* during installation. |
|
62 |
*/ |
|
63 |
||
64 |
/* |
|
65 |
* @test |
|
66 |
* @summary jpackage with --license-file |
|
67 |
* @library ../helpers |
|
58416 | 68 |
* @build jdk.jpackage.test.* |
69 |
* @compile LicenseTest.java |
|
58113 | 70 |
* @modules jdk.jpackage/jdk.jpackage.internal |
58416 | 71 |
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main |
72 |
* --jpt-run=LicenseTest.testCommon |
|
58113 | 73 |
*/ |
58304 | 74 |
|
75 |
/* |
|
76 |
* @test |
|
77 |
* @summary jpackage with --license-file |
|
78 |
* @library ../helpers |
|
79 |
* @modules jdk.jpackage/jdk.jpackage.internal |
|
58416 | 80 |
* @compile LicenseTest.java |
58304 | 81 |
* @requires (os.family == "linux") |
58416 | 82 |
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main |
83 |
* --jpt-run=LicenseTest.testCustomDebianCopyright |
|
84 |
* --jpt-run=LicenseTest.testCustomDebianCopyrightSubst |
|
58304 | 85 |
*/ |
86 |
||
58113 | 87 |
public class LicenseTest { |
58304 | 88 |
public static void testCommon() { |
89 |
new PackageTest().configureHelloApp() |
|
90 |
.addInitializer(cmd -> { |
|
58648 | 91 |
cmd.addArguments("--license-file", TKit.createRelativePathCopy( |
92 |
LICENSE_FILE)); |
|
58304 | 93 |
}) |
94 |
.forTypes(PackageType.LINUX) |
|
95 |
.addBundleVerifier(cmd -> { |
|
96 |
verifyLicenseFileInLinuxPackage(cmd, linuxLicenseFile(cmd)); |
|
97 |
}) |
|
98 |
.addInstallVerifier(cmd -> { |
|
58416 | 99 |
TKit.assertReadableFileExists(linuxLicenseFile(cmd)); |
58304 | 100 |
}) |
101 |
.addUninstallVerifier(cmd -> { |
|
102 |
verifyLicenseFileNotInstalledLinux(linuxLicenseFile(cmd)); |
|
103 |
}) |
|
104 |
.forTypes(PackageType.LINUX_DEB) |
|
105 |
.addInstallVerifier(cmd -> { |
|
106 |
verifyLicenseFileInstalledDebian(debLicenseFile(cmd)); |
|
107 |
}) |
|
108 |
.forTypes(PackageType.LINUX_RPM) |
|
109 |
.addInstallVerifier(cmd -> { |
|
110 |
verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd)); |
|
111 |
}) |
|
112 |
.run(); |
|
113 |
} |
|
114 |
||
115 |
public static void testCustomDebianCopyright() { |
|
116 |
new CustomDebianCopyrightTest().run(); |
|
117 |
} |
|
118 |
||
119 |
public static void testCustomDebianCopyrightSubst() { |
|
120 |
new CustomDebianCopyrightTest().withSubstitution(true).run(); |
|
121 |
} |
|
58113 | 122 |
|
123 |
private static Path rpmLicenseFile(JPackageCommand cmd) { |
|
124 |
final Path licenseRoot = Path.of( |
|
125 |
new Executor() |
|
126 |
.setExecutable("rpm") |
|
127 |
.addArguments("--eval", "%{_defaultlicensedir}") |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
128 |
.executeAndGetFirstLineOfOutput()); |
58113 | 129 |
final Path licensePath = licenseRoot.resolve(String.format("%s-%s", |
130 |
LinuxHelper.getPackageName(cmd), cmd.version())).resolve( |
|
131 |
LICENSE_FILE.getFileName()); |
|
132 |
return licensePath; |
|
133 |
} |
|
134 |
||
58304 | 135 |
private static Path linuxLicenseFile(JPackageCommand cmd) { |
136 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
137 |
switch (cmd.packageType()) { |
|
138 |
case LINUX_DEB: |
|
139 |
return debLicenseFile(cmd); |
|
140 |
||
141 |
case LINUX_RPM: |
|
142 |
return rpmLicenseFile(cmd); |
|
143 |
||
144 |
default: |
|
145 |
return null; |
|
146 |
} |
|
147 |
} |
|
148 |
||
58113 | 149 |
private static Path debLicenseFile(JPackageCommand cmd) { |
58303
88453b906981
8231279 : Change install location for copyright file for Debian package
herrick
parents:
58301
diff
changeset
|
150 |
return cmd.appInstallationDirectory().resolve("share/doc/copyright"); |
58113 | 151 |
} |
152 |
||
153 |
private static void verifyLicenseFileInLinuxPackage(JPackageCommand cmd, |
|
154 |
Path expectedLicensePath) { |
|
58416 | 155 |
TKit.assertTrue(LinuxHelper.getPackageFiles(cmd).filter(path -> path.equals( |
58113 | 156 |
expectedLicensePath)).findFirst().orElse(null) != null, |
157 |
String.format("Check license file [%s] is in %s package", |
|
158 |
expectedLicensePath, LinuxHelper.getPackageName(cmd))); |
|
159 |
} |
|
160 |
||
58304 | 161 |
private static void verifyLicenseFileInstalledRpm(Path licenseFile) throws |
162 |
IOException { |
|
58416 | 163 |
TKit.assertStringListEquals(Files.readAllLines(LICENSE_FILE), |
58304 | 164 |
Files.readAllLines(licenseFile), String.format( |
165 |
"Check contents of package license file [%s] are the same as contents of source license file [%s]", |
|
166 |
licenseFile, LICENSE_FILE)); |
|
58113 | 167 |
} |
168 |
||
58304 | 169 |
private static void verifyLicenseFileInstalledDebian(Path licenseFile) |
170 |
throws IOException { |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
171 |
|
58304 | 172 |
List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile( |
173 |
line -> !line.startsWith("License:")).collect( |
|
174 |
Collectors.toList()); |
|
175 |
// Remove leading `License:` followed by the whitespace from the first text line. |
|
176 |
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
|
177 |
|
58304 | 178 |
actualLines = DEBIAN_COPYRIGT_FILE_STRIPPER.apply(actualLines); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
179 |
|
58416 | 180 |
TKit.assertNotEquals(0, String.join("\n", actualLines).length(), |
58304 | 181 |
"Check stripped license text is not empty"); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
182 |
|
58416 | 183 |
TKit.assertStringListEquals(DEBIAN_COPYRIGT_FILE_STRIPPER.apply( |
58304 | 184 |
Files.readAllLines(LICENSE_FILE)), actualLines, String.format( |
185 |
"Check subset of package license file [%s] is a match of the source license file [%s]", |
|
186 |
licenseFile, LICENSE_FILE)); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
187 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
188 |
|
58113 | 189 |
private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) { |
58416 | 190 |
TKit.assertPathExists(licenseFile.getParent(), false); |
58304 | 191 |
} |
192 |
||
193 |
private static class CustomDebianCopyrightTest { |
|
194 |
CustomDebianCopyrightTest() { |
|
195 |
withSubstitution(false); |
|
196 |
} |
|
197 |
||
198 |
private List<String> licenseFileText(String copyright, String licenseText) { |
|
199 |
List<String> lines = new ArrayList(List.of( |
|
200 |
String.format("Copyright=%s", copyright), |
|
201 |
"Foo", |
|
202 |
"Bar", |
|
203 |
"Buz")); |
|
204 |
lines.addAll(List.of(licenseText.split("\\R", -1))); |
|
205 |
return lines; |
|
206 |
} |
|
207 |
||
208 |
private List<String> licenseFileText() { |
|
209 |
if (withSubstitution) { |
|
210 |
return licenseFileText("APPLICATION_COPYRIGHT", |
|
211 |
"APPLICATION_LICENSE_TEXT"); |
|
212 |
} else { |
|
213 |
return expetedLicenseFileText(); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
private List<String> expetedLicenseFileText() { |
|
218 |
return licenseFileText(copyright, licenseText); |
|
219 |
} |
|
220 |
||
221 |
CustomDebianCopyrightTest withSubstitution(boolean v) { |
|
222 |
withSubstitution = v; |
|
223 |
// Different values just to make easy to figure out from the test log which test was executed. |
|
224 |
if (v) { |
|
225 |
copyright = "Duke (C)"; |
|
226 |
licenseText = "The quick brown fox\n jumps over the lazy dog"; |
|
227 |
} else { |
|
228 |
copyright = "Java (C)"; |
|
229 |
licenseText = "How vexingly quick daft zebras jump!"; |
|
230 |
} |
|
231 |
return this; |
|
232 |
} |
|
233 |
||
234 |
void run() { |
|
58416 | 235 |
final Path srcLicenseFile = TKit.workDir().resolve("license"); |
58304 | 236 |
new PackageTest().configureHelloApp().forTypes(PackageType.LINUX_DEB) |
237 |
.addInitializer(cmd -> { |
|
238 |
// Create source license file. |
|
239 |
Files.write(srcLicenseFile, List.of( |
|
240 |
licenseText.split("\\R", -1))); |
|
241 |
||
242 |
cmd.setFakeRuntime(); |
|
243 |
cmd.setArgumentValue("--name", String.format("%s%s", |
|
244 |
withSubstitution ? "CustomDebianCopyrightWithSubst" : "CustomDebianCopyright", |
|
245 |
cmd.name())); |
|
246 |
cmd.addArguments("--license-file", srcLicenseFile); |
|
247 |
cmd.addArguments("--copyright", copyright); |
|
248 |
cmd.addArguments("--resource-dir", RESOURCE_DIR); |
|
249 |
||
250 |
// Create copyright template file in a resource dir. |
|
251 |
Files.createDirectories(RESOURCE_DIR); |
|
252 |
Files.write(RESOURCE_DIR.resolve("copyright"), |
|
253 |
licenseFileText()); |
|
254 |
}) |
|
255 |
.addInstallVerifier(cmd -> { |
|
256 |
Path installedLicenseFile = debLicenseFile(cmd); |
|
58416 | 257 |
TKit.assertStringListEquals(expetedLicenseFileText(), |
58304 | 258 |
DEBIAN_COPYRIGT_FILE_STRIPPER.apply(Files.readAllLines( |
259 |
installedLicenseFile)), String.format( |
|
260 |
"Check contents of package license file [%s] are the same as contents of source license file [%s]", |
|
261 |
installedLicenseFile, srcLicenseFile)); |
|
262 |
}) |
|
263 |
.run(); |
|
264 |
} |
|
265 |
||
266 |
private boolean withSubstitution; |
|
267 |
private String copyright; |
|
268 |
private String licenseText; |
|
269 |
||
58416 | 270 |
private final Path RESOURCE_DIR = TKit.workDir().resolve("resources"); |
58113 | 271 |
} |
272 |
||
58416 | 273 |
private static final Path LICENSE_FILE = TKit.TEST_SRC_ROOT.resolve( |
58113 | 274 |
Path.of("resources", "license.txt")); |
58304 | 275 |
|
276 |
private static final Function<List<String>, List<String>> DEBIAN_COPYRIGT_FILE_STRIPPER = (lines) -> Arrays.asList( |
|
277 |
String.join("\n", lines).stripTrailing().split("\n")); |
|
58113 | 278 |
} |