/* * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
finalboolean legacyOnly; // for running on older JDK's ( test validation )
// Resources we know to exist, that can be used for creating jar files. staticfinal String RES1 = "CLICompatibility.class"; staticfinal String RES2 = "CLICompatibility$Result.class";
@BeforeTest publicvoid setupResourcesForJar() throws Exception { // Copy the files that we are going to use for creating/updating test // jar files, so that they can be referred to without '-C dir'
Files.copy(TEST_CLASSES.resolve(RES1), USER_DIR.resolve(RES1));
Files.copy(TEST_CLASSES.resolve(RES2), USER_DIR.resolve(RES2));
}
staticfinal IOConsumer<InputStream> ASSERT_CONTAINS_RES1 = in -> { try (JarInputStream jin = new JarInputStream(in)) {
assertTrue(jarContains(jin, RES1), "Failed to find " + RES1);
}
}; staticfinal IOConsumer<InputStream> ASSERT_CONTAINS_RES2 = in -> { try (JarInputStream jin = new JarInputStream(in)) {
assertTrue(jarContains(jin, RES2), "Failed to find " + RES2);
}
}; staticfinal IOConsumer<InputStream> ASSERT_CONTAINS_MAINFEST = in -> { try (JarInputStream jin = new JarInputStream(in)) {
assertTrue(jin.getManifest() != null, "No META-INF/MANIFEST.MF");
}
}; staticfinal IOConsumer<InputStream> ASSERT_DOES_NOT_CONTAIN_MAINFEST = in -> { try (JarInputStream jin = new JarInputStream(in)) {
assertTrue(jin.getManifest() == null, "Found unexpected META-INF/MANIFEST.MF");
}
};
staticfinal FailCheckerWithMessage FAIL_TOO_MANY_MAIN_OPS = new FailCheckerWithMessage("You may not specify more than one '-cuxtid' options", /* legacy */ "{ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files");
// Create
@Test publicvoid createBadArgs() { final FailCheckerWithMessage FAIL_CREATE_NO_ARGS = new FailCheckerWithMessage( "'c' flag requires manifest or input files to be specified!");
@Test publicvoid updateBadArgs() { final FailCheckerWithMessage FAIL_UPDATE_NO_ARGS = new FailCheckerWithMessage( "'u' flag requires manifest, 'e' flag or input files to be specified!");
/* Creates a simple jar with entries of size 0, good enough for testing */ staticvoid createJar(Path path, String... entries) throws IOException {
FileUtils.deleteFileIfExistsWithRetry(path);
Path parent = path.getParent(); if (parent != null)
Files.createDirectories(parent); try (OutputStream out = Files.newOutputStream(path);
JarOutputStream jos = new JarOutputStream(out)) {
JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
jos.putNextEntry(je);
jos.closeEntry();
for (String entry : entries) {
je = new JarEntry(entry);
jos.putNextEntry(je);
jos.closeEntry();
}
}
}
staticclass FailCheckerWithMessage implements Consumer<Result> { final String[] messages;
FailCheckerWithMessage(String... m) {
messages = m;
}
@Override publicvoid accept(Result r) { //out.printf("%s%n", r.output); boolean found = false; for (String m : messages) { if (r.output.contains(m)) {
found = true; break;
}
}
assertTrue(found, "Excepted out to contain one of: " + Arrays.asList(messages)
+ " but got: " + r.output);
}
}
static Result jar(String... args) { return jarWithStdinAndWorkingDir(null, null, args);
}
// readAllBytes implementation so the test can be run pre 1.9 ( legacyOnly ) staticbyte[] readAllBytes(InputStream is) throws IOException { byte[] buf = newbyte[8192]; int capacity = buf.length; int nread = 0; int n; for (;;) { // read to EOF which may read more or less than initial buffer size while ((n = is.read(buf, nread, capacity - nread)) > 0)
nread += n;
// if the last call to read returned -1, then we're done if (n < 0) break;
// need to allocate a larger buffer
capacity = capacity << 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.