Skip to content

Commit 928179a

Browse files
committed
locale tests
1 parent df9c27c commit 928179a

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ apply plugin: 'java'
22
apply plugin: 'eclipse'
33
apply plugin: 'jacoco'
44

5+
tasks.withType(JavaCompile) {
6+
// this subproject requires -parameters option
7+
options.compilerArgs << '-parameters'
8+
options.encoding = 'UTF-8'
9+
}
10+
511
sourceSets {
612
// Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code
713
main
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.*;
6+
7+
import org.json.*;
8+
import org.junit.*;
9+
10+
/**
11+
* Note: This file is saved as UTF-8. Do not save as ASCII or the tests will
12+
* fail.
13+
*
14+
*/
15+
public class JSONObjectLocaleTest {
16+
/**
17+
* JSONObject built from a bean with locale-specific keys - that is, the key
18+
* fields are not LANG_ENGLISH.
19+
*/
20+
@Test
21+
public void jsonObjectByLocaleBean() {
22+
23+
MyLocaleBean myLocaleBean = new MyLocaleBean();
24+
25+
Locale.setDefault(new Locale("en"));
26+
JSONObject jsonen = new JSONObject(myLocaleBean);
27+
System.out.println("jsonen " + jsonen);
28+
29+
Locale.setDefault(new Locale("tr"));
30+
JSONObject jsontr = new JSONObject(myLocaleBean);
31+
System.out.println("jsontr " + jsontr);
32+
/**
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.
37+
*/
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("/ü"))));
46+
}
47+
48+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
HTTPTest.class,
1414
JSONStringerTest.class,
1515
JSONObjectTest.class,
16+
JSONObjectLocaleTest.class,
1617
JSONArrayTest.class,
1718
EnumTest.class,
1819
JSONPointerTest.class,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.json.junit;
2+
3+
public class MyLocaleBean {
4+
private final String id = "beanId";
5+
private final String i = "beanI";
6+
public String getId() {
7+
return id;
8+
}
9+
public String getI() {
10+
return i;
11+
}
12+
}

0 commit comments

Comments
 (0)