From d63650e58fdae7aa203e56f0852fce74c9ac98d4 Mon Sep 17 00:00:00 2001 From: Andru Stefanescu Date: Thu, 12 Aug 2021 18:32:28 +0100 Subject: [PATCH 1/5] Fix thumbnail error for svg files Signed-off-by: Andru Stefanescu --- .../tngtech/jgiven/attachment/MediaType.java | 2 +- jgiven-html5-report/build.gradle | 2 + .../html5/Html5AttachmentGenerator.java | 90 +++++++++++++++++-- .../html5/Html5AttachmentGeneratorTest.java | 42 ++++++++- .../src/test/resources/SampleSVG.svg | 3 + 5 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 jgiven-html5-report/src/test/resources/SampleSVG.svg diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/attachment/MediaType.java b/jgiven-core/src/main/java/com/tngtech/jgiven/attachment/MediaType.java index 2b27ed8363..ba65ddc935 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/attachment/MediaType.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/attachment/MediaType.java @@ -68,7 +68,7 @@ public static Type fromString( String string ) { /** * image/svg+xml */ - public static final MediaType SVG_UTF_8 = imageUtf8( "svg+xml" ); + public static final MediaType SVG_UTF_8 = imageUtf8( "svg+xml; charset=utf-8" ); /** * text/plain diff --git a/jgiven-html5-report/build.gradle b/jgiven-html5-report/build.gradle index 39e209eb08..1bf7e6a2a2 100644 --- a/jgiven-html5-report/build.gradle +++ b/jgiven-html5-report/build.gradle @@ -5,4 +5,6 @@ plugins { dependencies { implementation project(':jgiven-core') implementation project(':jgiven-html-app') + implementation 'org.apache.xmlgraphics:batik-transcoder:1.14' + implementation 'org.apache.xmlgraphics:batik-codec:1.14' } diff --git a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java index 675aacf7d0..2d8785b501 100644 --- a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java +++ b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java @@ -1,17 +1,19 @@ package com.tngtech.jgiven.report.html5; +import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.List; import java.util.Set; import javax.imageio.ImageIO; +import org.apache.batik.transcoder.TranscoderException; +import org.apache.batik.transcoder.TranscoderInput; +import org.apache.batik.transcoder.TranscoderOutput; +import org.apache.batik.transcoder.image.PNGTranscoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -145,7 +147,11 @@ private File writeFile( AttachmentModel attachment, MediaType mediaType ) { } Files.write( BaseEncoding.base64().decode( attachment.getValue() ), targetFile ); } else { - Files.write( attachment.getValue(), targetFile, Charsets.UTF_8 ); + Files.write(attachment.getValue().getBytes(Charsets.UTF_8), targetFile); + if (attachment.getMediaType().equals(com.tngtech.jgiven.attachment.MediaType.SVG_UTF_8.toString())) { + File thumbFile = getThumbnailFileFor(targetFile); + writeThumbnailForSVG(targetFile, thumbFile); + } } } catch( IOException e ) { log.error( "Error while trying to write attachment to file " + targetFile, e ); @@ -195,4 +201,78 @@ private byte[] bufferedImageToBase64( BufferedImage bi, String extension ) { } return imageArray; } + + private void writeThumbnailForSVG(File initialSVG, File thumbnailSVG) { + String base64PNGImage = getPNGFromSVG(initialSVG); + byte[] scaledDownInBytes = compressToThumbnail(base64PNGImage, "png"); + Dimension imageDimension = getImageDimension(scaledDownInBytes); + String base64ScaledDownContent = BaseEncoding.base64().encode(scaledDownInBytes); + createSVGThumbFile(thumbnailSVG, base64ScaledDownContent, imageDimension); + } + + Dimension getImageDimension(byte[] givenImage) { + ByteArrayInputStream bais = new ByteArrayInputStream(givenImage); + Dimension dimension = new Dimension(); + try { + BufferedImage image = ImageIO.read(bais); + dimension.height = image.getHeight(); + dimension.width = image.getWidth(); + } catch (IOException e) { + log.error("The converted png image cannot be read."); + } + + return dimension; + } + + void createSVGThumbFile(File targetFile, String base64Image, Dimension dimension) { + String xmlFormat = getXMLFormat(base64Image, dimension); + try { + FileWriter fileWriter = new FileWriter(targetFile); + fileWriter.write(xmlFormat); + fileWriter.close(); + } catch (IOException e) { + log.error("Error writing the thumbnail svg to " + targetFile); + } + } + + private String getXMLFormat(String base64Image, Dimension dimension) { + return ""+ + "" + + ""; + } + + String getPNGFromSVG(File givenSVG) { + PNGTranscoder transcoder = new PNGTranscoder(); + TranscoderInput transcoderInput = new TranscoderInput(); + TranscoderOutput transcoderOutput = new TranscoderOutput(); + + FileInputStream fis = null; + try { + fis = new FileInputStream(givenSVG); + } catch (FileNotFoundException e) { + log.error("Error while reading the initial svg file."); + return null; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + transcoderInput.setInputStream(fis); + transcoderOutput.setOutputStream(baos); + + try { + transcoder.transcode(transcoderInput, transcoderOutput); + } catch (TranscoderException e) { + e.printStackTrace(); + log.error("Error while transcoding the svg file to png. Is the svg formatted correctly?"); + return null; + } + + try { + fis.close(); + } catch (IOException e) { + log.error("Error closing the {} file", givenSVG); + } + + return BaseEncoding.base64().encode(baos.toByteArray()); + } } diff --git a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java index 6fda96e39e..9052c2d2ec 100644 --- a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java +++ b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java @@ -4,12 +4,12 @@ import static com.tngtech.jgiven.report.html5.Html5AttachmentGenerator.scaleDown; import static org.assertj.core.api.Assertions.assertThat; +import java.awt.*; import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Scanner; import javax.imageio.ImageIO; @@ -87,6 +87,42 @@ public void testFindingAndGeneratingAttachmentsInAllSteps() throws IOException { } + @Test + public void testGetImageDimensions() { + Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); + assertThat(generator.getImageDimension(BINARY_SAMPLE)).isEqualTo(new Dimension(22, 22)); + } + + @Test + public void testPNGConvertor() { + Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); + File sampleSVG = new File("src/test/resources/SampleSVG.svg"); + String pngContent = generator.getPNGFromSVG(sampleSVG); + + assertThat(generator.getImageDimension(BaseEncoding.base64().decode(pngContent))) + .isEqualTo(new Dimension(25, 25)); + } + + @Test + public void testSVGFilesHaveAGeneratedThumbnail() throws IOException { + Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); + File sampleSVG = new File("src/test/resources/SampleSVG.svg"); + Attachment sampleSVGAttachment= Attachment.fromTextFile(sampleSVG, MediaType.SVG_UTF_8) + .withFileName("SampleSVG"); + StepModel stepModel = new StepModel("svgTest", Lists.newArrayList()); + stepModel.addAttachment(sampleSVGAttachment); + + generator.visit(stepModel); + + File svgThumbnail = new File( temporaryFolderRule.getRoot().getPath() + + "/SampleSVG-thumb.svg" ); + + String pngContent = generator.getPNGFromSVG(svgThumbnail); + + assertThat(generator.getImageDimension(BaseEncoding.base64().decode(pngContent))) + .isEqualTo(new Dimension(MINIMAL_THUMBNAIL_SIZE, MINIMAL_THUMBNAIL_SIZE)); + } + private ReportModel generateReportModelWithAttachments() { Attachment nestedAttachment = Attachment.fromBinaryBytes( BINARY_SAMPLE, MediaType.GIF ).withFileName( "nestedAttachment" ); Attachment parentAttachment = Attachment.fromBinaryBytes( BINARY_SAMPLE, MediaType.GIF ).withFileName( "parentAttachment" ); diff --git a/jgiven-html5-report/src/test/resources/SampleSVG.svg b/jgiven-html5-report/src/test/resources/SampleSVG.svg new file mode 100644 index 0000000000..de2a784a31 --- /dev/null +++ b/jgiven-html5-report/src/test/resources/SampleSVG.svg @@ -0,0 +1,3 @@ + + \ No newline at end of file From b5cb372231541071f79141058c7bf31814fc49a0 Mon Sep 17 00:00:00 2001 From: Andru Stefanescu Date: Thu, 12 Aug 2021 18:53:39 +0100 Subject: [PATCH 2/5] Reformat code Signed-off-by: Andru Stefanescu --- .../html5/Html5AttachmentGenerator.java | 171 +++++++++--------- .../html5/Html5AttachmentGeneratorTest.java | 125 ++++++------- .../src/test/resources/SampleSVG.svg | 2 +- 3 files changed, 148 insertions(+), 150 deletions(-) diff --git a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java index 2d8785b501..d4aaf3c0e4 100644 --- a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java +++ b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java @@ -1,5 +1,18 @@ package com.tngtech.jgiven.report.html5; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; +import com.google.common.collect.Sets; +import com.google.common.io.BaseEncoding; +import com.google.common.io.Files; +import com.google.common.net.MediaType; +import com.tngtech.jgiven.exception.JGivenInstallationException; +import com.tngtech.jgiven.report.model.AttachmentModel; +import com.tngtech.jgiven.report.model.ReportModel; +import com.tngtech.jgiven.report.model.ReportModelVisitor; +import com.tngtech.jgiven.report.model.StepModel; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; @@ -7,9 +20,7 @@ import java.io.*; import java.util.List; import java.util.Set; - import javax.imageio.ImageIO; - import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; @@ -17,135 +28,121 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import com.google.common.collect.HashMultiset; -import com.google.common.collect.Multiset; -import com.google.common.collect.Sets; -import com.google.common.io.BaseEncoding; -import com.google.common.io.Files; -import com.google.common.net.MediaType; -import com.tngtech.jgiven.exception.JGivenInstallationException; -import com.tngtech.jgiven.report.model.AttachmentModel; -import com.tngtech.jgiven.report.model.ReportModel; -import com.tngtech.jgiven.report.model.ReportModelVisitor; -import com.tngtech.jgiven.report.model.StepModel; - class Html5AttachmentGenerator extends ReportModelVisitor { - private static final Logger log = LoggerFactory.getLogger( Html5AttachmentGenerator.class ); + private static final Logger log = LoggerFactory.getLogger(Html5AttachmentGenerator.class); private static final String ATTACHMENT_DIRNAME = "attachments"; public static final int MINIMAL_THUMBNAIL_SIZE = 20; private File attachmentsDir; private String subDir; - private Multiset fileCounter = HashMultiset.create(); - private Set usedFileNames = Sets.newHashSet(); + private final Multiset fileCounter = HashMultiset.create(); + private final Set usedFileNames = Sets.newHashSet(); private String htmlSubDir; public Html5AttachmentGenerator() { } @VisibleForTesting - public Html5AttachmentGenerator( File attachmentsDir ) { + public Html5AttachmentGenerator(File attachmentsDir) { this.attachmentsDir = attachmentsDir; } - public void generateAttachments( File targetDir, ReportModel model ) { - subDir = ATTACHMENT_DIRNAME + File.separatorChar + model.getClassName().replace( '.', File.separatorChar ); - htmlSubDir = subDir.replace( File.separatorChar, '/' ); - attachmentsDir = new File( targetDir, subDir ); + public void generateAttachments(File targetDir, ReportModel model) { + subDir = ATTACHMENT_DIRNAME + File.separatorChar + model.getClassName().replace('.', File.separatorChar); + htmlSubDir = subDir.replace(File.separatorChar, '/'); + attachmentsDir = new File(targetDir, subDir); - if( !attachmentsDir.exists() && !attachmentsDir.mkdirs() ) { - throw new JGivenInstallationException( "Could not create directory " + attachmentsDir ); + if (!attachmentsDir.exists() && !attachmentsDir.mkdirs()) { + throw new JGivenInstallationException("Could not create directory " + attachmentsDir); } - model.accept( this ); + model.accept(this); } @Override - public void visit( StepModel stepModel ) { + public void visit(StepModel stepModel) { List attachments = stepModel.getAttachments(); - for( AttachmentModel attachment : attachments ) { - writeAttachment( attachment ); + for (AttachmentModel attachment : attachments) { + writeAttachment(attachment); } } - private void writeAttachment( AttachmentModel attachment ) { + private void writeAttachment(AttachmentModel attachment) { String mimeType = attachment.getMediaType(); - MediaType mediaType = MediaType.parse( mimeType ); - File targetFile = writeFile( attachment, mediaType ); - attachment.setValue( htmlSubDir + "/" + targetFile.getName() ); - log.debug( "Attachment written to " + targetFile ); + MediaType mediaType = MediaType.parse(mimeType); + File targetFile = writeFile(attachment, mediaType); + attachment.setValue(htmlSubDir + "/" + targetFile.getName()); + log.debug("Attachment written to " + targetFile); } - private String getExtension( MediaType mediaType ) { - if( mediaType.is( MediaType.SVG_UTF_8 ) ) { + private String getExtension(MediaType mediaType) { + if (mediaType.is(MediaType.SVG_UTF_8)) { return "svg"; } - if( mediaType.is( MediaType.ICO ) ) { + if (mediaType.is(MediaType.ICO)) { return "ico"; } - if( mediaType.is( MediaType.BMP ) ) { + if (mediaType.is(MediaType.BMP)) { return "bmp"; } return mediaType.subtype(); } - File getTargetFile( String fileName, String extension ) { - if( fileName == null ) { + File getTargetFile(String fileName, String extension) { + if (fileName == null) { fileName = "attachment"; } - int count = fileCounter.count( fileName ); - fileCounter.add( fileName ); + int count = fileCounter.count(fileName); + fileCounter.add(fileName); String suffix = ""; - if( count > 0 ) { + if (count > 0) { count += 1; - suffix = String.valueOf( count ); + suffix = String.valueOf(count); } String fileNameWithExtension = fileName + suffix + "." + extension; - while( usedFileNames.contains( fileNameWithExtension ) ) { - fileCounter.add( fileName ); + while (usedFileNames.contains(fileNameWithExtension)) { + fileCounter.add(fileName); count++; - suffix = String.valueOf( count ); + suffix = String.valueOf(count); fileNameWithExtension = fileName + suffix + "." + extension; } - usedFileNames.add( fileNameWithExtension ); - return new File( attachmentsDir, fileNameWithExtension ); + usedFileNames.add(fileNameWithExtension); + return new File(attachmentsDir, fileNameWithExtension); } - private File getThumbnailFileFor( File originalImage ) { + private File getThumbnailFileFor(File originalImage) { String orgName = originalImage.getName(); String newName = ""; - int dotIndex = orgName.lastIndexOf( "." ); + int dotIndex = orgName.lastIndexOf("."); - if( dotIndex == -1 ) { + if (dotIndex == -1) { newName = orgName + "-thumb"; } else { - String extension = orgName.subSequence( dotIndex + 1, orgName.length() ).toString(); - newName = orgName.substring( 0, dotIndex ) + "-thumb." + extension; + String extension = orgName.subSequence(dotIndex + 1, orgName.length()).toString(); + newName = orgName.substring(0, dotIndex) + "-thumb." + extension; } - return new File( attachmentsDir, newName ); + return new File(attachmentsDir, newName); } - private File writeFile( AttachmentModel attachment, MediaType mediaType ) { - String extension = getExtension( mediaType ); - File targetFile = getTargetFile( attachment.getFileName(), extension ); + private File writeFile(AttachmentModel attachment, MediaType mediaType) { + String extension = getExtension(mediaType); + File targetFile = getTargetFile(attachment.getFileName(), extension); try { - if( attachment.isBinary() ) { - if( mediaType.is( MediaType.ANY_IMAGE_TYPE ) ) { - File thumbFile = getThumbnailFileFor( targetFile ); - byte[] thumbnail = compressToThumbnail( attachment.getValue(), extension ); - Files.write( thumbnail, thumbFile ); + if (attachment.isBinary()) { + if (mediaType.is(MediaType.ANY_IMAGE_TYPE)) { + File thumbFile = getThumbnailFileFor(targetFile); + byte[] thumbnail = compressToThumbnail(attachment.getValue(), extension); + Files.write(thumbnail, thumbFile); } - Files.write( BaseEncoding.base64().decode( attachment.getValue() ), targetFile ); + Files.write(BaseEncoding.base64().decode(attachment.getValue()), targetFile); } else { Files.write(attachment.getValue().getBytes(Charsets.UTF_8), targetFile); if (attachment.getMediaType().equals(com.tngtech.jgiven.attachment.MediaType.SVG_UTF_8.toString())) { @@ -153,26 +150,26 @@ private File writeFile( AttachmentModel attachment, MediaType mediaType ) { writeThumbnailForSVG(targetFile, thumbFile); } } - } catch( IOException e ) { - log.error( "Error while trying to write attachment to file " + targetFile, e ); + } catch (IOException e) { + log.error("Error while trying to write attachment to file " + targetFile, e); } return targetFile; } - private byte[] compressToThumbnail( String base64content, String extension ) { - byte[] imageBytes = BaseEncoding.base64().decode( base64content ); + private byte[] compressToThumbnail(String base64content, String extension) { + byte[] imageBytes = BaseEncoding.base64().decode(base64content); byte[] base64thumb = {}; try { - BufferedImage before = ImageIO.read( new ByteArrayInputStream( imageBytes ) ); - BufferedImage after = scaleDown( before ); - base64thumb = bufferedImageToBase64( after, extension ); - } catch( IOException e ) { - log.error( "Error while decoding the attachment to BufferedImage ", e ); + BufferedImage before = ImageIO.read(new ByteArrayInputStream(imageBytes)); + BufferedImage after = scaleDown(before); + base64thumb = bufferedImageToBase64(after, extension); + } catch (IOException e) { + log.error("Error while decoding the attachment to BufferedImage ", e); } return base64thumb; } - static BufferedImage scaleDown( BufferedImage before ) { + static BufferedImage scaleDown(BufferedImage before) { double xFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getWidth()); double yFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getHeight()); @@ -180,24 +177,24 @@ static BufferedImage scaleDown( BufferedImage before ) { int width = (int) Math.round(before.getWidth() * factor); int height = (int) Math.round(before.getHeight() * factor); - BufferedImage after = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB ); + BufferedImage after = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); - at.scale( factor, factor ); - AffineTransformOp scaleOp = new AffineTransformOp( at, AffineTransformOp.TYPE_BILINEAR ); + at.scale(factor, factor); + AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); - return scaleOp.filter( before, after ); + return scaleOp.filter(before, after); } - private byte[] bufferedImageToBase64( BufferedImage bi, String extension ) { + private byte[] bufferedImageToBase64(BufferedImage bi, String extension) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] imageArray = {}; try { - ImageIO.write( bi, extension, baos ); + ImageIO.write(bi, extension, baos); imageArray = baos.toByteArray(); baos.close(); - } catch( IOException e ) { - log.error( "Error while decoding the compressed BufferedImage to base64 ", e ); + } catch (IOException e) { + log.error("Error while decoding the compressed BufferedImage to base64 ", e); } return imageArray; } @@ -236,10 +233,10 @@ void createSVGThumbFile(File targetFile, String base64Image, Dimension dimension } private String getXMLFormat(String base64Image, Dimension dimension) { - return ""+ - "" + - ""; + return "" + + "" + + ""; } String getPNGFromSVG(File givenSVG) { diff --git a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java index 9052c2d2ec..3139bc18db 100644 --- a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java +++ b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java @@ -4,30 +4,25 @@ import static com.tngtech.jgiven.report.html5.Html5AttachmentGenerator.scaleDown; import static org.assertj.core.api.Assertions.assertThat; +import com.google.common.io.BaseEncoding; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.jgiven.attachment.Attachment; +import com.tngtech.jgiven.attachment.MediaType; +import com.tngtech.jgiven.report.model.ReportModel; +import com.tngtech.jgiven.report.model.ScenarioCaseModel; +import com.tngtech.jgiven.report.model.ScenarioModel; +import com.tngtech.jgiven.report.model.StepModel; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URISyntaxException; import java.util.ArrayList; -import java.util.Scanner; - import javax.imageio.ImageIO; - -import com.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; import org.assertj.core.util.Lists; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; - -import com.google.common.io.BaseEncoding; -import com.tngtech.jgiven.attachment.Attachment; -import com.tngtech.jgiven.attachment.MediaType; -import com.tngtech.jgiven.report.model.ReportModel; -import com.tngtech.jgiven.report.model.ScenarioCaseModel; -import com.tngtech.jgiven.report.model.ScenarioModel; -import com.tngtech.jgiven.report.model.StepModel; -import com.tngtech.jgiven.report.model.Word; import org.junit.runner.RunWith; @RunWith(DataProviderRunner.class) @@ -35,7 +30,7 @@ public class Html5AttachmentGeneratorTest { private static final String JSON_SAMPLE = "{}"; private static final byte[] BINARY_SAMPLE = BaseEncoding.base64().decode( - "R0lGODlhFgAWAOZiAAUFBd3d3eHh4be3tzg4ODU1NaysrMHBwTs7O39/f15eXs/Pz+fn5wwMDEVFRTIyMh0dHUpKSigoKJeXl3x8fKioqOTk5NPT05qamjc3N2RkZDExMYmJiUxMTDo6Ouvr62pqanFxcW1tbfz8/CAgIM3Nze7u7vn5+eLi4m5ubjMzM52dnRcXF5ubm/Pz801NTcvLyz09PSwsLISEhJmZmT8/P+Xl5SIiIggICJGRkVxcXCUlJfDw8K2trZaWlhAQED4+Pmtra2hoaMfHx0BAQFpaWsrKynl5eVZWVq+vr6Ojo/j4+IeHh3p6ehQUFNjY2Ly8vK6ursLCwmBgYJSUlHBwcICAgIODg/Hx8WJiYouLi9/f33JycsTExC4uLvb29v///wICAi4uLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAGIALAAAAAAWABYAAAf/gGKCgwVFPgFfXwEtUwWDj4MbIQJglQxblWACKV6QggQ9YCcTDmGmYQVaX2BKHpAEMGADJKe1YRJGYAsIkVFgKwC2tgBXYAcPglyywcLCVGBNYgUCLhCnDwi1LFkNpjgoAh4KYDmnMpUJpjcWYBOnGmBBNGAEpySrYAk77GAcp04jBgTgYSsCPiyVDDAzVQLRE2ERlmRSaCtJogvCJHzIZEWYAUUfbElgUGkEOltdAlQAY83UD5JgDHTAB+IUABMDhIARcYpAwmAGwVQ45QBMFQICbHQzpQHEwgIJWoaBIgCImCP9mjUbx0TQgwNgFGitheDEgQyDiCwAQ2FsGB1fKxZ0gFQD7AUkS4lKMfbCkxgVFCh9KYEBwxATmmag9SsIgQiBiQIMCBHDUyAAOw==" ); + "R0lGODlhFgAWAOZiAAUFBd3d3eHh4be3tzg4ODU1NaysrMHBwTs7O39/f15eXs/Pz+fn5wwMDEVFRTIyMh0dHUpKSigoKJeXl3x8fKioqOTk5NPT05qamjc3N2RkZDExMYmJiUxMTDo6Ouvr62pqanFxcW1tbfz8/CAgIM3Nze7u7vn5+eLi4m5ubjMzM52dnRcXF5ubm/Pz801NTcvLyz09PSwsLISEhJmZmT8/P+Xl5SIiIggICJGRkVxcXCUlJfDw8K2trZaWlhAQED4+Pmtra2hoaMfHx0BAQFpaWsrKynl5eVZWVq+vr6Ojo/j4+IeHh3p6ehQUFNjY2Ly8vK6ursLCwmBgYJSUlHBwcICAgIODg/Hx8WJiYouLi9/f33JycsTExC4uLvb29v///wICAi4uLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAGIALAAAAAAWABYAAAf/gGKCgwVFPgFfXwEtUwWDj4MbIQJglQxblWACKV6QggQ9YCcTDmGmYQVaX2BKHpAEMGADJKe1YRJGYAsIkVFgKwC2tgBXYAcPglyywcLCVGBNYgUCLhCnDwi1LFkNpjgoAh4KYDmnMpUJpjcWYBOnGmBBNGAEpySrYAk77GAcp04jBgTgYSsCPiyVDDAzVQLRE2ERlmRSaCtJogvCJHzIZEWYAUUfbElgUGkEOltdAlQAY83UD5JgDHTAB+IUABMDhIARcYpAwmAGwVQ45QBMFQICbHQzpQHEwgIJWoaBIgCImCP9mjUbx0TQgwNgFGitheDEgQyDiCwAQ2FsGB1fKxZ0gFQD7AUkS4lKMfbCkxgVFCh9KYEBwxATmmag9SsIgQiBiQIMCBHDUyAAOw=="); @Rule public final TemporaryFolder temporaryFolderRule = new TemporaryFolder(); @@ -44,46 +39,50 @@ public class Html5AttachmentGeneratorTest { public void testFileNameGeneration() { Html5AttachmentGenerator generator = new Html5AttachmentGenerator(); - assertThat( generator.getTargetFile( "foo", "txt" ).getName() ).isEqualTo( "foo.txt" ); - assertThat( generator.getTargetFile( "foo", "txt" ).getName() ).isEqualTo( "foo2.txt" ); - assertThat( generator.getTargetFile( "foo", "png" ).getName() ).isEqualTo( "foo3.png" ); - assertThat( generator.getTargetFile( "foo4", "png" ).getName() ).isEqualTo( "foo4.png" ); - assertThat( generator.getTargetFile( "foo", "png" ).getName() ).isEqualTo( "foo5.png" ); + assertThat(generator.getTargetFile("foo", "txt").getName()).isEqualTo("foo.txt"); + assertThat(generator.getTargetFile("foo", "txt").getName()).isEqualTo("foo2.txt"); + assertThat(generator.getTargetFile("foo", "png").getName()).isEqualTo("foo3.png"); + assertThat(generator.getTargetFile("foo4", "png").getName()).isEqualTo("foo4.png"); + assertThat(generator.getTargetFile("foo", "png").getName()).isEqualTo("foo5.png"); } @Test public void testScalingOfImageToMinimumSize() throws IOException, URISyntaxException { - Attachment attachment = Attachment.fromBinaryBytes( BINARY_SAMPLE, MediaType.GIF ); - BufferedImage before = ImageIO.read( new ByteArrayInputStream( BaseEncoding.base64().decode( attachment.getContent() ) ) ); - assertThat( before.getWidth() ).isEqualTo( 22 ); - assertThat( before.getHeight() ).isEqualTo( 22 ); - - Html5AttachmentGenerator generator = new Html5AttachmentGenerator( temporaryFolderRule.getRoot() ); - StepModel stepModel = new StepModel( "test", Lists.newArrayList() ); - stepModel.addAttachment( attachment ); - generator.visit( stepModel ); - - File writtenFile = new File( temporaryFolderRule.getRoot().getPath() + "/attachment-thumb.gif" ); - Attachment writtenAttachment = Attachment.fromBinaryFile( writtenFile, MediaType.GIF ); - BufferedImage after = ImageIO.read( new ByteArrayInputStream( BaseEncoding.base64().decode( writtenAttachment.getContent() ) ) ); - assertThat( after.getWidth() ).isEqualTo(MINIMAL_THUMBNAIL_SIZE); - assertThat( after.getHeight() ).isEqualTo(MINIMAL_THUMBNAIL_SIZE); + Attachment attachment = Attachment.fromBinaryBytes(BINARY_SAMPLE, MediaType.GIF); + BufferedImage before = ImageIO.read(new ByteArrayInputStream(BaseEncoding.base64() + .decode(attachment.getContent()))); + assertThat(before.getWidth()).isEqualTo(22); + assertThat(before.getHeight()).isEqualTo(22); + + Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); + StepModel stepModel = new StepModel("test", Lists.newArrayList()); + stepModel.addAttachment(attachment); + generator.visit(stepModel); + + File writtenFile = new File(temporaryFolderRule.getRoot().getPath() + "/attachment-thumb.gif"); + Attachment writtenAttachment = Attachment.fromBinaryFile(writtenFile, MediaType.GIF); + BufferedImage after = ImageIO.read(new ByteArrayInputStream(BaseEncoding.base64() + .decode(writtenAttachment.getContent()))); + assertThat(after.getWidth()).isEqualTo(MINIMAL_THUMBNAIL_SIZE); + assertThat(after.getHeight()).isEqualTo(MINIMAL_THUMBNAIL_SIZE); } @Test public void testFindingAndGeneratingAttachmentsInAllSteps() throws IOException { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator( temporaryFolderRule.getRoot() ); + Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); File root = temporaryFolderRule.getRoot(); - generator.generateAttachments( root, generateReportModelWithAttachments() ); + generator.generateAttachments(root, generateReportModelWithAttachments()); + + File parentStepFile = new File(temporaryFolderRule.getRoot().getPath() + + "/attachments/testing/parentAttachment.gif"); + File nestedStepFile = new File(temporaryFolderRule.getRoot().getPath() + + "/attachments/testing/nestedAttachment.gif"); - File parentStepFile = new File( temporaryFolderRule.getRoot().getPath() + "/attachments/testing/parentAttachment.gif" ); - File nestedStepFile = new File( temporaryFolderRule.getRoot().getPath() + "/attachments/testing/nestedAttachment.gif" ); - - Attachment writtenParentAttachment = Attachment.fromBinaryFile( parentStepFile, MediaType.GIF ); - Attachment writtenNestedAttachment = Attachment.fromBinaryFile( nestedStepFile, MediaType.GIF ); - assertThat( writtenParentAttachment.getContent() ).isNotNull(); - assertThat( writtenNestedAttachment.getContent() ).isNotNull(); + Attachment writtenParentAttachment = Attachment.fromBinaryFile(parentStepFile, MediaType.GIF); + Attachment writtenNestedAttachment = Attachment.fromBinaryFile(nestedStepFile, MediaType.GIF); + assertThat(writtenParentAttachment.getContent()).isNotNull(); + assertThat(writtenNestedAttachment.getContent()).isNotNull(); } @@ -107,15 +106,15 @@ public void testPNGConvertor() { public void testSVGFilesHaveAGeneratedThumbnail() throws IOException { Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); File sampleSVG = new File("src/test/resources/SampleSVG.svg"); - Attachment sampleSVGAttachment= Attachment.fromTextFile(sampleSVG, MediaType.SVG_UTF_8) + Attachment sampleSVGAttachment = Attachment.fromTextFile(sampleSVG, MediaType.SVG_UTF_8) .withFileName("SampleSVG"); StepModel stepModel = new StepModel("svgTest", Lists.newArrayList()); stepModel.addAttachment(sampleSVGAttachment); generator.visit(stepModel); - File svgThumbnail = new File( temporaryFolderRule.getRoot().getPath() + - "/SampleSVG-thumb.svg" ); + File svgThumbnail = new File(temporaryFolderRule.getRoot().getPath() + + "/SampleSVG-thumb.svg"); String pngContent = generator.getPNGFromSVG(svgThumbnail); @@ -124,27 +123,29 @@ public void testSVGFilesHaveAGeneratedThumbnail() throws IOException { } private ReportModel generateReportModelWithAttachments() { - Attachment nestedAttachment = Attachment.fromBinaryBytes( BINARY_SAMPLE, MediaType.GIF ).withFileName( "nestedAttachment" ); - Attachment parentAttachment = Attachment.fromBinaryBytes( BINARY_SAMPLE, MediaType.GIF ).withFileName( "parentAttachment" ); - StepModel parentStep = new StepModel( "test", Lists.newArrayList() ); - StepModel nestedStep = new StepModel( "test", Lists.newArrayList() ); - nestedStep.addAttachment( nestedAttachment ); - parentStep.addNestedStep( nestedStep ); - parentStep.addAttachment( parentAttachment ); + Attachment nestedAttachment = Attachment.fromBinaryBytes(BINARY_SAMPLE, MediaType.GIF) + .withFileName("nestedAttachment"); + Attachment parentAttachment = Attachment.fromBinaryBytes(BINARY_SAMPLE, MediaType.GIF) + .withFileName("parentAttachment"); + StepModel parentStep = new StepModel("test", Lists.newArrayList()); + StepModel nestedStep = new StepModel("test", Lists.newArrayList()); + nestedStep.addAttachment(nestedAttachment); + parentStep.addNestedStep(nestedStep); + parentStep.addAttachment(parentAttachment); ReportModel model = new ReportModel(); ArrayList scenarios = new ArrayList(); ScenarioModel scenarioModel = new ScenarioModel(); ScenarioCaseModel scenarioCase = new ScenarioCaseModel(); - scenarioCase.addStep( parentStep ); - scenarioModel.addCase( scenarioCase ); - scenarios.add( scenarioModel ); - model.setScenarios( scenarios ); - model.setClassName( "testing" ); + scenarioCase.addStep(parentStep); + scenarioModel.addCase(scenarioCase); + scenarios.add(scenarioModel); + model.setScenarios(scenarios); + model.setClassName("testing"); return model; } @Test - @DataProvider( value = { + @DataProvider(value = { "100, 10, 100, 10", "100, 100, 20, 20", "10, 100, 10, 100", @@ -155,8 +156,8 @@ public void testScaleDown(int initialWidth, int initialHeight, int expectedWidth BufferedImage image = new BufferedImage(initialWidth, initialHeight, BufferedImage.TYPE_INT_BGR); BufferedImage thumb = scaleDown(image); - assertThat( thumb.getWidth() ).isEqualTo(expectedWidth); - assertThat( thumb.getHeight() ).isEqualTo(expectedHeight); + assertThat(thumb.getWidth()).isEqualTo(expectedWidth); + assertThat(thumb.getHeight()).isEqualTo(expectedHeight); } } \ No newline at end of file diff --git a/jgiven-html5-report/src/test/resources/SampleSVG.svg b/jgiven-html5-report/src/test/resources/SampleSVG.svg index de2a784a31..3ad933432e 100644 --- a/jgiven-html5-report/src/test/resources/SampleSVG.svg +++ b/jgiven-html5-report/src/test/resources/SampleSVG.svg @@ -1,3 +1,3 @@ - \ No newline at end of file + From b1263506a41b2e1f1be5b391fa65db41873af781 Mon Sep 17 00:00:00 2001 From: Andru Stefanescu Date: Thu, 12 Aug 2021 20:01:38 +0100 Subject: [PATCH 3/5] Solve dependency issue Signed-off-by: Andru Stefanescu --- example-projects/android/build.gradle | 4 ++++ jgiven-android-test/build.gradle | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/example-projects/android/build.gradle b/example-projects/android/build.gradle index 47f2430ca0..b6059379cb 100644 --- a/example-projects/android/build.gradle +++ b/example-projects/android/build.gradle @@ -38,6 +38,10 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + + packagingOptions { + exclude 'license/**' + } } configurations { diff --git a/jgiven-android-test/build.gradle b/jgiven-android-test/build.gradle index 5a70a67a83..fca7c809dd 100644 --- a/jgiven-android-test/build.gradle +++ b/jgiven-android-test/build.gradle @@ -37,6 +37,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + packagingOptions { + exclude 'license/**' + } } configurations { From da9e7d962a3e38cfb7a858e77ab257bd11cc78bb Mon Sep 17 00:00:00 2001 From: Andru Stefanescu Date: Fri, 13 Aug 2021 17:41:34 +0100 Subject: [PATCH 4/5] Revert android dependecy and solve minor comments Signed-off-by: Andru Stefanescu --- example-projects/android/build.gradle | 5 --- jgiven-android-test/build.gradle | 4 -- jgiven-android/build.gradle | 4 ++ .../html5/Html5AttachmentGenerator.java | 40 ++++++------------- .../html5/Html5AttachmentGeneratorTest.java | 19 ++++----- 5 files changed, 25 insertions(+), 47 deletions(-) diff --git a/example-projects/android/build.gradle b/example-projects/android/build.gradle index b6059379cb..be5a0da8a4 100644 --- a/example-projects/android/build.gradle +++ b/example-projects/android/build.gradle @@ -38,10 +38,6 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - - packagingOptions { - exclude 'license/**' - } } configurations { @@ -59,7 +55,6 @@ dependencies { exclude group: "junit" } androidTestImplementation('com.tngtech.jgiven:jgiven-android:' + version) - androidTestImplementation('com.tngtech.jgiven:jgiven-junit:' + version) jgivenReport 'com.tngtech.jgiven:jgiven-html5-report:' + version jgivenReport 'org.slf4j:slf4j-simple:1.7.32' diff --git a/jgiven-android-test/build.gradle b/jgiven-android-test/build.gradle index fca7c809dd..5a70a67a83 100644 --- a/jgiven-android-test/build.gradle +++ b/jgiven-android-test/build.gradle @@ -37,10 +37,6 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - - packagingOptions { - exclude 'license/**' - } } configurations { diff --git a/jgiven-android/build.gradle b/jgiven-android/build.gradle index ef8b924785..6d6090910f 100644 --- a/jgiven-android/build.gradle +++ b/jgiven-android/build.gradle @@ -55,6 +55,10 @@ android { } +configurations.all { + exclude group: 'xml-apis', module: 'xml-apis-ext' +} + dependencies { api "com.google.guava:guava:$guavaAndroidVersion" api("net.bytebuddy:byte-buddy-android:$byteBuddyVersion") diff --git a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java index d4aaf3c0e4..f0d67c6945 100644 --- a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java +++ b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java @@ -145,7 +145,7 @@ private File writeFile(AttachmentModel attachment, MediaType mediaType) { Files.write(BaseEncoding.base64().decode(attachment.getValue()), targetFile); } else { Files.write(attachment.getValue().getBytes(Charsets.UTF_8), targetFile); - if (attachment.getMediaType().equals(com.tngtech.jgiven.attachment.MediaType.SVG_UTF_8.toString())) { + if (com.tngtech.jgiven.attachment.MediaType.SVG_UTF_8.toString().equals(attachment.getMediaType())) { File thumbFile = getThumbnailFileFor(targetFile); writeThumbnailForSVG(targetFile, thumbFile); } @@ -208,9 +208,8 @@ private void writeThumbnailForSVG(File initialSVG, File thumbnailSVG) { } Dimension getImageDimension(byte[] givenImage) { - ByteArrayInputStream bais = new ByteArrayInputStream(givenImage); Dimension dimension = new Dimension(); - try { + try (ByteArrayInputStream bais = new ByteArrayInputStream(givenImage)) { BufferedImage image = ImageIO.read(bais); dimension.height = image.getHeight(); dimension.width = image.getWidth(); @@ -223,10 +222,8 @@ Dimension getImageDimension(byte[] givenImage) { void createSVGThumbFile(File targetFile, String base64Image, Dimension dimension) { String xmlFormat = getXMLFormat(base64Image, dimension); - try { - FileWriter fileWriter = new FileWriter(targetFile); + try (FileWriter fileWriter = new FileWriter(targetFile)) { fileWriter.write(xmlFormat); - fileWriter.close(); } catch (IOException e) { log.error("Error writing the thumbnail svg to " + targetFile); } @@ -244,32 +241,21 @@ String getPNGFromSVG(File givenSVG) { TranscoderInput transcoderInput = new TranscoderInput(); TranscoderOutput transcoderOutput = new TranscoderOutput(); - FileInputStream fis = null; - try { - fis = new FileInputStream(givenSVG); - } catch (FileNotFoundException e) { - log.error("Error while reading the initial svg file."); - return null; - } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (FileInputStream fis = new FileInputStream(givenSVG); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + transcoderInput.setInputStream(fis); + transcoderOutput.setOutputStream(baos); - transcoderInput.setInputStream(fis); - transcoderOutput.setOutputStream(baos); - - try { transcoder.transcode(transcoderInput, transcoderOutput); - } catch (TranscoderException e) { - e.printStackTrace(); - log.error("Error while transcoding the svg file to png. Is the svg formatted correctly?"); - return null; - } - - try { - fis.close(); + return BaseEncoding.base64().encode(baos.toByteArray()); + } catch (FileNotFoundException e) { + log.error("Error while reading the initial svg file."); } catch (IOException e) { log.error("Error closing the {} file", givenSVG); + } catch (TranscoderException e) { + log.error("Error while transcoding the svg file to png. Is the svg formatted correctly?"); } - return BaseEncoding.base64().encode(baos.toByteArray()); + return null; } } diff --git a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java index 3139bc18db..82ec02543f 100644 --- a/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java +++ b/jgiven-html5-report/src/test/java/com/tngtech/jgiven/report/html5/Html5AttachmentGeneratorTest.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import javax.imageio.ImageIO; import org.assertj.core.util.Lists; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -28,23 +29,25 @@ @RunWith(DataProviderRunner.class) public class Html5AttachmentGeneratorTest { - private static final String JSON_SAMPLE = "{}"; + private Html5AttachmentGenerator generator; private static final byte[] BINARY_SAMPLE = BaseEncoding.base64().decode( "R0lGODlhFgAWAOZiAAUFBd3d3eHh4be3tzg4ODU1NaysrMHBwTs7O39/f15eXs/Pz+fn5wwMDEVFRTIyMh0dHUpKSigoKJeXl3x8fKioqOTk5NPT05qamjc3N2RkZDExMYmJiUxMTDo6Ouvr62pqanFxcW1tbfz8/CAgIM3Nze7u7vn5+eLi4m5ubjMzM52dnRcXF5ubm/Pz801NTcvLyz09PSwsLISEhJmZmT8/P+Xl5SIiIggICJGRkVxcXCUlJfDw8K2trZaWlhAQED4+Pmtra2hoaMfHx0BAQFpaWsrKynl5eVZWVq+vr6Ojo/j4+IeHh3p6ehQUFNjY2Ly8vK6ursLCwmBgYJSUlHBwcICAgIODg/Hx8WJiYouLi9/f33JycsTExC4uLvb29v///wICAi4uLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAGIALAAAAAAWABYAAAf/gGKCgwVFPgFfXwEtUwWDj4MbIQJglQxblWACKV6QggQ9YCcTDmGmYQVaX2BKHpAEMGADJKe1YRJGYAsIkVFgKwC2tgBXYAcPglyywcLCVGBNYgUCLhCnDwi1LFkNpjgoAh4KYDmnMpUJpjcWYBOnGmBBNGAEpySrYAk77GAcp04jBgTgYSsCPiyVDDAzVQLRE2ERlmRSaCtJogvCJHzIZEWYAUUfbElgUGkEOltdAlQAY83UD5JgDHTAB+IUABMDhIARcYpAwmAGwVQ45QBMFQICbHQzpQHEwgIJWoaBIgCImCP9mjUbx0TQgwNgFGitheDEgQyDiCwAQ2FsGB1fKxZ0gFQD7AUkS4lKMfbCkxgVFCh9KYEBwxATmmag9SsIgQiBiQIMCBHDUyAAOw=="); @Rule public final TemporaryFolder temporaryFolderRule = new TemporaryFolder(); + @Before + public void setup() { + generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); + } + @Test public void testFileNameGeneration() { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(); - assertThat(generator.getTargetFile("foo", "txt").getName()).isEqualTo("foo.txt"); assertThat(generator.getTargetFile("foo", "txt").getName()).isEqualTo("foo2.txt"); assertThat(generator.getTargetFile("foo", "png").getName()).isEqualTo("foo3.png"); assertThat(generator.getTargetFile("foo4", "png").getName()).isEqualTo("foo4.png"); assertThat(generator.getTargetFile("foo", "png").getName()).isEqualTo("foo5.png"); - } @Test @@ -55,7 +58,6 @@ public void testScalingOfImageToMinimumSize() throws IOException, URISyntaxExcep assertThat(before.getWidth()).isEqualTo(22); assertThat(before.getHeight()).isEqualTo(22); - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); StepModel stepModel = new StepModel("test", Lists.newArrayList()); stepModel.addAttachment(attachment); generator.visit(stepModel); @@ -70,7 +72,6 @@ public void testScalingOfImageToMinimumSize() throws IOException, URISyntaxExcep @Test public void testFindingAndGeneratingAttachmentsInAllSteps() throws IOException { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); File root = temporaryFolderRule.getRoot(); generator.generateAttachments(root, generateReportModelWithAttachments()); @@ -88,13 +89,11 @@ public void testFindingAndGeneratingAttachmentsInAllSteps() throws IOException { @Test public void testGetImageDimensions() { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); assertThat(generator.getImageDimension(BINARY_SAMPLE)).isEqualTo(new Dimension(22, 22)); } @Test public void testPNGConvertor() { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); File sampleSVG = new File("src/test/resources/SampleSVG.svg"); String pngContent = generator.getPNGFromSVG(sampleSVG); @@ -104,7 +103,6 @@ public void testPNGConvertor() { @Test public void testSVGFilesHaveAGeneratedThumbnail() throws IOException { - Html5AttachmentGenerator generator = new Html5AttachmentGenerator(temporaryFolderRule.getRoot()); File sampleSVG = new File("src/test/resources/SampleSVG.svg"); Attachment sampleSVGAttachment = Attachment.fromTextFile(sampleSVG, MediaType.SVG_UTF_8) .withFileName("SampleSVG"); @@ -159,5 +157,4 @@ public void testScaleDown(int initialWidth, int initialHeight, int expectedWidth assertThat(thumb.getWidth()).isEqualTo(expectedWidth); assertThat(thumb.getHeight()).isEqualTo(expectedHeight); } - -} \ No newline at end of file +} From 2cca80c2e667e2b9c9d1db55ced93af5fc73bd66 Mon Sep 17 00:00:00 2001 From: Andru Stefanescu Date: Mon, 16 Aug 2021 15:41:32 +0100 Subject: [PATCH 5/5] Solve minor comments Signed-off-by: Andru Stefanescu --- .../report/html5/Html5AttachmentGenerator.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java index f0d67c6945..f7afdf4f2d 100644 --- a/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java +++ b/jgiven-html5-report/src/main/java/com/tngtech/jgiven/report/html5/Html5AttachmentGenerator.java @@ -209,8 +209,8 @@ private void writeThumbnailForSVG(File initialSVG, File thumbnailSVG) { Dimension getImageDimension(byte[] givenImage) { Dimension dimension = new Dimension(); - try (ByteArrayInputStream bais = new ByteArrayInputStream(givenImage)) { - BufferedImage image = ImageIO.read(bais); + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(givenImage)) { + BufferedImage image = ImageIO.read(inputStream); dimension.height = image.getHeight(); dimension.width = image.getWidth(); } catch (IOException e) { @@ -241,13 +241,13 @@ String getPNGFromSVG(File givenSVG) { TranscoderInput transcoderInput = new TranscoderInput(); TranscoderOutput transcoderOutput = new TranscoderOutput(); - try (FileInputStream fis = new FileInputStream(givenSVG); - ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - transcoderInput.setInputStream(fis); - transcoderOutput.setOutputStream(baos); + try (FileInputStream inputStream = new FileInputStream(givenSVG); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + transcoderInput.setInputStream(inputStream); + transcoderOutput.setOutputStream(outputStream); transcoder.transcode(transcoderInput, transcoderOutput); - return BaseEncoding.base64().encode(baos.toByteArray()); + return BaseEncoding.base64().encode(outputStream.toByteArray()); } catch (FileNotFoundException e) { log.error("Error while reading the initial svg file."); } catch (IOException e) {