Skip to content

Commit

Permalink
Revert android dependecy and solve minor comments
Browse files Browse the repository at this point in the history
Signed-off-by: Andru Stefanescu <als209@cam.ac.uk>
  • Loading branch information
andru47 committed Aug 13, 2021
1 parent b126350 commit deb9d25
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 46 deletions.
4 changes: 0 additions & 4 deletions example-projects/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
exclude 'license/**'
}
}

configurations {
Expand Down
4 changes: 0 additions & 4 deletions jgiven-android-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

packagingOptions {
exclude 'license/**'
}
}

configurations {
Expand Down
4 changes: 4 additions & 0 deletions jgiven-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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());

Expand All @@ -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);

Expand All @@ -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");
Expand Down Expand Up @@ -159,5 +157,4 @@ public void testScaleDown(int initialWidth, int initialHeight, int expectedWidth
assertThat(thumb.getWidth()).isEqualTo(expectedWidth);
assertThat(thumb.getHeight()).isEqualTo(expectedHeight);
}

}
}

0 comments on commit deb9d25

Please sign in to comment.