Switch from Postgres to H2

This commit is contained in:
Ali Dehghani
2019-10-28 22:12:19 +03:30
parent db85c8f275
commit c4da87f511
20523 changed files with 1643929 additions and 0 deletions
@@ -0,0 +1,57 @@
package com.baeldung.filereader;
import java.io.*;
public class FileReaderExample {
public static String readAllCharactersOneByOne(Reader reader) throws IOException {
StringBuilder content = new StringBuilder();
int nextChar;
while ((nextChar = reader.read()) != -1) {
content.append((char) nextChar);
}
return String.valueOf(content);
}
public static String readMultipleCharacters(Reader reader, int length) throws IOException {
char[] buffer = new char[length];
int charactersRead = reader.read(buffer, 0, length);
if (charactersRead != -1) {
return new String(buffer, 0, charactersRead);
} else {
return "";
}
}
public static String readFile(String path) {
FileReader fileReader = null;
try {
fileReader = new FileReader(path);
return readAllCharactersOneByOne(fileReader);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static String readFileUsingTryWithResources(String path) {
try (FileReader fileReader = new FileReader(path)) {
return readAllCharactersOneByOne(fileReader);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@@ -0,0 +1,48 @@
package com.baeldung.createfiles;
import com.google.common.io.Files;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class CreateFilesUnitTest {
@Test
public void givenAnExistingDirectory_whenCreatingAFileWithAbsolutePath_thenFileIsCreated() throws IOException {
File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File fileWithAbsolutePath = new File(tempDirectory.getAbsolutePath() + "/myDirectory/testFile.txt");
assertFalse(fileWithAbsolutePath.exists());
Files.touch(fileWithAbsolutePath);
assertTrue(fileWithAbsolutePath.exists());
}
@Test
public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFileWithRelativePath_thenFileIsCreated() throws IOException {
File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt");
assertFalse(fileWithRelativePath.exists());
Files.touch(fileWithRelativePath);
assertTrue(fileWithRelativePath.exists());
}
@Test
public void whenCreatingAFileWithFileSeparator_thenFileIsCreated() throws IOException {
File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File newFile = new File(tempDirectory.getAbsolutePath() + File.separator + "newFile.txt");
assertFalse(newFile.exists());
Files.touch(newFile);
assertTrue(newFile.exists());
}
}
@@ -0,0 +1,179 @@
package com.baeldung.file;
import org.junit.Test;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class FileClassDemoUnitTest {
@Test
public void givenDirectoryCreated_whenMkdirIsInvoked_thenDirectoryIsDeleted() {
File directory = new File("testDirectory");
if (!directory.isDirectory() || !directory.exists()) {
directory.mkdir();
}
assertTrue(directory.delete());
}
@Test
public void givenFileCreated_whenCreateNewFileIsInvoked_thenFileIsDeleted() throws IOException {
File file = new File("testFile.txt");
if (!file.isFile() || !file.exists()) {
file.createNewFile();
}
assertTrue(file.delete());
}
@Test
public void givenFileCreated_whenCreateNewFileInvoked_thenMetadataIsAsExpected() throws IOException {
// different Operating systems have different separator characters
String separatorCharacter = System.getProperty("file.separator");
File parentDirectory = makeDirectory("filesDirectory");
File childFile = new File(parentDirectory, "file1.txt");
childFile.createNewFile();
assertTrue(childFile.getName().equals("file1.txt"));
assertTrue(childFile.getParentFile().getName().equals(parentDirectory.getName()));
assertTrue(childFile.getPath().equals(parentDirectory.getPath() + separatorCharacter + "file1.txt"));
removeDirectory(parentDirectory);
}
@Test(expected = FileNotFoundException.class)
public void givenReadOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeWrittenTo() throws IOException {
File parentDirectory = makeDirectory("filesDirectory");
File childFile = new File(parentDirectory, "file1.txt");
childFile.createNewFile();
childFile.setWritable(false);
FileOutputStream fos = new FileOutputStream(childFile);
fos.write("Hello World".getBytes()); // write operation
fos.flush();
fos.close();
removeDirectory(parentDirectory);
}
@Test(expected = FileNotFoundException.class)
public void givenWriteOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeReadFrom() throws IOException {
File parentDirectory = makeDirectory("filesDirectory");
File childFile = new File(parentDirectory, "file1.txt");
childFile.createNewFile();
childFile.setReadable(false);
FileInputStream fis = new FileInputStream(childFile);
fis.read(); // read operation
fis.close();
removeDirectory(parentDirectory);
}
@Test
public void givenFilesCreatedInDirectory_whenCreateNewFileInvoked_thenTheyCanBeListedAsExpected() throws IOException {
File directory = makeDirectory("filtersDirectory");
File csvFile = new File(directory, "csvFile.csv");
csvFile.createNewFile();
File txtFile = new File(directory, "txtFile.txt");
txtFile.createNewFile();
//normal listing
assertEquals(2, directory.list().length);
//filtered listing
FilenameFilter csvFilter = (dir, ext) -> ext.endsWith(".csv");
assertEquals(1, directory.list(csvFilter).length);
removeDirectory(directory);
}
@Test
public void givenDirectoryIsCreated_whenMkdirInvoked_thenDirectoryCanBeRenamed() {
File source = makeDirectory("source");
File destination = makeDirectory("destination");
source.renameTo(destination);
assertFalse(source.isDirectory());
assertTrue(destination.isDirectory());
removeDirectory(destination);
}
@Test
public void givenDataIsWrittenToFile_whenWriteIsInvoked_thenFreeSpaceOnSystemDecreases() throws IOException {
String name = System.getProperty("user.home") + System.getProperty("file.separator") + "test";
File testDir = makeDirectory(name);
File sample = new File(testDir, "sample.txt");
long freeSpaceBeforeWrite = testDir.getFreeSpace();
writeSampleDataToFile(sample);
long freeSpaceAfterWrite = testDir.getFreeSpace();
assertTrue(freeSpaceAfterWrite < freeSpaceBeforeWrite);
removeDirectory(testDir);
}
private static File makeDirectory(String directoryName) {
File directory = new File(directoryName);
directory.mkdir();
if (directory.isDirectory()) {
return directory;
}
throw new RuntimeException("Directory not created for " + directoryName);
}
private static void removeDirectory(File directory) {
// make sure you don't delete your home directory here
if (directory.getPath().equals(System.getProperty("user.home"))) {
return;
}
// remove directory and its files from system
if (directory != null && directory.exists()) {
// delete all files inside the directory
File[] filesInDirectory = directory.listFiles();
if (filesInDirectory != null) {
List<File> files = Arrays.asList(filesInDirectory);
files.forEach(f -> deleteFile(f));
}
// finally delete the directory itself
deleteFile(directory);
}
}
private static void deleteFile(File fileToDelete) {
if (fileToDelete != null && fileToDelete.exists()) {
fileToDelete.delete();
}
}
private static void writeSampleDataToFile(File sample) throws IOException {
//write sample text to file
try (FileOutputStream out = new FileOutputStream(sample)) {
for (int i = 1; i <= 100000; i++) {
String sampleText = "Sample line number " + i + "\n";
out.write(sampleText.getBytes());
}
}
}
}
@@ -0,0 +1,50 @@
package com.baeldung.filereader;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExampleUnitTest {
private static final String FILE_PATH = "src/test/resources/HelloWorld.txt";
@Test
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello, World!";
File file = new File(FILE_PATH);
try (FileReader fileReader = new FileReader(file)) {
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
Assert.assertEquals(expectedText, content);
}
}
@Test
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello";
File file = new File(FILE_PATH);
try (FileReader fileReader = new FileReader(file)) {
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
Assert.assertEquals(expectedText, content);
}
}
@Test
public void whenReadFile_thenReturnsContent() {
String expectedText = "Hello, World!";
String content = FileReaderExample.readFile(FILE_PATH);
Assert.assertEquals(expectedText, content);
}
@Test
public void whenReadFileUsingTryWithResources_thenReturnsContent() {
String expectedText = "Hello, World!";
String content = FileReaderExample.readFileUsingTryWithResources(FILE_PATH);
Assert.assertEquals(expectedText, content);
}
}
@@ -0,0 +1,47 @@
package com.baeldung.filewriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
public class FileWriterExampleUnitTest {
@After
public void tearDown() throws IOException {
Files.delete(Path.of("src/test/resources/FileWriterTest.txt"));
}
@Test
public void testWriteString() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testAppendString() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
// using another try with resources to reopen the file in append mode
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", true)) {
fileWriter.write("Hello Folks Again!");
}
Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testWriteCharArray() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!".toCharArray());
}
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
}
@@ -0,0 +1 @@
Hello, World!