undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2014, 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.
*/
import java.io.File;
import java.io.IOException;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
class MyStandardJavaFileManager
extends ForwardingJavaFileManager<StandardJavaFileManager>
implements StandardJavaFileManager {
MyStandardJavaFileManager(StandardJavaFileManager delegate) {
super(delegate);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
return fileManager.getJavaFileObjectsFromFiles(files);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
return fileManager.getJavaFileObjects(files);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
return fileManager.getJavaFileObjectsFromStrings(names);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
return fileManager.getJavaFileObjects(names);
}
@Override
public void setLocation(JavaFileManager.Location location, Iterable<? extends File> files) throws IOException {
fileManager.setLocation(location, files);
}
@Override
public Iterable<? extends File> getLocation(JavaFileManager.Location location) {
return fileManager.getLocation(location);
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2014, 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
* @bug 8059977
* @summary StandardJavaFileManager should support java.nio.file.Path.
* Test asPath method.
* @modules java.compiler
* jdk.compiler
* @build SJFM_TestBase
* @run main SJFM_AsPath
*/
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
/**
* For those paths which are supported by a file manager, such that
* a file object can encapsulate the path, verify that the underlying
* path can be recovered from the file object.
*/
public class SJFM_AsPath extends SJFM_TestBase {
public static void main(String... args) throws Exception {
new SJFM_AsPath().run();
}
@Test
void test_asPath(StandardJavaFileManager fm) throws IOException {
test_asPath(fm, getTestFilePaths());
test_asPath(fm, getTestZipPaths());
}
/**
* Tests the asPath method for a specific file manager and a series
* of paths.
*
* Note: instances of MyStandardJavaFileManager only support
* encapsulating paths for files in the default file system,
* and throw UnsupportedOperationException for asPath.
*
* @param fm the file manager to be tested
* @param paths the paths to be tested
* @throws IOException
*/
void test_asPath(StandardJavaFileManager fm, List<Path> paths) throws IOException {
if (!isGetFileObjectsSupported(fm, paths))
return;
boolean expectException = (fm instanceof MyStandardJavaFileManager);
Set<Path> ref = new HashSet<>(paths);
for (JavaFileObject fo : fm.getJavaFileObjectsFromPaths(paths)) {
try {
Path path = fm.asPath(fo);
if (expectException)
error("expected exception not thrown: " + UnsupportedOperationException.class.getName());
boolean found = ref.remove(path);
if (!found) {
error("Unexpected path found: " + path + "; expected one of " + ref);
}
} catch (Exception e) {
if (expectException && e instanceof UnsupportedOperationException)
continue;
error("unexpected exception thrown: " + e);
}
}
}
}

View file

@ -0,0 +1,174 @@
/*
* Copyright (c) 2014, 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
* @bug 8059977 8220687
* @summary StandardJavaFileManager should support java.nio.file.Path.
* Test getFileObject methods.
* @modules java.compiler
* jdk.compiler
* @build SJFM_TestBase
* @run main SJFM_GetFileObjects
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
/**
* For those paths supported by a file manager, verify that the paths
* can be encapsulated by file objects, such that the file objects can
* be used by a tool such as javac.
*/
public class SJFM_GetFileObjects extends SJFM_TestBase {
public static void main(String... args) throws Exception {
new SJFM_GetFileObjects().run();
}
@Test
void test_getJavaFileObjects(StandardJavaFileManager fm) throws IOException {
test_getJavaFileObjects(fm, getTestFilePaths());
test_getJavaFileObjects(fm, getTestZipPaths());
}
/**
* Tests the getJavaFileObjects method for a specific file manager
* and a series of paths.
*
* Note: instances of MyStandardJavaFileManager only support
* encapsulating paths for files in the default file system.
*
* @param fm the file manager to be tested
* @param paths the paths to be tested
* @throws IOException
*/
void test_getJavaFileObjects(StandardJavaFileManager fm, List<Path> paths) throws IOException {
boolean expectException = !isGetFileObjectsSupported(fm, paths);
try {
compile(fm.getJavaFileObjects(paths.toArray(new Path[paths.size()])));
if (expectException)
error("expected exception not thrown");
} catch (RuntimeException e) {
if (expectException && e instanceof IllegalArgumentException)
return;
error("unexpected exception thrown: " + e);
}
}
//----------------------------------------------------------------------------------------------
@Test
void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm) throws IOException {
test_getJavaFileObjectsFromPaths(fm, getTestFilePaths());
test_getJavaFileObjectsFromPaths(fm, getTestZipPaths());
}
/**
* Tests the getJavaFileObjectsFromPaths method for a specific file manager
* and a series of paths.
*
* Note: instances of MyStandardJavaFileManager only support
* encapsulating paths for files in the default file system.
*
* @param fm the file manager to be tested
* @param paths the paths to be tested
* @throws IOException
*/
void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm, List<Path> paths)
throws IOException {
boolean expectException = !isGetFileObjectsSupported(fm, paths);
try {
compile(fm.getJavaFileObjectsFromPaths(paths));
if (expectException)
error("expected exception not thrown: " + IllegalArgumentException.class.getName());
} catch (RuntimeException e) {
if (expectException && e instanceof IllegalArgumentException)
return;
error("unexpected exception thrown: " + e);
}
}
//----------------------------------------------------------------------------------------------
@Test
void test_getJavaFileObjectsFromPaths_Iterable(StandardJavaFileManager fm) throws IOException {
test_getJavaFileObjectsFromPaths_Iterable(fm, getTestFilePaths());
test_getJavaFileObjectsFromPaths_Iterable(fm, getTestZipPaths());
}
/**
* Tests the {@code getJavaFileObjectsFromPaths(Iterable)} method for a specific file
* manager and a series of paths.
*
* Note: instances of MyStandardJavaFileManager only support
* encapsulating paths for files in the default file system.
*
* @param fm the file manager to be tested
* @param paths the paths to be tested
* @throws IOException
*/
void test_getJavaFileObjectsFromPaths_Iterable(StandardJavaFileManager fm, List<Path> paths)
throws IOException {
boolean expectException = !isGetFileObjectsSupported(fm, paths);
try {
compile(fm.getJavaFileObjectsFromPaths((Iterable<Path>) paths));
if (expectException)
error("expected exception not thrown: " + IllegalArgumentException.class.getName());
} catch (RuntimeException e) {
if (expectException && e instanceof IllegalArgumentException)
return;
error("unexpected exception thrown: " + e);
}
}
//----------------------------------------------------------------------------------------------
/**
* Compiles a set of files.
*
* @param files the files to be compiled.
* @throws IOException
*/
void compile(Iterable<? extends JavaFileObject> files) throws IOException {
String name = "compile" + (compileCount++);
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
File f = new File(name);
f.mkdirs();
// use setLocation(Iterable<File>) to avoid relying on setLocationFromPaths
fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(f));
boolean ok = comp.getTask(null, fm, null, null, null, files).call();
if (!ok)
error(name + ": compilation failed");
}
}
int compileCount;
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2014, 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
* @bug 8059977
* @summary StandardJavaFileManager should support java.nio.file.Path.
* Test isSameFile method.
* @modules java.compiler
* jdk.compiler
* @build SJFM_TestBase
* @run main SJFM_IsSameFile
*/
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Callable;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
/**
* For those paths which are supported by a file manager, such that
* a file object can encapsulate the path, verify that the underlying
* paths can be compared.
*/
public class SJFM_IsSameFile extends SJFM_TestBase {
public static void main(String... args) throws Exception {
new SJFM_IsSameFile().run();
}
@Test
void test_isSameFile(StandardJavaFileManager fm) throws Exception {
test_isSameFile(fm, () -> getTestFilePaths());
test_isSameFile(fm, () -> getTestZipPaths());
}
/**
* Tests the isSameFile method for a specific file manager
* and a series of paths.
*
* Note: instances of MyStandardJavaFileManager only support
* encapsulating paths for files in the default file system.
*
* @param fm the file manager to be tested
* @param paths a generator for the paths to be tested
* @throws IOException
*/
void test_isSameFile(StandardJavaFileManager fm, Callable<List<Path>> paths) throws Exception {
if (!isGetFileObjectsSupported(fm, paths.call()))
return;
// use distinct paths and file objects in the following two sets
Iterable<? extends JavaFileObject> setA = fm.getJavaFileObjectsFromPaths(paths.call());
Iterable<? extends JavaFileObject> setB = fm.getJavaFileObjectsFromPaths(paths.call());
for (JavaFileObject a : setA) {
for (JavaFileObject b : setB) {
System.err.println("compare: a: " + a);
System.err.println(" b: " + b);
// Use the fileObject getName method to determine the expected result.
// For the files being tested, getName is the absolute path.
boolean expect = a.getName().equals(b.getName());
boolean actual = fm.isSameFile(a, b);
if (actual != expect) {
error("mismatch: actual:" + (actual ? "same" : "not same")
+ ", expect:" + (expect ? "same" : "not same"));
}
}
}
}
}

View file

@ -0,0 +1,181 @@
/*
* Copyright (c) 2014, 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
* @bug 8059977
* @summary StandardJavaFileManager should support java.nio.file.Path.
* Test get/setLocation methods.
* @modules java.compiler
* jdk.compiler
* @build SJFM_TestBase
* @run main SJFM_Locations
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.tools.JavaFileManager;
import javax.tools.StandardJavaFileManager;
/**
* For those paths which are supported by a file manager, verify
* that setLocation can accept such paths, and that getLocation
* can subsequently return the same paths.
*
* In addition, for files in the default file system, verify
* the combinations of setting a location using files or paths
* and then subsequently getting the location as files or paths.
*/
public class SJFM_Locations extends SJFM_TestBase {
public static void main(String... args) throws Exception {
new SJFM_Locations().run();
}
@Test
void test_locations(StandardJavaFileManager fm) throws IOException {
test_setFiles_getFiles(fm, getTestFileDirs());
test_setFiles_getPaths(fm, getTestFileDirs());
test_setPaths_getFiles(fm, getTestFilePathDirs());
test_setPaths_getPaths(fm, getTestFilePathDirs());
// test_setPaths_getPaths(fm, getTestZipPathDirs());
}
void test_setFiles_getFiles(StandardJavaFileManager fm, List<File> inFiles) throws IOException {
System.err.println("test_setFiles_getFiles");
JavaFileManager.Location l = newLocation();
fm.setLocation(l, inFiles);
Iterable<? extends File> outFiles = fm.getLocation(l);
compare(inFiles, outFiles);
}
void test_setFiles_getPaths(StandardJavaFileManager fm, List<File> inFiles) throws IOException {
System.err.println("test_setFiles_getPaths");
JavaFileManager.Location l = newLocation();
fm.setLocation(l, inFiles);
Iterable<? extends Path> outPaths = fm.getLocationAsPaths(l);
compare(inFiles, outPaths);
}
void test_setPaths_getFiles(StandardJavaFileManager fm, List<Path> inPaths) throws IOException {
System.err.println("test_setPaths_getFiles");
JavaFileManager.Location l = newLocation();
fm.setLocationFromPaths(l, inPaths);
Iterable<? extends File> outFiles = fm.getLocation(l);
compare(inPaths, outFiles);
}
void test_setPaths_getPaths(StandardJavaFileManager fm, List<Path> inPaths) throws IOException {
System.err.println("test_setPaths_getPaths");
JavaFileManager.Location l = newLocation();
fm.setLocationFromPaths(l, inPaths);
Iterable<? extends Path> outPaths = fm.getLocationAsPaths(l);
compare(inPaths, outPaths);
}
//----------------------------------------------------------------------------------------------
/**
* Gets a representative series of directories in the default file system,
* derived from the test.src directory and test.classes path.
*
* @return a list of directories, represented with {@code File}
* @throws IOException
*/
List<File> getTestFileDirs() throws IOException {
return Stream.of("test.src", "test.classes")
.map(s -> System.getProperty(s))
.flatMap(s -> Stream.of(s.split(File.pathSeparator, 0)))
.filter(s -> !s.isEmpty())
.map(s -> new File(s))
.collect(Collectors.toList());
}
/**
* Gets a representative series of directories in the default file system,
* derived from the test.src directory and test.classes path.
*
* @return a list of directories, represented with {@code Path}
* @throws IOException
*/
List<Path> getTestFilePathDirs() throws IOException {
return Stream.of("test.src", "test.classes")
.map(s -> System.getProperty(s))
.flatMap(s -> Stream.of(s.split(File.pathSeparator, 0)))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList());
}
/**
* Compares two lists of items by comparing their individual string representations.
*
* @param in the first set of items to be compared
* @param out the second set of items to be compared
*/
void compare(Iterable<?> in, Iterable<?> out) {
List<String> ins = toString(in);
List<String> outs = toString(out);
if (!ins.equals(outs)) {
error("mismatch in comparison");
System.err.println("in:");
for (String s: ins) System.err.println(s);
System.err.println("out:");
for (String s: outs) System.err.println(s);
}
}
List<String> toString(Iterable<?> iter) {
List<String> strings = new ArrayList<>();
for (Object item: iter)
strings.add(item.toString());
return strings;
}
/**
* Create an instance of a location.
* @return a location
*/
JavaFileManager.Location newLocation() {
final String name = "locn" + (count++);
return new JavaFileManager.Location() {
@Override
public String getName() {
return name;
}
@Override
public boolean isOutputLocation() {
return false;
}
};
}
int count = 0;
}

View file

@ -0,0 +1,227 @@
/*
* Copyright (c) 2014, 2025, 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.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
* Base class for unit tests for StandardJavaFileManager.
*/
class SJFM_TestBase {
/** Shared compiler instance. */
JavaCompiler comp;
/** A list of items to be closed when the test is complete. */
List<AutoCloseable> closeables;
/**
* Runs a test. This is the primary entry point and should generally be
* called from each test's main method.
* It calls all methods annotated with {@code @Test} with the instances
* of StandardJavaFileManager to be tested.
*
* @throws Exception if the test fails.
*/
void run() throws Exception {
comp = ToolProvider.getSystemJavaCompiler();
closeables = new ArrayList<>();
try (StandardJavaFileManager systemFileManager = comp.getStandardFileManager(null, null, null);
StandardJavaFileManager customFileManager = new MyStandardJavaFileManager(systemFileManager)) {
test(systemFileManager);
test(customFileManager);
} finally {
for (AutoCloseable c: closeables) {
try {
c.close();
} catch (IOException e) {
error("Exception closing " + c + ": " + e);
}
}
}
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
/**
* Get the file managers to be tested.
*
* Currently, two are provided:
* <ol>
* <li>the system-provided file manager
* <li>a custom file manager, which relies on the default methods provided in the
* StandardJavaFileManager interface
* </li>
*
* @return the file managers to be tested
*/
List<StandardJavaFileManager> getTestFileManagers() {
StandardJavaFileManager systemFileManager = comp.getStandardFileManager(null, null, null);
StandardJavaFileManager customFileManager = new MyStandardJavaFileManager(systemFileManager);
return Arrays.asList(systemFileManager, customFileManager);
}
/**
* Tests a specific file manager, by calling all methods annotated
* with {@code @Test} passing this file manager as an argument.
*
* @param fm the file manager to be tested
* @throws Exception if the test fails
*/
void test(StandardJavaFileManager fm) throws Exception {
System.err.println("Testing " + fm);
for (Method m: getClass().getDeclaredMethods()) {
Annotation a = m.getAnnotation(Test.class);
if (a != null) {
try {
System.err.println("Test " + m.getName());
m.invoke(this, new Object[] { fm });
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
throw (cause instanceof Exception) ? ((Exception) cause) : e;
}
System.err.println();
}
}
}
/** Marker annotation for test cases. */
@Retention(RetentionPolicy.RUNTIME)
@interface Test { }
/**
* Returns a series of paths for artifacts in the default file system.
* The paths are for the .java files in the test.src directory.
*
* @return a list of paths
* @throws IOException
*/
List<Path> getTestFilePaths() throws IOException {
String testSrc = System.getProperty("test.src");
return Files.list(Paths.get(testSrc))
.filter(p -> p.getFileName().toString().endsWith(".java"))
.collect(Collectors.toList());
}
private FileSystem zipfs;
private List<Path> zipPaths;
/**
* Returns a series of paths for artifacts in a non-default file system.
* A zip file is created containing copies of the .java files in the
* test.src directory. The paths that are returned refer to these files.
*
* @return a list of paths
* @throws IOException
*/
List<Path> getTestZipPaths() throws IOException {
if (zipfs == null) {
Path testZip = createSourceZip();
zipfs = FileSystems.newFileSystem(testZip, Map.of("accessMode", "readOnly"));
closeables.add(zipfs);
zipPaths = Files.list(zipfs.getRootDirectories().iterator().next())
.filter(p -> p.getFileName().toString().endsWith(".java"))
.collect(Collectors.toList());
}
return zipPaths;
}
/**
* Create a zip file containing the contents of the test.src directory.
*
* @return a path for the zip file.
* @throws IOException if there is a problem creating the file
*/
private Path createSourceZip() throws IOException {
Path testSrc = Paths.get(System.getProperty("test.src"));
Path testZip = Paths.get("test.zip");
try (OutputStream os = Files.newOutputStream(testZip)) {
try (ZipOutputStream zos = new ZipOutputStream(os)) {
Files.list(testSrc)
.filter(p -> p.getFileName().toString().endsWith(".java"))
.forEach(p -> {
try {
zos.putNextEntry(new ZipEntry(p.getFileName().toString()));
zos.write(Files.readAllBytes(p));
zos.closeEntry();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
}
}
return testZip;
}
/**
* Tests whether it is expected that a file manager will be able
* to create a series of file objects from a series of paths.
*
* MyStandardJavaFileManager does not support paths referring to
* non-default file systems.
*
* @param fm the file manager to be tested
* @param paths the paths to be tested
* @return
*/
boolean isGetFileObjectsSupported(StandardJavaFileManager fm, List<Path> paths) {
return !(fm instanceof MyStandardJavaFileManager
&& (paths.get(0).getFileSystem() != FileSystems.getDefault()));
}
/**
* Report an error.
*/
void error(String msg) {
System.err.println("Error: " + msg);
errors++;
}
/** Count of errors reported. */
int errors;
}