Make the configuration class work with streams instead of files.

pull/3/head
Bertrik Sikken 2017-09-04 01:53:53 +02:00
rodzic dec6fb2711
commit 442df50422
3 zmienionych plików z 31 dodań i 25 usunięć

Wyświetl plik

@ -1,6 +1,8 @@
package nl.sikken.bertrik;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
@ -138,11 +140,13 @@ public final class TtnHabBridge {
private static ITtnHabBridgeConfig readConfig(File file) throws IOException {
final TtnHabBridgeConfig config = new TtnHabBridgeConfig();
try {
config.load(file);
try (FileInputStream fis = new FileInputStream(file)) {
config.load(fis);
} catch (IOException e) {
LOG.info("Failed to load config {}, writing defaults", file.getAbsoluteFile());
config.save(file);
try (FileOutputStream fos = new FileOutputStream(file)) {
config.save(fos);
}
}
return config;
}

Wyświetl plik

@ -1,9 +1,8 @@
package nl.sikken.bertrik;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
@ -54,16 +53,14 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
}
/**
* Load settings from file.
* Load settings from stream.
*
* @param file the file
* @param is input stream containing the settings
* @throws IOException in case of a problem reading the file
*/
public void load(File file) throws IOException {
public void load(InputStream is) throws IOException {
final Properties properties = new Properties();
try (FileInputStream fis = new FileInputStream(file)) {
properties.load(fis);
}
properties.load(is);
for (EConfigItem e : EConfigItem.values()) {
String value = properties.getProperty(e.key);
if (value != null) {
@ -73,13 +70,13 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
}
/**
* Save settings to file.
* Save settings to stream.
*
* @param file the file
* @param os the output stream
* @throws IOException in case of a file problem
*/
public void save(File file) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.US_ASCII)) {
public void save(OutputStream os) throws IOException {
try (Writer writer = new OutputStreamWriter(os, StandardCharsets.US_ASCII)) {
for (EConfigItem e : EConfigItem.values()) {
// comment line
writer.append("# " + e.comment + "\n");

Wyświetl plik

@ -1,21 +1,18 @@
package nl.sikken.bertrik;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* Unit tests for TtnHabBridgeConfig.
*/
public final class TtnHabBridgeConfigTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
/**
* Verifies basic loading/saving of a configuration.
*
@ -23,10 +20,17 @@ public final class TtnHabBridgeConfigTest {
*/
@Test
public void testLoadSave() throws IOException {
final byte[] data;
// save
final TtnHabBridgeConfig config = new TtnHabBridgeConfig();
final File file = new File(tempFolder.getRoot(), "test.properties");
config.save(file);
config.load(file);
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
config.save(os);
data = os.toByteArray();
}
// load
try (InputStream is = new ByteArrayInputStream(data)) {
config.load(is);
}
}
/**
@ -40,6 +44,7 @@ public final class TtnHabBridgeConfigTest {
Assert.assertNotNull(config.getTtnMqttUrl());
Assert.assertNotNull(config.getTtnAppId());
Assert.assertNotNull(config.getTtnAppKey());
Assert.assertNotNull(config.getTtnGwCacheExpiry());
}
}