Skip to content

Commit a6f39fd

Browse files
committed
Merge pull request javaee-samples#130 from kubamarchwicki/master
First tests for javaee-samples#84
2 parents 48aa9d8 + 420e958 commit a6f39fd

File tree

9 files changed

+132
-303
lines changed

9 files changed

+132
-303
lines changed

servlet/file-upload/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<artifactId>file-upload</artifactId>
1313
<version>1.0-SNAPSHOT</version>
1414
<packaging>war</packaging>
15+
1516
</project>

servlet/file-upload/src/main/java/org/javaee7/servlet/file/upload/TestServlet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
*/
4040
package org.javaee7.servlet.file.upload;
4141

42-
import java.io.IOException;
43-
import java.io.PrintWriter;
4442
import javax.servlet.ServletException;
4543
import javax.servlet.annotation.MultipartConfig;
4644
import javax.servlet.annotation.WebServlet;
4745
import javax.servlet.http.HttpServlet;
4846
import javax.servlet.http.HttpServletRequest;
4947
import javax.servlet.http.HttpServletResponse;
5048
import javax.servlet.http.Part;
49+
import java.io.IOException;
50+
import java.io.PrintWriter;
5151

5252
/**
5353
* @author Arun Gupta

servlet/file-upload/src/main/webapp/index.jsp

-59
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javaee7.servlet.file.upload;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.entity.mime.MultipartEntity;
7+
import org.apache.http.entity.mime.content.FileBody;
8+
import org.apache.http.impl.client.DefaultHttpClient;
9+
import org.apache.http.util.EntityUtils;
10+
import org.jboss.arquillian.container.test.api.Deployment;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.arquillian.test.api.ArquillianResource;
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.spec.WebArchive;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.net.URISyntaxException;
21+
import java.net.URL;
22+
23+
import static org.hamcrest.CoreMatchers.*;
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* @author Jakub Marchwicki
28+
*/
29+
@RunWith(Arquillian.class)
30+
public class FileUploadTest {
31+
32+
@ArquillianResource
33+
private URL base;
34+
35+
@Deployment(testable = false)
36+
public static WebArchive deploy() throws URISyntaxException {
37+
return ShrinkWrap.create(WebArchive.class)
38+
.addClasses(TestServlet.class);
39+
}
40+
41+
@Test
42+
public void uploadFile() throws IOException, URISyntaxException {
43+
44+
HttpClient client = new DefaultHttpClient();
45+
HttpPost postRequest = new HttpPost(new URL(base, "TestServlet").toURI());
46+
47+
MultipartEntity multiPartEntity = new MultipartEntity();
48+
FileBody fileBody = new FileBody(new File("pom.xml"));
49+
multiPartEntity.addPart("attachment", fileBody);
50+
51+
postRequest.setEntity(multiPartEntity);
52+
HttpResponse response = client.execute(postRequest);
53+
54+
String servletOutput = EntityUtils.toString(response.getEntity());
55+
56+
assertThat(response.getStatusLine().getStatusCode(), is(equalTo(200)));
57+
assertThat(servletOutput, containsString("Received 1 parts"));
58+
assertThat(servletOutput, containsString("writing pom.xml part"));
59+
assertThat(servletOutput, containsString("uploaded to: /tmp/pom.xml"));
60+
}
61+
62+
}

servlet/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,19 @@
3333
<module>web-fragment</module>
3434
</modules>
3535

36+
<dependencies>
37+
<dependency>
38+
<groupId>org.apache.httpcomponents</groupId>
39+
<artifactId>httpclient</artifactId>
40+
<version>4.2.1</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.httpcomponents</groupId>
45+
<artifactId>httpmime</artifactId>
46+
<version>4.2.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
3651
</project>

servlet/resource-packaging/src/main/java/org/javaee7/servlet/resource/packaging/TestServlet.java

-129
This file was deleted.

servlet/resource-packaging/src/main/webapp/index.jsp

-58
This file was deleted.

0 commit comments

Comments
 (0)