mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-10-31 17:29:14 +00:00
Support for special cases: char, Character, URL, URI and enums.
This commit is contained in:
parent
210d9dcb3c
commit
d8a02ce356
|
@ -1,5 +1,7 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -91,7 +93,7 @@ class StringConverter implements ValueConverter, ValueWriter {
|
|||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return value.getClass().isAssignableFrom(String.class);
|
||||
return value instanceof String || value instanceof Character || value instanceof URL || value instanceof URI || value instanceof Enum;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,15 +1,33 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TomlWriterTest {
|
||||
|
||||
@Rule
|
||||
|
@ -276,6 +294,69 @@ public class TomlWriterTest {
|
|||
assertEquals(expected, new TomlWriter().write(aMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_urls() throws Exception {
|
||||
class WithUrl {
|
||||
URL url;
|
||||
URI uri;
|
||||
}
|
||||
|
||||
WithUrl from = new WithUrl();
|
||||
from.url = new URL("https://github.com");
|
||||
from.uri = new URI("https://bitbucket.com");
|
||||
|
||||
String expected = "url = \"https://github.com\"\n"
|
||||
+ "uri = \"https://bitbucket.com\"\n";
|
||||
|
||||
assertEquals(expected, new TomlWriter().write(from));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_enum() throws Exception {
|
||||
class WithEnum {
|
||||
RetentionPolicy retentionPolicy = RetentionPolicy.RUNTIME;
|
||||
}
|
||||
|
||||
assertEquals("retentionPolicy = \"RUNTIME\"\n", new TomlWriter().write(new WithEnum()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_char() throws Exception {
|
||||
class WithChar {
|
||||
char c = 'a';
|
||||
}
|
||||
|
||||
assertEquals("c = \"a\"\n", new TomlWriter().write(new WithChar()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_big_numbers() throws Exception {
|
||||
class WithBigNumbers {
|
||||
BigInteger bigInt = BigInteger.valueOf(1);
|
||||
BigDecimal bigDecimal = BigDecimal.valueOf(2.8);
|
||||
}
|
||||
|
||||
String expected = "bigInt = 1\n"
|
||||
+ "bigDecimal = 2.8\n";
|
||||
|
||||
assertEquals(expected, new TomlWriter().write(new WithBigNumbers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_wrappers() throws Exception {
|
||||
class WithWrappers {
|
||||
Character c = Character.valueOf('b');
|
||||
Long l = Long.valueOf(2);
|
||||
Double d = Double.valueOf(3.4);
|
||||
}
|
||||
|
||||
String expected = "c = \"b\"\n"
|
||||
+ "l = 2\n"
|
||||
+ "d = 3.4\n";
|
||||
|
||||
assertEquals(expected, new TomlWriter().write(new WithWrappers()));
|
||||
}
|
||||
|
||||
private static class SimpleTestClass {
|
||||
int a = 1;
|
||||
}
|
||||
|
@ -307,6 +388,7 @@ public class TomlWriterTest {
|
|||
private String readFile(File input) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(input));
|
||||
|
||||
try {
|
||||
StringBuilder w = new StringBuilder();
|
||||
String line = bufferedReader.readLine();
|
||||
while (line != null) {
|
||||
|
@ -315,5 +397,8 @@ public class TomlWriterTest {
|
|||
}
|
||||
|
||||
return w.toString();
|
||||
} finally {
|
||||
bufferedReader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue