* private TemporaryFolder temporaryFolder; * @@ -37,17 +41,29 @@ * // ... * } *- *
* @Test * public void testUsingTemporaryFolder(TemporaryFolder temporaryFolder) { * // ... * } *- *
+ * private static TemporaryFolder TEMPORARY_FOLDER; * - *+ * * *In both approaches the {@link TemporaryFolder} will be destroyed during {@code @AfterEach} and - * no exception will be thrown in cases where the deletion fails. + * @BeforeAll + * public static void setUp(TemporaryFolder givenTemporaryFolder) { + * TEMPORARY_FOLDER = givenTemporaryFolder + * // ... + * } + *
Usage examples:
*
@@ -112,29 +128,9 @@
* TemporaryFolder Rule
* @since 1.0.0
*/
-public class TemporaryFolderExtension implements AfterEachCallback, ParameterResolver {
-
- private static final String KEY = "temporaryFolder";
+public class TemporaryFolderExtension implements ParameterResolver {
- /**
- * If there is a {@link TemporaryFolder} associated with the current {@code extensionContext} then
- * destroy it.
- *
- * @param extensionContext the context in which the current test or container is being
- * executed
- */
- @Override
- public void afterEach(ExtensionContext extensionContext) {
- TemporaryFolder temporaryFolder =
- getStore(extensionContext, this.getClass()).get(KEY, TemporaryFolder.class);
- if (temporaryFolder != null) {
- try {
- temporaryFolder.destroy();
- } catch (Exception e) {
- // silent failures
- }
- }
- }
+ private static final Namespace NAMESPACE = Namespace.create(TemporaryFolderExtension.class);
/**
* Does this extension support injection for parameters of the type described by the given {@code
@@ -168,8 +164,10 @@ public boolean supportsParameter(
public Object resolveParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
- return getStore(extensionContext, this.getClass())
- .getOrComputeIfAbsent(KEY, key -> new TemporaryFolder());
+ return extensionContext
+ .getStore(NAMESPACE)
+ .getOrComputeIfAbsent(
+ parameterContext, key -> new TemporaryFolder(), TemporaryFolder.class);
}
private boolean appliesTo(Class> clazz) {
diff --git a/src/test/java/io/github/glytching/junit/extension/folder/TemporaryFolderExtensionBeforeAllTest.java b/src/test/java/io/github/glytching/junit/extension/folder/TemporaryFolderExtensionBeforeAllTest.java
new file mode 100644
index 0000000..db8ee87
--- /dev/null
+++ b/src/test/java/io/github/glytching/junit/extension/folder/TemporaryFolderExtensionBeforeAllTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.github.glytching.junit.extension.folder;
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
+
+@ExtendWith(TemporaryFolderExtension.class)
+public class TemporaryFolderExtensionBeforeAllTest {
+
+ private static TemporaryFolder TEMPORARY_FOLDER;
+
+ @BeforeAll
+ public static void setUp(TemporaryFolder givenTemporaryFolder) {
+ TEMPORARY_FOLDER = givenTemporaryFolder;
+ }
+
+ @AfterAll
+ public static void cleanUp() throws IOException {
+ try (Stream So, we test this extension behaviour by playing around with the {@link
- * TemporaryFolderExtension} directly.
- */
-public class TemporaryFolderExtensionMetaTest {
-
- private final TemporaryFolderExtension sut = new TemporaryFolderExtension();
- @Mock private ExtensionContext extensionContext;
- @Mock private Store store;
- @Mock private TemporaryFolder temporaryFolder;
-
- @BeforeEach
- public void prepare() {
- MockitoAnnotations.initMocks(this);
-
- when(extensionContext.getStore(
- Namespace.create(TemporaryFolderExtension.class, extensionContext)))
- .thenReturn(store);
- }
-
- @Test
- void willDestroyTemporaryFolderAfterEach() throws IOException {
- when(store.get(any(String.class), eq(TemporaryFolder.class))).thenReturn(temporaryFolder);
-
- sut.afterEach(extensionContext);
-
- verify(temporaryFolder).destroy();
- }
-
- @Test
- void willSwallowAnyExceptionEncounteredWhenDestroyingTheTemporaryFolder() throws IOException {
- when(store.get(any(String.class), eq(TemporaryFolder.class))).thenReturn(temporaryFolder);
-
- doThrow(new RuntimeException("Boom!")).when(temporaryFolder).destroy();
-
- sut.afterEach(extensionContext);
- }
-}
- *
- *
- * In addition, we cannot test these aspects using the {@link
- * io.github.glytching.junit.extension.util.ExtensionTester} because that has no access to the extension's
- * internals.
- *
- *