TomlWriter#write() throws IllegalArgumentException when source object is

of an invalid type.
This commit is contained in:
moandji.ezana 2015-07-14 01:51:09 +02:00
parent 16915c8edb
commit c90d4e1e71
2 changed files with 12 additions and 13 deletions

View file

@ -143,19 +143,18 @@ public class TomlWriter {
/**
* Write an Object in TOML to a {@link Writer}.
*
* @param from the object to be written
* @param from the object to be written. Can be a Map or a custom type.
* @param target the Writer to which TOML will be written. The Writer is not closed.
* @throws IOException if target.write() fails
* @throws IllegalStateException if
* @throws IllegalArgumentException if from is of an invalid type
*/
public void write(Object from, Writer target) throws IOException {
ValueWriter valueWriter = WRITERS.findWriterFor(from);
ValueWriter writer = WRITERS.findWriterFor(from);
if (writer == MAP_VALUE_WRITER || writer == OBJECT_VALUE_WRITER) {
WRITERS.findWriterFor(from).write(from, context);
if (valueWriter == MAP_VALUE_WRITER || valueWriter == OBJECT_VALUE_WRITER) {
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
valueWriter.write(from, context);
} else {
throw new IllegalStateException("Top-level value must not be a primitive or array.");
throw new IllegalArgumentException("An object of class " + from.getClass().getSimpleName() + " cannot produce valid TOML. Please pass in a Map or a custom type.");
}
}
}

View file

@ -475,32 +475,32 @@ public class TomlWriterTest {
assertEquals("a = 1\n", readFile(output));
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_string_fragment() {
new TomlWriter().write("fragment");
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_boolean_fragment() {
new TomlWriter().write(true);
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_number_fragment() {
new TomlWriter().write(42);
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_date_fragment() {
new TomlWriter().write(new Date());
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_array_fragment() {
new TomlWriter().write(new int[2]);
}
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_table_array_fragment() {
new TomlWriter().write(new SimpleTestClass[2]);
}