- implemented DateParam.equals() and DateParam.hashCode() which comes in handy when writing tests ...

This commit is contained in:
Gaetano Gallo 2018-02-27 10:15:19 +01:00
parent e241b31905
commit 1caf768fd5
3 changed files with 69 additions and 26 deletions

View File

@ -107,5 +107,4 @@ public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam
myPrefix = thePrefix;
return (T) this;
}
}

View File

@ -20,21 +20,26 @@ package ca.uhn.fhir.rest.param;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.BaseDateTimeDt;
import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.api.QualifiedParamList;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.ValidateUtil;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.*;
import ca.uhn.fhir.rest.api.QualifiedParamList;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.ValidateUtil;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQueryParameterType , */IQueryParameterOr<DateParam> {
@ -222,6 +227,20 @@ public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQuer
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DateParam)) {
return false;
}
DateParam other = (DateParam) obj;
return Objects.equals(getValue(), other.getValue()) &&
Objects.equals(getPrefix(), other.getPrefix());
}
@Override
public int hashCode() {
return Objects.hash(getValue(), getPrefix());
}
@Override
public String toString() {
@ -241,8 +260,5 @@ public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQuer
protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
return true;
}
}
}

View File

@ -1,17 +1,24 @@
package ca.uhn.fhir.rest.param;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Date;
import java.util.TimeZone;
import org.junit.Test;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import org.junit.Test;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.APPROXIMATE;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.EQUAL;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.NOT_EQUAL;
import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class DateParamTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateParamTest.class);
@ -106,5 +113,26 @@ public class DateParamTest {
assertEquals(ParamPrefixEnum.LESSTHAN_OR_EQUALS, new DateParam("le2012").getPrefix());
}
@Test()
public void testEqualsAndHashCode() {
List<DateParam> uniqueSamples = newArrayList(
new DateParam(),
new DateParam(NOT_EQUAL, new Date()),
new DateParam(EQUAL, new Date().getTime()),
new DateParam(APPROXIMATE, "2017-10-17T11:11:11.111-11:11"));
DateParam previous = null;
for (DateParam current : uniqueSamples) {
ourLog.info("Equals test samples: [previous=" + previous + ", current=" + current + "]");
assertThat(current, is(equalTo(copyOf(current))));
assertThat(current.hashCode(), is(equalTo(copyOf(current).hashCode())));
assertThat(current, is(not(equalTo(previous))));
assertThat(current.hashCode(), is(not(equalTo(previous != null ? previous.hashCode() : null))));
previous = current;
}
}
private DateParam copyOf(DateParam param) {
return new DateParam(param.getPrefix(), param.getValue());
}
}