@Override public List<String> getWebPaths(final Resource<?> r) { if (r != null) { WebResourceFacet web = project.getFacet(WebResourceFacet.class); List<DirectoryResource> webRootDirectories = web.getWebRootDirectories(); for (DirectoryResource d : webRootDirectories) { if (r.getFullyQualifiedName().startsWith(d.getFullyQualifiedName())) { String path = r.getFullyQualifiedName().substring(d.getFullyQualifiedName().length()); return getWebPaths(path); } } } return new ArrayList<String>(); }
@Override public List<String> getWebPaths(Resource<?> r) { List<String> results = new ArrayList<String>(); if (r != null) { WebResourceFacet web = project.getFacet(WebResourceFacet.class); List<DirectoryResource> webRootDirectories = web.getWebRootDirectories(); for (DirectoryResource d : webRootDirectories) { if (r.getFullyQualifiedName().startsWith(d.getFullyQualifiedName())) { String path = r.getFullyQualifiedName().substring(d.getFullyQualifiedName().length()); for (String p : getWebPaths(path)) { if (!results.contains(p)) results.add(p); } break; } } } return results; }
@Override public Resource<?> getResourceForWebPath(String path) { if (path != null) { WebResourceFacet web = project.getFacet(WebResourceFacet.class); List<DirectoryResource> webRootDirectories = web.getWebRootDirectories(); boolean matches = false; for (String mapping : getEffectiveFacesServletMappings()) { Matcher matcher = ServletUtil.mappingToRegex(mapping).matcher(path); if (matcher.matches()) { path = matcher.group(1); matches = true; break; } } while (path.startsWith("/")) { path = path.substring(1); } if (!matches) { return null; } List<String> strings = Arrays.asList(path.split("/")); for (DirectoryResource d : webRootDirectories) { Queue<String> queue = new LinkedList<String>(); queue.addAll(strings); Resource<?> temp = d; while (queue.size() > 1) { Resource<?> child = temp.getChild(queue.remove()); if ((child != null) && child.exists()) { temp = child; } else { break; } if (queue.isEmpty()) { return child; } } if (temp != null) { String name = queue.remove(); for (String suffix : getFacesSuffixes()) { Resource<?> child = null; if (name.endsWith(suffix)) { child = temp.getChild(name); } else { child = temp.getChild(name + suffix); } if ((child != null) && child.exists()) { return child; } } } } } return null; }
@Test public void testGenerate() throws Exception { Project current = getShell().getCurrentProject(); final String targetDir = "petClinic"; Project project = setupScaffoldProject(targetDir); queueInputLines(""); getShell().execute("entity --named Owner"); getShell().execute("field string --named firstName"); getShell().execute("field string --named lastName"); getShell().execute("field string --named address"); getShell().execute("field string --named city"); getShell().execute("field string --named telephone"); getShell().execute("field string --named homePage"); getShell().execute("field string --named email"); getShell().execute("field temporal --type DATE --named birthday"); getShell().execute("entity --named Vet"); getShell().execute("field string --named firstName"); getShell().execute("field string --named lastName"); getShell().execute("field string --named address"); getShell().execute("field string --named city"); getShell().execute("field string --named telephone"); getShell().execute("field string --named homePage"); getShell().execute("field string --named email"); getShell().execute("field temporal --type DATE --named birthday"); getShell().execute("field temporal --type DATE --named employedSince"); getShell().execute("field int --named specialty"); getShell().execute("entity --named Pet"); getShell().execute("field string --named name"); getShell().execute("field int --named type"); getShell().execute("field boolean --named sendReminders"); getShell().execute("field manyToOne --named owner --fieldType com.test.model.Owner"); getShell().execute("entity --named Visit"); getShell().execute("field string --named description"); getShell().execute("field temporal --type DATE --named visitDate"); getShell().execute("field manyToOne --named pet --fieldType com.test.model.Pet"); getShell().execute("field manyToOne --named vet --fieldType com.test.model.Vet"); getShell().execute("cd ~~"); queueInputLines("", "", "", "", ""); getShell().execute("scaffold-x from src/main/java/com/test/model/*"); WebResourceFacet web = project.getFacet(WebResourceFacet.class); // Check search screen has h:message FileResource<?> search = web.getWebResource(targetDir + "/pet/search.xhtml"); Assert.assertTrue(search.exists()); String contents = Streams.toString(search.getResourceInputStream()); String metawidget = "\t\t\t\t\t\t<h:outputLabel for=\"petBeanExampleType\" value=\"Type:\"/>\r\n" + "\t\t\t\t\t\t<h:panelGroup>\r\n" + "\t\t\t\t\t\t\t<h:inputText id=\"petBeanExampleType\" value=\"#{petBean.example.type}\"/>\r\n" + "\t\t\t\t\t\t\t<h:message for=\"petBeanExampleType\" styleClass=\"error\"/>\r\n" + "\t\t\t\t\t\t</h:panelGroup>"; Assert.assertTrue(contents.contains(metawidget)); // Check search screen has boolean graphic metawidget = "\t\t\t\t\t<h:link outcome=\"/" + targetDir + "/pet/view\">\r\n"; metawidget += "\t\t\t\t\t\t<f:param name=\"id\" value=\"#{_item.id}\"/>\r\n"; metawidget += "\t\t\t\t\t\t<h:outputText styleClass=\"#{_item.sendReminders ? 'boolean-true' : 'boolean-false'}\" value=\"\"/>\r\n"; metawidget += "\t\t\t\t\t</h:link>\r\n"; Assert.assertTrue(contents.contains(metawidget)); metawidget = "\t\t\t\t\t<h:link outcome=\"/" + targetDir + "/pet/view\">\r\n"; metawidget += "\t\t\t\t\t\t<f:param name=\"id\" value=\"#{_item.id}\"/>\r\n"; metawidget += "\t\t\t\t\t\t<h:outputText id=\"itemOwner\" value=\"#{_item.owner}\"/>\r\n"; metawidget += "\t\t\t\t\t</h:link>\r\n"; Assert.assertTrue(contents.contains(metawidget)); // Check create screen has h:selectBooleanCheckbox FileResource<?> create = web.getWebResource(targetDir + "/pet/create.xhtml"); Assert.assertTrue(create.exists()); contents = Streams.toString(create.getResourceInputStream()); metawidget = "\t\t\t\t<h:outputLabel for=\"petBeanPetSendReminders\" value=\"Send Reminders:\"/>\r\n"; metawidget += "\t\t\t\t<h:panelGroup>\r\n"; metawidget += "\t\t\t\t\t<h:selectBooleanCheckbox id=\"petBeanPetSendReminders\" value=\"#{petBean.pet.sendReminders}\"/>\r\n"; metawidget += "\t\t\t\t\t<h:message for=\"petBeanPetSendReminders\" styleClass=\"error\"/>\r\n"; metawidget += "\t\t\t\t</h:panelGroup>"; Assert.assertTrue(contents.contains(metawidget)); // Check view screen has boolean graphic FileResource<?> view = web.getWebResource(targetDir + "/pet/view.xhtml"); Assert.assertTrue(view.exists()); contents = Streams.toString(view.getResourceInputStream()); metawidget = "\t\t\t<h:outputLabel value=\"Send Reminders:\"/>\r\n"; metawidget += "\t\t\t<h:outputText styleClass=\"#{petBean.pet.sendReminders ? 'boolean-true' : 'boolean-false'}\" value=\"\"/>"; Assert.assertTrue(contents.contains(metawidget)); // Deploy to a real container and test this.webTest.setup(project); JavaClass clazz = this.webTest.from(current, FacesScaffoldPetClinicClient.class); this.webTest.buildDefaultDeploymentMethod( project, clazz, Arrays.asList( ".addAsResource(\"META-INF/persistence.xml\", \"META-INF/persistence.xml\")")); this.webTest.addAsTestClass(project, clazz); try { getShell().execute("build"); } catch (Exception e) { System.err.println(getOutput()); throw e; } }