Skip to content

Commit f41e1d0

Browse files
committed
tests for locale-independent keys
1 parent 928179a commit f41e1d0

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/test/java/org/json/junit/JSONObjectLocaleTest.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,42 @@
1414
*/
1515
public class JSONObjectLocaleTest {
1616
/**
17-
* JSONObject built from a bean with locale-specific keys - that is, the key
18-
* fields are not LANG_ENGLISH.
17+
* JSONObject built from a bean with locale-specific keys.
18+
* In the Turkish alphabet, there are 2 versions of the letter "i".
19+
* 'eh' I ı (dotless i)
20+
* 'ee' İ i (dotted i)
21+
* A problem can occur when parsing the public get methods for a bean.
22+
* If the method starts with getI... then the key name will be lowercased
23+
* to 'i' in English, and 'ı' in Turkish.
24+
* We want the keys to be consistent regardless of locale, so JSON-Java
25+
* lowercase operations are made to be locale-neutral by specifying
26+
* Locale.ROOT. This causes 'I' to be universally lowercased to 'i'
27+
* regardless of the locale currently in effect.
1928
*/
2029
@Test
2130
public void jsonObjectByLocaleBean() {
2231

2332
MyLocaleBean myLocaleBean = new MyLocaleBean();
2433

34+
/**
35+
* This is just the control case which happens when the locale.ROOT
36+
* lowercasing behavior is the same as the current locale.
37+
*/
2538
Locale.setDefault(new Locale("en"));
2639
JSONObject jsonen = new JSONObject(myLocaleBean);
27-
System.out.println("jsonen " + jsonen);
40+
assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length());
41+
assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i"));
42+
assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id"));
2843

29-
Locale.setDefault(new Locale("tr"));
30-
JSONObject jsontr = new JSONObject(myLocaleBean);
31-
System.out.println("jsontr " + jsontr);
3244
/**
33-
* In this test we exercise code that handles keys of 1-char and
34-
* multi-char length that include text from a non-English locale.
35-
* Turkish in this case. The JSONObject code should correctly retain the
36-
* non-EN_LANG chars in the key.
45+
* Without the JSON-Java change, these keys would be stored internally as
46+
* starting with the letter, 'ı' (dotless i), since the lowercasing of
47+
* the getI and getId keys would be specific to the Turkish locale.
3748
*/
38-
assertTrue("expected beanId",
39-
"Tlocaleüx".equals(jsonObject.getString("")));
40-
assertTrue("expected Tlocalü",
41-
"Tlocaleü".equals(jsonObject.getString("ü")));
42-
assertTrue("expected Tlocaleüx",
43-
"Tlocaleüx".equals((String)(jsonObject.query("/üx"))));
44-
assertTrue("expected Tlocalü",
45-
"Tlocaleü".equals((String)(jsonObject.query("/ü"))));
49+
Locale.setDefault(new Locale("tr"));
50+
JSONObject jsontr = new JSONObject(myLocaleBean);
51+
assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
52+
assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
53+
assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
4654
}
47-
4855
}

0 commit comments

Comments
 (0)