|
14 | 14 | */ |
15 | 15 | public class JSONObjectLocaleTest { |
16 | 16 | /** |
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. |
19 | 28 | */ |
20 | 29 | @Test |
21 | 30 | public void jsonObjectByLocaleBean() { |
22 | 31 |
|
23 | 32 | MyLocaleBean myLocaleBean = new MyLocaleBean(); |
24 | 33 |
|
| 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 | + */ |
25 | 38 | Locale.setDefault(new Locale("en")); |
26 | 39 | 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")); |
28 | 43 |
|
29 | | - Locale.setDefault(new Locale("tr")); |
30 | | - JSONObject jsontr = new JSONObject(myLocaleBean); |
31 | | - System.out.println("jsontr " + jsontr); |
32 | 44 | /** |
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. |
37 | 48 | */ |
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")); |
46 | 54 | } |
47 | | - |
48 | 55 | } |
0 commit comments