mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
tests for locale-independent keys
This commit is contained in:
parent
928179a1f3
commit
f41e1d012a
1 changed files with 26 additions and 19 deletions
|
@ -14,35 +14,42 @@ import org.junit.*;
|
||||||
*/
|
*/
|
||||||
public class JSONObjectLocaleTest {
|
public class JSONObjectLocaleTest {
|
||||||
/**
|
/**
|
||||||
* JSONObject built from a bean with locale-specific keys - that is, the key
|
* JSONObject built from a bean with locale-specific keys.
|
||||||
* fields are not LANG_ENGLISH.
|
* 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
|
@Test
|
||||||
public void jsonObjectByLocaleBean() {
|
public void jsonObjectByLocaleBean() {
|
||||||
|
|
||||||
MyLocaleBean myLocaleBean = new MyLocaleBean();
|
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"));
|
Locale.setDefault(new Locale("en"));
|
||||||
JSONObject jsonen = new JSONObject(myLocaleBean);
|
JSONObject jsonen = new JSONObject(myLocaleBean);
|
||||||
System.out.println("jsonen " + jsonen);
|
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"));
|
Locale.setDefault(new Locale("tr"));
|
||||||
JSONObject jsontr = new JSONObject(myLocaleBean);
|
JSONObject jsontr = new JSONObject(myLocaleBean);
|
||||||
System.out.println("jsontr " + jsontr);
|
assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
|
||||||
/**
|
assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
|
||||||
* In this test we exercise code that handles keys of 1-char and
|
assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
|
||||||
* 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("/ü"))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue