Interface Workspace
- All Superinterfaces:
AutoCloseable
This acts as a nexus for managing the lifetime of test sources and directories, and should be used within a try-with-resources block to ensure temporary files get released after the test completes.
While this interface may seem somewhat intimidating due to the number of methods it provides, you will usually only ever need to use a small subset of them. The main ones you probably will want to use are:
-
addSourcePathPackage(Path)
- for copying a source path tree from your file system. -
createSourcePathPackage()
- for creating a new source path tree. -
addClassPathPackage(Path)
- for adding a class path resource (usually a JAR or a directory of packages of classes). -
addModulePathModule(String, Path)
- for adding a module path resource (usually a JAR or a directory of packages of classes). -
getClassPathPackages()
- for fetching all class path resources. -
getModulePathModules()
- for fetching all module path resources. -
getSourceOutputPackages()
- for fetching all generated source packages. -
getSourceOutputModules()
- for fetching all generated source modules. -
close()
- to close any resources in the temporary file system.
A simple example of usage of this interface would be the following:
try (Workspace workspace = Workspaces.newWorkspace()) {
workspace
.createSourcePathPackage()
.copyContentsFrom("src", "test", "resources", "test-data");
var compilation = someCompiler.compile(workspace);
assertThat(compilation).isSuccessful();
}
As of 3.2.0, you can use a functional version of the above instead if this is more suitable for your use-case:
Workspaces.newWorkspace().use(workspace -> {
...
});
Remember that files that are created as the result of a compilation can be queried via
JctFileManager
, which is accessible on the compilation
result object. This may
more accurately represent the logical project structure that is the result of various
processing operations during compilation.
- Since:
- 0.0.1
- Author:
- Ashley Scopes
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A consumer functional interface that consumes a workspace and can throw a checked exception. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
addAnnotationProcessorPathModule
(String moduleName, Path path) Add a module to theannotation processor module path
.default void
Add a package to theannotation processor path
.default void
addClassOutputModule
(String moduleName, Path path) Add a module to theclass outputs
.default void
addClassOutputPackage
(Path path) Add a package to theclass outputs
.default void
addClassPathPackage
(Path path) Add a package to theclass path
.void
addModule
(JavaFileManager.Location location, String moduleName, Path path) Add an existing package root to this workspace and associate it with the given module name in the given location.default void
addModulePathModule
(String moduleName, Path path) Add a module to themodule path
.void
addPackage
(JavaFileManager.Location location, Path path) Add an existing package root to this workspace and associate it with the given location.default void
addSourceOutputModule
(String moduleName, Path path) Add a module to thegenerated source outputs
.default void
addSourceOutputPackage
(Path path) Add a package to thegenerated source outputs
.default void
addSourcePathModule
(String moduleName, Path path) Add a module to themodule source path
.default void
addSourcePathPackage
(Path path) Add a package to thelegacy source path
.void
close()
Attempt to close all resources in this workspace.default ManagedDirectory
createAnnotationProcessorPathModule
(String moduleName) Create a module in theannotation processor module path
.default ManagedDirectory
Create a package in theannotation processor path
.default ManagedDirectory
createClassOutputModule
(String moduleName) Create a module in theclass outputs
.default ManagedDirectory
Create a package in theclass outputs
.default ManagedDirectory
Create a package in theclass path
.createModule
(JavaFileManager.Location location, String moduleName) Create a new test directory for a package root and associate it with the given module name in the given location.default ManagedDirectory
createModulePathModule
(String moduleName) Create a module in themodule path
.createPackage
(JavaFileManager.Location location) Create a new test directory for a package root and associate it with the given location.default ManagedDirectory
createSourceOutputModule
(String moduleName) Create a module in thegenerated source outputs
.default ManagedDirectory
Create a package in thegenerated source outputs
.default ManagedDirectory
createSourcePathModule
(String moduleName) Create a module in themodule source path
.default ManagedDirectory
Create a package in thesource path
.Map
<JavaFileManager.Location, List<? extends PathRoot>> Get an immutable copy of the current paths to operate on.getAnnotationProcessorPathModule
(String moduleName) Get the path roots for theannotation processor module path
.Get the module path roots for theannotation processor module path
.Get the path roots for theannotation processor path
.getClassOutputModule
(String moduleName) Get the module path roots forclass outputs
for the given module name.Get the module path roots forclass outputs
.Get the non-module path roots forclass outputs
.Get the path roots for theclass path
.getModule
(JavaFileManager.Location location, String moduleName) Get the collection of path roots associated with the given module.getModulePathModule
(String moduleName) Get the path roots for themodule path
.Get the module path roots for themodule paths
.getModules
(JavaFileManager.Location location) Get the collection of modules associated with the given location.getPackages
(JavaFileManager.Location location) Get the collection of path roots associated with the given location.Get the path strategy in use.getSourceOutputModule
(String moduleName) Get the module path roots forsource outputs
for the given module name.Get the module path roots forsource outputs
.Get the non-module path roots forsource outputs
.getSourcePathModule
(String moduleName) Get the path roots for themodule source path
.Get the module path roots for themodule source paths
.Get the path roots for thesource path
.boolean
isClosed()
Determine if the workspace is closed or not.default <T extends Throwable>
voiduse
(Workspace.ThrowingWorkspaceConsumer<T> consumer) Functional equivalent of consuming this object with a try-with-resources.
-
Method Details
-
close
void close()Attempt to close all resources in this workspace.- Specified by:
close
in interfaceAutoCloseable
- Throws:
UncheckedIOException
- if an error occurs.
-
isClosed
boolean isClosed()Determine if the workspace is closed or not.- Returns:
true
if closed,false
if open.- Since:
- 0.4.0
-
getAllPaths
Map<JavaFileManager.Location,List<? extends PathRoot>> getAllPaths()Get an immutable copy of the current paths to operate on.- Returns:
- the paths.
-
getModule
Get the collection of path roots associated with the given module.Usually this should only ever contain one path root at a maximum, although
Workspace
does not explicitly enforce this constraint.If no results were found, then an empty collection is returned.
- Parameters:
location
- the module-oriented or output location.moduleName
- the module name within the location.- Returns:
- the collection of paths.
- Throws:
IllegalArgumentException
- if the location is neithermodule-oriented
or anoutput location
. This will also be raised if this method is called with an instance ofModuleLocation
(you should usegetPackages(Location)
instead for this).- Since:
- 0.1.0
- See Also:
-
getModules
Get the collection of modules associated with the given location.If no results were found, then an empty map is returned.
- Parameters:
location
- the location to get the modules for.- Returns:
- the map of module names to lists of associated paths.
- Throws:
IllegalArgumentException
- if the location is neithermodule-oriented
or anoutput location
. This will also be raised if this method is called with an instance ofModuleLocation
.- Since:
- 0.1.0
- See Also:
-
getPathStrategy
-
getPackages
Get the collection of path roots associated with the given location.If no results were found, then an empty collection is returned.
- Parameters:
location
- the location to get.- Returns:
- the collection of paths.
- Throws:
IllegalArgumentException
- if the location ismodule-oriented
.- Since:
- 0.1.0
- See Also:
-
addPackage
Add an existing package root to this workspace and associate it with the given location.The following constraints must be met, otherwise an
IllegalArgumentException
will be thrown:-
The
location
must not bemodule-oriented
. -
The
path
must exist.
For example, this would add the package tree in
src/test/resources/packages
into thesource code path
that is used to compile files:var path = Path.of("src", "test", "resources", "packages"); workspace.addPackage(StandardLocation.SOURCE_PATH, path);
- Parameters:
location
- the location to associate with.path
- the path to add.- Throws:
IllegalArgumentException
- if the inputs are invalid.- See Also:
-
The
-
addModule
Add an existing package root to this workspace and associate it with the given module name in the given location.The following constraints must be met, otherwise an
IllegalArgumentException
will be thrown:-
The
location
must bemodule-oriented
or anoutput location
. -
The
location
must not be amodule-location handle
already. -
The
path
must exist.
For example, this would add the package tree in
src/test/resources/packages
into themodule source code path
under the module namefoo.bar
:var path = Path.of("src", "test", "resources", "packages"); workspace.addModule(StandardLocation.MODULE_SOURCE_PATH, "foo.bar", path);
- Parameters:
location
- the location to associate with.moduleName
- the name of the module.path
- the path to add.- Throws:
IllegalArgumentException
- if the inputs are invalid.- See Also:
-
The
-
createPackage
Create a new test directory for a package root and associate it with the given location.This path will be destroyed when the workspace is
closed
.The following constraints must be met, otherwise an
IllegalArgumentException
will be thrown:-
The
location
must not bemodule-oriented
.
For example, this would create a new source root that you could add files to:
workspace.createPackage(StandardLocation.SOURCE_PATH);
- Parameters:
location
- the location to associate with.- Returns:
- the test directory that was created.
- Throws:
IllegalArgumentException
- if the inputs are invalid.- See Also:
-
The
-
createModule
Create a new test directory for a package root and associate it with the given module name in the given location.This path will be destroyed when the workspace is
closed
.The following constraints must be met, otherwise an
IllegalArgumentException
will be thrown:-
The
location
must bemodule-oriented
or anoutput location
. -
The
location
must not be amodule-location handle
already.
For example, this would create a new multi-module source root that you could add files to, for a module named
foo.bar
:workspace.createModule(StandardLocation.MODULE_SOURCE_PATH, "foo.bar");
- Parameters:
location
- the location to associate with.moduleName
- the module name to use.- Returns:
- the test directory that was created.
- Throws:
IllegalArgumentException
- if the inputs are invalid.- See Also:
-
The
-
addClassOutputPackage
Add a package to theclass outputs
.- Parameters:
path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addClassOutputModule
Add a module to theclass outputs
.- Parameters:
moduleName
- the module name.path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addSourceOutputPackage
Add a package to thegenerated source outputs
.- Parameters:
path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addSourceOutputModule
Add a module to thegenerated source outputs
.- Parameters:
moduleName
- the module name.path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addClassPathPackage
Add a package to theclass path
.If you are adding JPMS modules, you may want to use
addModulePathModule(String, Path)
instead.- Parameters:
path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addModulePathModule
Add a module to themodule path
.If you are adding non-JPMS modules, you may want to use
addClassPathPackage(Path)
instead.- Parameters:
moduleName
- the module name.path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addSourcePathPackage
Add a package to thelegacy source path
.This is the location you will usually tend to use for your source code.
If you wish to define multiple JPMS modules in your source code tree to compile together, you will want to consider using
addSourcePathModule(String, Path)
instead. For most purposes, however, this method is the one you will want to be using if your code is not in a named module directory (so not something likesrc/my.module/org/example/...
).- Parameters:
path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addSourcePathModule
Add a module to themodule source path
.Note that this will signal to the compiler to run in multi-module compilation mode. Any source packages that were
added
orcreated
will be ignored.If you are using a single-module, you can
add a source package
and include amodule-info.java
in the base directory instead.- Parameters:
moduleName
- the module name.path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addAnnotationProcessorPathPackage
Add a package to theannotation processor path
.Note that this will be ignored if the compiler is provided with explicit annotation processor instances to run.
- Parameters:
path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
addAnnotationProcessorPathModule
Add a module to theannotation processor module path
.Note that this will be ignored if the compiler is provided with explicit annotation processor instances to run.
- Parameters:
moduleName
- the module name.path
- the path to add.- Throws:
IllegalArgumentException
- if the path does not exist.- See Also:
-
createClassOutputPackage
Create a package in theclass outputs
.- Returns:
- the created test directory.
- See Also:
-
createClassOutputModule
Create a module in theclass outputs
.- Parameters:
moduleName
- the module name.- Returns:
- the created test directory.
- See Also:
-
createSourceOutputPackage
Create a package in thegenerated source outputs
.- Returns:
- the created test directory.
- See Also:
-
createSourceOutputModule
Create a module in thegenerated source outputs
.- Parameters:
moduleName
- the module name.- Returns:
- the created test directory.
- See Also:
-
createClassPathPackage
Create a package in theclass path
.If you are adding JPMS modules, you may want to use
createModulePathModule(String)
instead.- Returns:
- the created test directory.
- See Also:
-
createModulePathModule
Create a module in themodule path
.If you are adding non-JPMS modules, you may want to use
createClassPathPackage()
instead.- Parameters:
moduleName
- the module name.- Returns:
- the created test directory.
- See Also:
-
createSourcePathPackage
Create a package in thesource path
.If you wish to define multiple JPMS modules in your source code tree to compile together, you will want to consider using
createSourcePathModule(String)
instead. For most purposes, however, this method is the one you will want to be using if your code is not in a named module directory (so not something likesrc/my.module/org/example/...
).- Returns:
- the created test directory.
- See Also:
-
createSourcePathModule
Create a module in themodule source path
.Note that this will signal to the compiler to run in multi-module compilation mode. Any source packages that were
created
oradded
will be ignored.If you are using a single-module, you can
create a source package
and include amodule-info.java
in the base directory instead.- Parameters:
moduleName
- the module name.- Returns:
- the created test directory.
- See Also:
-
createAnnotationProcessorPathPackage
Create a package in theannotation processor path
.Note that this will be ignored if the compiler is provided with explicit annotation processor instances to run.
- Returns:
- the created test directory.
- See Also:
-
createAnnotationProcessorPathModule
Create a module in theannotation processor module path
.Note that this will be ignored if the compiler is provided with explicit annotation processor instances to run.
- Parameters:
moduleName
- the module name.- Returns:
- the created test directory.
- See Also:
-
getClassOutputPackages
Get the non-module path roots forclass outputs
.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getClassOutputModule
Get the module path roots forclass outputs
for the given module name.- Parameters:
moduleName
- the module name.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getClassOutputModules
Get the module path roots forclass outputs
.- Returns:
- the roots in a map of module names to lists of roots, or an empty map if none were found.
- Since:
- 0.1.0
-
getSourceOutputPackages
Get the non-module path roots forsource outputs
.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getSourceOutputModule
Get the module path roots forsource outputs
for the given module name.- Parameters:
moduleName
- the module name.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getSourceOutputModules
Get the module path roots forsource outputs
.- Returns:
- the roots in a map of module names to lists of roots, or an empty map if none were found.
- Since:
- 0.1.0
-
getClassPathPackages
Get the path roots for theclass path
.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getSourcePathPackages
Get the path roots for thesource path
.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getAnnotationProcessorPathPackages
Get the path roots for theannotation processor path
.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getAnnotationProcessorPathModule
Get the path roots for theannotation processor module path
.- Parameters:
moduleName
- the module name to get.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getAnnotationProcessorPathModules
Get the module path roots for theannotation processor module path
.- Returns:
- the roots in a map of module names to lists of roots, or an empty map if none were found.
- Since:
- 0.1.0
-
getSourcePathModule
Get the path roots for themodule source path
.- Parameters:
moduleName
- the module name to get.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getSourcePathModules
Get the module path roots for themodule source paths
.- Returns:
- the roots in a map of module names to lists of roots, or an empty map if none were found.
- Since:
- 0.1.0
-
getModulePathModule
Get the path roots for themodule path
.- Parameters:
moduleName
- the module name to get.- Returns:
- the roots in a collection, or an empty collection if none were found.
- Since:
- 0.1.0
-
getModulePathModules
Get the module path roots for themodule paths
.- Returns:
- the roots in a map of module names to lists of roots, or an empty map if none were found.
- Since:
- 0.1.0
-
use
Functional equivalent of consuming this object with a try-with-resources.This workspace will be
closed
upon completion of this method or upon an exception being raised.- Type Parameters:
T
- the exception that the consumer can throw, orRuntimeException
if no checked exception is thrown.- Parameters:
consumer
- the consumer to pass this object to.- Throws:
UncheckedIOException
- if the closure fails after the consumer is called.T
- the checked exception that the consumer can throw.- Since:
- 3.2.0
-