@Test public void shouldRenderTemplate() throws IOException, ParserConfigurationException, SAXException { Locale locale = new Locale("cs", "CZ"); LRProcessDefinition definition = EasyMock.createMock(LRProcessDefinition.class); EasyMock.expect(definition.getId()).andReturn("_4test_").anyTimes(); EasyMock.expect(definition.getMainClass()).andReturn(FourTestProcess.class.getName()).anyTimes(); DefaultTemplate template = new DefaultTemplate(); ResourceBundleService bundleService = EasyMock.createMock(ResourceBundleService.class); EasyMock.expect(bundleService.getResourceBundle("labels", locale)) .andReturn(new PropertyResourceBundle( new InputStreamReader(new ByteArrayInputStream(BUNLDE.getBytes()), Charset.forName("UTF-8")))) .anyTimes(); EasyMock.expect(bundleService.getResourceBundle("base", locale)) .andReturn(new PropertyResourceBundle( new InputStreamReader(new ByteArrayInputStream(BUNLDE.getBytes()), Charset.forName("UTF-8")))) .anyTimes(); EasyMock.replay(bundleService, definition); Injector injector = Guice.createInjector(new _Module(locale, bundleService)); injector.injectMembers(template); StringWriter stringWriter = new StringWriter(); template.renderInput(definition, stringWriter, null); Document parsedDocument = XMLUtils.parseDocument(new StringReader(stringWriter.toString())); Assert.assertTrue(parsedDocument.getDocumentElement().getNodeName().equals("div")); InputStream resStream = DefaultTemplateTest.class.getResourceAsStream("expecting.txt"); Document expected = XMLUtils.parseDocument(resStream); List<org.w3c.dom.Node> findNodesByType = XMLUtils.findNodesByType(expected.getDocumentElement(), org.w3c.dom.Node.COMMENT_NODE); StringBuilder expetedComments = new StringBuilder(); for (org.w3c.dom.Node node : findNodesByType) { Comment comm = (Comment) node; org.w3c.dom.Node parentNode = node.getParentNode(); parentNode.removeChild(node); expetedComments.append(comm.getData()); } StringBuilder parsedComments = new StringBuilder(); findNodesByType = XMLUtils.findNodesByType(parsedDocument.getDocumentElement(), org.w3c.dom.Node.COMMENT_NODE); for (org.w3c.dom.Node node : findNodesByType) { Comment comm = (Comment) node; org.w3c.dom.Node parentNode = node.getParentNode(); parentNode.removeChild(node); parsedComments.append(comm.getData()); } Assert.assertEquals(WhitespaceUtility.replace(expetedComments.toString()), WhitespaceUtility.replace(parsedComments.toString())); Diff diff = XMLUnit.compareXML(parsedDocument, expected); Assert.assertTrue(diff.toString(), diff.similar()); }
@Override public void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping) throws IOException { try { List<FieldDesc> fdescs = new ArrayList<DefaultTemplate.FieldDesc>(); String mainClass = definition.getMainClass(); Class<?> clz = Class.forName(mainClass); Method method = ProcessStarter.annotatedMethod(clz); if (method == null) throw new IllegalStateException("cannot find annotated process method !"); Annotation[][] annots = method.getParameterAnnotations(); Class<?>[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { Annotation[] ann = annots[i]; Annotation paramAnnot = ProcessStarter.findNameAnnot(ann); if (paramAnnot!=null) { String name = ((ParameterName)paramAnnot).value(); FieldDesc fdesc = new FieldDesc(name, paramsMapping != null && paramsMapping.containsKey(name) ? paramsMapping.get(name) : findDefaultValue(clz, types[i], name)); fdescs.add(fdesc); } } InputStream iStream = this.getClass().getResourceAsStream("default.st"); StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(iStream,"UTF-8"), DefaultTemplateLexer.class); StringTemplate template = templateGroup.getInstanceOf("form"); ResourceBundle resbundle = resourceBundleService.getResourceBundle("labels", localeProvider.get()); template.setAttribute("bundle", resourceBundleMap(resbundle)); template.setAttribute("fields", fdescs); template.setAttribute("process", definition.getId()); writer.write(template.toString()); } catch (ClassNotFoundException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalArgumentException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalAccessException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } }
DefaultTemplate implements ProcessInputTemplate { @Override public void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping) throws IOException { try { List<FieldDesc> fdescs = new ArrayList<DefaultTemplate.FieldDesc>(); String mainClass = definition.getMainClass(); Class<?> clz = Class.forName(mainClass); Method method = ProcessStarter.annotatedMethod(clz); if (method == null) throw new IllegalStateException("cannot find annotated process method !"); Annotation[][] annots = method.getParameterAnnotations(); Class<?>[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { Annotation[] ann = annots[i]; Annotation paramAnnot = ProcessStarter.findNameAnnot(ann); if (paramAnnot!=null) { String name = ((ParameterName)paramAnnot).value(); FieldDesc fdesc = new FieldDesc(name, paramsMapping != null && paramsMapping.containsKey(name) ? paramsMapping.get(name) : findDefaultValue(clz, types[i], name)); fdescs.add(fdesc); } } InputStream iStream = this.getClass().getResourceAsStream("default.st"); StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(iStream,"UTF-8"), DefaultTemplateLexer.class); StringTemplate template = templateGroup.getInstanceOf("form"); ResourceBundle resbundle = resourceBundleService.getResourceBundle("labels", localeProvider.get()); template.setAttribute("bundle", resourceBundleMap(resbundle)); template.setAttribute("fields", fdescs); template.setAttribute("process", definition.getId()); writer.write(template.toString()); } catch (ClassNotFoundException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalArgumentException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalAccessException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } } }
DefaultTemplate implements ProcessInputTemplate { @Override public void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping) throws IOException { try { List<FieldDesc> fdescs = new ArrayList<DefaultTemplate.FieldDesc>(); String mainClass = definition.getMainClass(); Class<?> clz = Class.forName(mainClass); Method method = ProcessStarter.annotatedMethod(clz); if (method == null) throw new IllegalStateException("cannot find annotated process method !"); Annotation[][] annots = method.getParameterAnnotations(); Class<?>[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { Annotation[] ann = annots[i]; Annotation paramAnnot = ProcessStarter.findNameAnnot(ann); if (paramAnnot!=null) { String name = ((ParameterName)paramAnnot).value(); FieldDesc fdesc = new FieldDesc(name, paramsMapping != null && paramsMapping.containsKey(name) ? paramsMapping.get(name) : findDefaultValue(clz, types[i], name)); fdescs.add(fdesc); } } InputStream iStream = this.getClass().getResourceAsStream("default.st"); StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(iStream,"UTF-8"), DefaultTemplateLexer.class); StringTemplate template = templateGroup.getInstanceOf("form"); ResourceBundle resbundle = resourceBundleService.getResourceBundle("labels", localeProvider.get()); template.setAttribute("bundle", resourceBundleMap(resbundle)); template.setAttribute("fields", fdescs); template.setAttribute("process", definition.getId()); writer.write(template.toString()); } catch (ClassNotFoundException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalArgumentException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalAccessException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } }  }
DefaultTemplate implements ProcessInputTemplate { @Override public void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping) throws IOException { try { List<FieldDesc> fdescs = new ArrayList<DefaultTemplate.FieldDesc>(); String mainClass = definition.getMainClass(); Class<?> clz = Class.forName(mainClass); Method method = ProcessStarter.annotatedMethod(clz); if (method == null) throw new IllegalStateException("cannot find annotated process method !"); Annotation[][] annots = method.getParameterAnnotations(); Class<?>[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { Annotation[] ann = annots[i]; Annotation paramAnnot = ProcessStarter.findNameAnnot(ann); if (paramAnnot!=null) { String name = ((ParameterName)paramAnnot).value(); FieldDesc fdesc = new FieldDesc(name, paramsMapping != null && paramsMapping.containsKey(name) ? paramsMapping.get(name) : findDefaultValue(clz, types[i], name)); fdescs.add(fdesc); } } InputStream iStream = this.getClass().getResourceAsStream("default.st"); StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(iStream,"UTF-8"), DefaultTemplateLexer.class); StringTemplate template = templateGroup.getInstanceOf("form"); ResourceBundle resbundle = resourceBundleService.getResourceBundle("labels", localeProvider.get()); template.setAttribute("bundle", resourceBundleMap(resbundle)); template.setAttribute("fields", fdescs); template.setAttribute("process", definition.getId()); writer.write(template.toString()); } catch (ClassNotFoundException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalArgumentException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalAccessException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } }  @Override void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping); static Map<String, String> resourceBundleMap(ResourceBundle bundle); }
DefaultTemplate implements ProcessInputTemplate { @Override public void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping) throws IOException { try { List<FieldDesc> fdescs = new ArrayList<DefaultTemplate.FieldDesc>(); String mainClass = definition.getMainClass(); Class<?> clz = Class.forName(mainClass); Method method = ProcessStarter.annotatedMethod(clz); if (method == null) throw new IllegalStateException("cannot find annotated process method !"); Annotation[][] annots = method.getParameterAnnotations(); Class<?>[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { Annotation[] ann = annots[i]; Annotation paramAnnot = ProcessStarter.findNameAnnot(ann); if (paramAnnot!=null) { String name = ((ParameterName)paramAnnot).value(); FieldDesc fdesc = new FieldDesc(name, paramsMapping != null && paramsMapping.containsKey(name) ? paramsMapping.get(name) : findDefaultValue(clz, types[i], name)); fdescs.add(fdesc); } } InputStream iStream = this.getClass().getResourceAsStream("default.st"); StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(iStream,"UTF-8"), DefaultTemplateLexer.class); StringTemplate template = templateGroup.getInstanceOf("form"); ResourceBundle resbundle = resourceBundleService.getResourceBundle("labels", localeProvider.get()); template.setAttribute("bundle", resourceBundleMap(resbundle)); template.setAttribute("fields", fdescs); template.setAttribute("process", definition.getId()); writer.write(template.toString()); } catch (ClassNotFoundException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalArgumentException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } catch (IllegalAccessException e) { LOGGER.log(Level.SEVERE,e.getMessage(),e); throw new IOException(e); } }  @Override void renderInput(LRProcessDefinition definition, Writer writer, Properties paramsMapping); static Map<String, String> resourceBundleMap(ResourceBundle bundle);  }