From 6d2dcf510ae1dc30a9aa6d5bedb848da4e31f05d Mon Sep 17 00:00:00 2001 From: Ziggy Date: Sun, 5 Jul 2020 01:34:29 +0200 Subject: [PATCH] Tests to ensure most functionalities of SdbGenerator work --- .../holocore/utilities/SdbGenerator.java | 9 +- .../holocore/utilities/TestSdbGenerator.java | 153 ++++++++++++++++++ 2 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/projectswg/holocore/utilities/TestSdbGenerator.java diff --git a/src/main/java/com/projectswg/holocore/utilities/SdbGenerator.java b/src/main/java/com/projectswg/holocore/utilities/SdbGenerator.java index a0f4cac7b..da584e989 100644 --- a/src/main/java/com/projectswg/holocore/utilities/SdbGenerator.java +++ b/src/main/java/com/projectswg/holocore/utilities/SdbGenerator.java @@ -28,6 +28,7 @@ package com.projectswg.holocore.utilities; import java.io.*; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; import java.util.Map; @@ -35,15 +36,19 @@ import java.util.Map.Entry; public class SdbGenerator implements Closeable, AutoCloseable { - private static final Charset ASCII = Charset.forName("ASCII"); + private static final Charset ASCII = StandardCharsets.US_ASCII; - private BufferedWriter writer; + private final BufferedWriter writer; public SdbGenerator(File file) throws FileNotFoundException { if (file == null) throw new NullPointerException("File cannot be null!"); this.writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), ASCII)); } + + public SdbGenerator(BufferedWriter writer) { + this.writer = writer; + } public void close() throws IOException { writer.close(); diff --git a/src/test/java/com/projectswg/holocore/utilities/TestSdbGenerator.java b/src/test/java/com/projectswg/holocore/utilities/TestSdbGenerator.java new file mode 100644 index 000000000..e51caaf40 --- /dev/null +++ b/src/test/java/com/projectswg/holocore/utilities/TestSdbGenerator.java @@ -0,0 +1,153 @@ +package com.projectswg.holocore.utilities; + +import org.junit.Before; +import org.junit.Test; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.*; + +import static org.junit.Assert.assertEquals; + +public class TestSdbGenerator { + private StringWriter stringWriter; + private SdbGenerator generator; + + @Before + public void setup() { + stringWriter = new StringWriter(); + generator = new SdbGenerator(new BufferedWriter(stringWriter)); + } + + @Test + public void testWriteColumnLine() throws IOException { + String col1 = "col1"; + String col2 = "col2"; + String separator = "\t"; + + generator.writeColumnNames(col1, col2); + generator.close(); // Flushes changes to the StringWriter + + String result = stringWriter.toString().replace("\n", ""); + String expected = col1 + separator + col2; + + assertEquals("Column names should be written in the given order, separated by a TAB", expected, result); + } + + @Test + public void testWriteRowLineStringType() throws IOException { + String cell = "cell"; + + generator.writeLine(cell); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + + assertEquals("String data type should be supported", cell, result); + } + + @Test + public void testWriteRowLineIntegerType() throws IOException { + int cell = 1234; + + generator.writeLine(cell); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + String expected = String.valueOf(cell); + + assertEquals("int data type should be supported", expected, result); + } + + @Test + public void testWriteRowLineNullType() throws IOException { + generator.writeLine(new Object[]{null}); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + String expected = ""; + + assertEquals("null should be written as an empty string", expected, result); + } + + @Test + public void testWriteRowLineCollectionTypeMultipleValues() throws IOException { + String valSeparator = ";"; + String val1 = "1"; + String val2 = "2"; + + Collection collection = Arrays.asList( + val1, + val2 + ); + + generator.writeLine(collection); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + String expected = val1 + valSeparator + val2; + + assertEquals("Multiple values in a Collection should be supported", expected, result); + } + + @Test + public void testWriteRowLineCollectionTypeSingleValue() throws IOException { + String val = "1"; + + Collection collection = Collections.singletonList(val); + + generator.writeLine(collection); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + + assertEquals("Single value in a Collection should be supported", val, result); + } + + @Test + public void testWriteRowLineCollectionOfCollections() throws IOException { + String val1 = "1"; + String val2 = "2"; + String val3 = "3"; + String val4 = "4"; + + String valSeparator = ";"; + + Collection collection1 = Arrays.asList(val1, val2); + Collection collection2 = Arrays.asList(val3, val4); + Collection> collectionCollection = Arrays.asList(collection1, collection2); + + generator.writeLine(collectionCollection); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + String expected = val1 + valSeparator + val2 + valSeparator + val3 + valSeparator + val4; + + assertEquals("Collections of collections should be supported", expected, result); + } + + @Test + public void testWriteRowLineMapTypeSingleValue() throws IOException { + String k1 = "k1"; + String k2 = "k2"; + String v1 = "v1"; + String v2 = "v2"; + String pairSeparator = "="; + String entrySeparator = ","; + + Map map = new TreeMap<>(Map.of( // TreeMap ensures the entries are always ordered the same way + k1, v1, + k2, v2 + )); + + generator.writeLine(map); + generator.close(); + + String result = stringWriter.toString().replace("\n", ""); + String expected = k1 + pairSeparator + v1 + entrySeparator + k2 + pairSeparator + v2; + + assertEquals("Single value in a Collection should be supported", expected, result); + } + +}