This PR is related to BAEL-6429 (#16250)

* This commit is related to BAEL-6429

This commit aims to add a .html file named test.

* This commit is related to BAEL-6429

This commit aims to add a test class named "OpenHtmlFilesUnitTest".

* Update OpenHtmlFilesUnitTest.java

The java.awt.HeadlessException occures, indicating that the current environment lacks a graphical display necessary for the Desktop class to function properly.
So i commented the method.
This commit is contained in:
MohamedHelmyKassab 2024-03-28 07:34:21 +02:00 committed by GitHub
parent 5d38f245cd
commit 40879b5237
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package com.baeldung.openhtmlfiles;
import org.junit.Test;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import static org.junit.Assert.*;
public class OpenHtmlFilesUnitTest {
public URL url;
public String absolutePath;
public OpenHtmlFilesUnitTest() throws URISyntaxException {
url = getClass().getResource("/test.html");
assert url != null;
File file = new File(url.toURI());
if (!file.exists()) {
fail();
}
absolutePath = file.getAbsolutePath();
}
/*
@Test
public void givenHtmlFile_whenUsingDesktopClass_thenOpenFileInDefaultBrowser() throws IOException {
File htmlFile = new File(absolutePath);
Desktop.getDesktop().browse(htmlFile.toURI());
assertTrue(true);
}
*/
@Test
public void givenHtmlFile_whenUsingProcessBuilder_thenOpenFileInDefaultBrowser() throws IOException {
ProcessBuilder pb;
if (System.getProperty("os.name").toLowerCase().contains("win")) {
pb = new ProcessBuilder("cmd.exe", "/c", "start", absolutePath);
} else {
pb = new ProcessBuilder("xdg-open", absolutePath);
}
pb.start();
assertTrue(true);
}
}

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
</title>
</head>
<body>
<h1>Hello dear friend</h1>
</body>
</html>