Skip to content

Commit b02e6d2

Browse files
committed
F return autoclosable from overriding method to use with try with resources
1 parent f3d11ca commit b02e6d2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

approvaltests-util/src/main/java/com/spun/util/tests/TestUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ public class TestUtils
2828
{
2929
private static Random random;
3030
private static Function2<Class, String, File> getSourceDirectory = ClassUtils::getSourceDirectory;
31-
public static void registerSourceDirectoryFinder(Function2<Class, String, File> getSourceDirectory)
31+
public static AutoCloseable registerSourceDirectoryFinder(Function2<Class, String, File> sourceDirectoryFinder)
3232
{
33-
TestUtils.getSourceDirectory = getSourceDirectory;
33+
Function2<Class, String, File> original = getSourceDirectory;
34+
AutoCloseable c = () -> TestUtils.getSourceDirectory = original;
35+
TestUtils.getSourceDirectory = sourceDirectoryFinder;
36+
return c;
3437
}
3538
public static File getFile(String startingDir)
3639
{

approvaltests/src/test/java/org/approvaltests/namer/JUnit5StackTraceNamerTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.approvaltests.namer;
22

3+
import java.io.File;
4+
5+
import org.junit.Assert;
6+
import org.junit.jupiter.api.Assertions;
37
import org.junit.jupiter.api.DisplayName;
48
import org.junit.jupiter.api.Nested;
59
import org.junit.jupiter.api.RepeatedTest;
@@ -10,6 +14,7 @@
1014

1115
import com.spun.util.LambdaThreadLauncher;
1216
import com.spun.util.ObjectUtils;
17+
import com.spun.util.tests.TestUtils;
1318

1419
public class JUnit5StackTraceNamerTest
1520
{
@@ -61,8 +66,18 @@ void approvalFromInsideLambda() throws Exception
6166
}));
6267
lambda.getThread().join(1000);
6368
if (caught[0] != null)
69+
{ throw ObjectUtils.throwAsError(caught[0]); }
70+
}
71+
@Test
72+
void testOverridingDirectoryFinder() throws Exception
73+
{
74+
try (AutoCloseable autoCloseable = TestUtils
75+
.registerSourceDirectoryFinder((c, s) -> new File("does not exist")))
6476
{
65-
throw ObjectUtils.throwAsError(caught[0]);
77+
StackTraceNamer name = new StackTraceNamer();
78+
File file = new File(name.getSourceFilePath() + this.getClass().getSimpleName() + ".java");
79+
Assertions.assertFalse(file.exists());
6680
}
81+
StackTraceNamerUtils.assertSourceFilePath(this.getClass().getSimpleName());
6782
}
6883
}

0 commit comments

Comments
 (0)