Skip to content

Commit

Permalink
Merge pull request #4 from TEIC/feature/junit
Browse files Browse the repository at this point in the history
example converter junit tests
  • Loading branch information
anneferger authored Mar 3, 2022
2 parents 630ba96 + c5d6c7f commit b50c06d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 11 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
with:
java-version: '11'
distribution: 'adopt'
- name: Install libreoffice
run: |
sudo apt-get update
sudo apt-get install libreoffice
sudo ln -s /usr/lib/libreoffice /usr/lib/openoffice
- name: maven-settings
uses: s4u/maven-settings-action@v2
with:
Expand All @@ -32,5 +37,5 @@ jobs:
- name: Upload Maven build artifact
uses: actions/upload-artifact@v2
with:
name: oo-converter-1.0.jar
path: /home/runner/work/oo-converter/oo-converter/target/oo-converter-1.0.jar
name: artifact
path: /home/runner/work/oo-converter/oo-converter/target/oo-converter-1.1.1-SNAPSHOT.jar
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
!*.ent
!*.xsd
!*.yml

!test-input.*.zip
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</build> -->
<groupId>uk.ac.ox.oucs.oxgarage.oo</groupId>
<artifactId>oo-converter</artifactId>
<version>1.1</version>
<version>1.1.1</version>
<name>OxGarage OO Converter</name>
<properties>

Expand Down Expand Up @@ -211,6 +211,13 @@
<version>0.6</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/uk/ac/ox/oucs/oxgarage/oo/OOConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ public class OOConverter implements Converter {
converter[i] = null;
}
}
public void convert(InputStream inputStream, OutputStream outputStream,
final ConversionActionArguments conversionDataTypes) throws ConverterException, IOException{
convert(inputStream, outputStream,conversionDataTypes, null);
}

public void convert(InputStream inputStream, OutputStream outputStream,
final ConversionActionArguments conversionDataTypes)
final ConversionActionArguments conversionDataTypes, String tempDir)
throws ConverterException, IOException {
try {
transform(inputStream, outputStream, conversionDataTypes.getInputType(), conversionDataTypes.getOutputType());
transform(inputStream, outputStream, conversionDataTypes.getInputType(), conversionDataTypes.getOutputType(), tempDir);
} catch (Exception e) {
e.printStackTrace();
throw new ConverterException(e.getMessage());
Expand All @@ -91,10 +95,18 @@ public List<ConversionActionArguments> getPossibleConversions() {
}

private File prepareTempDir() {
return prepareTempDir(null);
}
private File prepareTempDir(String tempDir) {
File inTempDir = null;
String uid = UUID.randomUUID().toString();
inTempDir = new File(EGEConstants.TEMP_PATH + File.separator + uid
+ File.separator);
if(tempDir!=null){
inTempDir = new File(tempDir + File.separator + uid
+ File.separator);
} else {
inTempDir = new File(EGEConstants.TEMP_PATH + File.separator + uid
+ File.separator);
}
inTempDir.mkdir();
return inTempDir;
}
Expand Down Expand Up @@ -133,10 +145,10 @@ private File searchForData(File dir, String regex) {
return null;
}

private void transform(InputStream inputStream, OutputStream outputStream, DataType input, DataType output)
private void transform(InputStream inputStream, OutputStream outputStream, DataType input, DataType output, String tempDir)
throws IOException, ConverterException {
File inTmpDir = prepareTempDir();
File outTmpDir = prepareTempDir();
File inTmpDir = prepareTempDir(tempDir);
File outTmpDir = prepareTempDir(tempDir);
try {
String inputExt = OOConfiguration.getExtension(input);
File inTmpFile = prepareInputData(inputStream, inTmpDir, inputExt);
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/uk/ac/ox/oucs/oxgarage/oo/OOConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uk.ac.ox.oucs.oxgarage.oo;

import pl.psnc.dl.ege.configuration.EGEConfigurationManager;
import pl.psnc.dl.ege.exception.ConverterException;
import pl.psnc.dl.ege.types.ConversionActionArguments;
import pl.psnc.dl.ege.types.DataType;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.*;

public class OOConverterTest {
private OOConverter converter;

@org.junit.Before
public void setUp() throws Exception {
converter = new OOConverter();
}

@org.junit.After
public void tearDown() throws Exception {
converter = null;
}

@org.junit.Test
public void convert() throws IOException, ConverterException {
//text to pdf
InputStream is = new FileInputStream("src/test/resources/test-input.txt.zip");
OutputStream os = new FileOutputStream("src/test/resources/test-output.pdf.zip");
DataType inputType = new DataType("txt","text/plain");
DataType outputType = new DataType("pdf","application/pdf");
ConversionActionArguments conversionActionArguments = new ConversionActionArguments(inputType, outputType, null);
String tempDir = "src/test/temp";
converter.convert(is, os, conversionActionArguments, tempDir);
assertNotNull(new File("src/test/resources/test-output.pdf.zip"));
is.close();
os.close();
//docx to TEI
is = new FileInputStream("src/test/resources/test-input.docx.zip");
os = new FileOutputStream("src/test/resources/test-output.txt.zip");
inputType = new DataType("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
outputType = new DataType("txt","text/plain");
conversionActionArguments = new ConversionActionArguments(inputType, outputType, null);
converter.convert(is, os, conversionActionArguments, tempDir);
assertNotNull(new File("src/test/resources/test-output.txt.zip"));
InputStream isout = new FileInputStream("src/test/resources/test-output.txt.zip");
EGEConfigurationManager.getInstance().getStandardIOResolver().decompressStream(isout, new File("src/test/resources/test-output.txt"));
//System.out.println(new String(Files. readAllBytes(Paths.get("src/test/resources/test-output.txt/result.txt")), "UTF-8"));
assertNotEquals("", new String(Files.readAllBytes(Paths.get("src/test/resources/test-output.txt/result.txt")), "UTF-8"));
is.close();
os.close();
isout.close();
}

@org.junit.Test
public void getPossibleConversions() {
assertNotNull(converter.getPossibleConversions());
//System.out.println(converter.getPossibleConversions());
}
}
Binary file added src/test/resources/test-input.docx.zip
Binary file not shown.
Binary file added src/test/resources/test-input.txt.zip
Binary file not shown.

0 comments on commit b50c06d

Please sign in to comment.