mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Do not write types that would not produce valid TOML
This commit is contained in:
parent
30d4898938
commit
dbbd2f5e0e
2 changed files with 39 additions and 8 deletions
|
@ -1,5 +1,7 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
|
||||
import static com.moandjiezana.toml.ObjectValueWriter.OBJECT_VALUE_WRITER;
|
||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -146,7 +148,13 @@ public class TomlWriter {
|
|||
* @throws IOException if target.write() fails
|
||||
*/
|
||||
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.");
|
||||
}
|
||||
|
||||
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
|
||||
WRITERS.findWriterFor(from).write(from, context);
|
||||
valueWriter.write(from, context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -14,6 +14,7 @@ import java.math.BigDecimal;
|
|||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
@ -283,12 +284,14 @@ public class TomlWriterTest {
|
|||
|
||||
@Test
|
||||
public void should_write_strings_to_toml_utf8() throws UnsupportedEncodingException {
|
||||
String input = " é foo € \b \t \n \f \r \" \\ ";
|
||||
assertEquals("\" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"", new TomlWriter().write(input));
|
||||
|
||||
// Check unicode code points greater than 0XFFFF
|
||||
input = " \uD801\uDC28 \uD840\uDC0B ";
|
||||
assertEquals("\" \\U00010428 \\U0002000B \"", new TomlWriter().write(input));
|
||||
Map<String, String> input = new HashMap<String, String>();
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -468,6 +471,26 @@ public class TomlWriterTest {
|
|||
|
||||
assertEquals("a = 1\n", readFile(output));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void should_not_write_date() throws Exception {
|
||||
new TomlWriter().write(new Date());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void should_not_write_boolean() throws Exception {
|
||||
new TomlWriter().write(Boolean.TRUE);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void should_not_write_number() throws Exception {
|
||||
new TomlWriter().write(Long.valueOf(1));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void should_not_write_list() throws Exception {
|
||||
new TomlWriter().write(Arrays.asList("a"));
|
||||
}
|
||||
|
||||
private String readFile(File input) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(input));
|
||||
|
|
Loading…
Reference in a new issue