Skip to content

Commit d9848df

Browse files
committed
Polishing
1 parent 0211454 commit d9848df

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@
7878
* <p>Consult the Javadoc for {@link TempDir} for details on the contract.
7979
*
8080
* @since 5.4
81-
* @see TempDir
81+
* @see TempDir @TempDir
8282
* @see Files#createTempDirectory
8383
*/
8484
class TempDirectory implements BeforeAllCallback, BeforeEachCallback, ParameterResolver {
8585

86+
// package-private for testing purposes
8687
static final Namespace NAMESPACE = Namespace.create(TempDirectory.class);
88+
static final String FILE_OPERATIONS_KEY = "file.operations";
89+
8790
private static final String KEY = "temp.dir";
8891
private static final String FAILURE_TRACKER = "failure.tracker";
8992
private static final String CHILD_FAILED = "child.failed";
9093

91-
// for testing purposes
92-
static final String FILE_OPERATIONS_KEY = "file.operations";
93-
9494
private final JupiterConfiguration configuration;
9595

9696
public TempDirectory(JupiterConfiguration configuration) {
@@ -127,9 +127,9 @@ public void beforeEach(ExtensionContext context) {
127127

128128
private static void installFailureTracker(ExtensionContext context) {
129129
context.getStore(NAMESPACE).put(FAILURE_TRACKER, (CloseableResource) () -> context.getParent() //
130-
.ifPresent(it -> {
130+
.ifPresent(parentContext -> {
131131
if (selfOrChildFailed(context)) {
132-
it.getStore(NAMESPACE).put(CHILD_FAILED, true);
132+
parentContext.getStore(NAMESPACE).put(CHILD_FAILED, true);
133133
}
134134
}));
135135
}
@@ -242,21 +242,22 @@ private TempDirFactory determineTempDirFactory(TempDir tempDir, Scope scope) {
242242
: ReflectionSupport.newInstance(factory);
243243
}
244244

245-
private void assertNonFinalField(Field field) {
245+
private static void assertNonFinalField(Field field) {
246246
if (ModifierSupport.isFinal(field)) {
247247
throw new ExtensionConfigurationException("@TempDir field [" + field + "] must not be declared as final.");
248248
}
249249
}
250250

251-
private void assertSupportedType(String target, Class<?> type) {
251+
private static void assertSupportedType(String target, Class<?> type) {
252252
if (type != Path.class && type != File.class) {
253253
throw new ExtensionConfigurationException("Can only resolve @TempDir " + target + " of type "
254254
+ Path.class.getName() + " or " + File.class.getName() + " but was: " + type.getName());
255255
}
256256
}
257257

258-
private Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext, TempDirFactory factory,
259-
CleanupMode cleanupMode, Scope scope, ExtensionContext extensionContext) {
258+
private static Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext,
259+
TempDirFactory factory, CleanupMode cleanupMode, Scope scope, ExtensionContext extensionContext) {
260+
260261
Namespace namespace = scope == Scope.PER_DECLARATION //
261262
? NAMESPACE.append(elementContext) //
262263
: NAMESPACE;
@@ -271,6 +272,7 @@ private Object getPathOrFile(Class<?> elementType, AnnotatedElementContext eleme
271272

272273
static CloseablePath createTempDir(TempDirFactory factory, CleanupMode cleanupMode, Class<?> elementType,
273274
AnnotatedElementContext elementContext, ExtensionContext extensionContext) {
275+
274276
try {
275277
return new CloseablePath(factory, cleanupMode, elementType, elementContext, extensionContext);
276278
}
@@ -302,12 +304,12 @@ private CloseablePath(TempDirFactory factory, CleanupMode cleanupMode, Class<?>
302304
this.annotatedElement = elementContext.getAnnotatedElement();
303305
this.extensionContext = extensionContext;
304306

305-
if (dir == null || !Files.isDirectory(dir)) {
307+
if (this.dir == null || !Files.isDirectory(this.dir)) {
306308
close();
307309
throw new PreconditionViolationException("temp directory must be a directory");
308310
}
309311

310-
if (elementType == File.class && !dir.getFileSystem().equals(FileSystems.getDefault())) {
312+
if (elementType == File.class && !this.dir.getFileSystem().equals(FileSystems.getDefault())) {
311313
close();
312314
throw new PreconditionViolationException(
313315
"temp directory with non-default file system cannot be injected into " + File.class.getName()
@@ -316,19 +318,20 @@ private CloseablePath(TempDirFactory factory, CleanupMode cleanupMode, Class<?>
316318
}
317319

318320
Path get() {
319-
return dir;
321+
return this.dir;
320322
}
321323

322324
@Override
323325
public void close() throws IOException {
324326
try {
325-
if (cleanupMode == NEVER || (cleanupMode == ON_SUCCESS && selfOrChildFailed(extensionContext))) {
327+
if (this.cleanupMode == NEVER
328+
|| (this.cleanupMode == ON_SUCCESS && selfOrChildFailed(this.extensionContext))) {
326329
logger.info(() -> String.format("Skipping cleanup of temp dir %s for %s due to CleanupMode.%s.",
327-
dir, descriptionFor(annotatedElement), cleanupMode.name()));
330+
this.dir, descriptionFor(this.annotatedElement), this.cleanupMode.name()));
328331
return;
329332
}
330333

331-
FileOperations fileOperations = extensionContext.getStore(NAMESPACE) //
334+
FileOperations fileOperations = this.extensionContext.getStore(NAMESPACE) //
332335
.getOrDefault(FILE_OPERATIONS_KEY, FileOperations.class, FileOperations.DEFAULT);
333336

334337
SortedMap<Path, IOException> failures = deleteAllFilesAndDirectories(fileOperations);
@@ -337,7 +340,7 @@ public void close() throws IOException {
337340
}
338341
}
339342
finally {
340-
factory.close();
343+
this.factory.close();
341344
}
342345
}
343346

@@ -370,14 +373,15 @@ private static String descriptionFor(Executable executable) {
370373

371374
private SortedMap<Path, IOException> deleteAllFilesAndDirectories(FileOperations fileOperations)
372375
throws IOException {
373-
if (dir == null || Files.notExists(dir)) {
376+
377+
if (this.dir == null || Files.notExists(this.dir)) {
374378
return Collections.emptySortedMap();
375379
}
376380

377381
SortedMap<Path, IOException> failures = new TreeMap<>();
378382
Set<Path> retriedPaths = new HashSet<>();
379-
tryToResetPermissions(dir);
380-
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
383+
tryToResetPermissions(this.dir);
384+
Files.walkFileTree(this.dir, new SimpleFileVisitor<Path>() {
381385

382386
@Override
383387
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
@@ -482,7 +486,7 @@ private IOException createIOExceptionWithAttachedFailures(SortedMap<Path, IOExce
482486
.map(this::relativizeSafely) //
483487
.map(path -> emptyPath.equals(path) ? "<root>" : path.toString()) //
484488
.collect(joining(", "));
485-
IOException exception = new IOException("Failed to delete temp directory " + dir.toAbsolutePath()
489+
IOException exception = new IOException("Failed to delete temp directory " + this.dir.toAbsolutePath()
486490
+ ". The following paths could not be deleted (see suppressed exceptions for details): "
487491
+ joinedPaths);
488492
failures.values().forEach(exception::addSuppressed);
@@ -500,7 +504,7 @@ private Path tryToDeleteOnExit(Path path) {
500504

501505
private Path relativizeSafely(Path path) {
502506
try {
503-
return dir.relativize(path);
507+
return this.dir.relativize(path);
504508
}
505509
catch (IllegalArgumentException e) {
506510
return path;

jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/CloseablePathTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ void setUpExtensionContext() {
104104
* that {@link TempDirFactory#createTempDirectory(AnnotatedElementContext, ExtensionContext)} may provide.
105105
*
106106
* @since 5.11
107-
*
108107
* @see TempDirFactory
109108
*/
110109
@Nested
@@ -265,7 +264,6 @@ private static void assertThatExtensionConfigurationExceptionIsThrownBy(Throwing
265264
* set to {@link CleanupMode#ALWAYS}, {@link CleanupMode#NEVER}, or {@link CleanupMode#ON_SUCCESS}.
266265
*
267266
* @since 5.9
268-
*
269267
* @see TempDir
270268
* @see CleanupMode
271269
*/

0 commit comments

Comments
 (0)