toml4j/src/test/java/com/moandjiezana/toml/TomlReadTest.java
2016-07-12 12:18:17 +02:00

63 lines
1.6 KiB
Java

package com.moandjiezana.toml;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.junit.Test;
public class TomlReadTest {
@Test
public void should_read_input_stream() throws Exception {
Toml toml = new Toml().read(getClass().getResourceAsStream("should_load_from_file.toml"));
assertEquals("value", toml.getString("key"));
}
@Test
public void should_read_reader() throws Exception {
Toml toml = new Toml().read(new StringReader("key=1"));
assertEquals(1, toml.getLong("key").intValue());
}
@Test
public void should_fail_on_missing_file() throws Exception {
try {
new Toml().read(new File("missing"));
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 {
new Toml().read(readerThatThrows);
fail("Exception should have been thrown");
} catch (RuntimeException e) {
assertThat(e.getCause(), instanceOf(IOException.class));
}
}
}