Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed May 25, 2022
1 parent 56a91a0 commit fa5cf7e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 11 additions & 4 deletions impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.eclipse.parsson;

import java.util.Objects;

import jakarta.json.JsonMergePatch;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
Expand Down Expand Up @@ -121,11 +123,16 @@ static JsonValue diff(JsonValue source, JsonValue target) {
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null || obj.getClass() != JsonMergePatchImpl.class)
} else if (obj == null) {
return false;
} else if (getClass() != JsonMergePatchImpl.class) {
return false;
return patch.equals(((JsonMergePatchImpl)obj).patch);
} else {
JsonMergePatchImpl other = (JsonMergePatchImpl) obj;
return Objects.equals(patch, other.patch);
}
}

/**
Expand All @@ -135,7 +142,7 @@ public boolean equals(Object obj) {
*/
@Override
public int hashCode() {
return patch.hashCode();
return Objects.hashCode(patch);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.eclipse.parsson.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.eclipse.parsson.JsonMergePatchImpl;
import org.junit.Test;

import jakarta.json.Json;
Expand All @@ -35,4 +38,28 @@ public void testToString() {
JsonMergePatch jsonMergePatch = Json.createMergePatch(jsonArray);
assertEquals("[1]", jsonMergePatch.toString());
}

@Test
public void testEquals() {
JsonMergePatchImpl j1 = new JsonMergePatchImpl(Json.createValue("test"));
JsonMergePatchImpl j2 = new JsonMergePatchImpl(Json.createValue("test"));
JsonMergePatchImpl j3 = new JsonMergePatchImpl(j1.toJsonValue());
JsonMergePatchImpl j4 = new JsonMergePatchImpl(Json.createValue("test2"));
JsonMergePatchImpl j5 = new JsonMergePatchImpl(null);

assertTrue(j1.equals(j1));

assertTrue(j1.equals(j2));
assertTrue(j2.equals(j1));

assertTrue(j1.equals(j3));
assertTrue(j3.equals(j1));

assertTrue(j2.equals(j3));
assertTrue(j3.equals(j2));

assertFalse(j1.equals(j4));
assertFalse(j1.equals(j5));
assertFalse(j1.equals(null));
}
}

0 comments on commit fa5cf7e

Please sign in to comment.