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 d3315d2
Show file tree
Hide file tree
Showing 2 changed files with 63 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
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.eclipse.parsson.tests;

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;

public class JsonMergePatchImplTest {

@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 d3315d2

Please sign in to comment.