Skip to content

Commit

Permalink
Merge pull request #3 from Edirom/feature/junit
Browse files Browse the repository at this point in the history
Feature/junit
  • Loading branch information
anneferger authored Sep 20, 2022
2 parents 0660fc8 + 3c50c8e commit 07a769c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 14 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
!*.ent
!*.xsd
!*.yml
!test-input.*.zip
!required.sh
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
</developer>
</developers>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.psnc.dl.ege</groupId>
<artifactId>ege-framework</artifactId>
Expand All @@ -65,7 +72,7 @@
</repositories>
<distributionManagement>
<repository>
<id>github</id>
<id>githubedirom</id>
<name>GitHub Edirom Apache Maven Packages</name>
<url>https://maven.pkg.github.com/edirom/lilypond-converter</url>
</repository>
Expand Down
4 changes: 4 additions & 0 deletions required.sh
Original file line number Diff line number Diff line change
@@ -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
43 changes: 32 additions & 11 deletions src/main/java/de/edirom/meigarage/lilypond/LilyPondConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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;
Expand All @@ -44,7 +48,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;
}
}
Expand All @@ -59,46 +63,54 @@ 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<String, String> properties) throws IOException,
DataType fromDataType, DataType toDataType, Map<String, String> properties, String tempDir) throws IOException,
ConverterException {

// LilyPond to PDF
if (fromDataType.getFormat().equals(Conversion.LILYPONDTOPDF.getIFormatId()) &&
toDataType.getFormat().equals(Conversion.LILYPONDTOPDF.getOFormatId())) {

properties.put("extension", "pdf");
performLilyPondTransformation(inputStream, outputStream, "pdf", properties);
performLilyPondTransformation(inputStream, outputStream, "pdf", properties, tempDir);

}
// LilyPond to PNG
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<String, String> properties) throws IOException, ConverterException {
Map<String, String> properties, String tempDir) throws IOException, ConverterException {

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, "^.*(?<!bin)$");
if(inputFile!=null) {
String newFileName = inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().lastIndexOf(".")) + ".ly";
inputFile.renameTo(new File(newFileName));

outTempDir = prepareTempDir();
outTempDir = prepareTempDir(tempDir);

ProcessBuilder builder = new ProcessBuilder();
builder.command("sh", "-c", "lilypond --output=" + outTempDir + " --format=" + format + " " + newFileName);
String command = "lilypond --output=" + outTempDir.getAbsolutePath() + " --formats=" + format + " " + newFileName;
LOGGER.debug(command);
builder.command("sh", "-c", command);

builder.directory(inTmpDir);
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (inStreamReader.readLine() != null) {
LOGGER.debug(inStreamReader.readLine());
}

LilyPondRunner runner = new LilyPondRunner(process.getInputStream());
Executors.newSingleThreadExecutor().submit(runner);
Expand All @@ -121,10 +133,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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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/test.png")), "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.ly.zip
Binary file not shown.

0 comments on commit 07a769c

Please sign in to comment.