Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toString, equals, hashCode in JsonMergePatch #56

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 @@ -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();
}
}

Original file line number Diff line number Diff line change
@@ -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));
}
}