-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IT for single and multiple file upload
- Loading branch information
Showing
6 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>inputFile</artifactId> | ||
<packaging>war</packaging> | ||
|
||
<name>Mojarra ${project.version} - Test - Faces 4.0 - h:inputFile</name> | ||
|
||
<build> | ||
<finalName>test-faces40-inputFile</finalName> | ||
</build> | ||
</project> |
67 changes: 67 additions & 0 deletions
67
...aces40/inputFile/src/main/java/com/sun/faces/test/servlet50/inputfile/Spec1555ITBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
package com.sun.faces.test.servlet50.inputfile; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.faces.application.FacesMessage; | ||
import jakarta.faces.context.FacesContext; | ||
import jakarta.inject.Named; | ||
import jakarta.servlet.http.Part; | ||
|
||
@Named | ||
@RequestScoped | ||
public class Spec1555ITBean { | ||
|
||
private Part singleSelection; | ||
private List<Part> multipleSelection; | ||
|
||
public void submitSingleSelection() { | ||
addAsMessage("singleSelection", singleSelection); | ||
} | ||
|
||
public void submitMultipleSelection() { | ||
for (Part part : multipleSelection) { | ||
addAsMessage("multipleSelection", part); | ||
} | ||
} | ||
|
||
private static void addAsMessage(String field, Part part) { | ||
String name = Paths.get(part.getSubmittedFileName()).getFileName().toString(); | ||
long size = part.getSize(); | ||
|
||
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("field: " + field + ", name: " + name + ", size: " + size)); | ||
} | ||
|
||
public Part getSingleSelection() { | ||
return singleSelection; | ||
} | ||
|
||
public void setSingleSelection(Part singleSelection) { | ||
this.singleSelection = singleSelection; | ||
} | ||
|
||
public List<Part> getMultipleSelection() { | ||
return multipleSelection; | ||
} | ||
|
||
public void setMultipleSelection(List<Part> multipleSelection) { | ||
this.multipleSelection = multipleSelection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>spec1555IT.xhtml</welcome-file> | ||
</welcome-file-list> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!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:head/> | ||
|
||
<h:body> | ||
<h:form id="singleSelectionFormNonAjax" enctype="multipart/form-data"> | ||
<h:inputFile id="input" value="#{spec1555ITBean.singleSelection}" /> | ||
<h:commandButton id="submit" value="submit" action="#{spec1555ITBean.submitSingleSelection}" /> | ||
</h:form> | ||
<h:form id="singleSelectionFormAjax" enctype="multipart/form-data"> | ||
<h:inputFile id="input" value="#{spec1555ITBean.singleSelection}" /> | ||
<h:commandButton id="submit" value="submit" action="#{spec1555ITBean.submitSingleSelection}"> | ||
<f:ajax execute="@form" render="@form :messages" /> | ||
</h:commandButton> | ||
</h:form> | ||
<h:form id="multipleSelectionFormNonAjax" enctype="multipart/form-data"> | ||
<h:inputFile id="input" value="#{spec1555ITBean.multipleSelection}" multiple="true" /> | ||
<h:commandButton id="submit" value="submit" action="#{spec1555ITBean.submitMultipleSelection}" /> | ||
</h:form> | ||
<h:form id="multipleSelectionFormAjax" enctype="multipart/form-data"> | ||
<h:inputFile id="input" value="#{spec1555ITBean.multipleSelection}" multiple="true" /> | ||
<h:commandButton id="submit" value="submit" action="#{spec1555ITBean.submitMultipleSelection}"> | ||
<f:ajax execute="@form" render="@form :messages" /> | ||
</h:commandButton> | ||
</h:form> | ||
<h:messages id="messages" /> | ||
</h:body> | ||
</html> |
164 changes: 164 additions & 0 deletions
164
test2/faces40/inputFile/src/test/java/com/sun/faces/test/servlet50/inputfile/Spec1555IT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
package com.sun.faces.test.servlet50.inputfile; | ||
|
||
import static java.lang.System.getProperty; | ||
import static java.nio.file.StandardOpenOption.APPEND; | ||
import static org.jboss.shrinkwrap.api.ShrinkWrap.create; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
|
||
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 org.xml.sax.helpers.AttributesImpl; | ||
|
||
import com.gargoylesoftware.htmlunit.WebClient; | ||
import com.gargoylesoftware.htmlunit.html.DomElement; | ||
import com.gargoylesoftware.htmlunit.html.HtmlElement; | ||
import com.gargoylesoftware.htmlunit.html.HtmlFileInput; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser; | ||
|
||
@RunWith(Arquillian.class) | ||
public class Spec1555IT { | ||
|
||
@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 testSingleSelectionNonAjax() throws Exception { | ||
testSingleSelection("singleSelectionFormNonAjax"); | ||
} | ||
|
||
@Test | ||
public void testSingleSelectionAjax() throws Exception { | ||
testSingleSelection("singleSelectionFormAjax"); | ||
} | ||
|
||
private void testSingleSelection(String form) throws Exception { | ||
HtmlPage page = webClient.getPage(webUrl + "spec1555IT.xhtml"); | ||
HtmlFileInput input = page.getHtmlElementById(form + ":input"); | ||
|
||
assertEquals("Multiple attribute is NOT set", "", input.getAttribute("multiple")); | ||
|
||
File file = generateTempFile("file", "bin", 123); | ||
input.setValueAttribute(file.getAbsolutePath()); | ||
page = page.getHtmlElementById(form + ":submit").click(); | ||
|
||
assertEquals("Value attribute is NOT set", "", page.getHtmlElementById(form + ":input").getAttribute("value")); | ||
|
||
HtmlElement messages = page.getHtmlElementById("messages"); | ||
|
||
assertEquals("There is 1 message", 1, messages.getChildElementCount()); | ||
|
||
DomElement message = messages.getChildElements().iterator().next(); | ||
|
||
assertEquals("Uploaded file has been received", "field: singleSelection, name: " + file.getName() + ", size: " + file.length(), message.asText()); | ||
} | ||
|
||
@Test | ||
public void testMultipleSelectionNonAjax() throws Exception { | ||
testMultipleSelection("multipleSelectionFormNonAjax"); | ||
} | ||
|
||
@Test | ||
public void testMultipleSelectionAjax() throws Exception { | ||
testMultipleSelection("multipleSelectionFormAjax"); | ||
} | ||
|
||
private void testMultipleSelection(String form) throws Exception { | ||
HtmlPage page = webClient.getPage(webUrl + "spec1555IT.xhtml"); | ||
HtmlFileInput input = page.getHtmlElementById(form + ":input"); | ||
|
||
assertEquals("Multiple attribute is set", "multiple", input.getAttribute("multiple")); | ||
|
||
File file1 = generateTempFile("file1", "bin", 123); | ||
File file2 = generateTempFile("file2", "bin", 234); | ||
File file3 = generateTempFile("file3", "bin", 345); | ||
input.setValueAttribute(file1.getAbsolutePath()); | ||
clone(input).setValueAttribute(file2.getAbsolutePath()); | ||
clone(input).setValueAttribute(file3.getAbsolutePath()); | ||
page = page.getHtmlElementById(form + ":submit").click(); | ||
|
||
assertEquals("Value attribute is NOT set", "", page.getHtmlElementById(form + ":input").getAttribute("value")); | ||
|
||
HtmlElement messages = page.getHtmlElementById("messages"); | ||
|
||
assertEquals("There are 3 messages", 3, messages.getChildElementCount()); | ||
|
||
Iterator<DomElement> iterator = messages.getChildElements().iterator(); | ||
DomElement message1 = iterator.next(); | ||
DomElement message2 = iterator.next(); | ||
DomElement message3 = iterator.next(); | ||
|
||
assertEquals("First uploaded file has been received", "field: multipleSelection, name: " + file1.getName() + ", size: " + file1.length(), message1.asText()); | ||
assertEquals("Second uploaded file has been received", "field: multipleSelection, name: " + file2.getName() + ", size: " + file2.length(), message2.asText()); | ||
assertEquals("Third uploaded file has been received", "field: multipleSelection, name: " + file3.getName() + ", size: " + file3.length(), message3.asText()); | ||
} | ||
|
||
private static File generateTempFile(String name, String ext, int size) throws IOException { | ||
Path path = Files.createTempFile(name, "." + ext); | ||
byte[] content = new byte[size]; | ||
Files.write(path, content, APPEND); | ||
return path.toFile(); | ||
} | ||
|
||
/** | ||
* HtmlUnit's HtmlFileInput doesn't support submitting multiple values. | ||
* The below is a work-around, found on https://stackoverflow.com/a/19654060 | ||
*/ | ||
private static HtmlFileInput clone(HtmlFileInput input) { | ||
AttributesImpl attributes = new AttributesImpl(); | ||
attributes.addAttribute(null, null, "type", null, "file"); | ||
attributes.addAttribute(null, null, "name", null, input.getNameAttribute()); | ||
HtmlFileInput cloned = (HtmlFileInput) new HtmlUnitNekoHtmlParser().getFactory("input").createElementNS(input.getPage(), null, "input", attributes); | ||
input.getParentNode().appendChild(cloned); | ||
return cloned; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,6 @@ | |
|
||
<modules> | ||
<module>selectItemGroups</module> | ||
<module>inputFile</module> | ||
</modules> | ||
</project> |