Skip to content

Commit

Permalink
5379 TempDir support for tests (helidon-io#5508)
Browse files Browse the repository at this point in the history
tempDir support for tests

Signed-off-by: aserkes <[email protected]>
  • Loading branch information
aserkes authored and trentjeff committed Nov 29, 2022
1 parent f5f6d24 commit 10038f1
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* the test class have finished.
*
*/
@Deprecated
public class TemporaryFolderExt implements BeforeEachCallback, AfterEachCallback {

private Path root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
import java.util.List;

import io.helidon.common.testing.junit5.RestoreSystemPropertiesExt;
import io.helidon.common.testing.junit5.TemporaryFolderExt;
import io.helidon.config.spi.ConfigNode.ObjectNode;
import io.helidon.config.spi.ConfigSource;

import com.xebialabs.restito.server.StubServer;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
import static com.xebialabs.restito.semantics.Action.status;
Expand All @@ -53,9 +52,6 @@ public class ConfigSourceMetaConfigTest {
private static final String TEST_ENV_VAR_VALUE = "This Is My ENV VARS Value.";
private static final String RELATIVE_PATH_TO_RESOURCE = "/src/test/resources/";

@RegisterExtension
public TemporaryFolderExt folder = TemporaryFolderExt.build();

@Test
@ExtendWith(RestoreSystemPropertiesExt.class)
public void testSystemProperties() {
Expand All @@ -65,9 +61,9 @@ public void testSystemProperties() {
ObjectNode.builder()
.addValue("type", "system-properties")
.build()));

ConfigSource source = singleSource(metaConfig);

assertThat(source, is(instanceOf(AbstractConfigSource.class)));

Config config = justFrom(source);
Expand Down Expand Up @@ -132,8 +128,7 @@ public void testFile() {
}

@Test
public void testDirectory() throws IOException {
File folder = this.folder.newFolder();
public void testDirectory(@TempDir File folder) throws IOException {
Files.write(Files.createFile(new File(folder, "username").toPath()), "libor".getBytes());
Files.write(Files.createFile(new File(folder, "password").toPath()), "^ery$ecretP&ssword".getBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import java.time.Instant;
import java.util.Optional;

import io.helidon.common.testing.junit5.TemporaryFolderExt;
import io.helidon.config.spi.ConfigContent;
import io.helidon.config.spi.ConfigNode.ObjectNode;
import io.helidon.config.spi.ConfigSource;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import static io.helidon.config.ValueNodeMatcher.valueNode;
import static org.hamcrest.CoreMatchers.not;
Expand All @@ -44,9 +43,9 @@
*/
public class DirectoryConfigSourceTest {

@RegisterExtension
static TemporaryFolderExt folder = TemporaryFolderExt.build();
@TempDir
File tempFolder;

@Test
public void testDescriptionMandatory() {
ConfigSource configSource = ConfigSources.directory("secrets").build();
Expand All @@ -71,7 +70,7 @@ public void testLoadNoDirectory() {

@Test
public void testLoadEmptyDirectory() throws IOException {
DirectoryConfigSource configSource = ConfigSources.directory(folder.newFolder().getAbsolutePath())
DirectoryConfigSource configSource = ConfigSources.directory(tempFolder.getAbsolutePath())
.build();

Optional<ConfigContent.NodeContent> maybeContent = configSource.load();
Expand All @@ -93,11 +92,10 @@ public void testLoadEmptyDirectory() throws IOException {

@Test
public void testLoadDirectory() throws IOException {
File folder = DirectoryConfigSourceTest.folder.newFolder();
Files.write(Files.createFile(new File(folder, "username").toPath()), "libor".getBytes());
Files.write(Files.createFile(new File(folder, "password").toPath()), "^ery$ecretP&ssword".getBytes());
Files.write(Files.createFile(new File(tempFolder, "username").toPath()), "libor".getBytes());
Files.write(Files.createFile(new File(tempFolder, "password").toPath()), "^ery$ecretP&ssword".getBytes());

DirectoryConfigSource configSource = ConfigSources.directory(folder.getAbsolutePath())
DirectoryConfigSource configSource = ConfigSources.directory(tempFolder.getAbsolutePath())
.build();

Optional<ConfigContent.NodeContent> maybeContent = configSource.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

import io.helidon.common.media.type.MediaType;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.testing.junit5.TemporaryFolderExt;
import io.helidon.config.spi.ConfigParser;
import io.helidon.config.spi.ConfigSource;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static io.helidon.common.testing.junit5.OptionalMatcher.optionalValue;
import static org.hamcrest.CoreMatchers.not;
Expand All @@ -48,10 +46,6 @@ public class FileConfigSourceTest {
private static final MediaType TEST_MEDIA_TYPE = MediaTypes.create("my/media/type");
private static final String RELATIVE_PATH_TO_RESOURCE = "/src/test/resources/";

@RegisterExtension
static TemporaryFolderExt folder = TemporaryFolderExt.build();


@Test
public void testDescriptionMandatory() {
ConfigSource configSource = ConfigSources.file("application.conf").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import java.nio.file.Paths;
import java.util.Optional;

import io.helidon.common.testing.junit5.TemporaryFolderExt;
import io.helidon.config.spi.ConfigContent;
import io.helidon.config.spi.OverrideSource;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -37,9 +35,6 @@ public class FileOverrideSourceTest {

private static final String RELATIVE_PATH_TO_RESOURCE = "/src/test/resources/";

@RegisterExtension
static TemporaryFolderExt folder = TemporaryFolderExt.build();

private static String getDir() {
return Paths.get("").toAbsolutePath() + RELATIVE_PATH_TO_RESOURCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

import java.io.File;
import java.nio.file.Files;

import io.helidon.common.testing.junit5.TemporaryFolderExt;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -33,13 +32,13 @@
*/
public class FileSourceHelperTest {

@RegisterExtension
static TemporaryFolderExt folder = TemporaryFolderExt.build();
@TempDir
Path tempFolder;

@Test
public void testDigestSameContent() throws Exception {
File file1 = folder.newFile("test1");
File file2 = folder.newFile("test2");
File file1 = Files.createFile(tempFolder.resolve("test1")).toFile();
File file2 = Files.createFile(tempFolder.resolve("test2")).toFile();
Files.write(file1.toPath(), "test file".getBytes());
Files.write(file2.toPath(), "test file".getBytes());

Expand All @@ -48,8 +47,8 @@ public void testDigestSameContent() throws Exception {

@Test
public void testDigestDifferentContent() throws Exception {
File file1 = folder.newFile("test1");
File file2 = folder.newFile("test2");
File file1 = Files.createFile(tempFolder.resolve("test1")).toFile();
File file2 = Files.createFile(tempFolder.resolve("test2")).toFile();
Files.write(file1.toPath(), "test file1".getBytes());
Files.write(file2.toPath(), "test file2".getBytes());

Expand Down
Loading

0 comments on commit 10038f1

Please sign in to comment.