|
| 1 | +package com.itextpdf.html2pdf.resolver.resource; |
| 2 | + |
| 3 | +import com.itextpdf.html2pdf.HtmlConverter; |
| 4 | +import com.itextpdf.styledxmlparser.LogMessageConstant; |
| 5 | +import com.itextpdf.test.ExtendedITextTest; |
| 6 | +import com.itextpdf.test.annotations.LogMessage; |
| 7 | +import com.itextpdf.test.annotations.LogMessages; |
| 8 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.experimental.categories.Category; |
| 17 | + |
| 18 | +@Category(IntegrationTest.class) |
| 19 | +public class LocalImageResolverReleaseTest extends ExtendedITextTest { |
| 20 | + |
| 21 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/resolver/resource/LocalImageResolverReleaseTest/"; |
| 22 | + public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/resolver/resource/LocalImageResolverReleaseTest/"; |
| 23 | + |
| 24 | + @BeforeClass |
| 25 | + public static void beforeClass() { |
| 26 | + createOrClearDestinationFolder(destinationFolder); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.ERROR_RESOLVING_PARENT_STYLES, count = 60)}) |
| 31 | + public void testThatSvgIsReleasedAfterConversion() throws IOException { |
| 32 | + String htmlFileName = "testWithSvg.html"; |
| 33 | + String svgFileName = "imageWithMultipleShapes.svg"; |
| 34 | + String imageFileName = "image.png"; |
| 35 | + String sourceHtmlFile = sourceFolder + htmlFileName; |
| 36 | + String sourceSvgFile = sourceFolder + svgFileName; |
| 37 | + String sourceImageFile = sourceFolder + imageFileName; |
| 38 | + |
| 39 | + String workDir = destinationFolder + "work/"; |
| 40 | + createDestinationFolder(workDir); |
| 41 | + String targetPdfFile = workDir + "target.pdf"; |
| 42 | + |
| 43 | + String workDirHtmlFile = workDir + htmlFileName; |
| 44 | + String workDirSvgFile = workDir + svgFileName; |
| 45 | + String workDirImageFile = workDir + imageFileName; |
| 46 | + Files.copy(Paths.get(sourceHtmlFile), Paths.get(workDirHtmlFile)); |
| 47 | + Files.copy(Paths.get(sourceSvgFile), Paths.get(workDirSvgFile)); |
| 48 | + Files.copy(Paths.get(sourceImageFile), Paths.get(workDirImageFile)); |
| 49 | + for (int i = 0; i < 10; i++) { |
| 50 | + HtmlConverter.convertToPdf(new File(workDirHtmlFile), new File(targetPdfFile)); |
| 51 | + } |
| 52 | + |
| 53 | + // The resource must be freed after the conversion |
| 54 | + File resourceToBeRemoved = new File(workDirSvgFile); |
| 55 | + resourceToBeRemoved.delete(); |
| 56 | + Assert.assertFalse(resourceToBeRemoved.exists()); |
| 57 | + |
| 58 | + resourceToBeRemoved = new File(workDirImageFile); |
| 59 | + resourceToBeRemoved.delete(); |
| 60 | + Assert.assertFalse(resourceToBeRemoved.exists()); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments