diff --git a/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java b/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java index 5a9904da..cc101014 100644 --- a/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java +++ b/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 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 @@ -16,6 +16,8 @@ package org.eclipse.parsson; +import java.util.Objects; + import jakarta.json.JsonMergePatch; import jakarta.json.JsonObject; import jakarta.json.JsonObjectBuilder; @@ -113,5 +115,43 @@ static JsonValue diff(JsonValue source, JsonValue target) { return builder.build(); } + /** + * Compares this {@code JsonMergePatchImpl} with another object. + * @param obj the object to compare this {@code JsonMergePatchImpl} against + * @return true if the given object is a {@code JsonMergePatchImpl} with the same + * reference tokens as this one, false otherwise. + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj == null) { + return false; + } else if (getClass() != JsonMergePatchImpl.class) { + return false; + } else { + JsonMergePatchImpl other = (JsonMergePatchImpl) obj; + return Objects.equals(patch, other.patch); + } + } + + /** + * Returns the hash code value for this {@code JsonMergePatchImpl}. + * + * @return the hash code value for this {@code JsonMergePatchImpl} object + */ + @Override + public int hashCode() { + return Objects.hashCode(patch); + } + + /** + * Returns the JSON Patch text + * @return the JSON Patch text + */ + @Override + public String toString() { + return patch.toString(); + } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java new file mode 100644 index 00000000..2379d093 --- /dev/null +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java @@ -0,0 +1,65 @@ +/* + * 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.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; +import jakarta.json.JsonArray; +import jakarta.json.JsonMergePatch; +import jakarta.json.JsonPatch; + +public class JsonMergePatch2Test { + + @Test + public void testToString() { + JsonArray jsonArray = Json.createArrayBuilder().add(Json.createValue(1)).build(); + JsonPatch jsonPatch = Json.createPatchBuilder(jsonArray).build(); + assertEquals("[1]", jsonPatch.toString()); + 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)); + } +}