ATS-675/ATS-702 Load resources from jar

Added Junit temporary folder to AbstractControllerTest in order to stream the files out of the jar and maintain the functionality of  getTestFile().
This commit is contained in:
David Edwards
2020-04-08 09:44:45 +01:00
parent f5bacf92af
commit c722166803
2 changed files with 45 additions and 0 deletions

View File

@@ -65,6 +65,12 @@
<groupId>org.messaginghub</groupId> <groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId> <artifactId>pooled-jms</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -44,6 +44,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
@@ -63,7 +64,10 @@ import org.alfresco.transform.client.model.config.Transformer;
import org.alfresco.transform.client.registry.TransformServiceRegistry; import org.alfresco.transform.client.registry.TransformServiceRegistry;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transformer.probes.ProbeTestTransform; import org.alfresco.transformer.probes.ProbeTestTransform;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.SpyBean;
@@ -84,6 +88,9 @@ import com.google.common.collect.ImmutableSet;
*/ */
public abstract class AbstractTransformerControllerTest public abstract class AbstractTransformerControllerTest
{ {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Autowired @Autowired
protected MockMvc mockMvc; protected MockMvc mockMvc;
@@ -162,6 +169,38 @@ public abstract class AbstractTransformerControllerTest
} }
protected File getTestFile(String testFilename, boolean required) throws IOException protected File getTestFile(String testFilename, boolean required) throws IOException
{
File testFile;
ClassLoader classLoader = getClass().getClassLoader();
URL testFileUrl = classLoader.getResource(testFilename);
if (required && testFileUrl == null)
{
throw new IOException("The test file " + testFilename +
" does not exist in the resources directory");
}
try
{
testFile = folder.newFile(testFilename);
}
catch (IOException e)
{
if(e.getMessage().contains("a file with the name \'" + testFilename + "\' already exists in the test folder"))
{
testFile = new File(URLDecoder.decode(folder.getRoot().getPath()+ File.separator + testFilename, "UTF-8"));
}
else
{
throw e;
}
}
InputStream inputStream = classLoader.getResourceAsStream(testFilename);
FileUtils.copyInputStreamToFile(inputStream, testFile);
return testFileUrl == null ? null : testFile;
}
protected File getTestFileOrig(String testFilename, boolean required) throws IOException
{ {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
URL testFileUrl = classLoader.getResource(testFilename); URL testFileUrl = classLoader.getResource(testFilename);