mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2025-08-08 11:03:31 +02:00
added contents
This commit is contained in:
101
Projekte/common/src/test/java/pp/util/config/ConfigTest.java
Normal file
101
Projekte/common/src/test/java/pp/util/config/ConfigTest.java
Normal file
@@ -0,0 +1,101 @@
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.util.config;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConfigTest {
|
||||
|
||||
private TestConfig config;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
config = new TestConfig();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFromProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("test.string", "hello"); //NON-NLS
|
||||
properties.setProperty("test.int", "42");
|
||||
properties.setProperty("test.boolean", "true"); //NON-NLS
|
||||
properties.setProperty("test.intArray", "1; 2 ;3;4");
|
||||
|
||||
config.readFrom(properties);
|
||||
|
||||
assertEquals("hello", config.getTestString());
|
||||
assertEquals(42, config.getTestInt());
|
||||
assertTrue(config.isTestBoolean());
|
||||
assertArrayEquals(new int[]{1, 2, 3, 4}, config.getTestIntArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFromFile() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("test.string", "fileTest");
|
||||
properties.setProperty("test.int", "24");
|
||||
properties.setProperty("test.boolean", "false"); //NON-NLS
|
||||
properties.setProperty("test.intArray", "10;20;30");
|
||||
|
||||
File tempFile = File.createTempFile("testConfig", ".properties");
|
||||
try (FileWriter writer = new FileWriter(tempFile)) {
|
||||
properties.store(writer, null);
|
||||
}
|
||||
|
||||
config.readFrom(tempFile);
|
||||
|
||||
assertEquals("fileTest", config.getTestString());
|
||||
assertEquals(24, config.getTestInt());
|
||||
assertFalse(config.isTestBoolean());
|
||||
assertArrayEquals(new int[]{10, 20, 30}, config.getTestIntArray());
|
||||
|
||||
// Clean up
|
||||
tempFile.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToType() {
|
||||
assertEquals(42, config.convertToType("42", int.class));
|
||||
assertEquals(true, config.convertToType("true", boolean.class)); //NON-NLS
|
||||
assertEquals(3.14, config.convertToType("3.14", double.class));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testConvertToTypeWithUnsupportedType() {
|
||||
config.convertToType("unsupported", Object.class); //NON-NLS
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("test.string", "stringValue");
|
||||
properties.setProperty("test.int", "123");
|
||||
properties.setProperty("test.boolean", "true"); //NON-NLS
|
||||
properties.setProperty("test.intArray", "5;6;7");
|
||||
|
||||
config.readFrom(properties);
|
||||
|
||||
String expected = "[\ntest.boolean -> testBoolean = true,\n" +
|
||||
"test.int -> testInt = 123,\n" +
|
||||
"test.intArray -> testIntArray = {5, 6, 7},\n" +
|
||||
"test.string -> testString = stringValue\n" +
|
||||
"]";
|
||||
assertEquals(expected, config.toString());
|
||||
}
|
||||
}
|
40
Projekte/common/src/test/java/pp/util/config/TestConfig.java
Normal file
40
Projekte/common/src/test/java/pp/util/config/TestConfig.java
Normal file
@@ -0,0 +1,40 @@
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.util.config;
|
||||
|
||||
class TestConfig extends Config {
|
||||
@Property("test.string")
|
||||
private String testString;
|
||||
|
||||
@Property("test.int")
|
||||
private int testInt;
|
||||
|
||||
@Property("test.boolean")
|
||||
private boolean testBoolean;
|
||||
|
||||
@Property("test.intArray")
|
||||
@Separator(";")
|
||||
private int[] testIntArray;
|
||||
|
||||
// Getters for testing
|
||||
public String getTestString() {
|
||||
return testString;
|
||||
}
|
||||
|
||||
public int getTestInt() {
|
||||
return testInt;
|
||||
}
|
||||
|
||||
public boolean isTestBoolean() {
|
||||
return testBoolean;
|
||||
}
|
||||
|
||||
public int[] getTestIntArray() {
|
||||
return testIntArray;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user