Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #101

Merged
merged 2 commits into from
Feb 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.metawidget.statically.faces.component.StaticUIMetawidget;
import org.metawidget.statically.faces.component.ValueHolder;
import org.metawidget.statically.faces.component.html.StaticHtmlMetawidget;
import org.metawidget.statically.faces.component.html.layout.HtmlMessage;
import org.metawidget.statically.faces.component.html.layout.HtmlPanelGrid;
import org.metawidget.statically.faces.component.html.layout.HtmlPanelGroup;
import org.metawidget.statically.faces.component.html.widgetbuilder.FaceletsParam;
Expand Down Expand Up @@ -545,6 +546,18 @@ protected void addColumnComponent(HtmlDataTable dataTable, Map<String, String> t
Facet footerFacet = new Facet();
footerFacet.putAttribute("name", "footer");
footerFacet.getChildren().add(footerMetawidget);

ReadableIdProcessor readableIdProcessor = metawidget.getWidgetProcessor(ReadableIdProcessor.class);

if (readableIdProcessor != null)
{
readableIdProcessor.processWidget(footerMetawidget, elementName, columnAttributes, metawidget);
}

HtmlMessage message = new HtmlMessage();
message.putAttribute("for", footerMetawidget.getAttribute("id"));
message.putAttribute("styleClass", "error");
footerFacet.getChildren().add(message);
column.getChildren().add(footerFacet);
}
}
Expand Down
34 changes: 21 additions & 13 deletions scaffold-faces/src/main/resources/scaffold/faces/BackingBean.jv
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.List;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
Expand Down Expand Up @@ -96,25 +97,32 @@ public class @{entity.getName()}Bean implements Serializable {
public String update() {
this.conversation.end();

if (this.id == null) {
this.entityManager.persist(this.@{ccEntity});
return "search?faces-redirect=true";
} else {
this.entityManager.merge(this.@{ccEntity});
return "view?faces-redirect=true&id=" + this.@{ccEntity}.getId();
try {
if (this.id == null) {
this.entityManager.persist(this.@{ccEntity});
return "search?faces-redirect=true";
} else {
this.entityManager.merge(this.@{ccEntity});
return "view?faces-redirect=true&id=" + this.@{ccEntity}.getId();
}
} catch( Exception e ) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( e.getMessage() ));
return null;
}
}

public String delete() {
this.conversation.end();
this.entityManager.remove(this.entityManager.find(@{entity.getName()}.class,
getId()));
return "search?faces-redirect=true";
}

public String cancel() {
this.conversation.end();
return "search?faces-redirect=true";
try {
this.entityManager.remove(this.entityManager.find(@{entity.getName()}.class,
getId()));
this.entityManager.flush();
return "search?faces-redirect=true";
} catch( Exception e ) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( e.getMessage() ));
return null;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<ui:define name="main">
<h:form id="create">
<h:messages globalOnly="true"/>
<h:messages globalOnly="true" styleClass="error"/>

@{metawidget}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ h1 {
margin: auto;
}

#content table .component .error {
#content .error {
color: red;
font-size: 10pt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<ui:define name="main">
<h:form id="search">
<h:panelGroup styleClass="search">
<h:messages globalOnly="true"/>

@{searchMetawidget}
<h:panelGroup styleClass="buttons">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void testGenerateFromEntity() throws Exception
"template=\"/resources/scaffold/page.xhtml"));

metawidget = new StringBuilder("<h:form id=\"create\">\n");
metawidget.append("\t\t\t<h:messages globalOnly=\"true\"/>\n\n");
metawidget.append("\t\t\t<h:messages globalOnly=\"true\" styleClass=\"error\"/>\n\n");
metawidget.append("\t\t\t<h:panelGrid columnClasses=\"label,component,required\" columns=\"3\">\r\n");
metawidget.append("\t\t\t\t<h:outputLabel for=\"customerBeanCustomerFirstName\" value=\"First Name:\"/>\r\n");
metawidget.append("\t\t\t\t<h:panelGroup>\r\n");
Expand Down Expand Up @@ -183,8 +183,7 @@ public void testGenerateFromEntity() throws Exception
"template=\"/resources/scaffold/page.xhtml"));

StringBuilder searchMetawidget = new StringBuilder("<h:form id=\"search\">\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n");
searchMetawidget.append("\t\t\t\t<h:messages globalOnly=\"true\"/>\r\n\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n\r\n");
searchMetawidget.append("\t\t\t\t<h:panelGrid columnClasses=\"label,component,required\" columns=\"3\">\r\n");
searchMetawidget
.append("\t\t\t\t\t<h:outputLabel for=\"customerBeanSearchFirstName\" value=\"First Name:\"/>\r\n");
Expand Down Expand Up @@ -280,7 +279,7 @@ public void testGenerateFromEntity() throws Exception
FileResource<?> css = web.getWebResource("resources/forge-style.css");
Assert.assertTrue(css.exists());
contents = Streams.toString(css.getResourceInputStream());
Assert.assertTrue(contents.contains("#content table .component .error {"));
Assert.assertTrue(contents.contains("#content .error {"));

Assert.assertTrue(web.getWebResource("resources/jboss-community.png").exists());
Assert.assertTrue(web.getWebResource("resources/remove.png").exists());
Expand Down Expand Up @@ -467,7 +466,7 @@ public void testGenerateFromNestedEntity() throws Exception
Assert.assertTrue(contents.contains(
"template=\"/resources/scaffold/page.xhtml"));
StringBuilder metawidget = new StringBuilder("\t\t<h:form id=\"create\">\n");
metawidget.append("\t\t\t<h:messages globalOnly=\"true\"/>\n\n");
metawidget.append("\t\t\t<h:messages globalOnly=\"true\" styleClass=\"error\"/>\n\n");
metawidget.append("\t\t\t<h:panelGrid columnClasses=\"label,component,required\" columns=\"3\">\r\n");
metawidget.append("\t\t\t\t<h:outputLabel for=\"customerBeanCustomerFirstName\" value=\"First Name:\"/>\r\n");
metawidget.append("\t\t\t\t<h:panelGroup>\r\n");
Expand Down Expand Up @@ -790,8 +789,7 @@ public void testGenerateOneToManyEntity() throws Exception
"template=\"/resources/scaffold/page.xhtml"));

StringBuilder searchMetawidget = new StringBuilder("<h:form id=\"search\">\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n");
searchMetawidget.append("\t\t\t\t<h:messages globalOnly=\"true\"/>\r\n\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n\r\n");
searchMetawidget.append("\t\t\t\t<h:panelGrid columnClasses=\"label,component,required\" columns=\"3\">\r\n");
searchMetawidget
.append("\t\t\t\t\t<h:outputLabel for=\"customerBeanSearchFirstName\" value=\"First Name:\"/>\r\n");
Expand Down Expand Up @@ -952,8 +950,7 @@ public void testGenerateEntityWithManyFields() throws Exception
"template=\"/resources/scaffold/page.xhtml"));

StringBuilder searchMetawidget = new StringBuilder("<h:form id=\"search\">\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n");
searchMetawidget.append("\t\t\t\t<h:messages globalOnly=\"true\"/>\r\n\r\n");
searchMetawidget.append("\t\t\t<h:panelGroup styleClass=\"search\">\r\n\r\n");
searchMetawidget.append("\t\t\t\t<h:panelGrid columnClasses=\"label,component,required\" columns=\"3\">\r\n");
searchMetawidget.append("\t\t\t\t\t<h:outputLabel for=\"customerBeanSearchField1\" value=\"Field 1:\"/>\r\n");
searchMetawidget.append("\t\t\t\t\t<h:panelGroup>\r\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ public void testEmbeddedSet()
result += "<h:column><f:facet name=\"header\"><h:outputText value=\"Description\"/></f:facet>";
result += "<h:link outcome=\"/scaffold/entityWidgetBuilderTest$Bar/view\"><f:param name=\"id\" value=\"#{_item.id}\"/>";
result += "<h:outputText id=\"itemDescription\" value=\"#{_item.description}\"/></h:link>";
result += "<f:facet name=\"footer\"><h:inputText id=\"entityWidgetBuilderTestBarBeanAddDescription\" value=\"#{entityWidgetBuilderTest$BarBean.add.description}\"/></f:facet>";
result += "<f:facet name=\"footer\">";
result += "<h:inputText id=\"entityWidgetBuilderTestBarBeanAddDescription\" value=\"#{entityWidgetBuilderTest$BarBean.add.description}\"/>";
result += "<h:message for=\"entityWidgetBuilderTestBarBeanAddDescription\" styleClass=\"error\"/>";
result += "</f:facet>";
result += "</h:column>";
result += "<h:column footerClass=\"remove-column\" headerClass=\"remove-column\"><h:commandLink action=\"#{_collection.remove(_item)}\" styleClass=\"remove-button\"/>";
result += "<f:facet name=\"footer\">";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public void testAll() throws Exception
assertEquals("Address City #2", form.getInputByName("create:addressBeanAddressCity").getValueAttribute());
page = page.getAnchorByText("Save").click();

// Create yet another Address

page = page.getAnchorByText("Create New").click();
form = page.getFormByName("create");
form.getInputByName("create:addressBeanAddressStreet").setValueAttribute("Address Street #3");
form.getInputByName("create:addressBeanAddressCity").setValueAttribute("Address City #3");
page = page.getAnchorByText("Save").click();

// Create a Customer

page = page.getAnchorByText("Customer").click();
Expand All @@ -96,19 +104,35 @@ public void testAll() throws Exception
form.getSelectByName("create:customerBeanCustomerAddressesSelect")
.setSelectedAttribute("1", true);
page = page.getHtmlElementById("create:customerBeanCustomerAddressesAdd").click();
HtmlTable table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerAddresses");
assertEquals("Address Street #1", table.getCellAt(1, 0).getTextContent());

// Test OneToMany (mappedBy)

form = page.getFormByName("create");
form.getSelectByName("create:customerBeanCustomerOrders:submittedOrderBeanAddAddress")
.setSelectedAttribute("2", true);
page = page.getHtmlElementById("create:customerBeanCustomerOrders:customerBeanCustomerOrdersAdd").click();
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerOrders");
assertEquals("Address Street #2 Address City #2 0", table.getCellAt(2, 0).getTextContent());

// Test adding multiple OneToMany (mappedBy) before clicking Save

form = page.getFormByName("create");
form.getSelectByName("create:customerBeanCustomerOrders:submittedOrderBeanAddAddress")
.setSelectedAttribute("3", true);
page = page.getHtmlElementById("create:customerBeanCustomerOrders:customerBeanCustomerOrdersAdd").click();
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerOrders");
assertTrue( "Address Street #2 Address City #2 0".equals(table.getCellAt(2, 0).getTextContent()) || "Address Street #3 Address City #3 0".equals(table.getCellAt(2, 0).getTextContent() ));
assertTrue( "Address Street #2 Address City #2 0".equals(table.getCellAt(3, 0).getTextContent()) || "Address Street #3 Address City #3 0".equals(table.getCellAt(3, 0).getTextContent() ));
assertTrue(!table.getCellAt(2, 0).getTextContent().equals(table.getCellAt(3, 0).getTextContent()));

page = page.getAnchorByText("Save").click();

// Test it all saved

assertTrue(page.asText().contains("Search Customer entities"));
HtmlTable table = (HtmlTable) page.getHtmlElementById("search:customerBeanPageItems");
table = (HtmlTable) page.getHtmlElementById("search:customerBeanPageItems");
assertEquals("Customer Firstname #1", table.getCellAt(1, 0).getTextContent());

page = page.getAnchorByText("Customer Firstname #1").click();
Expand All @@ -118,10 +142,23 @@ public void testAll() throws Exception
table = (HtmlTable) page.getHtmlElementById("customerBeanCustomerAddresses");
assertEquals("Address Street #1", table.getCellAt(1, 0).getTextContent());
table = (HtmlTable) page.getHtmlElementById("customerBeanCustomerOrders");
assertEquals("Address Street #2 Address City #2 0", table.getCellAt(1, 0).getTextContent());
assertTrue( "Address Street #2 Address City #2 0".equals(table.getCellAt(1, 0).getTextContent()) || "Address Street #3 Address City #3 0".equals(table.getCellAt(1, 0).getTextContent() ));
assertTrue( "Address Street #2 Address City #2 0".equals(table.getCellAt(2, 0).getTextContent()) || "Address Street #3 Address City #3 0".equals(table.getCellAt(2, 0).getTextContent() ));
assertTrue(!table.getCellAt(1, 0).getTextContent().equals(table.getCellAt(2, 0).getTextContent()));

// Test foreign key constraints

page = page.getAnchorByText("Address").click();
page = page.getAnchorByText("Address Street #1").click();
page = page.getAnchorByText("Edit").click();
assertTrue(!page.asXml().contains("<ul class=\"error\">"));
page = page.getAnchorByText("Delete").click();
assertTrue(page.asXml().contains("<ul class=\"error\">"));

// Test deleting the relationships

page = page.getAnchorByText("Customer").click();
page = page.getAnchorByText("Customer Firstname #1").click();
page = page.getAnchorByText("Edit").click();
assertTrue(page.asText().contains("Edit existing Customer"));
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerAddresses");
Expand All @@ -131,6 +168,8 @@ public void testAll() throws Exception
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerOrders");
page = table.getCellAt(2, 1).getHtmlElementsByTagName("a").get(0).click();
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerOrders");
page = table.getCellAt(2, 1).getHtmlElementsByTagName("a").get(0).click();
table = (HtmlTable) page.getHtmlElementById("create:customerBeanCustomerOrders");
assertEquals("", table.getCellAt(2, 0).getTextContent());
page = page.getAnchorByText("Save").click();

Expand Down