mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #64 from stleary/locale-tests-for-non-EN-keys
Locale tests for non en keys
This commit is contained in:
commit
a66abf22a8
4 changed files with 74 additions and 0 deletions
|
@ -2,6 +2,12 @@ apply plugin: 'java'
|
||||||
apply plugin: 'eclipse'
|
apply plugin: 'eclipse'
|
||||||
apply plugin: 'jacoco'
|
apply plugin: 'jacoco'
|
||||||
|
|
||||||
|
tasks.withType(JavaCompile) {
|
||||||
|
// this subproject requires -parameters option
|
||||||
|
options.compilerArgs << '-parameters'
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
// Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code
|
// Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code
|
||||||
main
|
main
|
||||||
|
|
55
src/test/java/org/json/junit/JSONObjectLocaleTest.java
Executable file
55
src/test/java/org/json/junit/JSONObjectLocaleTest.java
Executable file
|
@ -0,0 +1,55 @@
|
||||||
|
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.
|
||||||
|
* In the Turkish alphabet, there are 2 versions of the letter "i".
|
||||||
|
* 'eh' I ı (dotless i)
|
||||||
|
* 'ee' İ i (dotted i)
|
||||||
|
* A problem can occur when parsing the public get methods for a bean.
|
||||||
|
* If the method starts with getI... then the key name will be lowercased
|
||||||
|
* to 'i' in English, and 'ı' in Turkish.
|
||||||
|
* We want the keys to be consistent regardless of locale, so JSON-Java
|
||||||
|
* lowercase operations are made to be locale-neutral by specifying
|
||||||
|
* Locale.ROOT. This causes 'I' to be universally lowercased to 'i'
|
||||||
|
* regardless of the locale currently in effect.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectByLocaleBean() {
|
||||||
|
|
||||||
|
MyLocaleBean myLocaleBean = new MyLocaleBean();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is just the control case which happens when the locale.ROOT
|
||||||
|
* lowercasing behavior is the same as the current locale.
|
||||||
|
*/
|
||||||
|
Locale.setDefault(new Locale("en"));
|
||||||
|
JSONObject jsonen = new JSONObject(myLocaleBean);
|
||||||
|
assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length());
|
||||||
|
assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i"));
|
||||||
|
assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id"));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Without the JSON-Java change, these keys would be stored internally as
|
||||||
|
* starting with the letter, 'ı' (dotless i), since the lowercasing of
|
||||||
|
* the getI and getId keys would be specific to the Turkish locale.
|
||||||
|
*/
|
||||||
|
Locale.setDefault(new Locale("tr"));
|
||||||
|
JSONObject jsontr = new JSONObject(myLocaleBean);
|
||||||
|
assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
|
||||||
|
assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
|
||||||
|
assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ import org.junit.runners.Suite;
|
||||||
HTTPTest.class,
|
HTTPTest.class,
|
||||||
JSONStringerTest.class,
|
JSONStringerTest.class,
|
||||||
JSONObjectTest.class,
|
JSONObjectTest.class,
|
||||||
|
JSONObjectLocaleTest.class,
|
||||||
JSONArrayTest.class,
|
JSONArrayTest.class,
|
||||||
EnumTest.class,
|
EnumTest.class,
|
||||||
JSONPointerTest.class,
|
JSONPointerTest.class,
|
||||||
|
|
12
src/test/java/org/json/junit/MyLocaleBean.java
Executable file
12
src/test/java/org/json/junit/MyLocaleBean.java
Executable file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue