Last active
October 31, 2018 01:04
-
-
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
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
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