From a21407a612fc05ca6628a3c5ff0574c642b41448 Mon Sep 17 00:00:00 2001 From: Anne Ferger Date: Tue, 16 Aug 2022 09:28:18 +0200 Subject: [PATCH 1/3] starting junit tests --- pom.xml | 9 +++- .../meigarage/lilypond/LilyPondConverter.java | 32 ++++++++---- .../lilypond/LilyPondConverterTest.java | 51 +++++++++++++++++++ 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/test/java/de/edirom/meigarage/lilypond/LilyPondConverterTest.java diff --git a/pom.xml b/pom.xml index 0753536..f00a874 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,13 @@ + + + junit + junit + 4.13.2 + test + pl.psnc.dl.ege ege-api @@ -71,7 +78,7 @@ - github + githubedirom GitHub Edirom Apache Maven Packages https://maven.pkg.github.com/edirom/lilypond-converter diff --git a/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java b/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java index 465a4f9..c3b543a 100644 --- a/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java +++ b/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java @@ -26,8 +26,12 @@ public class LilyPondConverter implements Converter { protected static final String LILYPOND_VERSION = "2.20.0"; + public void convert(InputStream inputStream, OutputStream outputStream, ConversionActionArguments conversionDataTypes) throws ConverterException, IOException{ + convert(inputStream,outputStream,conversionDataTypes, null); + } + public void convert(InputStream inputStream, OutputStream outputStream, - ConversionActionArguments conversionDataTypes) + ConversionActionArguments conversionDataTypes, String tempDir) throws ConverterException, IOException { boolean found = false; @@ -43,7 +47,7 @@ public void convert(InputStream inputStream, OutputStream outputStream, + conversionDataTypes.getOutputType().toString() + " WITH profile " + profile ); convertDocument(inputStream, outputStream, cadt.getInputType(), cadt.getOutputType(), - cadt.getProperties()); + cadt.getProperties(), tempDir); found = true; } } @@ -58,7 +62,7 @@ public void convert(InputStream inputStream, OutputStream outputStream, * Prepares transformation : based on MIME type. */ private void convertDocument(InputStream inputStream, OutputStream outputStream, - DataType fromDataType, DataType toDataType, Map properties) throws IOException, + DataType fromDataType, DataType toDataType, Map properties, String tempDir) throws IOException, ConverterException { // LilyPond to PDF @@ -66,7 +70,7 @@ private void convertDocument(InputStream inputStream, OutputStream outputStream, toDataType.getFormat().equals(Conversion.LILYPONDTOPDF.getOFormatId())) { properties.put("extension", "pdf"); - performLilyPondTransformation(inputStream, outputStream, "pdf", properties); + performLilyPondTransformation(inputStream, outputStream, "pdf", properties, tempDir); } // LilyPond to PNG @@ -74,12 +78,12 @@ else if(fromDataType.getFormat().equals(Conversion.LILYPONDTOPNG.getIFormatId()) toDataType.getFormat().equals(Conversion.LILYPONDTOPNG.getOFormatId())) { properties.put("extension", "png"); - performLilyPondTransformation(inputStream, outputStream, "png", properties); + performLilyPondTransformation(inputStream, outputStream, "png", properties, tempDir); } } private void performLilyPondTransformation(InputStream inputStream, OutputStream outputStream, String format, - Map properties) throws IOException, ConverterException { + Map properties, String tempDir) throws IOException, ConverterException { File inTmpDir = null; File outTempDir = null; @@ -92,9 +96,10 @@ private void performLilyPondTransformation(InputStream inputStream, OutputStream String newFileName = inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().lastIndexOf(".")) + ".ly"; inputFile.renameTo(new File(newFileName)); - outTempDir = prepareTempDir(); + outTempDir = prepareTempDir(tempDir); ProcessBuilder builder = new ProcessBuilder(); + System.out.println("sh" + " -c" + " lilypond --output=" + outTempDir + " --format=" + format + " " + newFileName); builder.command("sh", "-c", "lilypond --output=" + outTempDir + " --format=" + format + " " + newFileName); builder.directory(inTmpDir); Process process = builder.start(); @@ -120,10 +125,19 @@ private void performLilyPondTransformation(InputStream inputStream, OutputStream } 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; } diff --git a/src/test/java/de/edirom/meigarage/lilypond/LilyPondConverterTest.java b/src/test/java/de/edirom/meigarage/lilypond/LilyPondConverterTest.java new file mode 100644 index 0000000..24c1362 --- /dev/null +++ b/src/test/java/de/edirom/meigarage/lilypond/LilyPondConverterTest.java @@ -0,0 +1,51 @@ +package de.edirom.meigarage.lilypond; + +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 LilyPondConverterTest { + private LilyPondConverter converter; + + @org.junit.Before + public void setUp() throws Exception { + converter = new LilyPondConverter(); + } + + @org.junit.After + public void tearDown() throws Exception { + converter = null; + } + + @org.junit.Test + public void convert() throws IOException, ConverterException { + InputStream is = new FileInputStream("src/test/resources/test-input.ly.zip"); + OutputStream os = new FileOutputStream("src/test/resources/test-output.png.zip"); + DataType inputType = new DataType("lilypond","text/x-lilypond"); + DataType outputType = new DataType("png","image/png"); + 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.png.zip")); + InputStream isout = new FileInputStream("src/test/resources/test-output.png.zip"); + EGEConfigurationManager.getInstance().getStandardIOResolver().decompressStream(isout, new File("src/test/resources/test-output.png")); + //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.png")), "UTF-8")); + is.close(); + os.close(); + isout.close(); + } + + @org.junit.Test + public void getPossibleConversions() { + assertNotNull(converter.getPossibleConversions()); + System.out.println(converter.getPossibleConversions()); + } +} \ No newline at end of file From 56143a70a64a9fdeef471b0da2d243f7573c006a Mon Sep 17 00:00:00 2001 From: Anne Ferger Date: Tue, 20 Sep 2022 17:08:32 +0200 Subject: [PATCH 2/3] finished junit test and fixed lilypond call --- .github/workflows/maven.yml | 7 +++++-- .gitignore | 1 + .../meigarage/lilypond/LilyPondConverter.java | 13 ++++++++++--- .../meigarage/lilypond/LilyPondConverterTest.java | 2 +- src/test/resources/test-input.ly.zip | Bin 0 -> 409 bytes 5 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/test-input.ly.zip diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index d6261b4..2e3870b 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -21,6 +21,9 @@ jobs: with: java-version: '11' distribution: 'adopt' + - name: Install required tools + run: | + sudo bash required.sh - name: maven-settings uses: s4u/maven-settings-action@v2 with: @@ -32,5 +35,5 @@ jobs: - name: Upload Maven build artifact uses: actions/upload-artifact@v2 with: - name: lilypond-converter-0.1.jar - path: /home/runner/work/lilypond-converter/lilypond-converter/target/lilypond-converter-0.1.jar + name: artifact + path: /home/runner/work/lilypond-converter/lilypond-converter/target/lilypond-converter-*.jar diff --git a/.gitignore b/.gitignore index b9daa88..0ada557 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ !*.ent !*.xsd !*.yml +!test-input.*.zip diff --git a/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java b/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java index 7716f0c..f869b21 100644 --- a/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java +++ b/src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java @@ -89,7 +89,7 @@ private void performLilyPondTransformation(InputStream inputStream, OutputStream File inTmpDir = null; File outTempDir = null; try { - inTmpDir = prepareTempDir(); + inTmpDir = prepareTempDir(tempDir); ior.decompressStream(inputStream, inTmpDir); // avoid processing files ending in .bin File inputFile = searchForData(inTmpDir, "^.*(?O@2TBL+OJ&!2_c zr0!%DUAHPg+$_xOm|k9T)y1V}HF;gHF8x1uNjRr~mQc5_y?uS()T$)&ENcg`pC1kD zCoH<=oPAH!`O^hS+j&LC?q82Ax$#iTX#L(q8%I0q--hQZ7OuOj<9hAZza;mvmET@@ zWXc~*SrXB*LRR`yRnnGrmX+singhRAZO~g5_r2%QK7o(#ejIvY82OmfwJ1Gik!8pO z>lMyjXQdAJb3M#$@lyPAb>kAj6r0_NwHHq9Uzg_8Y-VV7WmB-Cj`VUxcF87}yB5oe zr-Vq#iiT{N{y?j=bw${ND>obS*p=T@&c6Tn(EbAglA+OM(JkLEa6SAj_3_+seMSHH z@0x0V9Bc0RYNPoS6Us literal 0 HcmV?d00001 From 3c50c8ebe5f1c501e73946cbcc0bdec86e9ebfd5 Mon Sep 17 00:00:00 2001 From: Anne Ferger Date: Tue, 20 Sep 2022 17:11:14 +0200 Subject: [PATCH 3/3] add required.sh --- .gitignore | 1 + required.sh | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 required.sh diff --git a/.gitignore b/.gitignore index 0ada557..6f1f632 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ !*.xsd !*.yml !test-input.*.zip +!required.sh diff --git a/required.sh b/required.sh new file mode 100644 index 0000000..8356feb --- /dev/null +++ b/required.sh @@ -0,0 +1,4 @@ +#!/bin/bash +# installs lilypond into /usr/local/lilypond and /usr/local/bin as shortcut +curl -o /tmp/lilypond.sh https://lilypond.org/download/binaries/linux-64/lilypond-2.20.0-1.linux-64.sh +chmod a+x /tmp/lilypond.sh && /tmp/lilypond.sh --batch