diff --git a/src/main/java/com/moandjiezana/toml/ArrayValueWriter.java b/src/main/java/com/moandjiezana/toml/ArrayValueWriter.java index 2e51d28..c5de0c5 100644 --- a/src/main/java/com/moandjiezana/toml/ArrayValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/ArrayValueWriter.java @@ -1,12 +1,12 @@ package com.moandjiezana.toml; -import static com.moandjiezana.toml.ValueWriters.WRITERS; - import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import static com.moandjiezana.toml.ValueWriters.WRITERS; + abstract class ArrayValueWriter implements ValueWriter { static protected boolean isArrayish(Object value) { return value instanceof Collection || value.getClass().isArray(); @@ -24,7 +24,7 @@ abstract class ArrayValueWriter implements ValueWriter { return valueWriter.isPrimitiveType() || isArrayish(first); } - return false; + return true; } @SuppressWarnings("unchecked") diff --git a/src/main/java/com/moandjiezana/toml/TomlWriter.java b/src/main/java/com/moandjiezana/toml/TomlWriter.java index 75aabb2..2261433 100644 --- a/src/main/java/com/moandjiezana/toml/TomlWriter.java +++ b/src/main/java/com/moandjiezana/toml/TomlWriter.java @@ -146,15 +146,16 @@ public class TomlWriter { * @param from the object to be written * @param target the Writer to which TOML will be written. The Writer is not closed. * @throws IOException if target.write() fails + * @throws IllegalStateException if */ public void write(Object from, Writer target) throws IOException { ValueWriter valueWriter = WRITERS.findWriterFor(from); - - if (valueWriter != MAP_VALUE_WRITER && valueWriter != OBJECT_VALUE_WRITER) { - throw new IllegalArgumentException("An object of type " + from.getClass().getSimpleName() + " cannot produce valid TOML."); + + ValueWriter writer = WRITERS.findWriterFor(from); + if (writer == MAP_VALUE_WRITER || writer == OBJECT_VALUE_WRITER) { + WRITERS.findWriterFor(from).write(from, context); + } else { + throw new IllegalStateException("Top-level value must not be a primitive or array."); } - - WriterContext context = new WriterContext(indentationPolicy, datePolicy, target); - valueWriter.write(from, context); } } diff --git a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java index b4d2997..456ae8f 100644 --- a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java +++ b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java @@ -1,6 +1,10 @@ package com.moandjiezana.toml; -import static org.junit.Assert.assertEquals; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import org.junit.*; import java.io.InputStream; import java.io.InputStreamReader; @@ -15,15 +19,15 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import org.junit.Test; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import static org.junit.Assert.*; public class BurntSushiValidEncoderTest { + @Test + public void array_empty() throws Exception { + runEncoder("array-empty"); + } + @Test public void arrays_hetergeneous() throws Exception { runEncoder("arrays-hetergeneous"); diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index a7df780..928f546 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -220,7 +220,7 @@ public class TomlWriterTest { List aList = new LinkedList(); Float[] anArray = new Float[0]; } - assertEquals("", new TomlWriter().write(new TestClass())); + assertEquals("aList = []\nanArray = []\n", new TomlWriter().write(new TestClass())); } @Test @@ -284,14 +284,17 @@ public class TomlWriterTest { @Test public void should_write_strings_to_toml_utf8() throws UnsupportedEncodingException { - Map input = new HashMap(); - input.put("a", " é foo € \b \t \n \f \r \" \\ "); - input.put("b", " \uD801\uDC28 \uD840\uDC0B "); // Check unicode code points greater than 0XFFFF - - String expected = "a = \" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"\n" - + "b = \" \\U00010428 \\U0002000B \"\n"; - - assertEquals(expected, new TomlWriter().write(input)); + class Utf8Test { + String input; + } + + Utf8Test utf8Test = new Utf8Test(); + utf8Test.input = " é foo € \b \t \n \f \r \" \\ "; + assertEquals("input = \" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"\n", new TomlWriter().write(utf8Test)); + + // Check unicode code points greater than 0XFFFF + utf8Test.input = " \uD801\uDC28 \uD840\uDC0B "; + assertEquals("input = \" \\U00010428 \\U0002000B \"\n", new TomlWriter().write(utf8Test)); } @Test @@ -443,7 +446,7 @@ public class TomlWriterTest { assertEquals(expected, writer.write(o)); } - + private static class SimpleTestClass { int a = 1; } @@ -472,21 +475,36 @@ public class TomlWriterTest { assertEquals("a = 1\n", readFile(output)); } - @Test(expected=IllegalArgumentException.class) - public void should_not_write_date() throws Exception { + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_string_fragment() { + new TomlWriter().write("fragment"); + } + + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_boolean_fragment() { + new TomlWriter().write(true); + } + + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_number_fragment() { + new TomlWriter().write(42); + } + + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_date_fragment() { new TomlWriter().write(new Date()); } - - @Test(expected=IllegalArgumentException.class) - public void should_not_write_boolean() throws Exception { - new TomlWriter().write(Boolean.TRUE); + + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_array_fragment() { + new TomlWriter().write(new int[2]); } - - @Test(expected=IllegalArgumentException.class) - public void should_not_write_number() throws Exception { - new TomlWriter().write(Long.valueOf(1)); + + @Test(expected = IllegalStateException.class) + public void should_refuse_to_write_table_array_fragment() { + new TomlWriter().write(new SimpleTestClass[2]); } - + @Test(expected=IllegalArgumentException.class) public void should_not_write_list() throws Exception { new TomlWriter().write(Arrays.asList("a"));