|
| 1 | +package org.json.junit; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.StringReader; |
| 8 | + |
| 9 | +import org.json.JSONObject; |
| 10 | +import org.json.junit.data.GenericBeanInt; |
| 11 | +import org.json.junit.data.MyEnum; |
| 12 | +import org.json.junit.data.MyNumber; |
| 13 | +import org.json.junit.data.PersonRecord; |
| 14 | +import org.junit.Ignore; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for JSONObject support of Java record types. |
| 19 | + * |
| 20 | + * NOTE: These tests are currently ignored because PersonRecord is not an actual Java record. |
| 21 | + * The implementation now correctly detects actual Java records using reflection (Class.isRecord()). |
| 22 | + * These tests will need to be enabled and run with Java 17+ where PersonRecord can be converted |
| 23 | + * to an actual record type. |
| 24 | + * |
| 25 | + * This ensures backward compatibility - regular classes with lowercase method names will not |
| 26 | + * be treated as records unless they are actual Java record types. |
| 27 | + */ |
| 28 | +public class JSONObjectRecordTest { |
| 29 | + |
| 30 | + /** |
| 31 | + * Tests that JSONObject can be created from a record-style class. |
| 32 | + * Record-style classes use accessor methods like name() instead of getName(). |
| 33 | + * |
| 34 | + * NOTE: Ignored until PersonRecord is converted to an actual Java record (requires Java 17+) |
| 35 | + */ |
| 36 | + @Test |
| 37 | + @Ignore("Requires actual Java record type - PersonRecord needs to be a real record (Java 17+)") |
| 38 | + public void jsonObjectByRecord() { |
| 39 | + PersonRecord person = new PersonRecord("John Doe", 30, true); |
| 40 | + JSONObject jsonObject = new JSONObject(person); |
| 41 | + |
| 42 | + assertEquals("Expected 3 keys in the JSONObject", 3, jsonObject.length()); |
| 43 | + assertEquals("John Doe", jsonObject.get("name")); |
| 44 | + assertEquals(30, jsonObject.get("age")); |
| 45 | + assertEquals(true, jsonObject.get("active")); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test that Object methods (toString, hashCode, equals, etc.) are not included |
| 50 | + * |
| 51 | + * NOTE: Ignored until PersonRecord is converted to an actual Java record (requires Java 17+) |
| 52 | + */ |
| 53 | + @Test |
| 54 | + @Ignore("Requires actual Java record type - PersonRecord needs to be a real record (Java 17+)") |
| 55 | + public void recordStyleClassShouldNotIncludeObjectMethods() { |
| 56 | + PersonRecord person = new PersonRecord("Jane Doe", 25, false); |
| 57 | + JSONObject jsonObject = new JSONObject(person); |
| 58 | + |
| 59 | + // Should NOT include Object methods |
| 60 | + assertFalse("Should not include toString", jsonObject.has("toString")); |
| 61 | + assertFalse("Should not include hashCode", jsonObject.has("hashCode")); |
| 62 | + assertFalse("Should not include equals", jsonObject.has("equals")); |
| 63 | + assertFalse("Should not include clone", jsonObject.has("clone")); |
| 64 | + assertFalse("Should not include wait", jsonObject.has("wait")); |
| 65 | + assertFalse("Should not include notify", jsonObject.has("notify")); |
| 66 | + assertFalse("Should not include notifyAll", jsonObject.has("notifyAll")); |
| 67 | + |
| 68 | + // Should only have the 3 record fields |
| 69 | + assertEquals("Should only have 3 fields", 3, jsonObject.length()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test that enum methods are not included when processing an enum |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void enumsShouldNotIncludeEnumMethods() { |
| 77 | + MyEnum myEnum = MyEnum.VAL1; |
| 78 | + JSONObject jsonObject = new JSONObject(myEnum); |
| 79 | + |
| 80 | + // Should NOT include enum-specific methods like name(), ordinal(), values(), valueOf() |
| 81 | + assertFalse("Should not include name method", jsonObject.has("name")); |
| 82 | + assertFalse("Should not include ordinal method", jsonObject.has("ordinal")); |
| 83 | + assertFalse("Should not include declaringClass", jsonObject.has("declaringClass")); |
| 84 | + |
| 85 | + // Enums should still work with traditional getters if they have any |
| 86 | + // But should not pick up the built-in enum methods |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test that Number subclass methods are not included |
| 91 | + */ |
| 92 | + @Test |
| 93 | + public void numberSubclassesShouldNotIncludeNumberMethods() { |
| 94 | + MyNumber myNumber = new MyNumber(); |
| 95 | + JSONObject jsonObject = new JSONObject(myNumber); |
| 96 | + |
| 97 | + // Should NOT include Number methods like intValue(), longValue(), etc. |
| 98 | + assertFalse("Should not include intValue", jsonObject.has("intValue")); |
| 99 | + assertFalse("Should not include longValue", jsonObject.has("longValue")); |
| 100 | + assertFalse("Should not include doubleValue", jsonObject.has("doubleValue")); |
| 101 | + assertFalse("Should not include floatValue", jsonObject.has("floatValue")); |
| 102 | + |
| 103 | + // Should include the actual getter |
| 104 | + assertTrue("Should include number", jsonObject.has("number")); |
| 105 | + assertEquals("Should have 1 field", 1, jsonObject.length()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test that generic bean with get() and is() methods works correctly |
| 110 | + */ |
| 111 | + @Test |
| 112 | + public void genericBeanWithGetAndIsMethodsShouldNotBeIncluded() { |
| 113 | + GenericBeanInt bean = new GenericBeanInt(42); |
| 114 | + JSONObject jsonObject = new JSONObject(bean); |
| 115 | + |
| 116 | + // Should NOT include standalone get() or is() methods |
| 117 | + assertFalse("Should not include standalone 'get' method", jsonObject.has("get")); |
| 118 | + assertFalse("Should not include standalone 'is' method", jsonObject.has("is")); |
| 119 | + |
| 120 | + // Should include the actual getters |
| 121 | + assertTrue("Should include genericValue field", jsonObject.has("genericValue")); |
| 122 | + assertTrue("Should include a field", jsonObject.has("a")); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test that java.* classes don't have their methods picked up |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void javaLibraryClassesShouldNotIncludeTheirMethods() { |
| 130 | + StringReader reader = new StringReader("test"); |
| 131 | + JSONObject jsonObject = new JSONObject(reader); |
| 132 | + |
| 133 | + // Should NOT include java.io.Reader methods like read(), reset(), etc. |
| 134 | + assertFalse("Should not include read method", jsonObject.has("read")); |
| 135 | + assertFalse("Should not include reset method", jsonObject.has("reset")); |
| 136 | + assertFalse("Should not include ready method", jsonObject.has("ready")); |
| 137 | + assertFalse("Should not include skip method", jsonObject.has("skip")); |
| 138 | + |
| 139 | + // Reader should produce empty JSONObject (no valid properties) |
| 140 | + assertEquals("Reader should produce empty JSON", 0, jsonObject.length()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test mixed case - object with both traditional getters and record-style accessors |
| 145 | + * |
| 146 | + * NOTE: Ignored until PersonRecord is converted to an actual Java record (requires Java 17+) |
| 147 | + */ |
| 148 | + @Test |
| 149 | + @Ignore("Requires actual Java record type - PersonRecord needs to be a real record (Java 17+)") |
| 150 | + public void mixedGettersAndRecordStyleAccessors() { |
| 151 | + // PersonRecord has record-style accessors: name(), age(), active() |
| 152 | + // These should all be included |
| 153 | + PersonRecord person = new PersonRecord("Mixed Test", 40, true); |
| 154 | + JSONObject jsonObject = new JSONObject(person); |
| 155 | + |
| 156 | + assertEquals("Should have all 3 record-style fields", 3, jsonObject.length()); |
| 157 | + assertTrue("Should include name", jsonObject.has("name")); |
| 158 | + assertTrue("Should include age", jsonObject.has("age")); |
| 159 | + assertTrue("Should include active", jsonObject.has("active")); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test that methods starting with uppercase are not included (not valid record accessors) |
| 164 | + * |
| 165 | + * NOTE: Ignored until PersonRecord is converted to an actual Java record (requires Java 17+) |
| 166 | + */ |
| 167 | + @Test |
| 168 | + @Ignore("Requires actual Java record type - PersonRecord needs to be a real record (Java 17+)") |
| 169 | + public void methodsStartingWithUppercaseShouldNotBeIncluded() { |
| 170 | + PersonRecord person = new PersonRecord("Test", 50, false); |
| 171 | + JSONObject jsonObject = new JSONObject(person); |
| 172 | + |
| 173 | + // Record-style accessors must start with lowercase |
| 174 | + // Methods like Name(), Age() (uppercase) should not be picked up |
| 175 | + // Our PersonRecord only has lowercase accessors, which is correct |
| 176 | + |
| 177 | + assertEquals("Should only have lowercase accessors", 3, jsonObject.length()); |
| 178 | + } |
| 179 | +} |
0 commit comments