WordTemplate implements Template<E> { @Override public void toDocument(E data, OutputStream out) throws IOException, InvalidTemplateException { prepareDocument(); TemplateContext<E> context = contextFactory.createTemplateContext(this); context.bind(data); document.updateDynamicContent(context); document.writeAsOpenXML(out); } WordTemplate(TemplateData template, TemplateContextFactory contextFactory); @Override void toDocument(E data, OutputStream out); @Override void toTestDocument(OutputStream out); @Override void toPDFDocument(E data, OutputStream out); @Override String getDocumentName(); @Override Class<?> getInputType(); @Override Map<String, String> getProperties(); String getLanguage(); Locale getLocale(); User getLockingUser(); Date getCreatedAt(); User getEditor(); Date getEditedAt(); @Override void update(InputStream in, User editor); @Override Map<String, MappingDetail> getContentMapping(); @Override List<String> listContentIds(); @Override void validate(); @Override void write(File file); @Override void write(OutputStream out); TemplateData getTemplateData();  }
@Test public void toTestDocument() throws IOException, Docx4JException { when(templateContext.resolveValue("value")).thenReturn(TestDataModel.STRING_TEXT); WordTemplate<String> wordTemplate = createWordTemplate("org/wte4j/impl/simpleTemplate.docx"); File generatedDocument = File.createTempFile("generated", "docx"); try (OutputStream out = new FileOutputStream(generatedDocument)) { wordTemplate.toTestDocument(out); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage .load(generatedDocument); String content = XmlUtils.marshaltoString(wordMLPackage .getMainDocumentPart().getContents(), true); assertTrue(content.contains(TestDataModel.STRING_TEXT)); } finally { generatedDocument.delete(); } }
@Test public void toDocumentTest() throws IOException, Docx4JException { WordTemplate<String> wordTemplate = createWordTemplate("org/wte4j/impl/simpleTemplate.docx"); when(templateContext.resolveValue("value")).thenReturn("test123"); File generatedDocument = File.createTempFile("generated", "docx"); try (OutputStream out = new FileOutputStream(generatedDocument)) { wordTemplate.toDocument("gugus", out); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage .load(generatedDocument); String content = XmlUtils.marshaltoString(wordMLPackage .getMainDocumentPart().getContents(), true); assertTrue(content.contains("test123")); } finally { generatedDocument.deleteOnExit(); } }
@Test(expected = InvalidTemplateException.class) public void toDocumentWithInvalidDocument() throws IOException, Docx4JException { InvalidExpressionException error = new InvalidExpressionException(ExpressionError.FORMATTER_TYPE_MISSMATCH); when(templateContext.resolveValue(anyString())).thenThrow(error); WordTemplate<String> wordTemplate = createWordTemplate("org/wte4j/impl/dateValueTemplate.docx"); OutputStream out = new ByteArrayOutputStream(); wordTemplate.toDocument("test", out); }