Fix broken unit tests on Windows

This commit is contained in:
James Agnew 2014-10-01 16:02:22 -04:00
parent 751face0a6
commit c6dcd4b1af
7 changed files with 87 additions and 37 deletions

View File

@ -87,6 +87,15 @@
resources to be re-added (and the actual message to grow) with each encode pass. Thanks to
Alexander Kley for the test case!
</action>
<action type="fix">
JSON-encoded contained resources with the incorrect "_id" element (which should be "id", but some
incorrect examples exist on the FHIR specification) now parse correctly.
</action>
<action type="fix">
Several unit tests failed on Windows (or any platform with non UTF-8 default encoding). This may
have also caused resource validation to fail occasionally on these platforms as well.
Thanks to Bill de Beaubien for reporting!
</action>
</release>
<release version="0.6" date="2014-Sep-08" description="This release brings a number of new features and bug fixes!">
<!--

View File

@ -698,6 +698,10 @@ public class JsonParser extends BaseParser implements IParser {
} else if ("id".equals(nextName)) {
elementId = theObject.getString(nextName);
continue;
} else if ("_id".equals(nextName)) {
// _id is incorrect, but some early examples in the FHIR spec used it
elementId = theObject.getString(nextName);
continue;
} else if ("extension".equals(nextName)) {
JsonArray array = theObject.getJsonArray(nextName);
parseExtension(theState, array, false);

View File

@ -26,6 +26,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -40,6 +41,7 @@ import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.SAXException;
@ -50,6 +52,7 @@ import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome.Issue;
import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
class SchemaBaseValidator implements IValidator {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SchemaBaseValidator.class);
@ -77,9 +80,9 @@ class SchemaBaseValidator implements IValidator {
MyErrorHandler handler = new MyErrorHandler(theContext);
validator.setErrorHandler(handler);
String encodedResource = theContext.getXmlEncodedResource();
// ourLog.info(new FhirContext().newXmlParser().setPrettyPrint(true).encodeBundleToString((Bundle) theContext.getResource()));
// ourLog.info(new FhirContext().newXmlParser().setPrettyPrint(true).encodeBundleToString((Bundle) theContext.getResource()));
validator.validate(new StreamSource(new StringReader(encodedResource)));
} catch (SAXException e) {
throw new ConfigurationException("Could not apply schema file", e);
@ -115,11 +118,26 @@ class SchemaBaseValidator implements IValidator {
private Source loadXml(String theVersion, String theSystemId, String theSchemaName) {
String pathToBase = "ca/uhn/fhir/model/" + theVersion + "/schema/" + theSchemaName;
ourLog.debug("Going to load resource: {}", pathToBase);
InputStream baseIs = FhirValidator.class.getClassLoader().getResourceAsStream(pathToBase);
if (baseIs == null) {
throw new ValidationFailureException("No FHIR-BASE schema found");
}
Source baseSource = new StreamSource(baseIs, theSystemId);
baseIs = new BOMInputStream(baseIs, false);
InputStreamReader baseReader = new InputStreamReader(baseIs, Charset.forName("UTF-8"));
Source baseSource = new StreamSource(baseReader, theSystemId);
// String schema;
// try {
// schema = IOUtils.toString(baseIs, Charset.forName("UTF-8"));
// } catch (IOException e) {
// throw new InternalErrorException(e);
// }
//
// ourLog.info("Schema is:\n{}", schema);
//
// Source baseSource = new StreamSource(new StringReader(schema), theSystemId);
// Source baseSource = new StreamSource(baseIs, theSystemId);
return baseSource;
}
@ -180,31 +198,16 @@ class SchemaBaseValidator implements IValidator {
input.setSystemId(theSystemId);
input.setBaseURI(theBaseURI);
String pathToBase = "ca/uhn/fhir/model/" + myVersion + "/schema/" + theSystemId;
ourLog.debug("Loading referenced schema file: " + pathToBase);
InputStream baseIs = FhirValidator.class.getClassLoader().getResourceAsStream(pathToBase);
if (baseIs == null) {
throw new ValidationFailureException("No FHIR-BASE schema found");
}
ourLog.debug("Loading schema: {}", theSystemId);
byte[] schema;
try {
schema = IOUtils.toByteArray(new InputStreamReader(baseIs, "UTF-8"));
} catch (IOException e) {
throw new ValidationFailureException("Failed to load schema " + theSystemId, e);
}
// Account for BOM in UTF-8 text (this seems to choke Java 6's built in XML reader)
int offset = 0;
if (schema[0] == (byte) 0xEF && schema[1] == (byte) 0xBB && schema[2] == (byte) 0xBF) {
offset = 3;
}
try {
input.setCharacterStream(new InputStreamReader(new ByteArrayInputStream(schema, offset, schema.length - offset), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new ValidationFailureException("Failed to load schema " + theSystemId, e);
}
input.setByteStream(baseIs);
return input;
}

View File

@ -28,6 +28,7 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.api.annotation.Child;
@ -41,6 +42,7 @@ import ca.uhn.fhir.model.dstu.resource.Binary;
import ca.uhn.fhir.model.dstu.resource.Conformance;
import ca.uhn.fhir.model.dstu.resource.Conformance.RestResource;
import ca.uhn.fhir.model.dstu.resource.DiagnosticReport;
import ca.uhn.fhir.model.dstu.resource.ListResource;
import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.model.dstu.resource.Organization;
import ca.uhn.fhir.model.dstu.resource.Patient;
@ -85,7 +87,29 @@ public class JsonParserTest {
assertThat(out, containsString("<xhtml:div xmlns:xhtml=\\\"http://www.w3.org/1999/xhtml\\\">hello</xhtml:div>"));
}
@Test
public void testEncodeIds() {
Patient pt =new Patient();
pt.addIdentifier("sys", "val");
ListResource list = new ListResource();
list.setId("listId");
list.addEntry().setItem(new ResourceReferenceDt(pt));
String enc = ourCtx.newJsonParser().encodeResourceToString(list);
ourLog.info(enc);
assertThat(enc, containsString("\"id\":\"1\""));
ListResource parsed = ourCtx.newJsonParser().parseResource(ListResource.class,enc);
assertEquals(Patient.class, parsed.getEntryFirstRep().getItem().getResource().getClass());
enc = enc.replace("\"id\"", "\"_id\"");
parsed = ourCtx.newJsonParser().parseResource(ListResource.class,enc);
assertEquals(Patient.class, parsed.getEntryFirstRep().getItem().getResource().getClass());
}
@Test
public void testEncodingNullExtension() {
Patient p = new Patient();
@ -929,7 +953,7 @@ public class JsonParserTest {
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"));
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"), Charset.forName("UTF-8"));
JSON expected = JSONSerializer.toJSON(jsonString);
JSON actual = JSONSerializer.toJSON(encoded.trim());
@ -975,7 +999,7 @@ public class JsonParserTest {
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"));
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"), Charset.forName("UTF-8"));
JSON expected = JSONSerializer.toJSON(jsonString);
JSON actual = JSONSerializer.toJSON(encoded.trim());

View File

@ -73,6 +73,11 @@ public class XmlParserTest {
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserTest.class);
@BeforeClass
public static void beforeClass2() {
System.setProperty("file.encoding", "ISO-8859-1");
}
/**
* Thanks to Alexander Kley!
*/
@ -696,7 +701,7 @@ public class XmlParserTest {
IParser p = ourCtx.newXmlParser();
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/observation-example-eeg.xml"));
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/observation-example-eeg.xml"), Charset.forName("UTF-8"));
IResource resource = p.parseResource(string);
String result = p.encodeResourceToString(resource);
@ -709,7 +714,7 @@ public class XmlParserTest {
// Use new context here to ensure List isn't already loaded
IParser p = new FhirContext().newXmlParser();
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/feed-with-list.xml"));
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/feed-with-list.xml"), Charset.forName("UTF-8"));
Bundle bundle = p.parseBundle(string);
ListResource res = (ListResource) bundle.toListOfResources().get(2);
@ -722,7 +727,7 @@ public class XmlParserTest {
IParser p = ourCtx.newXmlParser();
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-dicom.xml"));
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-dicom.xml"), Charset.forName("UTF-8"));
IResource resource = p.parseResource(string);
String result = p.encodeResourceToString(resource);
@ -730,7 +735,7 @@ public class XmlParserTest {
// Nothing
string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-us-extensions.xml"));
string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-us-extensions.xml"), Charset.forName("UTF-8"));
resource = p.parseResource(string);
result = p.encodeResourceToString(resource);
@ -743,7 +748,7 @@ public class XmlParserTest {
IParser p = ourCtx.newXmlParser();
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/questionnaire-example.xml"));
String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/questionnaire-example.xml"), Charset.forName("UTF-8"));
IResource resource = p.parseResource(string);
String result = p.encodeResourceToString(resource);
@ -1188,7 +1193,7 @@ public class XmlParserTest {
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"));
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
String expected = (jsonString);
String actual = (encoded.trim());
@ -1220,7 +1225,7 @@ public class XmlParserTest {
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"));
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
String expected = (jsonString);
String actual = (encoded.trim());

View File

@ -521,7 +521,9 @@ public class ClientTest {
ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo");
client.getHistoryPatientInstance(new IdDt("111"), new InstantDt("2012-01-02T00:01:02"), new IntegerDt(12));
assertEquals("http://foo/Patient/111/_history?_since=2012-01-02T00%3A01%3A02&_count=12", capt.getAllValues().get(0).getURI().toString());
assertThat(capt.getAllValues().get(0).getURI().toString(), containsString("http://foo/Patient/111/_history?"));
assertThat(capt.getAllValues().get(0).getURI().toString(), containsString("_since=2012-01-02T00%3A01%3A02"));
assertThat(capt.getAllValues().get(0).getURI().toString(), containsString("_count=12"));
String expectedDateString = new InstantDt(new InstantDt("2012-01-02T00:01:02").getValue()).getValueAsString(); // ensures
// the
@ -529,7 +531,9 @@ public class ClientTest {
// timezone
expectedDateString = expectedDateString.replace(":", "%3A");
client.getHistoryPatientInstance(new IdDt("111"), new InstantDt("2012-01-02T00:01:02").getValue(), new IntegerDt(12).getValue());
assertEquals("http://foo/Patient/111/_history?_since=" + expectedDateString + "&_count=12", capt.getAllValues().get(1).getURI().toString());
assertThat(capt.getAllValues().get(1).getURI().toString(), containsString("http://foo/Patient/111/_history?"));
assertThat(capt.getAllValues().get(1).getURI().toString(), containsString("_since="+expectedDateString));
assertThat(capt.getAllValues().get(1).getURI().toString(), containsString("_count=12"));
client.getHistoryPatientInstance(new IdDt("111"), null, new IntegerDt(12));
assertEquals("http://foo/Patient/111/_history?_count=12", capt.getAllValues().get(2).getURI().toString());
@ -538,10 +542,10 @@ public class ClientTest {
assertEquals("http://foo/Patient/111/_history?_since=2012-01-02T00%3A01%3A02", capt.getAllValues().get(3).getURI().toString());
client.getHistoryPatientInstance(new IdDt("111"), new InstantDt(), new IntegerDt(12));
assertEquals("http://foo/Patient/111/_history?_count=12", capt.getAllValues().get(2).getURI().toString());
assertEquals("http://foo/Patient/111/_history?_count=12", capt.getAllValues().get(4).getURI().toString());
client.getHistoryPatientInstance(new IdDt("111"), new InstantDt("2012-01-02T00:01:02"), new IntegerDt());
assertEquals("http://foo/Patient/111/_history?_since=2012-01-02T00%3A01%3A02", capt.getAllValues().get(3).getURI().toString());
assertEquals("http://foo/Patient/111/_history?_since=2012-01-02T00%3A01%3A02", capt.getAllValues().get(5).getURI().toString());
}

View File

@ -163,6 +163,7 @@
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<runOrder>random</runOrder>
<!--<argLine>-Dfile.encoding=ISO-8859-1</argLine>-->
</configuration>
</plugin>
<plugin>