Skip to content

Commit

Permalink
update the scenario test and the junit smoke test, also include the r…
Browse files Browse the repository at this point in the history
…esources for the test.
  • Loading branch information
chappyer authored and asturio committed May 11, 2022
1 parent 32b78b1 commit 5006f28
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.lowagie.text.html;
import java.io.InputStream;

import com.lowagie.text.Document;
import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* This test class contains a series of smoke tests. The goal of these tests is not validate the generated document,
* but to ensure no exception is thrown.
*/
public class SAXmyHtmlHandlerTest {

/**
* Test scenario: a html file with a title will not generate.
*/
@Test
void testTitle_generate() {
InputStream is = SAXmyHtmlHandlerTest.class.getClassLoader().getResourceAsStream("parseTitle.html");
parseHtml(is);
}


/**
* Test scenario: a html file with a table will generate like html webpage.
*/
@Test
void testTable_generate() {
InputStream is = SAXmyHtmlHandlerTest.class.getClassLoader().getResourceAsStream("parseTable.html");
parseHtml(is);
}

/**
* Parse the input HTML file to PDF file.
* @param is The input stream of the HTML file.
*/
void parseHtml(InputStream is) {
try {
Document doc1 = new Document();
doc1.open();
HtmlParser.parse(doc1,is);
assertNotNull(doc1, () -> is + " was not parsed successfully");
} catch (Exception e) {
fail(() -> is + " resulted in " + e);
}
}
}
44 changes: 44 additions & 0 deletions openpdf/src/test/resources/parseTable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<br/>
<table>
<tr> <!--test-->
<td rowspan="4">line 1</td>
<td rowspan="4">line 2</td>
<td>line 3</td>
<td>line 4</td>
<td>line 5</td>
<td>line 6</td>
<td>line 7</td>
<td>line 8</td>
</tr>
<tr>
<td>line 3</td>
<td>line 4</td>
<td>line 5</td>
<td>line 6</td>
<td>line 7</td>
<td>line 8</td>
</tr>
<tr>
<td>line 3</td>
<td>line 4</td>
<td>line 5</td>
<td>line 6</td>
<td>line 7</td>
<td>line 8</td>
</tr>
<tr>
<td>line 3</td>
<td>line 4</td>
<td>line 5</td>
<td>line 6</td>
<td>line 7</td>
<td>line 8</td>
</tr>
</table>
</body>
</html>
11 changes: 11 additions & 0 deletions openpdf/src/test/resources/parseTitle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<p>This is a test</p>
<p>This is a test2</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $
*
* This code is part of the 'OpenPDF Tutorial'.
* You can find the complete tutorial at the following address:
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial
*
* This code 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.
*
*
*/

package com.lowagie.examples.html;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Generates a simple table HTML page.
*
* @author chappyer
*/

public class ParseTableHtml {

/**
* Generates an HTML page with a table
*
* @param args no arguments needed here
*/
public static void main(String[] args) {
System.out.println("Parse ParseTable");

// step 1: creation of a document-object
try (Document document = new Document()) {
PdfWriter.getInstance(document, Files.newOutputStream(Paths.get("parseTable.pdf")));
// step 2: we open the document
document.open();
// step 3: parsing the HTML document to convert it in PDF
HtmlParser.parse(document, ParseHelloHtml.class.getClassLoader().getResourceAsStream("com/lowagie/examples/html/parseTable.html"));
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $
*
* This code is part of the 'OpenPDF Tutorial'.
* You can find the complete tutorial at the following address:
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial
*
* This code 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.
*
*
*/

package com.lowagie.examples.html;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Generates a simple HTML page which contains a title.
*
* @author chappyer
*/

public class ParseTitleHtml {

/**
* Generates an HTML page which contains a title
*
* @param args no arguments needed here
*/
public static void main(String[] args) {

// step 1: creation of a document-object
try (Document document = new Document()) {
PdfWriter.getInstance(document, Files.newOutputStream(Paths.get("parseTitle.pdf")));
// step 2: we open the document
document.open();
// step 3: parsing the HTML document to convert it in PDF
HtmlParser.parse(document, ParseHelloHtml.class.getClassLoader().getResourceAsStream("com/lowagie/examples/html/parseTitle.html"));
} catch (DocumentException | IOException de) {
de.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<table>
<tr>
<td>line 3</td>
<td>line 4</td>
<td>line 4</td>
<td>line 4</td>
<td>line 4</td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<p>This is a test</p>
<p>This is a test2</p>
</body>
</html>

0 comments on commit 5006f28

Please sign in to comment.