From 969b583001570730ab94c9ff9626f7787fe5ac7b Mon Sep 17 00:00:00 2001 From: jomu Date: Mon, 30 May 2016 14:40:42 +0000 Subject: [PATCH] added first support for lists --- pdf/pom.xml | 23 +---- .../shared/pdf/DefaultTableRow.java | 71 ++++++++++++++ .../de/muehlencord/shared/pdf/GsonUtil.java | 3 +- .../muehlencord/shared/pdf/ListTemplate.java | 17 ++++ .../muehlencord/shared/pdf/PDFDocument.java | 93 +++++++++++++++++-- .../de/muehlencord/shared/pdf/PDFElement.java | 9 ++ .../muehlencord/shared/pdf/PDFTemplate.java | 24 ++--- .../muehlencord/shared/pdf/TableContent.java | 60 +++++++----- .../de/muehlencord/shared/pdf/TableRow.java | 21 +++++ .../shared/pdf/TemplateException.java | 25 +++++ .../java/de/muehlencord/shared/pdf/Text.java | 2 +- .../shared/pdf/DefaultTableRowTest.java | 35 +++++++ .../de/muehlencord/shared/pdf/Invoice.java | 25 +++++ .../muehlencord/shared/pdf/InvoiceLine.java | 58 ++++++++++++ .../shared/pdf/PDFDocumentTest.java | 35 ++++--- .../de/muehlencord/shared/pdf/TextTest.java | 27 ++++++ pdf/src/test/resources/log4j.xml | 6 +- 17 files changed, 450 insertions(+), 84 deletions(-) create mode 100644 pdf/src/main/java/de/muehlencord/shared/pdf/DefaultTableRow.java create mode 100644 pdf/src/main/java/de/muehlencord/shared/pdf/ListTemplate.java create mode 100644 pdf/src/main/java/de/muehlencord/shared/pdf/PDFElement.java create mode 100644 pdf/src/main/java/de/muehlencord/shared/pdf/TableRow.java create mode 100644 pdf/src/main/java/de/muehlencord/shared/pdf/TemplateException.java create mode 100644 pdf/src/test/java/de/muehlencord/shared/pdf/DefaultTableRowTest.java create mode 100644 pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java create mode 100644 pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java create mode 100644 pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java diff --git a/pdf/pom.xml b/pdf/pom.xml index 60cc1ef..7204733 100644 --- a/pdf/pom.xml +++ b/pdf/pom.xml @@ -19,7 +19,6 @@ junit junit - 4.12 test @@ -45,27 +44,13 @@ slf4j-log4j12 test - - org.seleniumhq.selenium - selenium-java - test - 2.44.0 - - - com.opera - operadriver - test - 1.5 - - - org.seleniumhq.selenium - selenium-remote-driver - - - commons-io commons-io + + org.apache.commons + commons-lang3 + \ No newline at end of file diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/DefaultTableRow.java b/pdf/src/main/java/de/muehlencord/shared/pdf/DefaultTableRow.java new file mode 100644 index 0000000..e65c1c0 --- /dev/null +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/DefaultTableRow.java @@ -0,0 +1,71 @@ +package de.muehlencord.shared.pdf; + +import com.google.gson.annotations.Expose; +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author joern.muehlencord + */ +public class DefaultTableRow extends TableRow { + + @Expose + private final List row; + + @Expose + private Boolean isList; + + @Expose + private String listName; + + @Expose + private String varName; + + public DefaultTableRow() { + this.row = new ArrayList<>(); + this.isList = false; + this.listName = null; + this.varName = null; + } + + + public void add(Text text) { + row.add(text); + } + + /* *** TableRow methods *** */ + @Override + public int getColumnCount() { + return row.size(); + } + + @Override + public Text getColumnValue(int columnPos) { + return row.get(columnPos); + } + + @Override + public boolean isList() { + return isList; + } + + @Override + public void createList(String listName, String varName) { + this.listName = listName; + this.varName = varName; + this.isList = true; + } + + @Override + public String getListName() { + return listName; + } + + @Override + public String getVarName() { + return varName; + } + + +} diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/GsonUtil.java b/pdf/src/main/java/de/muehlencord/shared/pdf/GsonUtil.java index c570340..092e20a 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/GsonUtil.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/GsonUtil.java @@ -9,11 +9,12 @@ import com.google.gson.GsonBuilder; */ public class GsonUtil { - public final static Gson getGsonInstance() { + protected final static Gson getGsonInstance() { return new GsonBuilder() .setPrettyPrinting() .excludeFieldsWithoutExposeAnnotation() .registerTypeAdapter(Content.class, new InterfaceAdapter<>()) + .registerTypeAdapter(TableRow.class, new InterfaceAdapter<>()) .create(); } diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/ListTemplate.java b/pdf/src/main/java/de/muehlencord/shared/pdf/ListTemplate.java new file mode 100644 index 0000000..f3d3cc6 --- /dev/null +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/ListTemplate.java @@ -0,0 +1,17 @@ +package de.muehlencord.shared.pdf; + +/** + * + * @author joern.muehlencord + */ +public interface ListTemplate { + + public void createList (String listName, String varName); + + public boolean isList(); + + public String getListName(); + + public String getVarName(); + +} diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFDocument.java b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFDocument.java index d4e41af..706597f 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFDocument.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFDocument.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import org.apache.commons.lang3.text.StrBuilder; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; @@ -32,33 +34,110 @@ public class PDFDocument { this.fontMap = null; } + public String toJson() { + return GsonUtil.getGsonInstance().toJson(this); + } + + public String fromJson() { + return GsonUtil.getGsonInstance().toJson(this); + } + + public String getTemplateString() throws TemplateException { + + ConcurrentLinkedDeque bracketStack = new ConcurrentLinkedDeque<>(); + int currentPosInTemplate = 0; + int currentStartOfSubelement = 0; + String templateString = GsonUtil.getGsonInstance().toJson(this); + String typeSearchString = " \"type\": \"de.muehlencord.shared.pdf.DefaultTableRow\""; + while (templateString.indexOf(typeSearchString, currentPosInTemplate) > 0) { + // get next element + currentPosInTemplate = templateString.indexOf(typeSearchString, currentPosInTemplate); + int posOpenBracket = templateString.substring(currentStartOfSubelement, currentPosInTemplate).lastIndexOf("{") + currentStartOfSubelement; + int posCloseBracket; + // store position of 1st open bracket + bracketStack.push(posOpenBracket); + + // work until closing bracket for this element + while ((!bracketStack.isEmpty()) && (currentPosInTemplate < templateString.length())) { + currentPosInTemplate += 1; + char currentChar = templateString.charAt(currentPosInTemplate); + if (currentChar == '{') { + // new open bracket found + posOpenBracket = currentPosInTemplate; + bracketStack.push(posOpenBracket); + } else if (currentChar == '}') { + if (bracketStack.isEmpty()) { + throw new TemplateException("Found closing bracket, but missing open bracket"); + } + // new open bracket found + posCloseBracket = currentPosInTemplate; + posOpenBracket = bracketStack.pop(); + if (bracketStack.isEmpty()) { + // next element starts behing the closing bracket earliests + currentStartOfSubelement = posCloseBracket + 1; + + String jsonSubString = templateString.substring(posOpenBracket, posCloseBracket + 1); + System.out.println(jsonSubString); + + // insert the list values into the gson string + TableRow element = GsonUtil.getGsonInstance().fromJson(jsonSubString, TableRow.class); + if (element.isList()) { + String listStartString = "<#list "; + listStartString += element.getListName(); + listStartString += " as "; + listStartString += element.getVarName(); + listStartString += ">\n"; + + String listEndString = "<#if ("+element.getVarName()+"?has_next)>,"; + listEndString += "\n"; + + String newString = templateString.substring(0, posOpenBracket); + newString += listStartString; + newString += templateString.substring(posOpenBracket, posCloseBracket+1); + newString += listEndString; + newString += templateString.substring(posCloseBracket+1, templateString.length()); + + templateString = newString; + currentPosInTemplate += listStartString.length(); + currentPosInTemplate += listEndString.length(); + System.out.println(templateString.substring(posOpenBracket-10, posCloseBracket + listStartString.length() + listEndString.length()+10)); + } + } + } + } + if (!bracketStack.isEmpty()) { + throw new TemplateException("Exception - stack not empty but end of string reached"); + } + } // for all types + + return templateString; + } + public void addFont(String name, Font font) { if (fontMap == null) { fontMap = new ConcurrentHashMap<>(); } fontMap.put(name, font); } - + protected PDFont getFont(String fontName) throws ConfigurationException { if (fontName.equals(PDType1Font.HELVETICA.getBaseFont())) { return PDType1Font.HELVETICA; } else if (fontName.equals(PDType1Font.HELVETICA_BOLD.getBaseFont())) { return PDType1Font.HELVETICA_BOLD; - }else { + } else { throw new ConfigurationException("Font " + fontName + " not supported"); } - } - + } + public Font getFontByAlias(String fontAlias) throws ConfigurationException { if ((fontMap != null) && (fontMap.containsKey(fontAlias))) { - return fontMap.get(fontAlias); + return fontMap.get(fontAlias); } else { throw new ConfigurationException("Font " + fontAlias + " not found in mapping. "); } } - - public PDFDocument addContent(Content content) { contentList.add(content); return this; diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFElement.java b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFElement.java new file mode 100644 index 0000000..ac92838 --- /dev/null +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFElement.java @@ -0,0 +1,9 @@ +package de.muehlencord.shared.pdf; + +/** + * + * @author joern.muehlencord + */ +public class PDFElement { + +} diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java index c4eccd0..f1137bb 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java @@ -3,10 +3,12 @@ package de.muehlencord.shared.pdf; import com.google.gson.Gson; import freemarker.template.Template; import freemarker.template.TemplateException; +import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.concurrent.ConcurrentHashMap; +import org.apache.commons.io.FileUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -32,9 +34,9 @@ public class PDFTemplate { this.template = template; this.dataModel = new ConcurrentHashMap<>(); } - - public void addToDatamodel (String key, Object value) { - this.dataModel.put (key, value); + + public void addToDatamodel(String key, Object value) { + this.dataModel.put(key, value); } public void create(String filenName) throws ConfigurationException, IOException { @@ -45,8 +47,8 @@ public class PDFTemplate { throw new IOException("Error while processing template", ex); } String json = out.toString(); - LOGGER.info(json); - + LOGGER.debug(json); + Gson gson = GsonUtil.getGsonInstance(); PDFDocument pdfDoc = gson.fromJson(json, PDFDocument.class); @@ -60,16 +62,16 @@ public class PDFTemplate { default: throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported"); } - doc.addPage(page); - + doc.addPage(page); + PDRectangle rect = page.getMediaBox(); - PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false); + PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false); Coordinate coord = null; for (Content content : pdfDoc.getContentList()) { - content.setDocument (pdfDoc); // FIXME move to serialization - content.setCoordinate (coord); + content.setDocument(pdfDoc); // FIXME move to serialization + content.setCoordinate(coord); coord = content.addContentToPdf(rect, cos); - } + } cos.close(); doc.save(filenName); } diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java b/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java index 207115a..d422286 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java @@ -19,17 +19,17 @@ public class TableContent extends Content { private final Font headerFont; @Expose - private TableHeader header; - + private final TableHeader header; + @Expose - private List> data = null; - - public TableContent (PDFDocument doc, Font hf) { - super (doc); + private List data = null; + + public TableContent(PDFDocument doc, Font hf) { + super(doc); this.header = new TableHeader(); - this.headerFont = hf; + this.headerFont = hf; this.data = new ArrayList<>(); - + } public TableContent(PDFDocument doc, Font hf, int x, int y) { @@ -38,56 +38,66 @@ public class TableContent extends Content { this.headerFont = hf; this.data = new ArrayList<>(); } - - public void addLine(String... values) { - addLine(Arrays.asList(values)); + + public DefaultTableRow addLine(String... values) { + return addLine(Arrays.asList(values)); } - public void addLine(List values) { - List newLine = new ArrayList<>(); + public DefaultTableRow addLine(List values) { + DefaultTableRow newLine = new DefaultTableRow(); values.stream().forEach((cellText) -> { newLine.add(new Text(cellText)); }); data.add(newLine); + return newLine; + } + + protected TableRow getRow (int no) { + return data.get(no); + } + + protected int getRowCount() { + return data.size(); } @Override protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { cos.beginText(); - + PDFont hFont = document.getFont(headerFont.getFontName()); PDFont standardFont = document.getFont(document.getStandardFont().getFontName()); int xOffSet = 0; for (int i = 0; i < getHeaders().size(); i++) { - xOffSet -= header.getColumnSize(i); + xOffSet -= header.getColumnSize(i); } int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding(); int currentX = x; int currentY = y; - + cos.setFont(hFont, headerFont.getFontSize()); cos.newLineAtOffset(x, y); for (int i = 0; i < header.size(); i++) { cos.showText(header.getHeader(i).getText()); cos.newLineAtOffset(header.getColumnSize(i), 0); } - if (data.size() == 0) { + if (data.isEmpty()) { currentY -= headerFont.getFontSize() - headerFont.getPadding(); } cos.setFont(standardFont, document.getStandardFont().getFontSize()); for (int lineNo = 0; lineNo < data.size(); lineNo++) { - List currentRow = data.get(lineNo); + TableRow currentRow = data.get(lineNo); cos.newLineAtOffset(xOffSet, yOffset); - currentY += yOffset; - for (int colNo = 0; colNo < currentRow.size(); colNo++) { - cos.showText(currentRow.get(colNo).getText()); + currentY += yOffset; + for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) { + cos.showText(currentRow.getColumnValue(colNo).getText()); cos.newLineAtOffset(header.getColumnSize(colNo), 0); } } - cos.endText(); - - return new Coordinate(currentX, currentY); + currentY += yOffset; + cos.endText(); + + return new Coordinate(currentX, currentY); } /* *** getter / setter *** */ @@ -97,5 +107,5 @@ public class TableContent extends Content { public TableHeader getHeaders() { return header; - } + } } diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/TableRow.java b/pdf/src/main/java/de/muehlencord/shared/pdf/TableRow.java new file mode 100644 index 0000000..0543c1c --- /dev/null +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/TableRow.java @@ -0,0 +1,21 @@ +package de.muehlencord.shared.pdf; + +/** + * + * @author joern.muehlencord + */ +public abstract class TableRow { + + public abstract int getColumnCount(); + + public abstract void createList(String listName, String varName); + + public abstract boolean isList(); + + public abstract Text getColumnValue(int columnPos); + + public abstract String getListName(); + + public abstract String getVarName(); + +} diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/TemplateException.java b/pdf/src/main/java/de/muehlencord/shared/pdf/TemplateException.java new file mode 100644 index 0000000..a8ed99e --- /dev/null +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/TemplateException.java @@ -0,0 +1,25 @@ +package de.muehlencord.shared.pdf; + +/** + * + * @author joern.muehlencord + */ +public class TemplateException extends Exception { + + /** + * Creates a new instance of TemplateException without detail + * message. + */ + public TemplateException() { + } + + /** + * Constructs an instance of TemplateException with the + * specified detail message. + * + * @param msg the detail message. + */ + public TemplateException(String msg) { + super(msg); + } +} diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/Text.java b/pdf/src/main/java/de/muehlencord/shared/pdf/Text.java index 38b500b..59224d2 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/Text.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/Text.java @@ -6,7 +6,7 @@ import com.google.gson.annotations.Expose; * * @author joern.muehlencord */ -public class Text { +public class Text extends PDFElement { @Expose private final String text; diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/DefaultTableRowTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/DefaultTableRowTest.java new file mode 100644 index 0000000..88c4c0c --- /dev/null +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/DefaultTableRowTest.java @@ -0,0 +1,35 @@ +package de.muehlencord.shared.pdf; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author joern.muehlencord + */ +public class DefaultTableRowTest { + + @Test + public void testFromJson() { + String jsonString = "{\n" + +" \"type\": \"de.muehlencord.shared.pdf.DefaultTableRow\",\n" + +" \"data\": {\n" + +" \"row\": [\n" + +" {\n" + +" \"text\": \"Rechnungs-Nr.:\"\n" + +" },\n" + +" {\n" + +" \"text\": \"${invoiceNumber}\"\n" + +" }\n" + +" ],\n" + +" \"isList\": false\n" + +" }\n" + +" }"; + + + TableRow tableRow = GsonUtil.getGsonInstance().fromJson(jsonString, TableRow.class); + assertNotNull ("tableRowObject", tableRow); + assertEquals ("column count", 2, tableRow.getColumnCount()); + assertFalse ("isList", tableRow.isList()); + } +} diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java b/pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java new file mode 100644 index 0000000..cb5dc8e --- /dev/null +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java @@ -0,0 +1,25 @@ +package de.muehlencord.shared.pdf; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author joern.muehlencord + */ +public class Invoice { + + private final List invoiceLines; + + public Invoice() { + this.invoiceLines = new ArrayList<>(); + } + + public void addInvoiceLine(InvoiceLine il) { + this.invoiceLines.add(il); + } + + public List getInvoiceLines() { + return invoiceLines; + } +} diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java b/pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java new file mode 100644 index 0000000..dc38532 --- /dev/null +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java @@ -0,0 +1,58 @@ +package de.muehlencord.shared.pdf; + +/** + * + * @author joern.muehlencord + */ +public class InvoiceLine { + + private String description; + private String price; + private String amount; + private String total; + + public InvoiceLine(String description, String price, String amount, String total) { + this.description = description; + this.price = price; + this.amount = amount; + this.total = total; + } + + + + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public String getAmount() { + return amount; + } + + public void setAmount(String amount) { + this.amount = amount; + } + + public String getTotal() { + return total; + } + + public void setTotal(String total) { + this.total = total; + } + + + +} diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java index 96640f5..701e3f5 100644 --- a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java @@ -1,17 +1,13 @@ package de.muehlencord.shared.pdf; -import com.google.gson.Gson; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; import java.util.Date; -import java.util.List; import org.apache.commons.io.FileUtils; -import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; @@ -21,18 +17,11 @@ import org.junit.Test; */ @FixMethodOrder public class PDFDocumentTest { - - private static Gson gson; - + private String jsonString = null; - @BeforeClass - public static void setUpClass() { - gson = GsonUtil.getGsonInstance(); - } - @Test - public void testToJson() throws FileNotFoundException, IOException, ConfigurationException { + public void testToJson() throws FileNotFoundException, IOException, ConfigurationException, TemplateException { System.out.println("testToJson"); PDFDocument doc = new PDFDocument(); doc.addFont("bold", new Font("Helvetica-Bold", 12, 2)); @@ -72,19 +61,20 @@ public class PDFDocumentTest { TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold")); invoiceLines.getHeaders() .add ("Menge", 100) - .add ("Beschreibung", 100) + .add ("Beschreibung", 300) .add ("Einzelpreis", 100) .add ("Summe", 100); - doc.addContent(invoiceLines); + invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10"); + invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline"); + invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20"); + doc.addContent(invoiceLines); TextContent test = new TextContent (doc) .addLine("Das ist ein Test"); doc.addContent (test); - - jsonString = gson.toJson(doc); - System.out.println(jsonString); - + jsonString = doc.getTemplateString(); + File file = new File("c:/temp/test.ftlh"); FileUtils.writeStringToFile(file, jsonString, "UTF-8"); @@ -97,9 +87,16 @@ public class PDFDocumentTest { Template template = cfg.getTemplate("test.ftlh"); PDFTemplate pdfDoc = new PDFTemplate(template); + + Invoice invoice = new Invoice(); + invoice.addInvoiceLine(new InvoiceLine ("Product 1", "10", "1", "10")); + invoice.addInvoiceLine(new InvoiceLine ("Product 2", "5", "10", "50")); + invoice.addInvoiceLine(new InvoiceLine ("Product 3", "100", "20", "2000")); + pdfDoc.addToDatamodel("invoiceDate", new Date()); pdfDoc.addToDatamodel("customerNumber", "8755"); pdfDoc.addToDatamodel("invoiceNumber", "1234567"); + pdfDoc.addToDatamodel("invoiceLines", invoice.getInvoiceLines()); pdfDoc.create("c:/temp/test.pdf"); } diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java new file mode 100644 index 0000000..3c7b0a3 --- /dev/null +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java @@ -0,0 +1,27 @@ +package de.muehlencord.shared.pdf; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author joern.muehlencord + */ +public class TextTest { + + public TextTest() { + } + + @Test + public void testFromJson() { + String jsonString = "{\n" + + "\"text\": \"Rechnungs-Nr.:\"\n" + + "}"; + + Text text = GsonUtil.getGsonInstance().fromJson(jsonString, Text.class); + assertNotNull ("text object", text); + assertEquals ("text value", "Rechnungs-Nr.:", text.getText()); + + } + +} diff --git a/pdf/src/test/resources/log4j.xml b/pdf/src/test/resources/log4j.xml index 9e9f5d7..887d158 100644 --- a/pdf/src/test/resources/log4j.xml +++ b/pdf/src/test/resources/log4j.xml @@ -12,6 +12,10 @@ + + + + @@ -26,7 +30,7 @@ - +