Skip to content

Commit

Permalink
TRUNK-5816 Close streams in test
Browse files Browse the repository at this point in the history
both tests fail on Windows with an error like

[INFO] Running org.openmrs.obs.BinaryStreamHandlerTest
Jul 17, 2020 11:42:59 AM org.junit.jupiter.engine.execution.JupiterEngineExecutionContext close
SEVERE: Caught exception while closing extension context: org.junit.jupiter.engine.descriptor.MethodExtensionContext@4fa274
java.io.IOException: Failed to delete temp directory ...\AppData\Local\Temp\junit9081338062257189768. The following paths could not be deleted (see suppressed exceptions for details): , TestingComplexObsSaving_1d883c5b-ae87-4e42-b5aa-2171b58c51da.dat, TestingComplexObsSaving_27cc4781-b820-466d-834c-70b4d221d24f.dat
...
        Suppressed: java.nio.file.FileSystemException: ...\AppData\Local\Temp\junit9081338062257189768\TestingComplexObsSaving_1d883c5b-ae87-4e42-b5aa-2171b58c51da.dat: The process cannot access the file because it is being used by another process.

Looking at spring-io/initializr#862 it could
well be that we are facing the same problem that we are not properly
closing our streams before exiting the test. Thus wrapping them in a
try-with resource block.

This theory makes sense since the saveObs uses OpenmrsUtil.copyFile
which does not close the inputstream but only the outputstream.
  • Loading branch information
teleivo committed Jul 17, 2020
1 parent 45204cb commit 0ee2d1f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
47 changes: 21 additions & 26 deletions api/src/test/java/org/openmrs/obs/BinaryStreamHandlerTest.java
Expand Up @@ -18,8 +18,8 @@
import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.openmrs.GlobalProperty;
import org.openmrs.Obs;
Expand Down Expand Up @@ -71,37 +71,32 @@ public void shouldNotSupportOtherViews() {

@Test
public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {
String mimetype = "application/octet-stream";
String filename = "TestingComplexObsSaving";
byte[] content = "Teststring".getBytes();

ByteArrayInputStream byteIn = new ByteArrayInputStream(content);

ComplexData complexData = new ComplexData(filename, byteIn);

// Construct 2 Obs to also cover the case where the filename exists already
Obs obs1 = new Obs();
obs1.setComplexData(complexData);

Obs obs2 = new Obs();
obs2.setComplexData(complexData);

adminService.saveGlobalProperty(new GlobalProperty(
OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR,
complexObsTestFolder.toAbsolutePath().toString()
));

String mimetype = "application/octet-stream";
String filename = "TestingComplexObsSaving";
byte[] content = "Teststring".getBytes();

// Execute save
handler.saveObs(obs1);
handler.saveObs(obs2);

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

assertEquals(complexObs1.getComplexData().getMimeType(), mimetype);
assertEquals(complexObs2.getComplexData().getMimeType(), mimetype);
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
Obs obs1 = new Obs();
obs1.setComplexData(complexData);
Obs obs2 = new Obs();
obs2.setComplexData(complexData);

handler.saveObs(obs1);
handler.saveObs(obs2);

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

assertEquals(complexObs1.getComplexData().getMimeType(), mimetype);
assertEquals(complexObs2.getComplexData().getMimeType(), mimetype);
}
}

}
48 changes: 24 additions & 24 deletions api/src/test/java/org/openmrs/obs/MediaHandlerTest.java
Expand Up @@ -19,8 +19,8 @@
import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.openmrs.GlobalProperty;
import org.openmrs.Obs;
Expand Down Expand Up @@ -71,35 +71,35 @@ public void shouldNotSupportOtherViews() {

@Test
public void saveObs_shouldRetrieveCorrectMimetype() throws IOException {
File sourceFile = new File(
"src" + File.separator + "test" + File.separator + "resources" + File.separator + "ComplexObsTestAudio.mp3");

FileInputStream in1 = new FileInputStream(sourceFile);
FileInputStream in2 = new FileInputStream(sourceFile);

ComplexData complexData1 = new ComplexData("TestingComplexObsSaving.mp3", in1);
ComplexData complexData2 = new ComplexData("TestingComplexObsSaving.mp3", in2);

// Construct 2 Obs to also cover the case where the filename exists already
Obs obs1 = new Obs();
obs1.setComplexData(complexData1);

Obs obs2 = new Obs();
obs2.setComplexData(complexData2);

adminService.saveGlobalProperty(new GlobalProperty(
OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR,
complexObsTestFolder.toAbsolutePath().toString()
));

handler.saveObs(obs1);
handler.saveObs(obs2);

Obs complexObs1 = handler.getObs(obs1, "RAW_VIEW");
Obs complexObs2 = handler.getObs(obs2, "RAW_VIEW");
File sourceFile = new File(
"src" + File.separator + "test" + File.separator + "resources" + File.separator + "ComplexObsTestAudio.mp3");

assertEquals( "audio/mpeg", complexObs1.getComplexData().getMimeType());
assertEquals("audio/mpeg", complexObs2.getComplexData().getMimeType());
try (FileInputStream in1 = new FileInputStream(sourceFile);
FileInputStream in2 = new FileInputStream(sourceFile)
) {
ComplexData complexData1 = new ComplexData("TestingComplexObsSaving.mp3", in1);
ComplexData complexData2 = new ComplexData("TestingComplexObsSaving.mp3", in2);

// Construct 2 Obs to also cover the case where the filename exists already
Obs obs1 = new Obs();
obs1.setComplexData(complexData1);
Obs obs2 = new Obs();
obs2.setComplexData(complexData2);

handler.saveObs(obs1);
handler.saveObs(obs2);

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

assertEquals( "audio/mpeg", complexObs1.getComplexData().getMimeType());
assertEquals("audio/mpeg", complexObs2.getComplexData().getMimeType());
}
}

}

0 comments on commit 0ee2d1f

Please sign in to comment.