Skip to content

Commit

Permalink
Add IT for f:selectItemGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Mar 27, 2021
1 parent 1d22163 commit 6e573a0
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test2/faces40/pom.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Contributors to Eclipse Foundation.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.mojarra.test2</groupId>
<artifactId>pom</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>

<groupId>org.glassfish.mojarra.faces40</groupId>
<artifactId>pom</artifactId>
<packaging>pom</packaging>

<name>Mojarra ${project.version} - Test - Faces 4.0</name>

<modules>
<module>selectItemGroups</module>
</modules>
</project>
37 changes: 37 additions & 0 deletions test2/faces40/selectItemGroups/pom.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Contributors to Eclipse Foundation.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.mojarra.faces40</groupId>
<artifactId>pom</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>

<artifactId>selectItemGroups</artifactId>
<packaging>war</packaging>

<name>Mojarra ${project.version} - Test - Faces 4.0 - f:selectItemGroups</name>

<build>
<finalName>test-faces40-selectItemGroups</finalName>
</build>
</project>
@@ -0,0 +1,34 @@
package com.sun.faces.test.servlet50.selectitemgroups;

import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.List;

public class Category {

private String name;
private List<Product> products = new ArrayList<>();

public Category() {
//
}

public Category(String name, Product... products) {
this.name = name;
this.products = asList(products);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Product> getProducts() {
return products;
}

}
@@ -0,0 +1,33 @@
package com.sun.faces.test.servlet50.selectitemgroups;

public class Product {

private Long id;
private String name;

public Product() {
//
}

public Product(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
@@ -0,0 +1,51 @@
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Contributors to Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package com.sun.faces.test.servlet50.selectitemgroups;

import java.util.ArrayList;
import java.util.List;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;

@Named
@RequestScoped
public class Spec1559ITBean {

private List<Category> categories = new ArrayList<>();
private Long selectedProductId;

@PostConstruct
public void init() {
categories.add(new Category("Animals", new Product(1L, "Dog"), new Product(2L, "Cat"), new Product(3L, "Fish")));
categories.add(new Category("Cars", new Product(4L, "Alfa"), new Product(5L, "Audi"), new Product(6L, "BMW")));
}

public List<Category> getCategories() {
return categories;
}

public Long getSelectedProductId() {
return selectedProductId;
}

public void setSelectedProductId(Long selectedProductId) {
this.selectedProductId = selectedProductId;
}
}
54 changes: 54 additions & 0 deletions test2/faces40/selectItemGroups/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Contributors to Eclipse Foundation.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
>
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>${webapp.projectStage}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>${webapp.partialStateSaving}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.STATE_SAVING_METHOD</param-name>
<param-value>${webapp.stateSavingMethod}</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.SERIALIZE_SERVER_STATE</param-name>
<param-value>${webapp.serializeServerState}</param-value>
</context-param>

<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>spec1559IT.xhtml</welcome-file>
</welcome-file-list>
</web-app>
36 changes: 36 additions & 0 deletions test2/faces40/selectItemGroups/src/main/webapp/spec1559IT.xhtml
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<!--
Copyright (c) 2021 Contributors to Eclipse Foundation.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
>
<h:body>
<h:form id="form">
<h:selectOneMenu id="input" value="#{spec1559ITBean.selectedProductId}">
<f:selectItemGroups value="#{spec1559ITBean.categories}" var="category" itemLabel="#{category.name}">
<f:selectItems value="#{category.products}" var="product" itemValue="#{product.id}" itemLabel="#{product.name}" />
</f:selectItemGroups>
</h:selectOneMenu>
<h:commandButton id="submit" value="submit" />
<h:messages id="messages" />
<h:outputText id="output" value="#{spec1559ITBean.selectedProductId}" />
</h:form>
</h:body>
</html>
@@ -0,0 +1,102 @@
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Contributors to Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package com.sun.faces.test.servlet50.selectitemgroups;

import static java.lang.System.getProperty;
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.net.URL;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;

@RunWith(Arquillian.class)
public class Spec1559IT {

@ArquillianResource
private URL webUrl;
private WebClient webClient;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return create(ZipImporter.class, getProperty("finalName") + ".war")
.importFrom(new File("target/" + getProperty("finalName") + ".war"))
.as(WebArchive.class);
}

@Before
public void setUp() {
webClient = new WebClient();
}

@After
public void tearDown() {
webClient.close();
}

@Test
public void test() throws Exception {
HtmlPage page = webClient.getPage(webUrl + "spec1559IT.xhtml");
HtmlSelect select = page.getHtmlElementById("form:input");

assertValidMarkup(select);

select.setSelectedAttribute(select.getOptionByValue("5"), true);

assertEquals("messages is empty before submit", "", page.getHtmlElementById("form:messages").asText());
assertEquals("output is empty before submit", "", page.getHtmlElementById("form:output").asText());

page = page.getHtmlElementById("form:submit").click();

assertValidMarkup(select);
assertEquals("messages is still empty after submit", "", page.getHtmlElementById("form:messages").asText());
assertEquals("output is '5' after submit", "5", page.getHtmlElementById("form:output").asText());
}

private static void assertValidMarkup(HtmlSelect select) {
assertEquals("select has 2 children", 2, select.getChildElementCount());

for (DomElement child : select.getChildElements()) {
assertEquals("child element is an optgroup", "optgroup", child.getNodeName());
assertEquals("child has in turn 3 grandchildren", 3, child.getChildElementCount());

for (DomElement grandchild : child.getChildElements()) {
assertEquals("grandchild element is an option", "option", grandchild.getNodeName());
}
}

assertEquals("select element has 6 options", 6, select.getOptions().size());
HtmlOption option = select.getOptionByValue("5");
assertEquals("5th option is 'Audi'", "Audi", option.getText());
}
}

0 comments on commit 6e573a0

Please sign in to comment.