-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@JimfsTempDir
and JimfsTempDirFactory
- Loading branch information
Showing
7 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/io/github/scordio/jimfs/junit/jupiter/JimfsTempDir.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright © 2024 Stefano Cordio ([email protected]) | ||
* | ||
* Licensed 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.scordio.jimfs.junit.jupiter; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.function.Supplier; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@TempDir(factory = JimfsTempDirFactory.class) | ||
public @interface JimfsTempDir { | ||
|
||
Configuration value() default Configuration.FOR_CURRENT_PLATFORM; | ||
|
||
enum Configuration { | ||
FOR_CURRENT_PLATFORM(com.google.common.jimfs.Configuration::forCurrentPlatform), | ||
OS_X(com.google.common.jimfs.Configuration::osX), | ||
UNIX(com.google.common.jimfs.Configuration::unix), | ||
WINDOWS(com.google.common.jimfs.Configuration::windows); | ||
|
||
private final Supplier<com.google.common.jimfs.Configuration> configuration; | ||
|
||
Configuration(Supplier<com.google.common.jimfs.Configuration> configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
Supplier<com.google.common.jimfs.Configuration> getConfiguration() { | ||
return configuration; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/io/github/scordio/jimfs/junit/jupiter/JimfsTempDirFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright © 2024 Stefano Cordio ([email protected]) | ||
* | ||
* Licensed 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.scordio.jimfs.junit.jupiter; | ||
|
||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.function.Supplier; | ||
import org.junit.jupiter.api.extension.AnnotatedElementContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.io.TempDirFactory; | ||
|
||
public final class JimfsTempDirFactory implements TempDirFactory { | ||
|
||
private static final String DEFAULT_PREFIX = "junit-"; | ||
|
||
private FileSystem fileSystem; | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Path createTempDirectory( | ||
AnnotatedElementContext elementContext, ExtensionContext extensionContext) | ||
throws IOException { | ||
Supplier<Configuration> configuration = | ||
elementContext | ||
.findAnnotation(JimfsTempDir.class) | ||
.map(JimfsTempDir::value) | ||
.map(JimfsTempDir.Configuration::getConfiguration) | ||
.orElse(Configuration::forCurrentPlatform); | ||
|
||
fileSystem = Jimfs.newFileSystem(configuration.get()); | ||
Path root = fileSystem.getRootDirectories().iterator().next(); | ||
return Files.createTempDirectory(root, DEFAULT_PREFIX); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void close() throws IOException { | ||
fileSystem.close(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/io/github/scordio/jimfs/junit/jupiter/JimfsTempDirFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright © 2024 Stefano Cordio ([email protected]) | ||
* | ||
* Licensed 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.scordio.jimfs.junit.jupiter; | ||
|
||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.osXFileSystem; | ||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.unixFileSystem; | ||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.windowsFileSystem; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.condition.OS.MAC; | ||
import static org.junit.jupiter.api.condition.OS.WINDOWS; | ||
|
||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledOnOs; | ||
import org.junit.jupiter.api.condition.EnabledOnOs; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
class JimfsTempDirFactoryTests { | ||
|
||
@TempDir(factory = JimfsTempDirFactory.class) | ||
Path tempDir; | ||
|
||
@EnabledOnOs(MAC) | ||
@Test | ||
void should_use_os_x_configuration() { | ||
assertThat(tempDir).satisfies(osXFileSystem()); | ||
} | ||
|
||
@DisabledOnOs({MAC, WINDOWS}) | ||
@Test | ||
void should_use_unix_configuration() { | ||
assertThat(tempDir).satisfies(unixFileSystem()); | ||
} | ||
|
||
@EnabledOnOs(WINDOWS) | ||
@Test | ||
void should_use_windows_configuration() { | ||
assertThat(tempDir).satisfies(windowsFileSystem()); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/test/java/io/github/scordio/jimfs/junit/jupiter/JimfsTempDirTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright © 2024 Stefano Cordio ([email protected]) | ||
* | ||
* Licensed 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.scordio.jimfs.junit.jupiter; | ||
|
||
import static io.github.scordio.jimfs.junit.jupiter.JimfsTempDir.Configuration.FOR_CURRENT_PLATFORM; | ||
import static io.github.scordio.jimfs.junit.jupiter.JimfsTempDir.Configuration.OS_X; | ||
import static io.github.scordio.jimfs.junit.jupiter.JimfsTempDir.Configuration.UNIX; | ||
import static io.github.scordio.jimfs.junit.jupiter.JimfsTempDir.Configuration.WINDOWS; | ||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.osXFileSystem; | ||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.unixFileSystem; | ||
import static io.github.scordio.jimfs.junit.jupiter.Requirements.windowsFileSystem; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledOnOs; | ||
import org.junit.jupiter.api.condition.EnabledOnOs; | ||
import org.junit.jupiter.api.condition.OS; | ||
|
||
class JimfsTempDirTests { | ||
|
||
@Nested | ||
class With_Default_Configuration { | ||
|
||
@JimfsTempDir Path tempDir; | ||
|
||
@EnabledOnOs(OS.MAC) | ||
@Test | ||
void should_use_os_x_configuration() { | ||
assertThat(tempDir).satisfies(osXFileSystem()); | ||
} | ||
|
||
@DisabledOnOs({OS.MAC, OS.WINDOWS}) | ||
@Test | ||
void should_use_unix_configuration() { | ||
assertThat(tempDir).satisfies(unixFileSystem()); | ||
} | ||
|
||
@EnabledOnOs(OS.WINDOWS) | ||
@Test | ||
void should_use_windows_configuration() { | ||
assertThat(tempDir).satisfies(windowsFileSystem()); | ||
} | ||
} | ||
|
||
@Nested | ||
class With_FOR_CURRENT_PLATFORM_Configuration { | ||
|
||
@JimfsTempDir(FOR_CURRENT_PLATFORM) | ||
Path tempDir; | ||
|
||
@EnabledOnOs(OS.MAC) | ||
@Test | ||
void should_use_os_x_configuration() { | ||
assertThat(tempDir).satisfies(osXFileSystem()); | ||
} | ||
|
||
@DisabledOnOs({OS.MAC, OS.WINDOWS}) | ||
@Test | ||
void should_use_unix_configuration() { | ||
assertThat(tempDir).satisfies(unixFileSystem()); | ||
} | ||
|
||
@EnabledOnOs(OS.WINDOWS) | ||
@Test | ||
void should_use_windows_configuration() { | ||
assertThat(tempDir).satisfies(windowsFileSystem()); | ||
} | ||
} | ||
|
||
@Nested | ||
class With_OS_X_Configuration { | ||
|
||
@JimfsTempDir(OS_X) | ||
Path tempDir; | ||
|
||
@Test | ||
void should_use_os_x_configuration() { | ||
assertThat(tempDir).satisfies(osXFileSystem()); | ||
} | ||
} | ||
|
||
@Nested | ||
class With_UNIX_Configuration { | ||
|
||
@JimfsTempDir(UNIX) | ||
Path tempDir; | ||
|
||
@Test | ||
void should_use_unix_configuration() { | ||
assertThat(tempDir).satisfies(unixFileSystem()); | ||
} | ||
} | ||
|
||
@Nested | ||
class With_WINDOWS_Configuration { | ||
|
||
@JimfsTempDir(WINDOWS) | ||
Path tempDir; | ||
|
||
@Test | ||
void should_use_windows_configuration() { | ||
assertThat(tempDir).satisfies(windowsFileSystem()); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/io/github/scordio/jimfs/junit/jupiter/Requirements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright © 2024 Stefano Cordio ([email protected]) | ||
* | ||
* Licensed 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.scordio.jimfs.junit.jupiter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.nio.file.Path; | ||
import java.util.function.Consumer; | ||
|
||
class Requirements { | ||
|
||
static Consumer<Path> osXFileSystem() { | ||
return tempDir -> { | ||
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs"); | ||
assertThat(tempDir.getFileSystem().getSeparator()).isEqualTo("/"); | ||
assertThat(tempDir.getFileSystem().getRootDirectories()) | ||
.map(Path::toString) | ||
.containsExactly("/"); | ||
}; | ||
} | ||
|
||
static Consumer<Path> unixFileSystem() { | ||
return tempDir -> { | ||
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs"); | ||
assertThat(tempDir.getFileSystem().getSeparator()).isEqualTo("/"); | ||
assertThat(tempDir.getFileSystem().getRootDirectories()) | ||
.map(Path::toString) | ||
.containsExactly("/"); | ||
}; | ||
} | ||
|
||
static Consumer<Path> windowsFileSystem() { | ||
return tempDir -> { | ||
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs"); | ||
assertThat(tempDir.getFileSystem().getSeparator()).isEqualTo("\\"); | ||
assertThat(tempDir.getFileSystem().getRootDirectories()) | ||
.map(Path::toString) | ||
.containsExactly("C:\\"); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores |