Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#702 removing temporary files in tests #779

Merged
merged 3 commits into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ The MIT License (MIT)
</site>
</distributionManagement>
<dependencies>
<!--
@todo #720:30min When https://github.com/teamed/qulice/issues/877 is
fixed upgrade qulice dependency to the one that contains the fix
and fix any issues in the project that result from this upgrade.
-->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/org/cactoos/io/GzipOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import java.util.zip.GZIPInputStream;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link org.cactoos.io.GzipOutput}.
Expand All @@ -42,6 +44,11 @@
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class GzipOutputTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void writeToGzipOutput() throws Exception {
Expand Down Expand Up @@ -72,7 +79,7 @@ public void writeToGzipOutput() throws Exception {
@Test(expected = IOException.class)
public void writeToClosedGzipOutput() throws Exception {
final OutputStream stream = new FileOutputStream(
new TempFile("cactoos", "txt").value().toFile()
this.folder.newFile("cactoos.txt")
);
stream.close();
new LengthOf(
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/org/cactoos/io/InputOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.takes.http.FtRemote;
import org.takes.tk.TkHtml;

Expand All @@ -56,6 +58,11 @@
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class InputOfTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void readsAlternativeInputForFileCase() throws IOException {
Expand All @@ -75,7 +82,8 @@ public void readsAlternativeInputForFileCase() throws IOException {

@Test
public void readsSimpleFileContent() throws IOException {
final Path temp = Files.createTempFile("cactoos-1", "txt-1");
final Path temp = this.folder.newFile("cactoos-1.txt-1")
.toPath();
final String content = "Hello, товарищ!";
Files.write(temp, content.getBytes(StandardCharsets.UTF_8));
MatcherAssert.assertThat(
Expand Down
18 changes: 13 additions & 5 deletions src/test/java/org/cactoos/io/InputStreamOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link InputStreamOf}.
Expand All @@ -46,10 +48,16 @@
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class InputStreamOfTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krzyk You have missed in this class usage of File.createTempFile in methods: readsFileContent, readsFromUri, readsFromUrl, readsFromTextWithCharset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SharplEr Good catch, fixed.

/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void readsSimpleFileContent() throws IOException {
final Path temp = Files.createTempFile("cactoos-1", "txt-1");
final Path temp = this.folder.newFile("cactoos-1.txt-1")
.toPath();
final String content = "Hello, товарищ!";
Files.write(temp, content.getBytes(StandardCharsets.UTF_8));
MatcherAssert.assertThat(
Expand Down Expand Up @@ -91,7 +99,7 @@ public void makesDataAvailable() throws IOException {

@Test
public void readsFileContent() throws IOException {
final File file = File.createTempFile("readFileContent", "txt-2");
final File file = this.folder.newFile("readFileContent.txt-2");
final String content = "Content in a file";
new LengthOf(
new TeeInput(content, file)
Expand Down Expand Up @@ -137,7 +145,7 @@ public void readsText() throws IOException {
@Test
public void readsFromUri() throws IOException {
final String content = "Content for reading through URI";
final File file = File.createTempFile("readFromUri", "txt-3");
final File file = this.folder.newFile("readFromUri.txt-3");
new LengthOf(
new TeeInput(content, file)
).intValue();
Expand All @@ -151,7 +159,7 @@ public void readsFromUri() throws IOException {
@Test
public void readsFromUrl() throws IOException {
final String content = "Content for reading through URL";
final File file = File.createTempFile("readFromUrl", "txt-4");
final File file = this.folder.newFile("readFromUrl.txt-4");
new LengthOf(
new TeeInput(content, file)
).intValue();
Expand Down Expand Up @@ -212,7 +220,7 @@ public void readsFromReaderWithCharset() throws IOException {

@Test
public void readsFromTextWithCharset() throws IOException {
final File file = File.createTempFile("readTextWithCharset", "txt-5");
final File file = this.folder.newFile("readTextWithCharset.txt-5");
final String content = "Content for reading text with charset";
new LengthOf(
new TeeInput(content, file)
Expand Down
12 changes: 9 additions & 3 deletions src/test/java/org/cactoos/io/LoggingOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link LoggingOutput}.
Expand All @@ -50,6 +51,11 @@
}
)
public final class LoggingOutputTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void logWriteZero() throws IOException {
Expand Down Expand Up @@ -112,7 +118,7 @@ public void logWriteText() throws IOException {
@Test
public void logWriteToLargeTextFile() throws Exception {
final Logger logger = new FakeLogger();
final Path temp = Files.createTempDirectory("ccts-1");
final Path temp = this.folder.newFolder("ccts-1").toPath();
final Path path = temp.resolve("x/y/z/file.txt");
new LengthOf(
new TeeInput(
Expand Down Expand Up @@ -142,7 +148,7 @@ public void logWriteToLargeTextFile() throws Exception {
@Test
public void logAllWriteToLargeTextFile() throws Exception {
final Logger logger = new FakeLogger(Level.WARNING);
final Path temp = Files.createTempDirectory("ccts-2");
final Path temp = this.folder.newFolder("ccts-2").toPath();
final Path path = temp.resolve("a/b/c/file.txt");
new LengthOf(
new TeeInput(
Expand Down
11 changes: 9 additions & 2 deletions src/test/java/org/cactoos/io/OutputStreamToTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
package org.cactoos.io;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.matchers.MatcherOf;
import org.cactoos.matchers.TextHasString;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link OutputStreamTo}.
Expand All @@ -42,10 +43,16 @@
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class OutputStreamToTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void writesLargeContentToFile() throws IOException {
final Path temp = Files.createTempFile("cactoos-1", "txt-1");
final Path temp = this.folder.newFile("cactoos-1.txt-1")
.toPath();
MatcherAssert.assertThat(
"Can't copy Input to Output and return Input",
new TextOf(
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/org/cactoos/io/ReaderAsBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link ReaderAsBytes}.
Expand All @@ -41,6 +43,11 @@
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class ReaderAsBytesTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void readsString() throws IOException {
Expand All @@ -58,7 +65,7 @@ public void readsString() throws IOException {

@Test
public void readsAsBytesAndDeletesTempFile() throws Exception {
final Path file = new TempFile().value();
final Path file = this.folder.newFile().toPath();
new ReaderAsBytes(
new ReaderOf(file)
).asBytes();
Expand Down
20 changes: 14 additions & 6 deletions src/test/java/org/cactoos/io/TeeOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
package org.cactoos.io;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.cactoos.matchers.MatcherOf;
import org.cactoos.matchers.TextHasString;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link TeeOutput}.
Expand All @@ -40,6 +43,11 @@
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class TeeOutputTest {
/**
* Temporary files generator.
*/
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void copiesContent() {
Expand Down Expand Up @@ -126,23 +134,23 @@ public void copiesWithWriterAndCharset() {
@Test
public void copiesWithPath() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final TempFile file = new TempFile();
final File file = this.folder.newFile();
MatcherAssert.assertThat(
"Can't copy Output with path",
new TextOf(
new TeeInput(
new InputOf("Hello, товарищ! with path"),
new TeeOutput(
new OutputTo(baos),
file.value()
file.toPath()
)
)
),
new TextHasString(
new MatcherOf<>(
str -> {
return new String(
new TextOf(file.value()).asString()
new TextOf(file.toPath()).asString()
).equals(str);
}
)
Expand All @@ -153,23 +161,23 @@ public void copiesWithPath() throws Exception {
@Test
public void copiesWithFile() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final TempFile file = new TempFile();
final File file = this.folder.newFile();
MatcherAssert.assertThat(
"Can't copy Output with file",
new TextOf(
new TeeInput(
new InputOf("Hello, товарищ! with file"),
new TeeOutput(
new OutputTo(baos),
file.value().toFile()
file
)
)
),
new TextHasString(
new MatcherOf<>(
str -> {
return new String(
new TextOf(file.value()).asString()
new TextOf(file.toPath()).asString()
).equals(str);
}
)
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import org.cactoos.matchers.TextHasString;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link WriterAsOutputStream}.
Expand All @@ -48,6 +50,11 @@
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class WriterAsOutputStreamTest {
/**
* Temporary files and folders generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void writesToByteArray() {
Expand Down Expand Up @@ -84,7 +91,8 @@ public void writesToByteArray() {

@Test
public void writesLargeContentToFile() throws IOException {
final Path temp = Files.createTempFile("cactoos-1", "txt-1");
final Path temp = this.folder.newFile("cactoos-1.txt-1")
.toPath();
MatcherAssert.assertThat(
"Can't copy Input to Output and return Input",
new TextOf(
Expand Down Expand Up @@ -115,7 +123,7 @@ public void writesLargeContentToFile() throws IOException {

@Test
public void writesToFileAndRemovesIt() throws Exception {
final Path temp = new TempFile().value();
final Path temp = this.folder.newFile().toPath();
final String content = "Hello, товарищ! How are you?";
new LengthOf(
new TeeInput(
Expand Down
Loading