diff --git a/pom.xml b/pom.xml index fd51b6c..1d0019d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.moacirrf netbeans-markdown - 3.2 + 4.0 nbm Netbeans Markdown https://github.com/moacirrf/netbeans-markdown @@ -22,7 +22,7 @@ scm:git:https://github.com/moacirrf/netbeans-markdown scm:git:https://github.com/moacirrf/netbeans-markdown https://github.com/moacirrf/netbeans-markdown/tree/${project.scm.tag} - v3.1 + v4.0 diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ExportAction.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ExportAction.java index e3da26c..804b4f2 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ExportAction.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ExportAction.java @@ -43,10 +43,9 @@ @ActionRegistration( displayName = "#CTL_ExportAction" ) -// path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions", @ActionReferences(value = { - @ActionReference(path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions", name = "Markdown Exporters", position = 1425), //@ActionReference(path = "Editors/Popup/"+MarkdownDataObject.MIME_TYPE+"/Actions", position = 1425), -// @ActionReference(path = "UI/ToolActions/"+MarkdownDataObject.MIME_TYPE+"/Actions", position = 2950) + @ActionReference(path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions", name = "Markdown Exporters", position = 1425), + @ActionReference(path = "Editors/" + MarkdownDataObject.MIME_TYPE + "/Popup", position = 9999) }) @Messages("CTL_ExportAction=Export to...") public final class ExportAction implements ActionListener { @@ -68,8 +67,8 @@ public void actionPerformed(ActionEvent ev) { JOptionPane.showMessageDialog(null, MESSAGE_SUCCESS); } }); - - var dd = new DialogDescriptor(pane, TITLE_SELECT_DESTINY, true, null); + + var dd = new DialogDescriptor(pane, TITLE_SELECT_DESTINY, true, null); dd.setClosingOptions(new String[0]); dd.setOptions(new String[0]); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ImageHelper.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ImageHelper.java index a6da0a8..586855b 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ImageHelper.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ImageHelper.java @@ -157,7 +157,7 @@ public static URL downloadImage(URL url) { } catch (URISyntaxException | InterruptedException | IOException ex) { Exceptions.printStackTrace(ex); - if (ex instanceof InterruptedException) { + if (ex instanceof InterruptedException && Thread.currentThread() != null) { Thread.currentThread().interrupt(); } } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java b/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java index c9882c0..12f1b2a 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java @@ -65,7 +65,7 @@ separatorAfter = 500 ), @ActionReference( - path = "Loaders/text/x-markdown/Actions", + path = "Loaders/"+MarkdownDataObject.MIME_TYPE+"/Actions", id = @ActionID(category = "Edit", id = "org.openide.actions.DeleteAction"), position = 600 ), @@ -102,10 +102,11 @@ public class MarkdownDataObject extends MultiDataObject { public static final String MIME_TYPE = "text/x-markdown-nb"; + public static final String MIME_TYPE_OTHER = "text/x-markdown"; public MarkdownDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException { super(pf, loader); - registerEditor("text/x-markdown", true); + registerEditor(MIME_TYPE_OTHER, true); } @Override diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/TempDir.java b/src/main/java/io/github/moacirrf/netbeans/markdown/TempDir.java index 411b0d9..f445e1c 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/TempDir.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/TempDir.java @@ -35,7 +35,7 @@ public final class TempDir { private static Path NOT_LOADED_IMAGE; - public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown"; + private static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown"; public static Path getTempDir() { Path path = Paths.get(TEMP_DIR_PLUGIN); @@ -62,7 +62,7 @@ public static Path getCantLoadImage() { private static void createCantLoadImage() { try (var inputStream = Icons.class.getResourceAsStream(NB_MARKDOWN_NOT_LOAD_IMAGE)) { byte[] bytes = inputStream.readAllBytes(); - NOT_LOADED_IMAGE = Path.of(TEMP_DIR_PLUGIN, NB_MARKDOWN_NOT_LOAD_IMAGE); + NOT_LOADED_IMAGE = Path.of(getTempDir().toAbsolutePath().toString(), NB_MARKDOWN_NOT_LOAD_IMAGE); Files.write(NOT_LOADED_IMAGE, bytes, CREATE, TRUNCATE_EXISTING); } catch (IOException ex) { Exceptions.printStackTrace(ex); diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/ImageHelperTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/ImageHelperTest.java index f450763..89645db 100644 --- a/src/test/java/io/github/moacirrf/netbeans/markdown/ImageHelperTest.java +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/ImageHelperTest.java @@ -26,10 +26,14 @@ import java.nio.file.Path; import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; import static java.nio.file.StandardOpenOption.WRITE; +import org.junit.After; import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; +import org.mockito.MockedStatic; +import static org.mockito.Mockito.mockStatic; import org.openide.util.Exceptions; /** @@ -44,6 +48,18 @@ public class ImageHelperTest { private static final String PNG_HTTP_URL = "https://raw.githubusercontent.com/moacirrf/netbeans-markdown/main/src/main/resources/io/github/moacirrf/netbeans/markdown/code_template.png"; + private MockedStatic mockedStatic; + + @Before + public void setup() { + mockedStatic = mockStatic(TempDir.class); + } + + @After + public void tearDown() { + mockedStatic.close(); + } + @Rule public TemporaryFolder folder = new TemporaryFolder(); @@ -60,7 +76,9 @@ public void testIsHttpUrl() throws MalformedURLException { @Test public void testDownloadImage() throws MalformedURLException, URISyntaxException { - + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + URL url = ImageHelper.downloadImage(new URL(SVG_HTTP_URL)); var path = Path.of(url.toURI()); @@ -68,10 +86,14 @@ public void testDownloadImage() throws MalformedURLException, URISyntaxException assertTrue(Files.exists(path)); path.toFile().delete(); + TempDirTest.removeTempDir(); } @Test public void testConvertSVGToPNG() throws MalformedURLException, URISyntaxException { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + URL url = ImageHelper.downloadImage(new URL(SVG_HTTP_URL)); URL urlPng = ImageHelper.convertSVGToPNG(url); @@ -82,13 +104,14 @@ public void testConvertSVGToPNG() throws MalformedURLException, URISyntaxExcepti assertTrue(file.exists()); file.delete(); + TempDirTest.removeTempDir(); } - + @Test - public void testGetImageType(){ + public void testGetImageType() { try { - assertEquals(ImageHelper.getImageType(URI.create(SVG_HTTP_URL).toURL()), "svg"); ; - assertEquals(ImageHelper.getImageType(URI.create(PNG_HTTP_URL).toURL()), "png"); ; + assertEquals(ImageHelper.getImageType(URI.create(SVG_HTTP_URL).toURL()), "svg");; + assertEquals(ImageHelper.getImageType(URI.create(PNG_HTTP_URL).toURL()), "png");; } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); } @@ -96,6 +119,8 @@ public void testGetImageType(){ @Test public void testFileExistsByHash() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); try { Path path = folder.newFile("teste.png").toPath(); byte[] bytes = URI.create(PNG_HTTP_URL).toURL().openStream().readAllBytes(); @@ -105,6 +130,7 @@ public void testFileExistsByHash() { Files.delete(path); folder.delete(); + TempDirTest.removeTempDir(); } catch (IOException ex) { Exceptions.printStackTrace(ex); } diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/TempDirTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/TempDirTest.java new file mode 100644 index 0000000..37a678f --- /dev/null +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/TempDirTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2024 Moacir da Roza Flores + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.moacirrf.netbeans.markdown; + +import java.io.IOException; +import static java.lang.System.getProperty; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import org.openide.util.Exceptions; + +/** + * + * @author Moacir da Roza Flores + */ +public final class TempDirTest { + + private static final String NB_MARKDOWN_NOT_LOAD_IMAGE = "nb_markdown_not_load_image.png"; + + private static Path NOT_LOADED_IMAGE; + + public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown_teste"; + + public static Path getTempDir() { + Path path = Paths.get(TEMP_DIR_PLUGIN); + if (!Files.exists(path)) { + try { + Files.createDirectory(path); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + createCantLoadImage(); + } + return path; + } + + public static Path getCantLoadImage() { + if (Files.exists(getTempDir())) { + if (NOT_LOADED_IMAGE == null) { + createCantLoadImage(); + } + } + return NOT_LOADED_IMAGE; + } + + private static void createCantLoadImage() { + try (var inputStream = Icons.class.getResourceAsStream(NB_MARKDOWN_NOT_LOAD_IMAGE)) { + byte[] bytes = inputStream.readAllBytes(); + NOT_LOADED_IMAGE = Path.of(TEMP_DIR_PLUGIN, NB_MARKDOWN_NOT_LOAD_IMAGE); + Files.write(NOT_LOADED_IMAGE, bytes, CREATE, TRUNCATE_EXISTING); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + + public static void removeTempDir() { + clearTempFolder(getTempDir()); + } + + private static void clearTempFolder(Path path) { + try { + if (Files.isDirectory(path) && Files.list(path).count() > 0) { + Files.list(path).forEach(it -> clearTempFolder(it)); + } + path.toFile().setWritable(true); + Files.deleteIfExists(path); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + + private TempDirTest() { + } +} diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/export/DocxExporterTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/export/DocxExporterTest.java index a4c0d93..d94de6b 100644 --- a/src/test/java/io/github/moacirrf/netbeans/markdown/export/DocxExporterTest.java +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/export/DocxExporterTest.java @@ -16,15 +16,21 @@ */ package io.github.moacirrf.netbeans.markdown.export; +import io.github.moacirrf.netbeans.markdown.TempDir; +import io.github.moacirrf.netbeans.markdown.TempDirTest; import java.io.File; import java.net.URISyntaxException; import java.nio.file.Path; import static java.util.Arrays.asList; import java.util.List; +import org.junit.After; import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; +import org.mockito.MockedStatic; +import static org.mockito.Mockito.mockStatic; import org.openide.util.Exceptions; /** @@ -36,8 +42,22 @@ public class DocxExporterTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); + private MockedStatic mockedStatic; + + @Before + public void setup() { + mockedStatic = mockStatic(TempDir.class); + } + + @After + public void tearDown() { + mockedStatic.close(); + } + //@Test public void testExportJoinMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); @@ -47,12 +67,16 @@ public void testExportJoinMds() { List files = exporter.export(exporterConfig); assertFalse("No one docx was generated", files.isEmpty()); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); } @Test public void testExportSepratedMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); - var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1) , InputModel.from(getMdfile("test_2.md"), 0)); + var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); var exporterConfig = ExporterConfig.newSeparatedFile(folder.getRoot(), mdFiles); var exporter = new DocxExporter(); @@ -61,9 +85,15 @@ public void testExportSepratedMds() { assertFalse("No one docx was generated", files.isEmpty()); assertTrue("Must have two docx", files.size() == 2); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); + } public File getMdfile(String name) { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + try { var file = Path.of(DocxExporterTest.class.getResource(name).toURI()).toFile(); assertTrue("Md file font exists", file.exists()); diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/export/HTMLExporterTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/export/HTMLExporterTest.java index c5e96cb..4a7fa68 100644 --- a/src/test/java/io/github/moacirrf/netbeans/markdown/export/HTMLExporterTest.java +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/export/HTMLExporterTest.java @@ -16,15 +16,21 @@ */ package io.github.moacirrf.netbeans.markdown.export; +import io.github.moacirrf.netbeans.markdown.TempDir; +import io.github.moacirrf.netbeans.markdown.TempDirTest; import java.io.File; import java.net.URISyntaxException; import java.nio.file.Path; import static java.util.Arrays.asList; import java.util.List; +import org.junit.After; import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; +import org.mockito.MockedStatic; +import static org.mockito.Mockito.mockStatic; import org.openide.util.Exceptions; /** @@ -36,8 +42,23 @@ public class HTMLExporterTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); + private MockedStatic mockedStatic; + + @Before + public void setup() { + mockedStatic = mockStatic(TempDir.class); + } + + @After + public void tearDown() { + mockedStatic.close(); + } + @Test public void testExportJoinMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); var exporterConfig = ExporterConfig.newUniqueFile(folder.getRoot(), mdFiles, "output"); @@ -45,10 +66,14 @@ public void testExportJoinMds() { List files = exporter.export(exporterConfig); assertFalse("No one html was generated", files.isEmpty()); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); } @Test public void testExportSepratedMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); @@ -59,6 +84,8 @@ public void testExportSepratedMds() { assertFalse("No one html was generated", files.isEmpty()); assertTrue("Must have two html", files.size() == 2); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); } public File getMdfile(String name) { diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/export/PDFExporterTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/export/PDFExporterTest.java index 08182e8..38c60bb 100644 --- a/src/test/java/io/github/moacirrf/netbeans/markdown/export/PDFExporterTest.java +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/export/PDFExporterTest.java @@ -16,15 +16,21 @@ */ package io.github.moacirrf.netbeans.markdown.export; +import io.github.moacirrf.netbeans.markdown.TempDir; +import io.github.moacirrf.netbeans.markdown.TempDirTest; import java.io.File; import java.net.URISyntaxException; import java.nio.file.Path; import static java.util.Arrays.asList; import java.util.List; +import org.junit.After; import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; +import org.mockito.MockedStatic; +import static org.mockito.Mockito.mockStatic; import org.openide.util.Exceptions; /** @@ -36,8 +42,23 @@ public class PDFExporterTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); + private MockedStatic mockedStatic; + + @Before + public void setup() { + mockedStatic = mockStatic(TempDir.class); + } + + @After + public void tearDown() { + mockedStatic.close(); + } + @Test public void testExportJoinMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); var exporterConfig = ExporterConfig.newUniqueFile(folder.getRoot(), mdFiles, "output"); @@ -45,10 +66,14 @@ public void testExportJoinMds() { List files = exporter.export(exporterConfig); assertFalse("No one pdf was generated", files.isEmpty()); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); } @Test public void testExportSepratedMds() { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0)); @@ -59,6 +84,8 @@ public void testExportSepratedMds() { assertFalse("No one pdf was generated", files.isEmpty()); assertTrue("Must have two pdf", files.size() == 2); files.forEach(f -> assertTrue("File not found", f.exists())); + + TempDirTest.removeTempDir(); } public File getMdfile(String name) { diff --git a/src/test/java/io/github/moacirrf/netbeans/markdown/html/ImageTest.java b/src/test/java/io/github/moacirrf/netbeans/markdown/html/ImageTest.java index 52969d3..ceddc68 100644 --- a/src/test/java/io/github/moacirrf/netbeans/markdown/html/ImageTest.java +++ b/src/test/java/io/github/moacirrf/netbeans/markdown/html/ImageTest.java @@ -17,18 +17,18 @@ package io.github.moacirrf.netbeans.markdown.html; import io.github.moacirrf.netbeans.markdown.TempDir; +import io.github.moacirrf.netbeans.markdown.TempDirTest; import static java.io.File.separator; import java.io.IOException; -import static java.lang.System.getProperty; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import javax.swing.KeyStroke; +import org.junit.After; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import org.junit.Before; import org.junit.Test; import org.mockito.MockedStatic; import static org.mockito.Mockito.mockStatic; -import org.openide.util.Exceptions; +import org.openide.util.Utilities; /** * @@ -36,114 +36,119 @@ */ public class ImageTest { - public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown_teste"; - private HtmlBuilder htmlBuilder = HtmlBuilder.getInstance(); - public static Path getTempDirTeste() { - Path path = Paths.get(TEMP_DIR_PLUGIN); - if (!Files.exists(path)) { - try { - Files.createDirectory(path); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - return path; + private MockedStatic mockedStatic; + + @Before + public void setup() { + mockedStatic = mockStatic(TempDir.class); + } + + @After + public void tearDown() { + mockedStatic.close(); } @Test - public void testResizableImage() { - try (MockedStatic mockedStatic = mockStatic(TempDir.class)) { - mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste()); + public void testResizableImage() throws IOException { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + + var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; + var html = "\n" + + " \n" + + " \n" + + " \n" + + ""; + + var expected = String.format(html, srcExpected); - var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; - var html = "\n" - + " \n" - + " \n" - + " \n" - + ""; + var given = ""; - var expected = String.format(html, srcExpected); + var result = htmlBuilder.build(given); - var given = ""; + assertNotNull(result); + assertEquals(expected, result); - var result = htmlBuilder.build(given); + TempDirTest.removeTempDir(); - assertNotNull(result); - assertEquals(expected, result); - } } @Test - public void testImage() { - try (MockedStatic mockedStatic = mockStatic(TempDir.class)) { - mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste()); + public void testImage() throws IOException { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); - var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; + var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; - String html = "\n" - + " \n" - + "

\"Tux,

\n" - + " \n" - + ""; + String html = "\n" + + " \n" + + "

\"Tux,

\n" + + " \n" + + ""; - var expected = String.format(html, srcExpected); + var expected = String.format(html, srcExpected); - var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n"; + var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n"; - var result = htmlBuilder.build(given); + var result = htmlBuilder.build(given); + + assertNotNull(result); + assertEquals(expected, result); + + TempDirTest.removeTempDir(); - assertNotNull(result); - assertEquals(expected, result); - } } @Test - public void testResizableImageWithParameters() { - try (MockedStatic mockedStatic = mockStatic(TempDir.class)) { - mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste()); + public void testResizableImageWithParameters() throws IOException { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir(), TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + + var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; + var html = "\n" + + " \n" + + " \n" + + " \n" + + ""; - var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; - var html = "\n" - + " \n" - + " \n" - + " \n" - + ""; + var expected = String.format(html, srcExpected); - var expected = String.format(html, srcExpected); + var given = ""; - var given = ""; + var result = htmlBuilder.build(given); - var result = htmlBuilder.build(given); + assertNotNull(result); + assertEquals(expected, result); + + TempDirTest.removeTempDir(); - assertNotNull(result); - assertEquals(expected, result); - } } @Test - public void testImageWithParameters() { - try (MockedStatic mockedStatic = mockStatic(TempDir.class)) { - mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste()); + public void testImageWithParameters() throws IOException { + mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir(), TempDirTest.getTempDir()); + mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage()); + + var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; - var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png"; + String html = "\n" + + " \n" + + "

\"Tux,

\n" + + " \n" + + ""; - String html = "\n" - + " \n" - + "

\"Tux,

\n" - + " \n" - + ""; + var expected = String.format(html, srcExpected); - var expected = String.format(html, srcExpected); + var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png?raw=true \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n"; - var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png?raw=true \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n"; + var result = htmlBuilder.build(given); - var result = htmlBuilder.build(given); + assertNotNull(result); + assertEquals(expected, result); - assertNotNull(result); - assertEquals(expected, result); - } + TempDirTest.removeTempDir(); } }