mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-11-01 01:39:26 +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;
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
@ -275,7 +293,70 @@ public class TomlWriterTest {
|
||||||
"\"e/f\" = 4\n";
|
"\"e/f\" = 4\n";
|
||||||
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,13 +388,17 @@ 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));
|
||||||
|
|
||||||
StringBuilder w = new StringBuilder();
|
try {
|
||||||
String line = bufferedReader.readLine();
|
StringBuilder w = new StringBuilder();
|
||||||
while (line != null) {
|
String line = bufferedReader.readLine();
|
||||||
w.append(line).append('\n');
|
while (line != null) {
|
||||||
line = bufferedReader.readLine();
|
w.append(line).append('\n');
|
||||||
}
|
line = bufferedReader.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
return w.toString();
|
return w.toString();
|
||||||
|
} finally {
|
||||||
|
bufferedReader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue