mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 03:30:00 +00:00
TomlWriter#write() throws IllegalArgumentException when source object is
of an invalid type.
This commit is contained in:
parent
16915c8edb
commit
c90d4e1e71
2 changed files with 12 additions and 13 deletions
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue