Skip to content

Instantly share code, notes, and snippets.

@Toparvion
Last active October 31, 2018 01:04
Show Gist options
  • Save Toparvion/9f7dad57d18e3d7d72bdecdaa72cf33a to your computer and use it in GitHub Desktop.
Save Toparvion/9f7dad57d18e3d7d72bdecdaa72cf33a to your computer and use it in GitHub Desktop.
Sample test showcasing a side effect introduced with IDEA JDK 11 inspection for Files.write* methods
package tech.toparvion.analog.util.dev;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Toparvion
*/
class Jdk11FileWriteTest {
private static final String RECORD = "Из России with love ;)";
private static final Charset CHARSET = StandardCharsets.UTF_8;
@Test
@DisplayName("A string written with Files.write method can be read back correctly with Files.readString.")
void testFilesWriteMethod() throws IOException {
Path testFilePath = Files.createTempFile("jdk11-files-write-test-", ".txt");
System.out.println("Test file path for Files.write(): " + testFilePath);
//noinspection ReadWriteStringCanBeUsed
Files.write(testFilePath, RECORD.getBytes(CHARSET));
String actual = Files.readString(testFilePath, CHARSET);
assertEquals(RECORD, actual);
}
@Test
@DisplayName("A string written with Files.writeString method can be read back correctly with Files.readString.")
void testFilesWriteStringMethod() throws IOException {
Path testFilePath = Files.createTempFile("jdk11-files-write-string-test-", ".txt");
System.out.println("Test file path for Files.writeString(): " + testFilePath);
Files.writeString(testFilePath, RECORD, CHARSET);
String actual = Files.readString(testFilePath, CHARSET);
assertEquals(RECORD, actual);
}
@AfterEach
void tearDown() {
// temp files are not removed deliberately in order to keep them for manual inspection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment