Skip to content

Commit

Permalink
address code review feedback:
Browse files Browse the repository at this point in the history
- fix copyright in unit test
- add hashCode method for Form class
- swap asMap to parameters in Form equality check
- fix Boolean warning in Form unit test
  • Loading branch information
david-walters-14 committed Oct 3, 2024
1 parent b756b76 commit 66cacdf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
12 changes: 11 additions & 1 deletion jaxrs-api/src/main/java/jakarta/ws/rs/core/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public boolean equals(Object object) {
if (object == this) return true;
if (getClass() != object.getClass()) return false;
Form form = (Form) object;
return parameters.equals(form.asMap());
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();
}
}
31 changes: 24 additions & 7 deletions jaxrs-api/src/test/java/jakarta/ws/rs/core/FormTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
* 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
Expand All @@ -22,6 +22,18 @@
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() {
Expand All @@ -38,24 +50,29 @@ public void testSameObjectEquals() {
@Test
public void testDifferentObjectTypeEquals() {
Form form = new Form("field", "value");
Boolean booleanValue = new Boolean(false);
Boolean booleanValue = Boolean.TRUE;
assertFalse(form.equals(booleanValue));
}

@Test
public void testFormContentEquals() {
final String NAME_FIELD = "name";
final String AGE_FIELD = "age";
final String NAME_VALUE = "john";
final String AGE_VALUE = "25";
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", "USA");
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());
}
}

0 comments on commit 66cacdf

Please sign in to comment.