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

closes #1266, implement Form equals method and add unit tests #1288

Merged
merged 4 commits into from
Oct 18, 2024
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
27 changes: 26 additions & 1 deletion jaxrs-api/src/main/java/jakarta/ws/rs/core/Form.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011 Oracle and/or its affiliates and others. 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 Down Expand Up @@ -93,4 +93,29 @@ public Form param(final String name, final String value) {
public MultivaluedMap<String, String> asMap() {
return parameters;
}

mkarg marked this conversation as resolved.
Show resolved Hide resolved
/**
* Compare for equality
*
* @param object the object to compare to.
* @return {@code true}, if the object is a {@code Form} with the same form parameters, {@code false} otherwise.
*/
@Override
public boolean equals(Object object) {
mkarg marked this conversation as resolved.
Show resolved Hide resolved
if (object == null) return false;
if (object == this) return true;
if (getClass() != object.getClass()) return false;
Form form = (Form) object;
return parameters.equals(form.parameters);
}

/**
* Returns the hashCode of the underlying form parameters.
*
* @return a hash code value for the underlying form parameters.
*/
@Override
public int hashCode() {
return parameters.hashCode();
mkarg marked this conversation as resolved.
Show resolved Hide resolved
}
}
78 changes: 78 additions & 0 deletions jaxrs-api/src/test/java/jakarta/ws/rs/core/FormTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 David Walters. 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 jakarta.ws.rs.core;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FormTest {

private static final String AGE_FIELD = "age";

private static final String AGE_VALUE = "25";

private static final String LOCATION_FIELD = "location";

private static final String LOCATION_VALUE = "USA";

private static final String NAME_FIELD = "name";

private static final String NAME_VALUE = "john";

@Test
public void testNullEquals() {
Form form = new Form();
assertFalse(form.equals(null));
}

@Test
public void testSameObjectEquals() {
Form form = new Form();
assertTrue(form.equals(form));
}

@Test
public void testDifferentObjectTypeEquals() {
Form form = new Form("field", "value");
Boolean booleanValue = Boolean.TRUE;
assertFalse(form.equals(booleanValue));
}

@Test
public void testFormContentEquals() {
Form form1 = new Form(NAME_FIELD,NAME_VALUE).param(AGE_FIELD, AGE_VALUE);
Form form2 = new Form(NAME_FIELD,NAME_VALUE).param(AGE_FIELD, AGE_VALUE);
assertTrue(form1.equals(form2));
form2 = new Form(AGE_FIELD, AGE_VALUE).param(NAME_FIELD,NAME_VALUE);
assertTrue(form1.equals(form2));
form2 = new Form(AGE_VALUE, AGE_FIELD).param(NAME_VALUE, NAME_FIELD);
assertFalse(form1.equals(form2));
form2 = new Form(AGE_FIELD, AGE_VALUE).param(NAME_FIELD,NAME_VALUE).param(LOCATION_FIELD, LOCATION_VALUE);
assertFalse(form1.equals(form2));
}

@Test
public void testHashCode() {
Form form1 = new Form(NAME_FIELD,NAME_VALUE).param(AGE_FIELD, AGE_VALUE);
Form form2 = new Form(NAME_FIELD,NAME_VALUE).param(AGE_FIELD, AGE_VALUE);
assertTrue(form1.hashCode() == form2.hashCode());
form2.param(LOCATION_FIELD, LOCATION_VALUE);
assertFalse(form1.hashCode() == form2.hashCode());
}
}