- added guava-testlib as test dependency to modules hapi-fhir-structures-dstu2 and hapi-fhir-structures-dstu3

- added DateRangeParam.equals(Object) and DateRangeParam.hashCode()
- fixed broken DateParamTest
This commit is contained in:
Gaetano Gallo 2018-02-27 13:28:24 +01:00
parent 1caf768fd5
commit 3009ec7f74
7 changed files with 88 additions and 30 deletions

View File

@ -229,6 +229,9 @@ public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQuer
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DateParam)) {
return false;
}

View File

@ -413,6 +413,24 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DateRangeParam)) {
return false;
}
DateRangeParam other = (DateRangeParam) obj;
return Objects.equals(myLowerBound, other.myLowerBound) &&
Objects.equals(myUpperBound, other.myUpperBound);
}
@Override
public int hashCode() {
return Objects.hash(myLowerBound, myUpperBound);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
@ -484,7 +502,5 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + myUpperBound.getPrefix().getValue());
}
}
}
}

View File

@ -172,7 +172,11 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>

View File

@ -2,21 +2,19 @@ package ca.uhn.fhir.rest.param;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
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 java.util.concurrent.TimeUnit.SECONDS;
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;
@ -115,24 +113,18 @@ public class DateParamTest {
@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());
Date now = new Date();
Date later = new Date(now.getTime() + SECONDS.toMillis(666));
new EqualsTester()
.addEqualityGroup(new DateParam(),
new DateParam(null))
.addEqualityGroup(new DateParam(NOT_EQUAL, now),
new DateParam(NOT_EQUAL, now.getTime()))
.addEqualityGroup(new DateParam(EQUAL, now),
new DateParam(EQUAL, now.getTime()))
.addEqualityGroup(new DateParam(EQUAL, later),
new DateParam(EQUAL, later.getTime()))
.addEqualityGroup(new DateParam(APPROXIMATE, "2011-11-11T11:11:11.111-11:11"))
.testEquals();
}
}

View File

@ -261,7 +261,11 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>

View File

@ -1,12 +1,28 @@
package ca.uhn.fhir.rest.param;
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.GREATERTHAN;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.GREATERTHAN_OR_EQUALS;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.LESSTHAN;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.LESSTHAN_OR_EQUALS;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.NOT_EQUAL;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.SECONDS;
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.assertNotNull;
import static org.junit.Assert.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.google.common.testing.EqualsTester;
import org.junit.AfterClass;
import org.junit.Test;
@ -183,7 +199,26 @@ public class DateRangeParamTest {
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Test()
public void testEqualsAndHashCode() {
Date lowerBound = new Date(currentTimeMillis());
Date upperBound = new Date(lowerBound.getTime() + SECONDS.toMillis(1));
new EqualsTester()
.addEqualityGroup(new DateRangeParam(),
new DateRangeParam((Date) null, (Date) null))
.addEqualityGroup(new DateRangeParam(lowerBound, upperBound),
new DateRangeParam(new DateParam(GREATERTHAN_OR_EQUALS, lowerBound), new DateParam(LESSTHAN_OR_EQUALS, upperBound)))
.addEqualityGroup(new DateRangeParam(new DateParam(EQUAL, lowerBound)),
new DateRangeParam(new DateParam(null, lowerBound)),
new DateRangeParam(new DateParam(EQUAL, lowerBound), new DateParam(EQUAL, lowerBound)))
.addEqualityGroup(new DateRangeParam(lowerBound, null),
new DateRangeParam(new DateParam(GREATERTHAN_OR_EQUALS, lowerBound), null))
.addEqualityGroup(new DateRangeParam(null, upperBound),
new DateRangeParam(null, new DateParam(LESSTHAN_OR_EQUALS, upperBound)))
.testEquals();
}
private static DateRangeParam create(String theLower, String theUpper) throws InvalidRequestException {
DateRangeParam p = new DateRangeParam();
List<QualifiedParamList> tokens = new ArrayList<QualifiedParamList>();
@ -202,5 +237,4 @@ public class DateRangeParamTest {
public static Date parseM1(String theString) throws ParseException {
return new Date(ourFmt.parse(theString).getTime() - 1L);
}
}

View File

@ -510,6 +510,11 @@
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-schematron</artifactId>