Skip to content

Commit 770cb9c

Browse files
authored
Merge pull request #84 from johnjaylward/FixBeanKeyNameing
New test cases for Bean Name customization
2 parents 37f1f4c + 193a382 commit 770cb9c

File tree

6 files changed

+165
-6
lines changed

6 files changed

+165
-6
lines changed

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

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.json.junit.data.GenericBean;
3838
import org.json.junit.data.GenericBeanInt;
3939
import org.json.junit.data.MyBean;
40+
import org.json.junit.data.MyBeanCustomName;
41+
import org.json.junit.data.MyBeanCustomNameSubClass;
4042
import org.json.junit.data.MyBigNumberBean;
4143
import org.json.junit.data.MyEnum;
4244
import org.json.junit.data.MyEnumField;
@@ -371,7 +373,7 @@ public void verifyPutCollection() {
371373

372374

373375
/**
374-
* Verifies that the put Map has backwards compatability with RAW types pre-java5.
376+
* Verifies that the put Map has backwards compatibility with RAW types pre-java5.
375377
*/
376378
@Test
377379
public void verifyPutMap() {
@@ -467,7 +469,7 @@ public void jsonObjectByMapWithNullValue() {
467469
*/
468470
@SuppressWarnings("boxing")
469471
@Test
470-
public void jsonObjectByBean() {
472+
public void jsonObjectByBean1() {
471473
/**
472474
* Default access classes have to be mocked since JSONObject, which is
473475
* not in the same package, cannot call MyBean methods by reflection.
@@ -501,6 +503,73 @@ public void jsonObjectByBean() {
501503
assertTrue("expected 0 callbacks[1] items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0);
502504
}
503505

506+
/**
507+
* JSONObject built from a bean that has custom field names.
508+
*/
509+
@Test
510+
public void jsonObjectByBean2() {
511+
JSONObject jsonObject = new JSONObject(new MyBeanCustomName());
512+
assertNotNull(jsonObject);
513+
assertEquals("Wrong number of keys found:",
514+
5,
515+
jsonObject.keySet().size());
516+
assertFalse("Normal field name (someString) processing did not work",
517+
jsonObject.has("someString"));
518+
assertFalse("Normal field name (myDouble) processing did not work",
519+
jsonObject.has("myDouble"));
520+
assertFalse("Normal field name (someFloat) found",
521+
jsonObject.has("someFloat"));
522+
assertFalse("Ignored field found!",
523+
jsonObject.has("ignoredInt"));
524+
assertTrue("Normal field name (someInt) processing did not work",
525+
jsonObject.has("someInt"));
526+
assertTrue("Normal field name (someLong) processing did not work",
527+
jsonObject.has("someLong"));
528+
assertTrue("Overridden String field name (myStringField) not found",
529+
jsonObject.has("myStringField"));
530+
assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found",
531+
jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
532+
assertTrue("Overridden String field name (InterfaceField) not found",
533+
jsonObject.has("InterfaceField"));
534+
}
535+
536+
/**
537+
* JSONObject built from a bean that has custom field names inherited from a parent class.
538+
*/
539+
@Test
540+
public void jsonObjectByBean3() {
541+
JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass());
542+
assertNotNull(jsonObject);
543+
assertEquals("Wrong number of keys found:",
544+
7,
545+
jsonObject.keySet().size());
546+
assertFalse("Normal int field name (someInt) found, but was overridden",
547+
jsonObject.has("someInt"));
548+
assertFalse("Normal field name (myDouble) processing did not work",
549+
jsonObject.has("myDouble"));
550+
assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!",
551+
jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
552+
assertFalse("Normal field name (someFloat) found",
553+
jsonObject.has("someFloat"));
554+
assertFalse("Ignored field found!",
555+
jsonObject.has("ignoredInt"));
556+
assertFalse("Ignored field at the same level as forced name found",
557+
jsonObject.has("ShouldBeIgnored"));
558+
assertTrue("Overridden int field name (newIntFieldName) not found",
559+
jsonObject.has("newIntFieldName"));
560+
assertTrue("Normal field name (someLong) processing did not work",
561+
jsonObject.has("someLong"));
562+
assertTrue("Overridden String field name (myStringField) not found",
563+
jsonObject.has("myStringField"));
564+
assertTrue(jsonObject.has("AMoreNormalName"));
565+
assertTrue("Overridden String field name (InterfaceField) not found",
566+
jsonObject.has("InterfaceField"));
567+
assertTrue("Forced field not found!",
568+
jsonObject.has("forcedInt"));
569+
assertTrue("Normally ignored field (getable) with explicit property name not found",
570+
jsonObject.has("Getable"));
571+
}
572+
504573
/**
505574
* A bean is also an object. But in order to test the JSONObject
506575
* ctor that takes an object and a list of names,
@@ -541,7 +610,7 @@ public void jsonObjectByResourceBundle() {
541610
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
542611
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
543612
}
544-
613+
545614
/**
546615
* Exercise the JSONObject.accumulate() method
547616
*/
@@ -2855,7 +2924,7 @@ public void testGenericBean() {
28552924
public void testGenericIntBean() {
28562925
GenericBeanInt bean = new GenericBeanInt(42);
28572926
final JSONObject jo = new JSONObject(bean);
2858-
assertEquals(jo.keySet().toString(), 9, jo.length());
2927+
assertEquals(jo.keySet().toString(), 10, jo.length());
28592928
assertEquals(42, jo.get("genericValue"));
28602929
assertEquals("Expected the getter to only be called once",
28612930
1, bean.genericGetCounter);

src/test/java/org/json/junit/data/GenericBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public GenericBean(T genericValue) {
2020
}
2121

2222
/** */
23-
private T genericValue;
23+
protected T genericValue;
2424
/** to be used by the calling test to see how often the getter is called */
2525
public int genericGetCounter;
2626
/** to be used by the calling test to see how often the setter is called */

src/test/java/org/json/junit/data/GenericBeanInt.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GenericBeanInt extends GenericBean<Integer> {
1313

1414
/** @return the a */
1515
public char getA() {
16-
return a;
16+
return this.a;
1717
}
1818

1919
/**
@@ -25,6 +25,33 @@ public boolean getable() {
2525
return false;
2626
}
2727

28+
/**
29+
* Should not be beanable
30+
*
31+
* @return false
32+
*/
33+
public boolean get() {
34+
return false;
35+
}
36+
37+
/**
38+
* Should not be beanable
39+
*
40+
* @return false
41+
*/
42+
public boolean is() {
43+
return false;
44+
}
45+
46+
/**
47+
* Should be beanable
48+
*
49+
* @return false
50+
*/
51+
public boolean isB() {
52+
return this.genericValue.equals((Integer.valueOf(this.a+1)));
53+
}
54+
2855
/**
2956
* @param genericValue
3057
* the value to initiate with.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.json.junit.data;
2+
3+
import org.json.JSONPropertyName;
4+
5+
/**
6+
* Test bean for the {@link JSONPropertyName} annotation.
7+
*/
8+
public class MyBeanCustomName implements MyBeanCustomNameInterface {
9+
public int getSomeInt() { return 42; }
10+
@JSONPropertyName("")
11+
public long getSomeLong() { return 42L; }
12+
@JSONPropertyName("myStringField")
13+
public String getSomeString() { return "someStringValue"; }
14+
@JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!")
15+
public double getMyDouble() { return 0.0d; }
16+
@Override
17+
public float getSomeFloat() { return 2.0f; }
18+
@Override
19+
public int getIgnoredInt() { return 40; }
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.json.junit.data;
2+
3+
import org.json.JSONPropertyIgnore;
4+
import org.json.JSONPropertyName;
5+
6+
public interface MyBeanCustomNameInterface {
7+
@JSONPropertyName("InterfaceField")
8+
float getSomeFloat();
9+
@JSONPropertyIgnore
10+
int getIgnoredInt();
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
*
3+
*/
4+
package org.json.junit.data;
5+
6+
import org.json.JSONPropertyIgnore;
7+
import org.json.JSONPropertyName;
8+
9+
/**
10+
* Test bean to verify that the {@link org.json.JSONPropertyName} annotation
11+
* is inherited.
12+
*/
13+
public class MyBeanCustomNameSubClass extends MyBeanCustomName {
14+
@Override
15+
@JSONPropertyName("forcedInt")
16+
public int getIgnoredInt() { return 42*42; }
17+
@Override
18+
@JSONPropertyName("newIntFieldName")
19+
public int getSomeInt() { return 43; }
20+
@Override
21+
public String getSomeString() { return "subClassString"; }
22+
@Override
23+
@JSONPropertyName("AMoreNormalName")
24+
public double getMyDouble() { return 1.0d; }
25+
@Override
26+
public float getSomeFloat() { return 3.0f; }
27+
@JSONPropertyIgnore
28+
@JSONPropertyName("ShouldBeIgnored")
29+
public boolean getShouldNotBeJSON() { return true; }
30+
@JSONPropertyName("Getable")
31+
public boolean getable() { return true; }
32+
}

0 commit comments

Comments
 (0)