Skip to content

Commit

Permalink
More test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 6, 2023
1 parent 32c1339 commit b8fb26e
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class OpenPdfFontHelper {

private OpenPdfFontHelper() {}

private static int handleFontStyle( int style, int fontStyle ) {
if ( fontStyle == DocPara.STYLE_BOLD ) {
style = Font.BOLD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Iterator;

import org.fugerit.java.core.log.LogFacade;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.doc.base.model.DocElement;
import org.fugerit.java.doc.base.model.DocFooter;
import org.fugerit.java.doc.base.model.DocHeader;
Expand All @@ -19,15 +19,12 @@
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

import lombok.extern.slf4j.Slf4j;

/**
* This is the handler to use
*
* @author fugerit79
*
*/
@Slf4j
public class PdfHelper extends PdfPageEventHelper {

public PdfHelper( OpenPdfHelper docHelper ) {
Expand Down Expand Up @@ -56,8 +53,7 @@ public void onStartPage(PdfWriter writer, Document document) {
try {
OpenPpfDocHandler.handleElements( document, this.getDocHeader().docElements(), docHelper );
} catch (Exception e) {
LogFacade.getLog().error( "ITextDocHandler - PdfHelper.onStartPage : "+e );
throw new RuntimeException( e );
throw new ConfigRuntimeException( e );
}
}
}
Expand All @@ -69,7 +65,7 @@ public void onOpenDocument(PdfWriter writer, Document document) {
try {
this.baseFont = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
} catch (DocumentException | IOException e) {
log.warn( "Error : "+e, e );
throw new ConfigRuntimeException( e );
}
}

Expand Down Expand Up @@ -108,46 +104,11 @@ public void onEndPage(PdfWriter writer, Document document) {

totalOffset+= rowOffset;
} else {
throw new RuntimeException( "Element not allowed in footer (accepted only DocPara) : "+current );
throw new ConfigRuntimeException( "Element not allowed in footer (accepted only DocPara) : "+current );
}

}
cb.endText();

// while ( itElements.hasNext() ) {
// DocElement current = (DocElement)itElements.next();
// if ( current instanceof DocPara ) {
// DocPara para = (DocPara) current;
// String originalText = para.getText();
// String text = ITextDocHandler.createText( docHelper.getParams(), originalText );
// float textBase = document.bottom() - totalOffset;
// float textSize = baseFont.getWidthPoint(text, footerTextSize);
// cb.beginText();
// cb.setFontAndSize(baseFont, footerTextSize);
// if( para.getAlign() == DocPara.ALIGN_CENTER ) {
// cb.setTextMatrix((document.right() / 2), textBase);
// cb.showText(text);
// cb.endText();
// //cb.addTemplate(totalPages, (document.right() / 2) + textSize, textBase);
// } else if( para.getAlign() == DocPara.ALIGN_LEFT ) {
// cb.setTextMatrix(document.left(), textBase);
// cb.showText(text);
// cb.endText();
// //cb.addTemplate(totalPages, document.left() + textSize, textBase);
// } else {
// float adjust = baseFont.getWidthPoint("0", footerTextSize);
// cb.setTextMatrix(document.right() - textSize - adjust, textBase);
// cb.showText(text);
// cb.endText();
// //cb.addTemplate(totalPages, document.right() - adjust, textBase);
// }
//
// } else {
// throw new RuntimeException( "Element not allowed in footer (accepted only DocPara) : "+current );
// }
// }


cb.endText();
// restore writer state
cb.restoreState();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,78 @@
package test.org.fugerit.java.doc.mod.itext.poc;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.doc.mod.openpdf.HtmlTypeHandler;
import org.fugerit.java.doc.mod.openpdf.PdfTypeHandler;
import org.fugerit.java.doc.mod.openpdf.RtfTypeHandler;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPpfDocHandler;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.lowagie.text.pdf.BaseFont;

public class TestDefaultDoc extends TestDocBase {

private static final String CUSTOM_FONT = "TitilliumWeb";

@BeforeClass
public static void init() {
try {
OpenPpfDocHandler.registerFont( CUSTOM_FONT , "src/test/resources/font/TitilliumWeb-Regular.ttf");
} catch (Exception e) {
throw new ConfigRuntimeException( e );
}
}

private static final String DEFAULT_DOC = "default_doc";

private static final String DEFAULT_DOC_ALT = "default_doc_alt";

@Test
public void testOpenFailPDF() {
Assert.assertThrows( AssertionError.class , () -> this.testDocWorker( "default_doc_fail1" , PdfTypeHandler.HANDLER ) );
}

@Test
public void testCustomFont() {
BaseFont font = OpenPpfDocHandler.findFont( CUSTOM_FONT );
Assert.assertNotNull(font);
}

@Test
public void testOpenPDF() {
this.testDocWorker( "default_doc" , PdfTypeHandler.HANDLER );
boolean ok = this.testDocWorker( DEFAULT_DOC , PdfTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

@Test
public void testOpenHTML() {
this.testDocWorker( "default_doc" , HtmlTypeHandler.HANDLER );
boolean ok = this.testDocWorker( DEFAULT_DOC , HtmlTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

@Test
public void testOpenRTF() {
this.testDocWorker( "default_doc" , RtfTypeHandler.HANDLER );
boolean ok = this.testDocWorker( DEFAULT_DOC , RtfTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

@Test
public void testOpenAltPDF() {
boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , PdfTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

@Test
public void testOpenAltHTML() {
boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , HtmlTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

@Test
public void testOpenAltRTF() {
boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , RtfTypeHandler.HANDLER );
Assert.assertTrue(ok);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
@Slf4j
public class TestDocBase {

protected void testDocWorker( String testCase, DocTypeHandler handler ) {
protected boolean testDocWorker( String testCase, DocTypeHandler handler ) {
boolean ok = false;
String inputXml = "xml/"+testCase+".xml" ;
File outputFile = new File( "target", testCase+"."+handler.getType() );
log.info( "inputXml:{}, outputFile:{}", inputXml, outputFile );
try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( inputXml ) );
OutputStream os = new FileOutputStream( outputFile ) ) {
handler.handle( DocInput.newInput( handler.getType() , reader ) , DocOutput.newOutput(os) );
ok = true;
} catch (Exception e) {
String message = "Error : "+e.getMessage();
log.error( message , e );
fail( message );
}
return ok;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test.org.fugerit.java.doc.mod.itext.poc;

import org.fugerit.java.doc.mod.openpdf.helpers.PhraseParent;
import org.junit.Assert;
import org.junit.Test;

import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;

public class TestOpenPdfHelper {

@Test
public void test001() throws Exception {
PhraseParent parent = new PhraseParent( new Phrase() );
parent.add( new Paragraph() );
Assert.assertNotNull( parent );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test.org.fugerit.java.doc.mod.itext.poc;

import java.util.Properties;

import org.fugerit.java.doc.mod.openpdf.helpers.OpenPdfHelper;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestPhraseParent {

@Test
public void test001() throws Exception {
OpenPdfHelper helper = new OpenPdfHelper();
helper.setParams( new Properties() );
log.info( "helper.getDefFontStyle() -> {}", helper.getDefFontStyle() );
Assert.assertNotNull( helper.getParams() );
}

}
Binary file added src/test/resources/font/TitilliumWeb-Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file added src/test/resources/font/TitilliumWeb-Italic.ttf
Binary file not shown.
Binary file not shown.
Binary file added src/test/resources/test/img_test_teal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 19 additions & 6 deletions src/test/resources/xml/default_doc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@
<info name="doc-subject">fj doc venus sample source xml</info>
<info name="doc-author">fugerit79</info>
<info name="doc-language">en</info>
<!-- font must be loaded -->
<!--
<info name="default-font-name">TitilliumWeb</info>
-->
<!-- additional properties -->
<info name="set-total-page">true</info>
<info name="html-css-link">/css/test.css</info>
<header-ext>
<para align="center" fore-color="#eeeeee">header test</para>
</header-ext>
<footer-ext>
<para align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
<para align="left">test</para>
<para align="center">${r"${currentPage}"} / ${r"${pageCount}"}</para>
<para align="right">test</para>
</footer-ext>
</metadata>
<body>
<para>My sample title</para>
<para font-name="times-roman" style="bold">Test times roman</para>
<para font-name="courier" style="bolditalic">Courier</para>
<para font-name="symbol" style="italic">Symbol</para>
<para font-name="helvetica" style="underline">Symbol</para>
<para size="-1" fore-color="#dddddd">Test default font</para>
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
<row>
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
Expand All @@ -39,6 +47,11 @@
<cell><para><![CDATA[Oakshield]]></para></cell>
<cell><para><![CDATA[King]]></para></cell>
</row>
<row>
<cell><phrase><![CDATA[Phrase]]></phrase></cell>
<cell><phrase anchor="1"><![CDATA[Oakshield]]></phrase></cell>
<cell><phrase link="1"><![CDATA[King]]></phrase></cell>
</row>
</table>
</body>

Expand Down
61 changes: 61 additions & 0 deletions src/test/resources/xml/default_doc_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<doc
xmlns="http://javacoredoc.fugerit.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-0.xsd" >

<metadata>
<!-- Margin for document : left;right;top;bottom -->
<info name="margins">10;10;10;30</info>
<info name="excel-table-id">excel-table=print</info>
<!-- documenta meta information -->
<info name="doc-title">Basic example</info>
<info name="doc-subject">fj doc venus sample source xml</info>
<info name="doc-author">fugerit79</info>
<info name="doc-language">en</info>
<!-- font must be loaded -->
<info name="page-orient">horizontal</info>
<header>
<phrase fore-color="#222222" leading="3" align="right">header test</phrase>
</header>
<footer>
<para fore-color="#222222" leading="3" align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
</footer>
</metadata>
<body>
<image scaling="100" url="cl://test/img_test_teal.jpg"/>
<phrase leading="3">My sample title</phrase>
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
<row>
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
<cell align="center"><para style="bold">Surname</para></cell>
<cell align="center"><para style="bold">Title</para></cell>
</row>
<row>
<cell><para><![CDATA[Luthien]]></para></cell>
<cell><para><![CDATA[Tinuviel]]></para></cell>
<cell><para><![CDATA[Queen]]></para></cell>
</row>
<row>
<cell><para><![CDATA[Thorin]]></para></cell>
<cell><para><![CDATA[Oakshield]]></para></cell>
<cell><para><![CDATA[King]]></para></cell>
</row>
<row>
<cell><barcode text="0123456789123"/></cell>
<cell><image scaling="100" url="cl://test/img_test_teal.jpg"/></cell>
<cell>
<table columns="2" colwidths="50;50" width="100" id="a-table" padding="2">
<row>
<cell><para>1</para></cell>
<cell><para>2</para></cell>
</row>
</table>
</cell>
</row>
</table>
</body>

<page-break/>

</doc>
Loading

0 comments on commit b8fb26e

Please sign in to comment.