Skip to content

Commit

Permalink
impl. removal of buffers and corrupted artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 8, 2024
1 parent a67fb25 commit 35a08e2
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto;
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.input.BOMInputStream;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -158,4 +156,17 @@ protected void startTimer(){
protected void stopTimer(){
log.info("Compilation time: {}ms", Instant.now().toEpochMilli() - initTimer);
}

@Getter
private final List<File> tempFiles = new LinkedList<>();
protected File createTempFile() throws IOException {
File bufferHtml = File.createTempFile("pirilampo-buffer-", ".tmp");
tempFiles.add(bufferHtml);
log.info("Created buffer file: {}", bufferHtml);
return bufferHtml;
}

protected void deleteAllTempFiles(){
tempFiles.forEach(File::delete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public void build() throws Exception {
startTimer();
GherkinDocumentParser gherkinDocumentParser = new GherkinDocumentParser(parameters, feature);

File outArtifact = getOutArtifact(parameters);

try (
FileOutputStream fos = new FileOutputStream(getOutArtifact(parameters));
FileOutputStream fos = new FileOutputStream(outArtifact);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8));
PrintWriter out = new PrintWriter(bw);
){
Expand All @@ -54,8 +56,11 @@ public void build() throws Exception {
gherkinDocumentParser.build(out);

out.print("</div></div></div></body></html>");
} catch (Throwable e){
outArtifact.delete();
throw e;
} finally {
stopTimer();
}

stopTimer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public FeatureToPDFCompiler(ParametersDto parameters) {
public void build() throws Exception {
startTimer();

File bufferHtml = File.createTempFile("pirilampo-buffer-", ".html");
log.info("Created buffer file: {}", bufferHtml);
File bufferHtml = createTempFile();
File outArtifact = getOutArtifact(parameters);

try (
Expand Down Expand Up @@ -68,23 +67,19 @@ public void build() throws Exception {
new GherkinDocumentParser(parameters, feature).build(out);

out.print("</body></html>");
} catch (Throwable e){
bufferHtml.delete();
throw e;
} finally {
if(bufferHtml.exists()){
pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath()));
}
}

bufferHtml.delete();
if(bufferHtml.exists()){
pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath()));
}

pdfParser.closeDocument();
} catch (Throwable e){
outArtifact.delete();
deleteAllTempFiles();
throw e;
} finally {
stopTimer();
}

stopTimer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ public void build() throws Exception {
Set<File> arquivos = listFolder(parameters.getProjectSource());
if(arquivos.isEmpty()) return;

File outArtifact = getOutArtifact(parameters);

try (
FileOutputStream fos = new FileOutputStream(getOutArtifact(parameters));
FileOutputStream fos = new FileOutputStream(outArtifact);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8));
PrintWriter out = new PrintWriter(bw);
){
Expand Down Expand Up @@ -219,8 +221,11 @@ public void build() throws Exception {
writeResourceToOut("htmlTemplate/dist/feature-pasta-angular.min.js", out);
out.print("</script>\n");
out.print("</body></html>");
} catch (Throwable e){
outArtifact.delete();
throw e;
} finally {
stopTimer();
}

stopTimer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void build() throws Exception {
pdfParser.initDocument(fosPDF);

for (File feature : arquivos) {
File bufferHtml = File.createTempFile("pirilampo-buffer-", ".html");
log.info("Created buffer file: {}", bufferHtml);
File bufferHtml = createTempFile();

try (
FileOutputStream fosHTML = new FileOutputStream(bufferHtml);
Expand Down Expand Up @@ -80,24 +79,20 @@ public void build() throws Exception {
}

out.print("</body></html>");
} catch (Throwable e){
bufferHtml.delete();
throw e;
} finally {
if(bufferHtml.exists()){
pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); //@TODO: not closeable
}
}

bufferHtml.delete();
if(bufferHtml.exists()){
pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); //@TODO: not closeable
}
}

pdfParser.closeDocument();
} catch (Throwable e){
outArtifact.delete();
deleteAllTempFiles();
throw e;
} finally {
stopTimer();
}

stopTimer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public abstract class Common {
protected final File featureFile = new File(Objects.requireNonNull(Thread.currentThread()
.getContextClassLoader()
.getResource("feature/xxx.Feature")).getFile());

protected final File featureErrorFolder = new File(Objects.requireNonNull(Thread.currentThread()
.getContextClassLoader()
.getResource("feature_error")).getFile());

protected final File featureErrorFile = new File(Objects.requireNonNull(Thread.currentThread()
.getContextClassLoader()
.getResource("feature_error/yyy.feature")).getFile());
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum;
import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum;
import com.github.clagomess.pirilampo.core.exception.FeatureException;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

public class FeatureToHTMLCompilerTest extends Common {
private final File targetFile = new File("target/FeatureToHTMLCompilerTest");

@BeforeEach
public void setup(){
if(!targetFile.isDirectory()){
assertTrue(targetFile.mkdir());
}else{
Arrays.stream(targetFile.listFiles()).forEach(File::delete);
}
}

@Test
public void build() throws Exception {
File targetFile = new File("target/FeatureToHTMLCompilerTest");
if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir());

ParametersDto parameters = new ParametersDto();
parameters.setProjectSource(featureFile);
parameters.setProjectTarget(targetFile);
Expand Down Expand Up @@ -46,5 +57,15 @@ public void build() throws Exception {
*/
}

// @TODO: impl unit for test remove buffer on error
@Test
public void checkDeletedBuffersOnError() {
ParametersDto parameters = new ParametersDto();
parameters.setProjectSource(featureErrorFile);
parameters.setProjectTarget(targetFile);
parameters.setCompilationType(CompilationTypeEnum.FEATURE);
parameters.setCompilationArtifact(CompilationArtifactEnum.HTML);

assertThrowsExactly(FeatureException.class, () -> new FeatureToHTMLCompiler(parameters).build());
assertFalse(new File(targetFile, "yyy.html").exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum;
import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum;
import com.github.clagomess.pirilampo.core.exception.FeatureException;
import lombok.val;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.StreamSupport;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class FeatureToPDFCompilerTest extends Common {
private final File targetFile = new File("target/FeatureToPDFCompilerTest");

@BeforeEach
public void setup(){
if(!targetFile.isDirectory()){
assertTrue(targetFile.mkdir());
}else{
Arrays.stream(targetFile.listFiles()).forEach(File::delete);
}
}

@Test
@Timeout(8)
public void build() throws Exception {
File targetFile = new File("target/FeatureToPDFCompilerTest");
if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir());

ParametersDto parameters = new ParametersDto();
parameters.setProjectName("_AA_");
parameters.setProjectVersion("_BB_");
Expand Down Expand Up @@ -63,5 +76,17 @@ public void build() throws Exception {
// @TODO: also, validate html
}

// @TODO: impl unit for test remove buffer on error
@Test
public void checkDeletedBuffersOnError() {
ParametersDto parameters = new ParametersDto();
parameters.setProjectSource(featureErrorFile);
parameters.setProjectTarget(targetFile);
parameters.setCompilationType(CompilationTypeEnum.FEATURE);
parameters.setCompilationArtifact(CompilationArtifactEnum.PDF);

val compiler = new FeatureToPDFCompiler(parameters);
assertThrowsExactly(FeatureException.class, compiler::build);
assertFalse(new File(targetFile, "yyy.pdf").exists());
Assertions.assertThat(compiler.getTempFiles()).noneMatch(File::exists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@

import com.github.clagomess.pirilampo.core.Common;
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.compilers.FolderToHTMLCompiler;
import com.github.clagomess.pirilampo.core.exception.FeatureException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
public class FolderToHTMLCompilerTest extends Common {
private final File targetFile = new File("target/FolderToHTMLCompilerTest");

@BeforeEach
public void setup(){
if(!targetFile.isDirectory()){
assertTrue(targetFile.mkdir());
}else{
Arrays.stream(targetFile.listFiles()).forEach(File::delete);
}
}

@Test
public void build() throws Exception {
File logoFile = new File(Thread.currentThread()
.getContextClassLoader()
.getResource("logo_xxx.png").getFile());

File targetFile = new File("target/FolderToHTMLCompilerTest");
if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir());

ParametersDto parameters = new ParametersDto();
parameters.setMenuColor("#666");
parameters.setProjectLogo(logoFile);
Expand All @@ -45,9 +55,6 @@ public void build() throws Exception {

@Test
public void build_master() throws Exception {
File targetFile = new File("target/FolderToHTMLCompilerTest");
if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir());

ParametersDto parameters = new ParametersDto();
parameters.setProjectSource(featureFolder);
parameters.setProjectMasterSource(featureMasterFolder);
Expand All @@ -62,5 +69,14 @@ public void build_master() throws Exception {
assertTrue(htmlString.contains("YYY_MASTER_YYY"));
}

// @TODO: impl unit for test remove buffer on error
@Test
public void checkDeletedBuffersOnError() {
ParametersDto parameters = new ParametersDto();
parameters.setProjectSource(featureErrorFolder);
parameters.setProjectTarget(targetFile);

assertThrowsExactly(FeatureException.class, () -> new FolderToHTMLCompiler(parameters).build());

assertFalse(new File(targetFile,"html/index.html").exists());
}
}
Loading

0 comments on commit 35a08e2

Please sign in to comment.