2014-04-08 14:37:02 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
2014-04-10 13:07:45 +00:00
|
|
|
import static org.hamcrest.Matchers.instanceOf;
|
2014-04-08 14:37:02 +00:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2014-04-10 13:07:45 +00:00
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
import static org.junit.Assert.fail;
|
2014-04-08 14:37:02 +00:00
|
|
|
|
2014-04-10 13:07:45 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Reader;
|
2014-04-08 14:37:02 +00:00
|
|
|
import java.io.StringReader;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-07-12 10:18:17 +00:00
|
|
|
public class TomlReadTest {
|
2014-04-08 14:37:02 +00:00
|
|
|
|
|
|
|
@Test
|
2016-07-12 10:18:17 +00:00
|
|
|
public void should_read_input_stream() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(getClass().getResourceAsStream("should_load_from_file.toml"));
|
2014-04-08 14:37:02 +00:00
|
|
|
|
|
|
|
assertEquals("value", toml.getString("key"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-07-12 10:18:17 +00:00
|
|
|
public void should_read_reader() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(new StringReader("key=1"));
|
2014-04-08 14:37:02 +00:00
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("key").intValue());
|
|
|
|
}
|
2014-04-10 13:07:45 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_fail_on_missing_file() throws Exception {
|
|
|
|
try {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read(new File("missing"));
|
2014-04-10 13:07:45 +00:00
|
|
|
fail("Exception should have been thrown");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_fail_on_io_error() throws Exception {
|
|
|
|
Reader readerThatThrows = new Reader() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int read(char[] cbuf, int off, int len) throws IOException {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read(readerThatThrows);
|
2014-04-10 13:07:45 +00:00
|
|
|
fail("Exception should have been thrown");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertThat(e.getCause(), instanceOf(IOException.class));
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 11:10:10 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_read_toml_without_defaults() {
|
|
|
|
Toml toml1 = new Toml().read("a = 1");
|
|
|
|
Toml toml2 = new Toml().read(toml1);
|
|
|
|
|
|
|
|
assertEquals(toml1.getLong("a"), toml2.getLong("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_read_toml_and_merge_with_defaults() {
|
|
|
|
Toml toml1 = new Toml().read("a = 1\nc = 3\nd = 5");
|
|
|
|
Toml toml2 = new Toml().read("b = 2\nc = 4");
|
|
|
|
Toml mergedToml = new Toml(toml1).read(toml2);
|
|
|
|
|
|
|
|
assertEquals(toml1.getLong("a"), mergedToml.getLong("a"));
|
|
|
|
assertEquals(toml2.getLong("b"), mergedToml.getLong("b"));
|
|
|
|
assertEquals(toml2.getLong("c"), mergedToml.getLong("c"));
|
|
|
|
assertEquals(toml1.getLong("d"), mergedToml.getLong("d"));
|
|
|
|
}
|
2014-04-08 14:37:02 +00:00
|
|
|
}
|