Skip to content

Commit

Permalink
Add @JimfsTempDir and JimfsTempDirFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Jul 27, 2024
1 parent 795b178 commit 6511f81
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>0.1.0-SNAPSHOT</version>

<name>Jimfs JUnit Jupiter</name>
<description>A JUnit Jupiter `@TempDir` extension based on Jimfs</description>
<description>A JUnit Jupiter `@TempDir` extension based on the in-memory file system Jimfs</description>
<url>https://github.com/scordio/jimfs-junit-jupiter</url>
<inceptionYear>2024</inceptionYear>
<licenses>
Expand Down Expand Up @@ -64,6 +64,12 @@
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
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;
}
}
}
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();
}
}
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());
}
}
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());
}
}
}
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:\\");
};
}
}
1 change: 1 addition & 0 deletions src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores

0 comments on commit 6511f81

Please sign in to comment.