@Test public void testCreateEnum() throws Exception { Project project = projectFactory.createTempProject(); facetFactory.install(project, JavaSourceFacet.class); JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class); JavaEnumSource javaEnum = Roaster.parse( JavaEnumSource.class, "package org.jboss.forge.test; public enum CreditCardType{}"); facet.saveJavaSource(javaEnum); JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType"); Assert.assertNotNull(javaResource); Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaEnum.class))); CommandController controller = testHarness.createCommandController(JavaAddEnumConstantCommand.class, javaResource); controller.initialize(); controller.setValueFor("named", "VISA"); Assert.assertTrue(controller.isValid()); Assert.assertTrue(controller.canExecute()); Result result = controller.execute(); Assert.assertThat(result, is(not(instanceOf(Failed.class)))); javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType"); JavaEnumSource enumClass = javaResource.getJavaType(); List<EnumConstantSource> enumConstants = enumClass.getEnumConstants(); Assert.assertEquals(1, enumConstants.size()); Assert.assertEquals("VISA", enumConstants.get(0).getName()); }
@Test public void testJavaDocParsing() throws Exception { String text = "/** Text */ public class MyClass{}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); JavaDocSource<JavaClassSource> javaDoc = javaClass.getJavaDoc(); Assert.assertNotNull(javaDoc); Assert.assertEquals("Text", javaDoc.getText()); Assert.assertTrue(javaDoc.getTagNames().isEmpty()); }
@Test public void testJavaDocParsingTags() throws Exception { String text = "/** Do Something\n*@author George Gastaldi*/ public class MyClass{}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); JavaDocSource<JavaClassSource> javaDoc = javaClass.getJavaDoc(); Assert.assertNotNull(javaDoc); Assert.assertEquals("Do Something", javaDoc.getText()); Assert.assertEquals(1, javaDoc.getTagNames().size()); JavaDocTag authorTag = javaDoc.getTags("@author").get(0); Assert.assertEquals("@author", authorTag.getName()); Assert.assertEquals("George Gastaldi", authorTag.getValue()); }
@Test public void testJavaDocMultiLineShouldNotConcatenateWords() throws Exception { String text = "/**" + LINE_SEPARATOR + "* The country where this currency is used mostly. This field is just for" + LINE_SEPARATOR + "* informational purposes and have no effect on any processing." + LINE_SEPARATOR + "*/" + LINE_SEPARATOR + "public class MyClass{}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); JavaDocSource<JavaClassSource> javaDoc = javaClass.getJavaDoc(); String expected = "The country where this currency is used mostly. This field is just for informational purposes and have no effect on any processing."; Assert.assertEquals(expected, javaDoc.getFullText()); }
@Test public void testJavaDocEnumConstant() throws Exception { String text = "public enum MyEnum {" + "/**" + LINE_SEPARATOR + " * Do Something" + LINE_SEPARATOR + " * @author George Gastaldi" + LINE_SEPARATOR + " */" + LINE_SEPARATOR + "" + "MY_CONSTANT;}"; JavaEnumSource javaEnum = Roaster.parse(JavaEnumSource.class, text); Assert.assertFalse(javaEnum.hasJavaDoc()); EnumConstantSource enumConstant = javaEnum.getEnumConstant("MY_CONSTANT"); Assert.assertTrue(enumConstant.hasJavaDoc()); JavaDocSource<EnumConstantSource> javaDoc = enumConstant.getJavaDoc(); String expected = "Do Something" + LINE_SEPARATOR + "@author George Gastaldi"; Assert.assertEquals(expected, javaDoc.getFullText()); }
@Test public void testJavaDocGetFullText() throws Exception { String text = "/**" + LINE_SEPARATOR + " * Do Something" + LINE_SEPARATOR + " * @author George Gastaldi" + LINE_SEPARATOR + " */" + LINE_SEPARATOR + "public class MyClass" + LINE_SEPARATOR + "{" + LINE_SEPARATOR + "}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); Assert.assertTrue(javaClass.hasJavaDoc()); JavaDocSource<JavaClassSource> javaDoc = javaClass.getJavaDoc(); String expected = "Do Something" + LINE_SEPARATOR + "@author George Gastaldi"; Assert.assertEquals(expected, javaDoc.getFullText()); }
@Test public void testJavaDocMethod() throws Exception { String text = "public class MyClass {" + "/**" + LINE_SEPARATOR + " * Do Something" + LINE_SEPARATOR + " * @author George Gastaldi" + LINE_SEPARATOR + " */" + LINE_SEPARATOR + "" + "private void foo(){};}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); Assert.assertFalse(javaClass.hasJavaDoc()); Assert.assertEquals(1, javaClass.getMethods().size()); MethodSource<JavaClassSource> method = javaClass.getMethods().get(0); Assert.assertTrue(method.hasJavaDoc()); JavaDocSource<MethodSource<JavaClassSource>> javaDoc = method.getJavaDoc(); String expected = "Do Something" + LINE_SEPARATOR + "@author George Gastaldi"; Assert.assertEquals(expected, javaDoc.getFullText()); }
@SuppressWarnings({"unchecked", "rawtypes"}) private List<Resource<?>> generateFromEntity( String targetDir, final Resource<?> template, final JavaClassSource entity) { resetMetaWidgets(); // Track the list of resources generated List<Resource<?>> result = new ArrayList<>(); try { JavaSourceFacet java = this.project.getFacet(JavaSourceFacet.class); WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class); JPAFacet<PersistenceCommonDescriptor> jpa = this.project.getFacet(JPAFacet.class); loadTemplates(); Map<Object, Object> context = CollectionUtils.newHashMap(); context.put("entity", entity); String ccEntity = StringUtils.decapitalize(entity.getName()); context.put("ccEntity", ccEntity); context.put("rmEntity", ccEntity + "ToDelete"); setPrimaryKeyMetaData(context, entity); // Prepare qbeMetawidget this.qbeMetawidget.setPath(entity.getQualifiedName()); StringWriter stringWriter = new StringWriter(); this.qbeMetawidget.write(stringWriter, this.backingBeanTemplateQbeMetawidgetIndent); context.put("qbeMetawidget", stringWriter.toString().trim()); // Prepare removeEntityMetawidget this.rmEntityMetawidget.setPath(entity.getQualifiedName()); stringWriter = new StringWriter(); this.rmEntityMetawidget.write(stringWriter, this.backingBeanTemplateRmEntityMetawidgetIndent); context.put("rmEntityMetawidget", stringWriter.toString().trim()); // Prepare Java imports Set<String> qbeMetawidgetImports = this.qbeMetawidget.getImports(); Set<String> rmEntityMetawidgetImports = this.rmEntityMetawidget.getImports(); Set<String> metawidgetImports = CollectionUtils.newHashSet(); metawidgetImports.addAll(qbeMetawidgetImports); metawidgetImports.addAll(rmEntityMetawidgetImports); metawidgetImports.remove(entity.getQualifiedName()); context.put( "metawidgetImports", CollectionUtils.toString(metawidgetImports, ";\r\nimport ", true, false)); // Prepare JPA Persistence Unit context.put("persistenceUnitName", jpa.getConfig().getOrCreatePersistenceUnit().getName()); // Create the Backing Bean for this entity JavaClassSource viewBean = Roaster.parse( JavaClassSource.class, FreemarkerTemplateProcessor.processTemplate(context, this.backingBeanTemplate)); viewBean.setPackage(java.getBasePackage() + "." + DEFAULT_FACES_PACKAGE); result.add( ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewBean), viewBean.toString())); // Set new context for view generation context = getTemplateContext(targetDir, template); String beanName = StringUtils.decapitalize(viewBean.getName()); context.put("beanName", beanName); context.put("ccEntity", ccEntity); context.put("entityName", StringUtils.uncamelCase(entity.getName())); setPrimaryKeyMetaData(context, entity); // Prepare entityMetawidget this.entityMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + "." + ccEntity)); this.entityMetawidget.setPath(entity.getQualifiedName()); this.entityMetawidget.setReadOnly(false); this.entityMetawidget.setStyle(null); // Generate create writeEntityMetawidget( context, this.createTemplateEntityMetawidgetIndent, this.createTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/create.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.createTemplate))); // Generate view this.entityMetawidget.setReadOnly(true); writeEntityMetawidget( context, this.viewTemplateEntityMetawidgetIndent, this.viewTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/view.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.viewTemplate))); // Generate search this.searchMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".example")); this.searchMetawidget.setPath(entity.getQualifiedName()); this.beanMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".pageItems")); this.beanMetawidget.setPath(viewBean.getQualifiedName() + "/pageItems"); writeSearchAndBeanMetawidget( context, this.searchTemplateSearchMetawidgetIndent, this.searchTemplateBeanMetawidgetIndent, this.searchTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/search.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.searchTemplate))); // Generate navigation result.add(generateNavigation(targetDir)); // Need ViewUtils and forge.taglib.xml for forgeview:asList JavaClassSource viewUtils = Roaster.parse( JavaClassSource.class, FreemarkerTemplateProcessor.processTemplate(context, this.viewUtilsTemplate)); viewUtils.setPackage(viewBean.getPackage()); result.add( ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewUtils), viewUtils.toString())); context.put("viewPackage", viewBean.getPackage()); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource("WEB-INF/classes/META-INF/forge.taglib.xml"), FreemarkerTemplateProcessor.processTemplate(context, this.taglibTemplate))); createInitializers(entity); } catch (Exception e) { throw new RuntimeException("Error generating default scaffolding: " + e.getMessage(), e); } return result; }