Skip to content

Commit

Permalink
TRUNK-5816 Attempt fix failing HandlerTests on Windows
Browse files Browse the repository at this point in the history
according to spotbugs
https://spotbugs.readthedocs.io/en/latest/bugDescriptions.html#obl-method-may-fail-to-clean-up-stream-or-resource-obl-unsatisfied-obligation
both Handlers that have failing tests on Windows are not cleaning up
InputStream's on their getObs() method

I am trying to close these streams since we know in the test the
ComplexData.data is an InputStream
  • Loading branch information
teleivo committed Jul 17, 2020
1 parent aff6a21 commit 6dc1a74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 8 additions & 2 deletions api/src/test/java/org/openmrs/obs/BinaryStreamHandlerTest.java
Expand Up @@ -17,6 +17,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -84,6 +85,8 @@ public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {
String filename = "TestingComplexObsSaving";
byte[] content = "Teststring".getBytes();

Obs complexObs1 = null;
Obs complexObs2 = null;
try (ByteArrayInputStream byteIn = new ByteArrayInputStream(content)) {
ComplexData complexData = new ComplexData(filename, byteIn);
// Construct 2 Obs to also cover the case where the filename exists already
Expand All @@ -95,11 +98,14 @@ public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {
handler.saveObs(obs1);
handler.saveObs(obs2);

Obs complexObs1 = handler.getObs(obs1, "RAW_VIEW");
Obs complexObs2 = handler.getObs(obs2, "RAW_VIEW");
complexObs1 = handler.getObs(obs1, "RAW_VIEW");
complexObs2 = handler.getObs(obs2, "RAW_VIEW");

assertEquals(complexObs1.getComplexData().getMimeType(), mimetype);
assertEquals(complexObs2.getComplexData().getMimeType(), mimetype);
} finally {
((InputStream) complexObs1.getComplexData().getData()).close();
((InputStream) complexObs1.getComplexData().getData()).close();
}
}
}
12 changes: 9 additions & 3 deletions api/src/test/java/org/openmrs/obs/MediaHandlerTest.java
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -82,7 +83,9 @@ public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {

File sourceFile = new File(
"src" + File.separator + "test" + File.separator + "resources" + File.separator + "ComplexObsTestAudio.mp3");


Obs complexObs1 = null;
Obs complexObs2 = null;
try (FileInputStream in1 = new FileInputStream(sourceFile);
FileInputStream in2 = new FileInputStream(sourceFile)
) {
Expand All @@ -98,11 +101,14 @@ public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {
handler.saveObs(obs1);
handler.saveObs(obs2);

Obs complexObs1 = handler.getObs(obs1, "RAW_VIEW");
Obs complexObs2 = handler.getObs(obs2, "RAW_VIEW");
complexObs1 = handler.getObs(obs1, "RAW_VIEW");
complexObs2 = handler.getObs(obs2, "RAW_VIEW");

assertEquals( "audio/mpeg", complexObs1.getComplexData().getMimeType());
assertEquals("audio/mpeg", complexObs2.getComplexData().getMimeType());
} finally {
((InputStream) complexObs1.getComplexData().getData()).close();
((InputStream) complexObs1.getComplexData().getData()).close();
}
}
}

0 comments on commit 6dc1a74

Please sign in to comment.