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 2e649a1
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package org.fugerit.java.doc.mod.openpdf.helpers;

import java.awt.Color;
import java.io.IOException;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.doc.base.model.DocPara;
import org.fugerit.java.doc.base.xml.DocModelUtils;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;

public class OpenPdfFontHelper {

private OpenPdfFontHelper() {}

public static BaseFont createBaseFontSafe( String name, String encoding, boolean embedded) {
BaseFont result = null;
try {
result = BaseFont.createFont(name, encoding, embedded);
} catch (DocumentException | IOException e) {
throw new ConfigRuntimeException( e );
}
return result;
}

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 @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Properties;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.log.LogFacade;
import org.fugerit.java.core.util.regex.ParamFinder;
Expand Down Expand Up @@ -398,6 +399,14 @@ public void handleDoc(DocBase docBase) throws Exception {
this.document.close();
}

public static void handleElementsSafe( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) {
try {
handleElements(document, itDoc, docHelper);
} catch (Exception e) {
throw new ConfigRuntimeException( e );
}
}

public static void handleElements( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) throws Exception {
while ( itDoc.hasNext() ) {
DocElement docElement = (DocElement)itDoc.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.fugerit.java.doc.mod.openpdf.helpers;

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;
import org.fugerit.java.doc.base.model.DocPara;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
Expand All @@ -19,15 +17,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 All @@ -53,24 +48,14 @@ public void onStartPage(PdfWriter writer, Document document) {
this.currentPageNumber = writer.getPageNumber();
this.docHelper.getParams().setProperty( OpenPpfDocHandler.PARAM_PAGE_CURRENT , String.valueOf( writer.getPageNumber() ) );
if ( this.getDocHeader() != null ) {
try {
OpenPpfDocHandler.handleElements( document, this.getDocHeader().docElements(), docHelper );
} catch (Exception e) {
LogFacade.getLog().error( "ITextDocHandler - PdfHelper.onStartPage : "+e );
throw new RuntimeException( e );
}
OpenPpfDocHandler.handleElementsSafe( document, this.getDocHeader().docElements(), docHelper );
}
}

public void onOpenDocument(PdfWriter writer, Document document) {
totalPages = writer.getDirectContent().createTemplate(100, 100);
totalPages.setBoundingBox( new Rectangle(-20, -20, 100, 100) );
//this.baseFont = ITextDocHandler.findFont( this.docHelper.getDefFontName() );
try {
this.baseFont = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
} catch (DocumentException | IOException e) {
log.warn( "Error : "+e, e );
}
this.baseFont = OpenPdfFontHelper.createBaseFontSafe(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
}

public void onEndPage(PdfWriter writer, Document document) {
Expand Down Expand Up @@ -108,46 +93,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,40 @@
package test.org.fugerit.java.doc.mod.itext.poc;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPdfFontHelper;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPpfDocHandler;
import org.fugerit.java.doc.mod.openpdf.helpers.PdfHelper;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestPdfHelper {

@Test
public void testSetGet() {
PdfHelper helper = new PdfHelper( null );
log.info( "getCurrentPageNumber -> {}", helper.getCurrentPageNumber() );
int current = 10;
helper.setPageNumberAlignment(current);
log.info( "getPageNumberAlignment -> {}", helper.getPageNumberAlignment() );
Assert.assertEquals( current , helper.getPageNumberAlignment() );
}


@Test
public void testTandleElementSafe() {
Assert.assertThrows( ConfigRuntimeException.class , () -> OpenPpfDocHandler.handleElementsSafe(null, null, null) );
}

@Test
public void testCreateFontSafe() {
Assert.assertThrows( RuntimeException.class , () -> OpenPdfFontHelper.createBaseFontSafe(null, null, false) );
}

@Test
public void testCreateFontSafe1() {
Assert.assertThrows( ConfigRuntimeException.class , () -> OpenPdfFontHelper.createBaseFontSafe( "no", "no", false) );
}
}
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
Loading

0 comments on commit 2e649a1

Please sign in to comment.