diff --git a/src/test/java/de/tudresden/inf/mci/brailleplot/configparser/JavaPropertiesConfigurationParserTest.java b/src/test/java/de/tudresden/inf/mci/brailleplot/configparser/JavaPropertiesConfigurationParserTest.java new file mode 100644 index 0000000000000000000000000000000000000000..911ac361e7e3f8c3f63e4691cc21a310a2c89e93 --- /dev/null +++ b/src/test/java/de/tudresden/inf/mci/brailleplot/configparser/JavaPropertiesConfigurationParserTest.java @@ -0,0 +1,137 @@ +package de.tudresden.inf.mci.brailleplot.configparser; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.Arrays; +import java.util.HashSet; +import java.util.NoSuchElementException; +import java.util.Set; + + +public class JavaPropertiesConfigurationParserTest { + + public static Printer mPrinterConfig; + public static Format mFormatConfig; + + public static File getResource(String fileName) { + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + File resourceFile = new File(classLoader.getResource(fileName).getFile()); + return resourceFile; + } + + // Correct use testcases + @Test @BeforeAll + public static void testSuccessfulParsing() { + String defaultConfigPath = getResource("defaultConfig.properties").getAbsolutePath(); + String configPath = getResource("dummyPrinterConfig.properties").getAbsolutePath(); + Assertions.assertDoesNotThrow(() -> { + // Parse defaults + ConfigurationParser defaultPropertyParser = new JavaPropertiesConfigurationParser(defaultConfigPath); + Printer defaultPrinterConfig = defaultPropertyParser.getPrinter(); + Format defaultFormatConfig = defaultPropertyParser.getFormat("default"); + + // Parse actual properties with defaults set + ConfigurationParser parser = new JavaPropertiesConfigurationParser(configPath, defaultPrinterConfig, defaultFormatConfig); + mPrinterConfig = parser.getPrinter(); + Set<String> properties = mPrinterConfig.getPropertyNames(); + Set<String> formats = parser.getFormatNames(); + mFormatConfig = parser.getFormat("B5"); + }); + } + @Test + public void testCorrectValues() { + // default values - not overwritten + Assertions.assertEquals(35, mPrinterConfig.getProperty("max.charsPerLine").toInt()); + Assertions.assertEquals(29, mPrinterConfig.getProperty("max.linesPerPage").toInt()); + + Assertions.assertEquals(10, mFormatConfig.getProperty("margin.left").toInt()); + + // overwritten values + Assertions.assertEquals(true, mPrinterConfig.getProperty("equidistantSupport").toBool()); + Assertions.assertEquals(2.5, mPrinterConfig.getProperty("max.characterDistance").toDouble()); + + Assertions.assertEquals(176, mFormatConfig.getProperty("page.width").toInt()); + Assertions.assertEquals(250, mFormatConfig.getProperty("page.height").toInt()); + + // values without default + Assertions.assertEquals("Index Everest-D V4", mPrinterConfig.getProperty("name").toString()); + + Assertions.assertEquals(false, mFormatConfig.getProperty("isPortrait").toBool()); + } + @Test + public void testCompatibleTypeConversion() { + Assertions.assertEquals("35", mPrinterConfig.getProperty("max.charsPerLine").toString()); + Assertions.assertEquals(false, mPrinterConfig.getProperty("max.characterDistance").toBool()); + + Assertions.assertEquals(250.0, mFormatConfig.getProperty("page.height").toDouble()); + } + @Test + public void testFallbackProperties() { + + String specifiedByConfig[] = {"name", "max.characterDistance", "equidistantSupport"}; + String specifiedByFallback[] = {"max.charsPerLine", "max.linesPerPage"}; + + // config shall extend the fallback + HashSet<String> expectedPropertyNames = new HashSet<>(Arrays.asList(specifiedByConfig)); + expectedPropertyNames.addAll(new HashSet<>(Arrays.asList(specifiedByFallback))); + + HashSet<String> propertyNames = mPrinterConfig.getPropertyNames(); + Assertions.assertEquals(expectedPropertyNames, propertyNames); + } + + + // Incorrect use testcases + @Test + public void testIllegalFile() { + Assertions.assertThrows( + ConfigurationParsingException.class, + () -> new JavaPropertiesConfigurationParser("nonexistent.properties") + ); + } + @Test + public void testIllegalProperty() { + String configPath = getResource("illegalPropertyNameExample.properties").getAbsolutePath(); + Assertions.assertThrows( + ConfigurationValidationException.class, + () -> new JavaPropertiesConfigurationParser(configPath) + ); + } + @Test + public void testIllegalValue() { + String configPath = getResource("illegalPropertyValueExample.properties").getAbsolutePath(); + Assertions.assertThrows( + ConfigurationValidationException.class, + () -> new JavaPropertiesConfigurationParser(configPath) + ); + } + @Test + public void testNonexistentFormat() { + String configPath = getResource("defaultConfig.properties").getAbsolutePath(); + Assertions.assertThrows(NoSuchElementException.class, () -> { + ConfigurationParser parser = new JavaPropertiesConfigurationParser(configPath); + parser.getFormat("B5"); + }); + } + @Test + public void testNonexistentProperties() { + String configPath = getResource("defaultConfig.properties").getAbsolutePath(); + Assertions.assertThrows(NoSuchElementException.class, () -> { + ValidProperty spooderman = mPrinterConfig.getProperty("spooderman"); + }); + Assertions.assertThrows(NoSuchElementException.class, () -> { + ValidProperty doge = mFormatConfig.getProperty("doge"); + }); + } + @Test + public void testIncompatibleTypeConversion() { + Assertions.assertThrows(NumberFormatException.class, () -> mPrinterConfig.getProperty("equidistantSupport").toInt()); + Assertions.assertThrows(NumberFormatException.class, () -> mPrinterConfig.getProperty("max.characterDistance").toInt()); + Assertions.assertThrows(NumberFormatException.class, () -> mPrinterConfig.getProperty("name").toDouble()); + + Assertions.assertThrows(NumberFormatException.class, () -> mFormatConfig.getProperty("isPortrait").toDouble()); + } + +} diff --git a/src/test/resources/defaultConfig.properties b/src/test/resources/defaultConfig.properties new file mode 100644 index 0000000000000000000000000000000000000000..94a89776af8b12a99a6f4067b897fb3a410f0dd3 --- /dev/null +++ b/src/test/resources/defaultConfig.properties @@ -0,0 +1,13 @@ +# Default Printer Properties +printer.max.charsPerLine=35 +printer.max.linesPerPage=29 +printer.equidistantSupport=false +printer.max.characterDistance=2.5 + +# Default Format Definition (assume A4 portrait) +format.default.page.width=210 +format.default.page.height=297 +format.default.margin.left=10 +format.default.margin.top=10 +format.default.margin.right=10 +format.default.margin.bottom=10 \ No newline at end of file diff --git a/src/test/resources/dummyPrinterConfig.properties b/src/test/resources/dummyPrinterConfig.properties new file mode 100644 index 0000000000000000000000000000000000000000..7816cf76845b39fa7d8eba137f7bdd3597a43efd --- /dev/null +++ b/src/test/resources/dummyPrinterConfig.properties @@ -0,0 +1,13 @@ +# General Printer Properties +printer.name=Index Everest-D V4 +printer.max.characterDistance=2.5 +printer.equidistantSupport=true + +# A4 Format +format.A4.page.width=210 +format.A4.page.height=297 + +# B5 Format +format.B5.page.width=176 +format.B5.page.height=250 +format.B5.isPortrait=false \ No newline at end of file diff --git a/src/test/resources/illegalPropertyNameExample.properties b/src/test/resources/illegalPropertyNameExample.properties new file mode 100644 index 0000000000000000000000000000000000000000..ebe2c842f33436a5f32071422b0a36690787c2b8 --- /dev/null +++ b/src/test/resources/illegalPropertyNameExample.properties @@ -0,0 +1,9 @@ +# Default Printer Properties +printer.max.charsPerLine=35 +printer.max.linesPerPage=29 +printer.equidistantSupport=false +printer.max.characterDistance=2.5 + +# Default Format Definition (assume A4 portrait) +format.default.page.width=210 +fomrat.default.page.height=140 \ No newline at end of file diff --git a/src/test/resources/illegalPropertyValueExample.properties b/src/test/resources/illegalPropertyValueExample.properties new file mode 100644 index 0000000000000000000000000000000000000000..8135ba242657a64d8b1d075b594ea38d3097ffd1 --- /dev/null +++ b/src/test/resources/illegalPropertyValueExample.properties @@ -0,0 +1,9 @@ +# Default Printer Properties +printer.max.charsPerLine=35 +printer.max.linesPerPage=29 +printer.equidistantSupport=false +printer.max.characterDistance=2.5 + +# Default Format Definition (assume A4 portrait) +format.default.page.width=210 +format.default.page.height=abc \ No newline at end of file