Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow UISelectItemsConverter in saved state

Update UISelectItems.UISelectItemsConverter to be a public static class
allowing it to be saved with component state. The UISelectItems
component is not located by searching children from the getAsString()
method.

Also add new integration test to ensure that the error does not
resurface again in the future.

Issue: 53
  • Loading branch information...
commit b5beb7d4d9a5960b3f6b3cd7eedbd3d497bed256 1 parent 6bdc7b2
@pccampos pccampos authored committed
View
21 ...ojarra-it/src/test/java/org/springframework/springfaces/traveladvisor/integrationtest/SelectItemsIT.java
@@ -27,6 +27,7 @@
import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectManyEnumSelectItemsPage;
import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectManyStringSelectItemsPage;
import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectOneBooleanSelectItemsPage;
+import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectOneJpaPartialSelectItemsPage;
import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectOneJpaSelectItemsPage;
import org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems.SelectOption;
import org.springframework.springfaces.traveladvisor.integrationtest.rule.ShowcasePages;
@@ -43,7 +44,7 @@
@Test
public void shouldSelectOneJpa() throws Exception {
- SelectOneJpaSelectItemsPage page = pages.get(SelectOneJpaSelectItemsPage.class);
+ SelectOneJpaSelectItemsPage page = this.pages.get(SelectOneJpaSelectItemsPage.class);
List<SelectOption> selectOptions = page.getSelectOptions();
assertThat(selectOptions.size(), is(28));
assertThat(selectOptions.get(0).getValue(), is(""));
@@ -64,8 +65,20 @@ public void shouldSelectOneJpa() throws Exception {
}
@Test
+ public void shouldSelectOneJpaPartial() throws Exception {
+ // Issue #53
+ SelectOneJpaPartialSelectItemsPage page = this.pages.get(SelectOneJpaPartialSelectItemsPage.class);
+ assertThat(page.getSelectOptions().size(), is(28));
+ page.selectSecondOption();
+ page = page.clickUpdateButton();
+ page.selectSecondOptionOnSecondSelect();
+ page = (SelectOneJpaPartialSelectItemsPage) page.clickSubmitButton();
+ assertThat(page.getSelectOptions().get(1).getSelected(), is("true"));
+ }
+
+ @Test
public void shouldSelectManyString() throws Exception {
- SelectManyStringSelectItemsPage page = pages.get(SelectManyStringSelectItemsPage.class);
+ SelectManyStringSelectItemsPage page = this.pages.get(SelectManyStringSelectItemsPage.class);
List<SelectOption> selectOptions = page.getSelectOptions();
assertThat(selectOptions.size(), is(4));
assertThat(selectOptions.get(0).getValue(), is("Framework"));
@@ -87,7 +100,7 @@ public void shouldSelectManyString() throws Exception {
@Test
public void shouldSelectOneBoolean() throws Exception {
- SelectOneBooleanSelectItemsPage page = pages.get(SelectOneBooleanSelectItemsPage.class);
+ SelectOneBooleanSelectItemsPage page = this.pages.get(SelectOneBooleanSelectItemsPage.class);
List<SelectOption> selectOptions = page.getSelectOptions();
assertThat(selectOptions.size(), is(3));
assertThat(selectOptions.get(0).getValue(), is(""));
@@ -112,7 +125,7 @@ public void shouldSelectOneBoolean() throws Exception {
@Test
public void shouldSelectManyEnum() throws Exception {
- SelectManyEnumSelectItemsPage page = pages.get(SelectManyEnumSelectItemsPage.class);
+ SelectManyEnumSelectItemsPage page = this.pages.get(SelectManyEnumSelectItemsPage.class);
List<SelectOption> selectOptions = page.getSelectOptions();
assertThat(selectOptions.size(), is(3));
assertThat(selectOptions.get(0).getValue(), is("JAVA"));
View
46 ...ework/springfaces/traveladvisor/integrationtest/page/selectitems/SelectOneJpaPartialSelectItemsPage.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010-2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.springfaces.traveladvisor.integrationtest.page.selectitems;
+
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.ui.Select;
+import org.springframework.springfaces.integrationtest.selenium.page.PageURL;
+
+@PageURL("/selectitems/selectonejpapartial")
+public class SelectOneJpaPartialSelectItemsPage extends AbstractMenuSelectItemsPageObject {
+
+ public SelectOneJpaPartialSelectItemsPage(WebDriver webDriver) {
+ super(webDriver);
+ }
+
+ @Override
+ protected boolean isCorrectPage(String title) {
+ return title.equals("Select Items - Select One (JPA Partial)");
+ }
+
+ public SelectOneJpaPartialSelectItemsPage clickUpdateButton() {
+ getWebDriver().findElement(By.id("form:update")).click();
+ return newPage(getClass());
+ }
+
+ public void selectSecondOptionOnSecondSelect() {
+ WebElement selectElement = getWebDriver().findElement(By.id("form:select2"));
+ Select select = new Select(selectElement);
+ select.selectByIndex(1);
+ }
+}
View
20 ...-showcase/src/main/java/org/springframework/springfaces/showcase/selectitems/ExampleSelectItemsBean.java
@@ -31,6 +31,10 @@
private Author author;
+ private Author author2;
+
+ private boolean panelVisible;
+
public Boolean getBooleanValue() {
return this.booleanValue;
}
@@ -62,4 +66,20 @@ public Author getAuthor() {
public void setAuthor(Author author) {
this.author = author;
}
+
+ public Author getAuthor2() {
+ return this.author2;
+ }
+
+ public void setAuthor2(Author author2) {
+ this.author2 = author2;
+ }
+
+ public boolean isPanelVisible() {
+ return this.panelVisible;
+ }
+
+ public void setPanelVisible(boolean panelVisible) {
+ this.panelVisible = panelVisible;
+ }
}
View
6 ...ase/src/main/java/org/springframework/springfaces/showcase/selectitems/SelectItemsExampleController.java
@@ -24,10 +24,16 @@
* MVC Controller to demonstrate select items.
*
* @author Phillip Webb
+ * @author Pedro Casagrande de Campos
*/
@Controller
public class SelectItemsExampleController {
+ @RequestMapping("/selectitems/selectonejpapartial")
+ public Model selectOneJpaPartial() {
+ return new ExtendedModelMap().addAttribute("exampleBean", new ExampleSelectItemsBean());
+ }
+
@RequestMapping("/selectitems/selectonejpa")
public Model selectOneJpa() {
// Select items values can be bound to any bean, items contents can constructed dynamically using EL
View
30 ...ngfaces-samples/springfaces-showcase/src/main/webapp/WEB-INF/pages/selectitems/selectonejpapartial.xhtml
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<ui:composition xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:ui="http://java.sun.com/jsf/facelets"
+ xmlns:h="http://java.sun.com/jsf/html"
+ xmlns:f="http://java.sun.com/jsf/core"
+ xmlns:util="http://java.sun.com/jsf/composite/components/util"
+ xmlns:s="http://springframework.org/tags/springfaces">
+
+ <h:head>
+ <title>Select Items - Select One (JPA Partial)</title>
+ </h:head>
+
+ <h:body>
+ <h:form id="form">
+ <h:selectOneMenu id="select" value="#{exampleBean.author}">
+ <s:selectItems value="#{authorRepository.all}" var="author" itemLabel="#{author.firstName} #{author.lastName}"/>
+ </h:selectOneMenu>
+ <h:commandButton id="update" process="select" update="panel" value="Update">
+ <f:setPropertyActionListener target="#{exampleBean.panelVisible}" value="true" />
+ </h:commandButton>
+ <h:panelGroup id="panel">
+ <h:selectOneMenu id="select2" value="#{exampleBean.author2}" rendered="#{exampleBean.panelVisible}">
+ <s:selectItems value="#{authorRepository.all}" var="author" itemLabel="#{author.firstName} #{author.lastName}"/>
+ </h:selectOneMenu>
+ </h:panelGroup>
+ <h:commandButton id="submit" value="Submit"/>
+ </h:form>
+ </h:body>
+</ui:composition>
View
15 springfaces/src/main/java/org/springframework/springfaces/selectitems/ui/UISelectItems.java
@@ -102,6 +102,7 @@
* attribute can be used to override this behavior.
*
* @author Phillip Webbb
+ * @author Pedro Casagrande de Campos
* @see ObjectMessageSource
* @see SelectItemsConverter
*/
@@ -633,9 +634,19 @@ public Object getValue() {
/**
* Internal JSF {@link Converter} used to convert items from the outer class.
*/
- class UISelectItemsConverter extends SelectItemsConverter {
+ public static class UISelectItemsConverter extends SelectItemsConverter {
+
public String getAsString(FacesContext context, UIComponent component, Object value) {
- return UISelectItems.this.getItemConverterStringValue(value);
+ return getUISelectItems(component).getItemConverterStringValue(value);
+ }
+
+ private UISelectItems getUISelectItems(UIComponent component) {
+ for (UIComponent child : component.getChildren()) {
+ if (child instanceof UISelectItems) {
+ return (UISelectItems) child;
+ }
+ }
+ throw new IllegalStateException("Unable to find UISelectItems in childen of " + component.getClientId());
}
}
}
View
13 springfaces/src/test/java/org/springframework/springfaces/selectitems/ui/UISelectItemsTest.java
@@ -74,6 +74,7 @@
* Tests for {@link UISelectItems}.
*
* @author Phillip Webb
+ * @author Pedro Casagrande de Campos
*/
public class UISelectItemsTest {
@@ -97,6 +98,17 @@ public void setup() {
MockitoAnnotations.initMocks(this);
this.selectItems = new UISelectItems() {
@Override
+ public void setParent(UIComponent parent) {
+ if (getParent() != null) {
+ getParent().getChildren().remove(this);
+ }
+ super.setParent(parent);
+ if (getParent() != null) {
+ getParent().getChildren().add(this);
+ }
+ }
+
+ @Override
protected TypeDescriptor getTypeDescriptor(ValueExpression valueExpression, ELContext elContext) {
return UISelectItemsTest.this.typeDescriptor;
}
@@ -519,7 +531,6 @@ public void run() throws Exception {
@Entity
static class SampleEntity {
@Id
- @SuppressWarnings("unused")
private String id = "ABC";
}
Please sign in to comment.
Something went wrong with that request. Please try again.