Support for special cases: char, Character, URL, URI and enums.

This commit is contained in:
moandji.ezana 2015-06-29 20:52:32 +02:00
parent 210d9dcb3c
commit d8a02ce356
2 changed files with 101 additions and 14 deletions

View file

@ -1,5 +1,7 @@
package com.moandjiezana.toml; package com.moandjiezana.toml;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -91,7 +93,7 @@ class StringConverter implements ValueConverter, ValueWriter {
@Override @Override
public boolean canWrite(Object value) { 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 @Override

View file

@ -1,15 +1,33 @@
package com.moandjiezana.toml; 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.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import java.io.*; @SuppressWarnings("unused")
import java.text.SimpleDateFormat;
import java.util.*;
import static org.junit.Assert.assertEquals;
public class TomlWriterTest { public class TomlWriterTest {
@Rule @Rule
@ -276,6 +294,69 @@ public class TomlWriterTest {
assertEquals(expected, new TomlWriter().write(aMap)); 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 { private static class SimpleTestClass {
int a = 1; int a = 1;
} }
@ -307,6 +388,7 @@ public class TomlWriterTest {
private String readFile(File input) throws IOException { private String readFile(File input) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(input)); BufferedReader bufferedReader = new BufferedReader(new FileReader(input));
try {
StringBuilder w = new StringBuilder(); StringBuilder w = new StringBuilder();
String line = bufferedReader.readLine(); String line = bufferedReader.readLine();
while (line != null) { while (line != null) {
@ -315,5 +397,8 @@ public class TomlWriterTest {
} }
return w.toString(); return w.toString();
} finally {
bufferedReader.close();
}
} }
} }