Moved BurntSushi encoder tests to own class to make it easier to see

which tests have been implemented.
This commit is contained in:
moandji.ezana 2015-06-29 10:13:12 +02:00
parent 9968add0ae
commit 123aa21aed
2 changed files with 231 additions and 113 deletions

View file

@ -0,0 +1,214 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
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;
public class BurntSushiValidEncoderTest {
@Test
public void arrays_hetergeneous() throws Exception {
runEncoder("arrays-hetergeneous");
}
@Test
public void arrays_nested() throws Exception {
runEncoder("arrays-nested");
}
@Test
public void datetime() throws Exception {
runEncoder("datetime");
}
@Test
public void empty() throws Exception {
runEncoder("empty");
}
@Test
public void example() throws Exception {
runEncoder("example");
}
@Test
public void float_() throws Exception {
runEncoder("float");
}
@Test
public void implicit_and_explicit_before() throws Exception {
runEncoder("implicit-and-explicit-before");
}
@Test
public void implicit_groups() throws Exception {
runEncoder("implicit-groups");
}
@Test
public void long_float() throws Exception {
runEncoder("long-float");
}
@Test
public void long_integer() throws Exception {
runEncoder("long-integer");
}
@Test
public void key_special_chars_modified() throws Exception {
runEncoder("key-special-chars-modified");
}
@Test
public void integer() throws Exception {
runEncoder("integer");
}
@Test
public void string_empty() throws Exception {
runEncoder("string-empty");
}
@Test
public void string_escapes_modified() throws Exception {
runEncoder("string-escapes-modified");
}
@Test
public void string_simple() throws Exception {
runEncoder("string-simple");
}
@Test
public void table_array_implicit() throws Exception {
runEncoder("table-array-implicit");
}
@Test
public void table_array_many() throws Exception {
runEncoder("table-array-many");
}
@Test
public void table_array_nest_modified() throws Exception {
// Modified to remove stray spaces in the expected TOML
runEncoder("table-array-nest-modified",
new TomlWriter().setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)));
}
@Test
public void table_array_one() throws Exception {
runEncoder("table-array-one");
}
private static final Gson GSON = new Gson();
private void runEncoder(String testName) {
runEncoder(testName,
new TomlWriter().
wantTerseArrays(true).
setTimeZone(TimeZone.getTimeZone("UTC")).
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")));
}
private void runEncoder(String testName, TomlWriter tomlWriter) {
InputStream inputTomlStream = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
String expectedToml = convertStreamToString(inputTomlStream);
Reader inputJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement jsonInput = GSON.fromJson(inputJsonReader, JsonElement.class);
Map<String, Object> enriched = enrichJson(jsonInput.getAsJsonObject());
String encoded = tomlWriter.write(enriched);
assertEquals(expectedToml, encoded);
}
// Enrich toml-test JSON trees into native Java types, suiteable
// for consumption by TomlWriter.
private Map<String, Object> enrichJson(JsonObject jsonObject) {
Map<String, Object> enriched = new LinkedHashMap<String, Object>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
enriched.put(entry.getKey(), enrichJsonElement(entry.getValue()));
}
return enriched;
}
Object enrichJsonElement(JsonElement jsonElement) {
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("type") && jsonObject.has("value")) {
return enrichPrimitive(jsonObject);
}
return enrichJson(jsonElement.getAsJsonObject());
} else if (jsonElement.isJsonArray()) {
List<Object> tables = new LinkedList<Object>();
for (JsonElement arrayElement : jsonElement.getAsJsonArray()) {
tables.add(enrichJsonElement(arrayElement));
}
return tables;
}
throw new AssertionError("received unexpected JsonElement: " + jsonElement);
}
private Object enrichPrimitive(JsonObject jsonObject) {
String type = jsonObject.getAsJsonPrimitive("type").getAsString();
if ("bool".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsBoolean();
} else if ("integer".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsBigInteger();
} else if ("float".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsDouble();
} else if ("string".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsString();
} else if ("datetime".equals(type)) {
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString = jsonObject.getAsJsonPrimitive("value").getAsString();
try {
return iso8601Format.parse(dateString);
} catch (ParseException e) {
throw new AssertionError("failed to parse datetime '" + dateString + "': " + e.getMessage());
}
} else if ("array".equals(type)) {
JsonArray jsonArray = jsonObject.getAsJsonArray("value");
List<Object> enriched = new LinkedList<Object>();
for (JsonElement arrayElement : jsonArray) {
enriched.add(enrichJsonElement(arrayElement));
}
return enriched;
}
throw new AssertionError("enrichPrimitive: received unknown type " + type);
}
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}

View file

@ -1,8 +1,6 @@
package com.moandjiezana.toml;
import com.google.gson.*;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
@ -10,11 +8,24 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class BurntSushiValidTest {
@ -32,13 +43,11 @@ public class BurntSushiValidTest {
@Test
public void arrays_hetergeneous() throws Exception {
run("arrays-hetergeneous");
runEncoder("arrays-hetergeneous");
}
@Test
public void arrays_nested() throws Exception {
run("arrays-nested");
runEncoder("arrays-nested");
}
@Test
@ -59,25 +68,21 @@ public class BurntSushiValidTest {
@Test
public void datetime() throws Exception {
run("datetime");
runEncoder("datetime");
}
@Test
public void empty() throws Exception {
run("empty");
runEncoder("empty");
}
@Test
public void example() throws Exception {
run("example");
runEncoder("example");
}
@Test
public void float_() throws Exception {
run("float");
runEncoder("float");
}
@Test
@ -88,19 +93,16 @@ public class BurntSushiValidTest {
@Test
public void implicit_and_explicit_before() throws Exception {
run("implicit-and-explicit-before");
runEncoder("implicit-and-explicit-before");
}
@Test
public void implicit_groups() throws Exception {
run("implicit-groups");
runEncoder("implicit-groups");
}
@Test
public void integer() throws Exception {
run("integer");
runEncoder("integer");
}
@Test
@ -126,7 +128,6 @@ public class BurntSushiValidTest {
@Test
public void key_special_chars_modified() throws Exception {
run("key-special-chars-modified");
runEncoder("key-special-chars-modified");
}
@Test @Ignore
@ -142,13 +143,11 @@ public class BurntSushiValidTest {
@Test
public void long_float() throws Exception {
run("long-float");
runEncoder("long-float");
}
@Test
public void long_integer() throws Exception {
run("long-integer");
runEncoder("long-integer");
}
@Test @Ignore
@ -174,7 +173,6 @@ public class BurntSushiValidTest {
@Test
public void string_empty() throws Exception {
run("string-empty");
runEncoder("string-empty");
}
@Test @Ignore
@ -185,13 +183,11 @@ public class BurntSushiValidTest {
@Test
public void string_escapes_modified() throws Exception {
run("string-escapes-modified");
runEncoder("string-escapes-modified");
}
@Test
public void string_simple() throws Exception {
run("string-simple");
runEncoder("string-simple");
}
@Test
@ -202,13 +198,11 @@ public class BurntSushiValidTest {
@Test
public void table_array_implicit() throws Exception {
run("table-array-implicit");
runEncoder("table-array-implicit");
}
@Test
public void table_array_many() throws Exception {
run("table-array-many");
runEncoder("table-array-many");
}
@Test
@ -216,17 +210,9 @@ public class BurntSushiValidTest {
run("table-array-nest");
}
@Test
public void table_array_nest_modified() throws Exception {
// Same test, but with stray spaces in the expected TOML removed
runEncoder("table-array-nest-modified",
new TomlWriter().setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)));
}
@Test
public void table_array_one() throws Exception {
run("table-array-one");
runEncoder("table-array-one");
}
@Test
@ -285,88 +271,6 @@ public class BurntSushiValidTest {
return s.hasNext() ? s.next() : "";
}
private void runEncoder(String testName) {
runEncoder(testName,
new TomlWriter().
wantTerseArrays(true).
setTimeZone(TimeZone.getTimeZone("UTC")).
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")));
}
private void runEncoder(String testName, TomlWriter tomlWriter) {
InputStream inputTomlStream = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
String expectedToml = convertStreamToString(inputTomlStream);
Reader inputJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement jsonInput = GSON.fromJson(inputJsonReader, JsonElement.class);
Map<String, Object> enriched = enrichJson(jsonInput.getAsJsonObject());
String encoded = tomlWriter.write(enriched);
assertEquals(expectedToml, encoded);
}
// Enrich toml-test JSON trees into native Java types, suiteable
// for consumption by TomlWriter.
private Map<String, Object> enrichJson(JsonObject jsonObject) {
Map<String, Object> enriched = new LinkedHashMap<String, Object>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
enriched.put(entry.getKey(), enrichJsonElement(entry.getValue()));
}
return enriched;
}
Object enrichJsonElement(JsonElement jsonElement) {
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("type") && jsonObject.has("value")) {
return enrichPrimitive(jsonObject);
}
return enrichJson(jsonElement.getAsJsonObject());
} else if (jsonElement.isJsonArray()) {
List<Object> tables = new LinkedList<Object>();
for (JsonElement arrayElement : jsonElement.getAsJsonArray()) {
tables.add(enrichJsonElement(arrayElement));
}
return tables;
}
throw new AssertionError("received unexpected JsonElement: " + jsonElement);
}
private Object enrichPrimitive(JsonObject jsonObject) {
String type = jsonObject.getAsJsonPrimitive("type").getAsString();
if ("bool".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsBoolean();
} else if ("integer".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsBigInteger();
} else if ("float".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsDouble();
} else if ("string".equals(type)) {
return jsonObject.getAsJsonPrimitive("value").getAsString();
} else if ("datetime".equals(type)) {
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString = jsonObject.getAsJsonPrimitive("value").getAsString();
try {
return iso8601Format.parse(dateString);
} catch (ParseException e) {
throw new AssertionError("failed to parse datetime '" + dateString + "': " + e.getMessage());
}
} else if ("array".equals(type)) {
JsonArray jsonArray = jsonObject.getAsJsonArray("value");
List<Object> enriched = new LinkedList<Object>();
for (JsonElement arrayElement : jsonArray) {
enriched.add(enrichJsonElement(arrayElement));
}
return enriched;
}
throw new AssertionError("enrichPrimitive: received unknown type " + type);
}
private static final Gson GSON = new Gson();
private static final Gson TEST_GSON = new GsonBuilder()
.registerTypeAdapter(Boolean.class, serialize(Boolean.class))