updated unit tests

added datamodel support
This commit is contained in:
jomu
2016-05-05 22:22:16 +00:00
parent 238d46c118
commit 77e783f77a
5 changed files with 103 additions and 49 deletions

View File

@ -1,29 +1,43 @@
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;
/**
*
* @author jomu
*/
@FixMethodOrder
public class PDFDocumentTest {
private static Gson gson;
private String jsonString = null;
@BeforeClass
public static void setUpClass() {
gson = GsonUtil.getGsonInstance();
}
@Test
public void testToJson() {
public void testToJson() throws FileNotFoundException, IOException, ConfigurationException {
System.out.println("testToJson");
PDFDocument doc = new PDFDocument();
doc.addFont("bold", new Font("Helvetica-Bold", 12, 2));
doc.addFont("helv12", new Font("Helvetica", 12, 2));
doc.setPaperSize(PaperSize.A4);
TextContent addressContent = new TextContent(doc, 40, 692, "Max Mustermann")
.addLine("Musterstraße 123")
@ -36,14 +50,43 @@ public class PDFDocumentTest {
.addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung");
doc.addContent(invoiceInfoInformation);
TextContent informationContent = new TextContent(doc, 400, 662);
informationContent.addLine ("Anzeigenabrechnung", "bold");
informationContent.addLine ("Veronika Mühlencord", "helv12");
informationContent.addLine ("Telefon: 05257/940154", "helv12");
informationContent.addLine ("Telefax: 05257/940156", "helv12");
doc.addContent (informationContent);
TextContent informationContent = new TextContent(doc, 400, 662);
informationContent.addLine("Anzeigenabrechnung", "bold");
informationContent.addLine("Veronika Mühlencord", "helv12");
informationContent.addLine("Telefon: 05257/940154", "helv12");
informationContent.addLine("Telefax: 05257/940156", "helv12");
informationContent.addLine();
informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold");
doc.addContent(informationContent);
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 500);
List<String> headers = new ArrayList<>();
headers.add("Kunden-Nr");
headers.add("${customer.customerNumber}");
List<Integer> colSize = new ArrayList<>();
colSize.add (100);
colSize.add (100);
informationContent2.setHeader(headers, colSize);
System.out.println(gson.toJson(doc));
jsonString = gson.toJson(doc);
System.out.println(jsonString);
File file = new File("c:/temp/test.ftlh");
FileUtils.writeStringToFile(file, jsonString, "UTF-8");
// create pdf
Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
cfg.setDirectoryForTemplateLoading(new File("c:/temp"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
Template template = cfg.getTemplate("test.ftlh");
PDFTemplate pdfDoc = new PDFTemplate(template);
pdfDoc.addToDatamodel("invoiceDate", new Date());
pdfDoc.create("c:/temp/test.pdf");
}
}

View File

@ -1,31 +0,0 @@
package de.muehlencord.shared.pdf;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
/**
*
* @author jomu
*/
public class PDFTemplateTest {
@Test
// @Ignore // TODO get template from test resources
public void testCreate() throws IOException, ConfigurationException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
cfg.setDirectoryForTemplateLoading(new File("c:/temp"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
Template template = cfg.getTemplate("test.ftlh");
PDFTemplate doc = new PDFTemplate(template);
doc.create ("c:/temp/test.pdf");
}
}