/* * Copyright (c) 2009, 2015, 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.
*/
// Test URIs returned from JavacFileManager and its support classes. // For a variety of file objects, verify the validity of FileObject.toUri() // by verifying the URI exists and points to the same contents as the file // object itself
Set<String> foundClasses = new TreeSet<String>();
Set<String> foundJars = new TreeSet<String>();
void run() throws Exception { // names for entries to be created in directories and jar files
String[] entries = { "p/A.class", "p/resources/A-1.jpg" };
// test various combinations of directories and jar files, intended to // cover all sources of URIs within JavacFileManager's support classes
// Verify that we hit the files we intended
checkCoverage("classes", foundClasses, "DirectoryFileObject", "JarFileObject");
// Verify that we hit the jar files we intended
checkCoverage("jar files", foundJars, "jar", "jar jar");
}
// use a new file manager for each test void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception {
JarURLConnection c;
System.err.println("Test " + f); try {
fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
int count = 0; for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH,
pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
System.err.println("checking " + fo); // record the file object class name for coverage checks later
foundClasses.add(fo.getClass().getSimpleName());
testFileObject(fo);
count++;
}
if (expect > 0 && count != expect) thrownew Exception("wrong number of entries found: "
+ count + ", expected " + expect);
} finally {
fm.close();
}
}
void testFileObject(JavaFileObject fo) throws Exception { // test the validity of the result of toUri() by using URLConnection // and comparing the results of reading from the connection with the // result of reading from the file object directly.
URI uri = fo.toUri();
System.err.println("uri: " + uri);
URLConnection urlconn = uri.toURL().openConnection(); if (urlconn instanceof JarURLConnection) {
JarURLConnection jarconn = (JarURLConnection) urlconn;
File f = new File(jarconn.getJarFile().getName()); // record access to the jar file for coverage checks later
foundJars.add(f.getName());
}
try { byte[] uriData = read(urlconn.getInputStream()); byte[] foData = read(fo.openInputStream()); if (!Arrays.equals(uriData, foData)) { if (uriData.length != foData.length) thrownew Exception("data size differs: uri data "
+ uriData.length + " bytes, fo data " + foData.length+ " bytes"); for (int i = 0; i < uriData.length; i++) { if (uriData[i] != foData[i]) thrownew Exception("unexpected data returned at offset " + i
+ ", uri data " + uriData[i] + ", fo data " + foData[i]);
} thrownew AssertionError("cannot find difference");
}
} finally { // In principle, simply closing the result of urlconn.getInputStream() // should have been sufficient. But the internal JarURLConnection // does not close the JarFile in an expeditious manner, thus preventing // jtreg from deleting the jar file before starting the next test. // Therefore we force access to the JarURLConnection to close the // JarFile when necessary. if (urlconn instanceof JarURLConnection) {
JarURLConnection jarconn = (JarURLConnection) urlconn;
jarconn.getJarFile().close();
}
}
}
void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
Set<String> e = new TreeSet<String>(Arrays.asList(expect)); if (!found.equals(e)) {
e.removeAll(found); thrownew Exception("expected " + label + " not used: " + e);
}
}
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 ist noch experimentell.