From 928179a1f3d2bd0817803277298af3bf4ee706c9 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 14 Feb 2017 08:30:22 -0600 Subject: [PATCH] locale tests --- build.gradle | 6 +++ .../org/json/junit/JSONObjectLocaleTest.java | 48 +++++++++++++++++++ .../java/org/json/junit/JunitTestSuite.java | 1 + .../java/org/json/junit/MyLocaleBean.java | 12 +++++ 4 files changed, 67 insertions(+) create mode 100755 src/test/java/org/json/junit/JSONObjectLocaleTest.java create mode 100755 src/test/java/org/json/junit/MyLocaleBean.java diff --git a/build.gradle b/build.gradle index d2969d4..58259f9 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,12 @@ apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'jacoco' +tasks.withType(JavaCompile) { + // this subproject requires -parameters option + options.compilerArgs << '-parameters' + options.encoding = 'UTF-8' +} + sourceSets { // Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code main diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java new file mode 100755 index 0000000..a6cb996 --- /dev/null +++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java @@ -0,0 +1,48 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.json.*; +import org.junit.*; + +/** + * Note: This file is saved as UTF-8. Do not save as ASCII or the tests will + * fail. + * + */ +public class JSONObjectLocaleTest { + /** + * JSONObject built from a bean with locale-specific keys - that is, the key + * fields are not LANG_ENGLISH. + */ + @Test + public void jsonObjectByLocaleBean() { + + MyLocaleBean myLocaleBean = new MyLocaleBean(); + + Locale.setDefault(new Locale("en")); + JSONObject jsonen = new JSONObject(myLocaleBean); + System.out.println("jsonen " + jsonen); + + Locale.setDefault(new Locale("tr")); + JSONObject jsontr = new JSONObject(myLocaleBean); + System.out.println("jsontr " + jsontr); + /** + * In this test we exercise code that handles keys of 1-char and + * multi-char length that include text from a non-English locale. + * Turkish in this case. The JSONObject code should correctly retain the + * non-EN_LANG chars in the key. + */ + assertTrue("expected beanId", + "Tlocaleüx".equals(jsonObject.getString(""))); + assertTrue("expected Tlocalü", + "Tlocaleü".equals(jsonObject.getString("ü"))); + assertTrue("expected Tlocaleüx", + "Tlocaleüx".equals((String)(jsonObject.query("/üx")))); + assertTrue("expected Tlocalü", + "Tlocaleü".equals((String)(jsonObject.query("/ü")))); + } + +} diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java index 3a7223e..36bec60 100644 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ b/src/test/java/org/json/junit/JunitTestSuite.java @@ -13,6 +13,7 @@ import org.junit.runners.Suite; HTTPTest.class, JSONStringerTest.class, JSONObjectTest.class, + JSONObjectLocaleTest.class, JSONArrayTest.class, EnumTest.class, JSONPointerTest.class, diff --git a/src/test/java/org/json/junit/MyLocaleBean.java b/src/test/java/org/json/junit/MyLocaleBean.java new file mode 100755 index 0000000..0d68c39 --- /dev/null +++ b/src/test/java/org/json/junit/MyLocaleBean.java @@ -0,0 +1,12 @@ +package org.json.junit; + +public class MyLocaleBean { + private final String id = "beanId"; + private final String i = "beanI"; + public String getId() { + return id; + } + public String getI() { + return i; + } +}