Created
October 21, 2011 23:36
-
-
Save PiotrNowicki/1305267 to your computer and use it in GitHub Desktop.
Update for JERSEY-777 enhancement. Added tests for multipart form-data for file and inputstream based entities with Grizzly server.
This file contains hidden or 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
Index: src/test/java/com/sun/jersey/multipart/file/FileBasedOperationsHelper.java | |
=================================================================== | |
--- src/test/java/com/sun/jersey/multipart/file/FileBasedOperationsHelper.java (revision 0) | |
+++ src/test/java/com/sun/jersey/multipart/file/FileBasedOperationsHelper.java (revision 0) | |
@@ -0,0 +1,109 @@ | |
+/* | |
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
+ * | |
+ * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. | |
+ * | |
+ * The contents of this file are subject to the terms of either the GNU | |
+ * General Public License Version 2 only ("GPL") or the Common Development | |
+ * and Distribution License("CDDL") (collectively, the "License"). You | |
+ * may not use this file except in compliance with the License. You can | |
+ * obtain a copy of the License at | |
+ * http://glassfish.java.net/public/CDDL+GPL_1_1.html | |
+ * or packager/legal/LICENSE.txt. See the License for the specific | |
+ * language governing permissions and limitations under the License. | |
+ * | |
+ * When distributing the software, include this License Header Notice in each | |
+ * file and include the License file at packager/legal/LICENSE.txt. | |
+ * | |
+ * GPL Classpath Exception: | |
+ * Oracle designates this particular file as subject to the "Classpath" | |
+ * exception as provided by Oracle in the GPL Version 2 section of the License | |
+ * file that accompanied this code. | |
+ * | |
+ * Modifications: | |
+ * If applicable, add the following below the License Header, with the fields | |
+ * enclosed by brackets [] replaced by your own identifying information: | |
+ * "Portions Copyright [year] [name of copyright owner]" | |
+ * | |
+ * Contributor(s): | |
+ * If you wish your version of this file to be governed by only the CDDL or | |
+ * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
+ * elects to include this software in this distribution under the [CDDL or GPL | |
+ * Version 2] license." If you don't indicate a single choice of license, a | |
+ * recipient has the option to distribute your version of this file under | |
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
+ * its licensees as provided above. However, if you add GPL Version 2 code | |
+ * and therefore, elected the GPL Version 2 license, then the option applies | |
+ * only if the new code is made subject to such option by the copyright | |
+ * holder. | |
+ */ | |
+package com.sun.jersey.multipart.file; | |
+ | |
+import static org.junit.Assert.assertEquals; | |
+ | |
+import java.io.File; | |
+import java.net.URL; | |
+ | |
+import javax.ws.rs.core.Response; | |
+ | |
+import com.sun.jersey.api.client.ClientResponse; | |
+ | |
+/** | |
+ * Convenience helper class for file-related tests. | |
+ * | |
+ * It's responsible i.e. for locating the resources in the classpath (to be used | |
+ * for multipart form data file upload) or verifying of the upload result. | |
+ * | |
+ * @see FileDataBodyPartTest | |
+ * @see StreamDataBodyPartTest | |
+ * | |
+ * @author <a href="mailto:[email protected]">Piotr Nowicki</a> | |
+ * | |
+ */ | |
+public class FileBasedOperationsHelper { | |
+ | |
+ /** | |
+ * Gets the <code>File</code> instance from the class-loader relative path. | |
+ * | |
+ * @param path | |
+ * class-loader relative path to the resource | |
+ * @return File located in the given <code>path</code>. | |
+ * | |
+ * @throws IllegalArgumentException | |
+ * if the File in the given path cannot be accessed | |
+ */ | |
+ public static File getFileByRelativePath(String path) { | |
+ URL url = Thread.currentThread().getContextClassLoader() | |
+ .getResource(path); | |
+ File file = new File(url.getFile()); | |
+ | |
+ if (!file.canRead()) { | |
+ throw new IllegalArgumentException("Cannot access file " + file); | |
+ } | |
+ | |
+ return file; | |
+ } | |
+ | |
+ /** | |
+ * Verify that the file has been correctly uploaded to the server. | |
+ * | |
+ * @param expectedLength | |
+ * expected length (in Bytes) of the uploaded file | |
+ * @param response | |
+ * server response for client upload request | |
+ */ | |
+ public static void verifyUploadedFile(long expectedLength, | |
+ ClientResponse response) { | |
+ | |
+ // Assumed that the server responded with uploded file-length value | |
+ long actualLength = Long.valueOf(response.getEntity(String.class)); | |
+ | |
+ int expectedStatus = Response.Status.OK.getStatusCode(); | |
+ int actualStatus = response.getStatus(); | |
+ | |
+ assertEquals("Unsuccessful response from the server", expectedStatus, | |
+ actualStatus); | |
+ assertEquals("Size of uploaded file has changed", expectedLength, | |
+ actualLength); | |
+ } | |
+} | |
Index: src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartResource.java | |
=================================================================== | |
--- src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartResource.java (revision 0) | |
+++ src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartResource.java (revision 0) | |
@@ -0,0 +1,107 @@ | |
+/* | |
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
+ * | |
+ * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. | |
+ * | |
+ * The contents of this file are subject to the terms of either the GNU | |
+ * General Public License Version 2 only ("GPL") or the Common Development | |
+ * and Distribution License("CDDL") (collectively, the "License"). You | |
+ * may not use this file except in compliance with the License. You can | |
+ * obtain a copy of the License at | |
+ * http://glassfish.java.net/public/CDDL+GPL_1_1.html | |
+ * or packager/legal/LICENSE.txt. See the License for the specific | |
+ * language governing permissions and limitations under the License. | |
+ * | |
+ * When distributing the software, include this License Header Notice in each | |
+ * file and include the License file at packager/legal/LICENSE.txt. | |
+ * | |
+ * GPL Classpath Exception: | |
+ * Oracle designates this particular file as subject to the "Classpath" | |
+ * exception as provided by Oracle in the GPL Version 2 section of the License | |
+ * file that accompanied this code. | |
+ * | |
+ * Modifications: | |
+ * If applicable, add the following below the License Header, with the fields | |
+ * enclosed by brackets [] replaced by your own identifying information: | |
+ * "Portions Copyright [year] [name of copyright owner]" | |
+ * | |
+ * Contributor(s): | |
+ * If you wish your version of this file to be governed by only the CDDL or | |
+ * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
+ * elects to include this software in this distribution under the [CDDL or GPL | |
+ * Version 2] license." If you don't indicate a single choice of license, a | |
+ * recipient has the option to distribute your version of this file under | |
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
+ * its licensees as provided above. However, if you add GPL Version 2 code | |
+ * and therefore, elected the GPL Version 2 license, then the option applies | |
+ * only if the new code is made subject to such option by the copyright | |
+ * holder. | |
+ */ | |
+package com.sun.jersey.multipart.file; | |
+ | |
+import java.io.File; | |
+ | |
+import javax.ws.rs.Consumes; | |
+import javax.ws.rs.POST; | |
+import javax.ws.rs.Path; | |
+import javax.ws.rs.Produces; | |
+import javax.ws.rs.core.MediaType; | |
+import javax.ws.rs.core.Response; | |
+ | |
+import com.sun.jersey.multipart.FormDataParam; | |
+ | |
+/** | |
+ * <p> | |
+ * Endpoint resource definition for {@link FileDataBodyPartTest}. | |
+ * </p> | |
+ * | |
+ * @author <a href="mailto:[email protected]">Piotr Nowicki</a> | |
+ */ | |
+@Path(FileDataBodyPartResource.ROOT) | |
+public class FileDataBodyPartResource { | |
+ | |
+ /** | |
+ * Root path of this resource class. | |
+ */ | |
+ public static final String ROOT = "/fileBodyPart"; | |
+ | |
+ /** | |
+ * Path specific to the 'simple upload' resource. | |
+ */ | |
+ public static final String PATH_SIMPLE_UPLOAD = "/simpleUpload"; | |
+ | |
+ /** | |
+ * Name of the expected parameter for 'simple upload' resource. | |
+ */ | |
+ public static final String SIMPLE_UPLOAD_PARAM_NAME = "file"; | |
+ | |
+ /** | |
+ * Endpoint for File-based multipart form data requests. | |
+ * | |
+ * @param file | |
+ * file which is uploading | |
+ * @return length of the uploaded file (in Bytes) if the file was | |
+ * successfully uploaded; error code otherwise | |
+ */ | |
+ @Path(PATH_SIMPLE_UPLOAD) | |
+ @POST | |
+ @Produces(MediaType.TEXT_PLAIN) | |
+ @Consumes(MediaType.MULTIPART_FORM_DATA) | |
+ public Response simpleUpload( | |
+ @FormDataParam(value = SIMPLE_UPLOAD_PARAM_NAME) File file) { | |
+ Response result; | |
+ | |
+ // Just do some basic tests if the file is correctly accessible. | |
+ if (file != null && file.canRead()) { | |
+ String fileLength = file.length() + ""; | |
+ | |
+ result = Response.ok().type(MediaType.TEXT_PLAIN_TYPE) | |
+ .entity(fileLength).build(); | |
+ } else { | |
+ result = Response.serverError().type(MediaType.TEXT_PLAIN_TYPE) | |
+ .build(); | |
+ } | |
+ | |
+ return result; | |
+ } | |
+} | |
Index: src/test/java/com/sun/jersey/multipart/file/StreamDataBodyPartTest.java | |
=================================================================== | |
--- src/test/java/com/sun/jersey/multipart/file/StreamDataBodyPartTest.java (revision 5478) | |
+++ src/test/java/com/sun/jersey/multipart/file/StreamDataBodyPartTest.java (working copy) | |
@@ -39,31 +39,55 @@ | |
*/ | |
package com.sun.jersey.multipart.file; | |
+import static com.sun.jersey.multipart.file.FileBasedOperationsHelper.verifyUploadedFile; | |
+ | |
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import javax.ws.rs.core.MediaType; | |
+import com.sun.jersey.api.client.Client; | |
+import com.sun.jersey.api.client.ClientResponse; | |
+import com.sun.jersey.api.client.UniformInterfaceException; | |
+import com.sun.jersey.api.client.WebResource; | |
+import com.sun.jersey.api.client.config.DefaultClientConfig; | |
import com.sun.jersey.core.header.ContentDisposition; | |
import com.sun.jersey.multipart.BodyPartTest; | |
+import com.sun.jersey.multipart.MultiPart; | |
+import com.sun.jersey.multipart.impl.AbstractGrizzlyServerTester; | |
+ | |
/** | |
- * Tests for the {@link StreamDataBodyPart} class which checks the class' main | |
+ * Tests for the {@link StreamDataBodyPart} class which checks the class main | |
* contract and new functionality. | |
* | |
* @see StreamDataBodyPart | |
* @see FileDataBodyPartTest | |
* | |
- * @author PedroKowalski ([email protected]) | |
+ * @author <a href="mailto:[email protected]">Piotr Nowicki</a> | |
* | |
*/ | |
public class StreamDataBodyPartTest extends BodyPartTest { | |
+ | |
+ /** | |
+ * Server for file uploading. | |
+ * @see FileDataBodyPartTest | |
+ */ | |
+ static AbstractGrizzlyServerTester server = new AbstractGrizzlyServerTester( | |
+ StreamDataBodyPartTest.class.getName()) { | |
+ }; | |
/** | |
+ * A hook to client requests made to the <code>server</code>. | |
+ */ | |
+ Client client; | |
+ | |
+ /** | |
* Class under test. | |
*/ | |
- private StreamDataBodyPart cut; | |
+ StreamDataBodyPart cut; | |
+ | |
public StreamDataBodyPartTest(String testName) { | |
super(testName); | |
} | |
@@ -80,6 +104,9 @@ | |
@Override | |
protected void tearDown() throws Exception { | |
bodyPart = null; | |
+ client = null; | |
+ server.stopServer(); | |
+ | |
super.tearDown(); | |
} | |
@@ -287,6 +314,40 @@ | |
} | |
/////////////////////////////////////////////////////////////////////////// | |
+ // Server tests | |
+ /////////////////////////////////////////////////////////////////////////// | |
+ | |
+ public void testSimpleUpload() { | |
+ server.startServer(FileDataBodyPartResource.class); | |
+ client = Client.create(new DefaultClientConfig()); | |
+ | |
+ String path = FileDataBodyPartResource.ROOT | |
+ + FileDataBodyPartResource.PATH_SIMPLE_UPLOAD; | |
+ | |
+ WebResource.Builder builder = client.resource(server.getUri()) | |
+ .path(path).accept(MediaType.TEXT_PLAIN_TYPE) | |
+ .type(MediaType.MULTIPART_FORM_DATA_TYPE); | |
+ try { | |
+ // Just some random data to fake the input stream. | |
+ byte[] data = new byte[] {5, 44, 2, -33, 91, 125}; | |
+ | |
+ InputStream is = new ByteArrayInputStream(data); | |
+ | |
+ // Filename of the sent stream is not relevant for this test. | |
+ StreamDataBodyPart streamData = new StreamDataBodyPart( | |
+ FileDataBodyPartResource.SIMPLE_UPLOAD_PARAM_NAME, is); | |
+ MultiPart part = new MultiPart().bodyPart(streamData); | |
+ | |
+ // Invoke the real request. | |
+ ClientResponse result = builder.post(ClientResponse.class, part); | |
+ | |
+ verifyUploadedFile(data.length, result); | |
+ } catch (UniformInterfaceException e) { | |
+ fail("Caught exception: " + e); | |
+ } | |
+ } | |
+ | |
+ /////////////////////////////////////////////////////////////////////////// | |
// Misc tests | |
/////////////////////////////////////////////////////////////////////////// | |
Index: src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartTest.java | |
=================================================================== | |
--- src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartTest.java (revision 5478) | |
+++ src/test/java/com/sun/jersey/multipart/file/FileDataBodyPartTest.java (working copy) | |
@@ -39,116 +39,181 @@ | |
*/ | |
package com.sun.jersey.multipart.file; | |
-import com.sun.jersey.multipart.BodyPart; | |
-import com.sun.jersey.multipart.BodyPartTest; | |
+import static com.sun.jersey.multipart.file.FileBasedOperationsHelper.getFileByRelativePath; | |
+import static com.sun.jersey.multipart.file.FileBasedOperationsHelper.verifyUploadedFile; | |
+ | |
import java.io.File; | |
+ | |
import javax.ws.rs.core.MediaType; | |
+import com.sun.jersey.api.client.Client; | |
+import com.sun.jersey.api.client.ClientResponse; | |
+import com.sun.jersey.api.client.UniformInterfaceException; | |
+import com.sun.jersey.api.client.WebResource; | |
+import com.sun.jersey.api.client.config.DefaultClientConfig; | |
+import com.sun.jersey.multipart.BodyPart; | |
+import com.sun.jersey.multipart.BodyPartTest; | |
+import com.sun.jersey.multipart.MultiPart; | |
+import com.sun.jersey.multipart.impl.AbstractGrizzlyServerTester; | |
+ | |
/** | |
- * <p>Test case for {@link BodyPart}.</p> | |
- * | |
+ * <p> | |
+ * Test case for {@link BodyPart}. | |
+ * </p> | |
+ * | |
* @author [email protected] | |
* @author [email protected] | |
+ * @author <a href="mailto:[email protected]">Piotr Nowicki</a> | |
*/ | |
-public class FileDataBodyPartTest | |
- extends BodyPartTest { | |
+public class FileDataBodyPartTest extends BodyPartTest { | |
- public FileDataBodyPartTest(String testName) { | |
- super(testName); | |
- } | |
+ /** | |
+ * Server for file uploading. | |
+ */ | |
+ // We're already extending a class; using composition instead of | |
+ // inheritance. | |
+ static AbstractGrizzlyServerTester server = new AbstractGrizzlyServerTester( | |
+ FileDataBodyPartTest.class.getName()) { | |
+ }; | |
- @Override | |
- protected void setUp() | |
- throws Exception { | |
- super.setUp(); | |
- bodyPart = new FileDataBodyPart(); | |
- } | |
+ /** | |
+ * Path to the exemplary but real (must exist and be reachable by the | |
+ * class-loader) file which will be uploaded. | |
+ */ | |
+ public static final String SIMPLE_INPUT_FILENAME = "simpleInputFile.dat"; | |
- @Override | |
- protected void tearDown() | |
- throws Exception { | |
- bodyPart = null; | |
- super.tearDown(); | |
- } | |
+ /** | |
+ * A hook to client requests made to the <code>server</code>. | |
+ */ | |
+ Client client; | |
- @Override | |
- public void testEntity() { | |
- try { | |
- bodyPart.setEntity("foo bar baz"); | |
- } | |
- catch (UnsupportedOperationException exception) { | |
- //exception expected. | |
- } | |
- } | |
+ public FileDataBodyPartTest(String testName) { | |
+ super(testName); | |
+ } | |
- public void testCreateFDBP() { | |
+ @Override | |
+ protected void setUp() throws Exception { | |
+ // Only few methods uses Grizzly server, so server startup is made in | |
+ // beginning of the particular server-aware test method. | |
+ super.setUp(); | |
+ bodyPart = new FileDataBodyPart(); | |
+ } | |
- FileDataBodyPart fdbp = (FileDataBodyPart) bodyPart; | |
- assertNull(fdbp.getFormDataContentDisposition()); | |
- assertNull(fdbp.getName()); | |
- assertNull(fdbp.getValue()); | |
- assertTrue(fdbp.isSimple()); | |
- String name; | |
+ @Override | |
+ protected void tearDown() throws Exception { | |
+ bodyPart = null; | |
- File file = new File("pom.xml"); | |
- name = "xml"; | |
- fdbp = new FileDataBodyPart(name, file); | |
- MediaType expectedType = MediaType.APPLICATION_XML_TYPE; | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
- fdbp.setName(name); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
- fdbp.setFileEntity(file); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
+ // The cleanup of client data is harmful and can be done in advance. | |
+ client = null; | |
+ server.stopServer(); | |
+ | |
+ super.tearDown(); | |
+ } | |
- fdbp = new FileDataBodyPart(name, file, expectedType); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
- fdbp.setFileEntity(file, expectedType); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
+ public void testSimpleUpload() { | |
+ server.startServer(FileDataBodyPartResource.class); | |
+ client = Client.create(new DefaultClientConfig()); | |
+ String resourcePath = FileDataBodyPartResource.ROOT | |
+ + FileDataBodyPartResource.PATH_SIMPLE_UPLOAD; | |
- file = new File("pom.png"); | |
- name = "png"; | |
- fdbp = new FileDataBodyPart("png", file); | |
- expectedType = DefaultMediaTypePredictor.CommonMediaTypes.PNG.getMediaType(); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
+ WebResource.Builder builder = client.resource(server.getUri()) | |
+ .path(resourcePath).accept(MediaType.TEXT_PLAIN_TYPE) | |
+ .type(MediaType.MULTIPART_FORM_DATA_TYPE); | |
+ try { | |
+ File file = getFileByRelativePath(SIMPLE_INPUT_FILENAME); | |
- file = new File("pom.zip"); | |
- fdbp = new FileDataBodyPart(name, file); | |
- expectedType = DefaultMediaTypePredictor.CommonMediaTypes.ZIP.getMediaType(); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
+ // Prepare multipart data to be sent. | |
+ FileDataBodyPart fileData = new FileDataBodyPart( | |
+ FileDataBodyPartResource.SIMPLE_UPLOAD_PARAM_NAME, file); | |
+ MultiPart data = new MultiPart().bodyPart(fileData); | |
- file = new File("pom.avi"); | |
- fdbp = new FileDataBodyPart(name, file); | |
- expectedType = DefaultMediaTypePredictor.CommonMediaTypes.AVI.getMediaType(); | |
- checkEntityAttributes(name, fdbp, file, expectedType); | |
+ // Invoke the real request. | |
+ ClientResponse result = builder.post(ClientResponse.class, data); | |
- } | |
+ verifyUploadedFile(file.length(), result); | |
+ } catch (UniformInterfaceException e) { | |
+ fail("Caught exception: " + e); | |
+ } | |
+ } | |
- private void checkEntityAttributes(final String name, | |
- final FileDataBodyPart fdbp, | |
- final File file, | |
- final MediaType expectedType) { | |
- if (name != null) { | |
- assertEquals(name, fdbp.getName()); | |
- assertEquals(name, fdbp.getFormDataContentDisposition().getName()); | |
- assertEquals(file.getName(), fdbp.getContentDisposition(). | |
- getFileName()); | |
- if (file.exists()) { | |
- assertEquals(file.length(), | |
- fdbp.getContentDisposition().getSize()); | |
- assertEquals(file.lastModified(), | |
- fdbp.getContentDisposition().getModificationDate().getTime()); | |
- } | |
- else { | |
- assertEquals(-1, fdbp.getContentDisposition().getSize()); | |
- } | |
- } | |
- else { | |
- assertNull(fdbp.getName()); | |
- assertNull(fdbp.getFormDataContentDisposition()); | |
- } | |
- assertEquals(file, fdbp.getEntity()); | |
- assertTrue(!fdbp.isSimple()); | |
- assertEquals(expectedType, fdbp.getMediaType()); | |
- } | |
+ @Override | |
+ public void testEntity() { | |
+ try { | |
+ bodyPart.setEntity("foo bar baz"); | |
+ } catch (UnsupportedOperationException exception) { | |
+ // exception expected. | |
+ } | |
+ } | |
+ | |
+ public void testCreateFDBP() { | |
+ | |
+ FileDataBodyPart fdbp = (FileDataBodyPart) bodyPart; | |
+ assertNull(fdbp.getFormDataContentDisposition()); | |
+ assertNull(fdbp.getName()); | |
+ assertNull(fdbp.getValue()); | |
+ assertTrue(fdbp.isSimple()); | |
+ String name; | |
+ | |
+ File file = new File("pom.xml"); | |
+ name = "xml"; | |
+ fdbp = new FileDataBodyPart(name, file); | |
+ MediaType expectedType = MediaType.APPLICATION_XML_TYPE; | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ fdbp.setName(name); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ fdbp.setFileEntity(file); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ | |
+ fdbp = new FileDataBodyPart(name, file, expectedType); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ fdbp.setFileEntity(file, expectedType); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ | |
+ file = new File("pom.png"); | |
+ name = "png"; | |
+ fdbp = new FileDataBodyPart("png", file); | |
+ expectedType = DefaultMediaTypePredictor.CommonMediaTypes.PNG | |
+ .getMediaType(); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ | |
+ file = new File("pom.zip"); | |
+ fdbp = new FileDataBodyPart(name, file); | |
+ expectedType = DefaultMediaTypePredictor.CommonMediaTypes.ZIP | |
+ .getMediaType(); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ | |
+ file = new File("pom.avi"); | |
+ fdbp = new FileDataBodyPart(name, file); | |
+ expectedType = DefaultMediaTypePredictor.CommonMediaTypes.AVI | |
+ .getMediaType(); | |
+ checkEntityAttributes(name, fdbp, file, expectedType); | |
+ | |
+ } | |
+ | |
+ private void checkEntityAttributes(final String name, | |
+ final FileDataBodyPart fdbp, final File file, | |
+ final MediaType expectedType) { | |
+ if (name != null) { | |
+ assertEquals(name, fdbp.getName()); | |
+ assertEquals(name, fdbp.getFormDataContentDisposition().getName()); | |
+ assertEquals(file.getName(), fdbp.getContentDisposition() | |
+ .getFileName()); | |
+ if (file.exists()) { | |
+ assertEquals(file.length(), fdbp.getContentDisposition() | |
+ .getSize()); | |
+ assertEquals(file.lastModified(), fdbp.getContentDisposition() | |
+ .getModificationDate().getTime()); | |
+ } else { | |
+ assertEquals(-1, fdbp.getContentDisposition().getSize()); | |
+ } | |
+ } else { | |
+ assertNull(fdbp.getName()); | |
+ assertNull(fdbp.getFormDataContentDisposition()); | |
+ } | |
+ assertEquals(file, fdbp.getEntity()); | |
+ assertTrue(!fdbp.isSimple()); | |
+ assertEquals(expectedType, fdbp.getMediaType()); | |
+ } | |
+ | |
} | |
Index: src/test/java/com/sun/jersey/multipart/impl/AbstractGrizzlyServerTester.java | |
=================================================================== | |
--- src/test/java/com/sun/jersey/multipart/impl/AbstractGrizzlyServerTester.java (revision 5478) | |
+++ src/test/java/com/sun/jersey/multipart/impl/AbstractGrizzlyServerTester.java (working copy) | |
@@ -40,16 +40,19 @@ | |
package com.sun.jersey.multipart.impl; | |
-import com.sun.jersey.api.container.ContainerFactory; | |
-import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; | |
-import com.sun.jersey.api.core.ResourceConfig; | |
+import java.io.IOException; | |
+import java.net.URI; | |
+ | |
+import javax.ws.rs.core.UriBuilder; | |
+ | |
import junit.framework.TestCase; | |
+ | |
import org.glassfish.grizzly.http.server.HttpHandler; | |
import org.glassfish.grizzly.http.server.HttpServer; | |
-import javax.ws.rs.core.UriBuilder; | |
-import java.io.IOException; | |
-import java.net.URI; | |
+import com.sun.jersey.api.container.ContainerFactory; | |
+import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; | |
+import com.sun.jersey.api.core.ResourceConfig; | |
/** | |
* | |
@@ -85,7 +88,7 @@ | |
return UriBuilder.fromUri("http://localhost").port(port).path(CONTEXT).build(); | |
} | |
- public void startServer(Class... resources) { | |
+ public void startServer(Class<?>... resources) { | |
start(ContainerFactory.createContainer(HttpHandler.class, resources)); | |
} | |
@@ -121,7 +124,7 @@ | |
} | |
public void stopServer() { | |
- if (httpServer.isStarted()) { | |
+ if (httpServer != null && httpServer.isStarted()) { | |
httpServer.stop(); | |
} | |
} | |
Index: src/test/resources/simpleInputFile.dat | |
=================================================================== | |
--- src/test/resources/simpleInputFile.dat (revision 0) | |
+++ src/test/resources/simpleInputFile.dat (revision 0) | |
@@ -0,0 +1,5 @@ | |
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut | |
+ labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco | |
+ laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in | |
+ voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat | |
+ non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
\ No newline at end of file | |
Index: src/main/java/com/sun/jersey/multipart/file/StreamDataBodyPart.java | |
=================================================================== | |
--- src/main/java/com/sun/jersey/multipart/file/StreamDataBodyPart.java (revision 5478) | |
+++ src/main/java/com/sun/jersey/multipart/file/StreamDataBodyPart.java (working copy) | |
@@ -53,22 +53,23 @@ | |
* Represents an {@link InputStream} based file submission as a part of the | |
* multipart/form-data. | |
* </p> | |
- * | |
+ * | |
* <p> | |
* It sets the {@link InputStream} as a body part with the default | |
* {@link MediaType#APPLICATION_OCTET_STREAM_TYPE} (if not specified by the | |
- * user).<br /><strong>Note</strong> that the MIME type of the entity cannot be | |
+ * user).<br /> | |
+ * <strong>Note</strong> that the MIME type of the entity cannot be | |
* automatically predicted as in case of {@link FileDataBodyPart}. | |
* </p> | |
- * | |
+ * | |
* <p> | |
* The filename of the attachment is set by the user or defaults to the part's | |
* name. | |
* </p> | |
- * | |
+ * | |
* @see FileDataBodyPart | |
- * @author PedroKowalski ([email protected]) | |
- * | |
+ * @author <a href="mailto:[email protected]">Piotr Nowicki</a> | |
+ * | |
*/ | |
public class StreamDataBodyPart extends FormDataBodyPart { | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment