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

Issue #2 - Added a few unit tests for the FileStorage #7

Merged
merged 2 commits into from
Oct 1, 2020
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
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,28 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit-jupiter-api.version>5.7.0</junit-jupiter-api.version>
<hamcrest.version>2.2</hamcrest.version>
</properties>
<modules>
<module>shielddb-core</module>
<module>shielddb-demo-app</module>
<module>shielddb-gson-backend</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter-api.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down Expand Up @@ -108,6 +123,11 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
30 changes: 22 additions & 8 deletions shielddb-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nf.fr.k49</groupId>
<artifactId>shielddb-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
</parent>
<artifactId>shielddb-core</artifactId>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nf.fr.k49</groupId>
<artifactId>shielddb-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
</parent>
<artifactId>shielddb-core</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package nf.fr.k49.shielddb.core.storage;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.fail;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.CoreMatchers.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class FileStorageTest {

static Path testWorkDir;

@BeforeAll
static void setUpBeforeClass() throws Exception {
testWorkDir = Files.createTempDirectory(FileStorageTest.class.getSimpleName());
}

@AfterAll
static void tearDownAfterClass() throws Exception {
Files.walk(testWorkDir).map(Path::toFile).forEach(File::delete);
}


private Path createFilePathForTest() {
return Paths.get(testWorkDir.toString(), UUID.randomUUID().toString());
}

private String getExistentFilePathForTest() throws IOException {
return Files.write(createFilePathForTest(), UUID.randomUUID().toString().getBytes(Charset.defaultCharset()))
.toString();
}

private String getExistentFilePathForTest(Charset charset) throws IOException {
return Files.write(createFilePathForTest(), UUID.randomUUID().toString().getBytes(charset)).toString();
}

@Test
@Order(0)
final void testFileStorageString() throws IOException {
String existentFilePath = getExistentFilePathForTest();
assertDoesNotThrow(() -> {
new FileStorage(existentFilePath);
}, "must be able to be instantiated when it's passed as arguments a valid and existent file path");
}

@Test
@Order(1)
final void testFileStorageStringCharset() throws IOException {
String targetFilePath = getExistentFilePathForTest(StandardCharsets.ISO_8859_1);
assertDoesNotThrow(() -> {
new FileStorage(targetFilePath, StandardCharsets.ISO_8859_1);
}, "must be able to be instantiated when it's passed as arguments a valid and existent file path and a valid charset");
}

@Test
@Order(2)
final void testRead() throws IOException {
String targetFilePath = getExistentFilePathForTest(StandardCharsets.UTF_8);
String expectedContent = Files.readAllLines(Paths.get(targetFilePath), StandardCharsets.UTF_8)
.stream().collect(Collectors.joining(""));
FileStorage fileStorage = new FileStorage(targetFilePath, StandardCharsets.UTF_8);
String actualContent = fileStorage.read();
assertThat("the read method does not working as expected: unexpected retrieved content", actualContent,
equalTo(expectedContent));
}

@Test
@Order(3)
final void testWrite() throws IOException {
String targetFilePath = getExistentFilePathForTest(StandardCharsets.UTF_8);
FileStorage fileStorage = new FileStorage(targetFilePath, StandardCharsets.UTF_8);
String expectedContent = UUID.randomUUID().toString();
fileStorage.write(expectedContent);
String actualContent = Files.readAllLines(Paths.get(targetFilePath), StandardCharsets.UTF_8).stream()
.collect(Collectors.joining(""));
assertThat("the write method does not working as expected: unexpected content has been wrote", expectedContent,
equalTo(actualContent));
}

@Test
@Order(4)
final void testClear() throws IOException {
String targetFilePath = getExistentFilePathForTest(StandardCharsets.UTF_8);
FileStorage fileStorage = new FileStorage(targetFilePath, StandardCharsets.UTF_8);
String expectedContent = "";
fileStorage.clear();
String actualContent = Files.readAllLines(Paths.get(targetFilePath), StandardCharsets.UTF_8).stream()
.collect(Collectors.joining(""));
assertThat("the clear method does not working as expected: the retrieved content wasn't cleaned", expectedContent,
equalTo(actualContent));
}

}