Skip to content

Commit 77d142d

Browse files
authored
Merge pull request #81 from johnjaylward/FixFalsePositiveSimilar
Test cases to verify Similar methods
2 parents bf26eba + dae88d7 commit 77d142d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.json.junit;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertNull;
56
import static org.junit.Assert.assertTrue;
67

@@ -54,6 +55,32 @@ public class JSONArrayTest {
5455
"\"-1\""+
5556
"]";
5657

58+
/**
59+
* Tests that the similar method is working as expected.
60+
*/
61+
@Test
62+
public void verifySimilar() {
63+
final String string1 = "HasSameRef";
64+
JSONArray obj1 = new JSONArray()
65+
.put("abc")
66+
.put(string1)
67+
.put(2);
68+
69+
JSONArray obj2 = new JSONArray()
70+
.put("abc")
71+
.put(string1)
72+
.put(3);
73+
74+
JSONArray obj3 = new JSONArray()
75+
.put("abc")
76+
.put(new String(string1))
77+
.put(2);
78+
79+
assertFalse("Should eval to false", obj1.similar(obj2));
80+
81+
assertTrue("Should eval to true", obj1.similar(obj3));
82+
}
83+
5784
/**
5885
* Attempt to create a JSONArray with a null string.
5986
* Expects a NullPointerException.

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@
5858
* otherwise be impossible.
5959
*/
6060
public class JSONObjectTest {
61+
62+
/**
63+
* Tests that the similar method is working as expected.
64+
*/
65+
@Test
66+
public void verifySimilar() {
67+
final String string1 = "HasSameRef";
68+
JSONObject obj1 = new JSONObject()
69+
.put("key1", "abc")
70+
.put("key2", 2)
71+
.put("key3", string1);
72+
73+
JSONObject obj2 = new JSONObject()
74+
.put("key1", "abc")
75+
.put("key2", 3)
76+
.put("key3", string1);
77+
78+
JSONObject obj3 = new JSONObject()
79+
.put("key1", "abc")
80+
.put("key2", 2)
81+
.put("key3", new String(string1));
82+
83+
assertFalse("Should eval to false", obj1.similar(obj2));
84+
85+
assertTrue("Should eval to true", obj1.similar(obj3));
86+
87+
}
6188

6289
/**
6390
* JSONObject built from a bean, but only using a null value.

0 commit comments

Comments
 (0)