Skip to content
Snippets Groups Projects
Commit 181b3fc4 authored by Leonard Kupper's avatar Leonard Kupper
Browse files

Add tests.

parent 39403e8f
No related branches found
No related tags found
2 merge requests!7Feat/configuration parser 5,!4WIP: Feat/configuration parser 5
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());
}
}
# 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
# 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
# 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
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment