One more test for #304

This commit is contained in:
jamesagnew 2016-03-02 07:29:09 -05:00
parent 94f50686bc
commit 970bc3ed89
3 changed files with 46 additions and 35 deletions

View File

@ -300,9 +300,16 @@ public class FhirTerser {
case COMPOSITE_DATATYPE: {
BaseRuntimeElementCompositeDefinition<?> childDef = (BaseRuntimeElementCompositeDefinition<?>) def;
for (BaseRuntimeChildDefinition nextChild : childDef.getChildrenAndExtension()) {
List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
List<?> values = nextChild.getAccessor().getValues(theElement);
if (values != null) {
for (IBase nextValue : values) {
for (Object nextValueObject : values) {
IBase nextValue;
try {
nextValue = (IBase) nextValueObject;
} catch (ClassCastException e) {
String s = "Found instance of " + nextValueObject.getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName();
throw new ClassCastException(s);
}
if (nextValue == null) {
continue;
}

View File

@ -1,33 +0,0 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.List;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.Test;
public class ModelDstu3Test {
/**
* See #304
*/
@SuppressWarnings("rawtypes")
@Test
public void testPopulateWrongGenericType() {
Patient p = new Patient();
List names = Arrays.asList("name");
p.setName(null);
try {
p.setName(names);
fail();
} catch (ClassCastException e) {
assertEquals("Failed to set invalid value, found element in list of type String but expected ca.uhn.fhir.model.dstu2.composite.HumanNameDt", e.getMessage());
}
}
}

View File

@ -0,0 +1,37 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
public class ModelDstu3Test {
private static FhirContext ourCtx = FhirContext.forDstu3();
/**
* See #304
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPopulateWrongGenericType() {
Patient p = new Patient();
List names = Arrays.asList("name");
List existingNames = p.getName();
existingNames.addAll(names);
try {
ourCtx.newXmlParser().encodeResourceToString(p);
} catch (ClassCastException e) {
assertEquals("Found instance of class java.lang.String - Did you set a field value to the incorrect type? Expected org.hl7.fhir.instance.model.api.IBase", e.getMessage());
}
}
}