Author: nbelaevski
Date: 2011-04-21 09:15:55 -0400 (Thu, 21 Apr 2011)
New Revision: 22437
Added:
trunk/ui/input/ui/src/test/java/org/richfaces/request/
trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java
trunk/ui/input/ui/src/test/java/org/richfaces/request/UploadedFile25Test.java
Log:
RF-10128
Added: trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java
===================================================================
--- trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java
(rev 0)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java 2011-04-21
13:15:55 UTC (rev 22437)
@@ -0,0 +1,121 @@
+/*
+ * GENERATED FILE - DO NOT EDIT
+ */
+
+package org.richfaces.request;
+
+import static org.easymock.EasyMock.createControl;
+import static org.jboss.test.faces.mock.FacesMockController.findMethod;
+import static org.jboss.test.faces.mock.FacesMockController.invokeMethod;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+
+import org.easymock.IMocksControl;
+import org.easymock.internal.ThrowableWrapper;
+import org.jboss.test.faces.mock.FacesMockController.MockObject;
+
+public class MockUploadResource extends FileUploadResource implements MockObject {
+
+ private final IMocksControl control;
+
+ private final String name;
+
+ /**
+ * Default constructor
+ */
+ public MockUploadResource() {
+ super(null, null);
+ this.control = createControl();
+ this.name = null;
+ }
+
+ /**
+ * @param control
+ */
+ public MockUploadResource(IMocksControl control, String name) {
+ super(null, null);
+ this.control = control;
+ this.name = name;
+ }
+
+ public IMocksControl getControl() {
+ return control;
+ }
+
+ public String toString() {
+ return getClass().getSimpleName() + (name != null ? name : "");
+ }
+
+ public boolean equals(Object obj) {
+ return this == obj;
+ }
+
+ public int hashCode() {
+ if (name != null) {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + name.hashCode();
+ result = prime * result + getClass().getName().hashCode();
+ return result;
+ } else {
+ return System.identityHashCode(this);
+ }
+ }
+
+ private static final Method createMethod0 = findMethod(FileUploadResource.class,
"create");
+
+ public void create() throws IOException {
+ invokeMethod(this.control, this, createMethod0);
+ }
+
+ private static final Method completeMethod0 = findMethod(FileUploadResource.class,
"complete");
+
+ public void complete() {
+ invokeMethod(this.control, this, completeMethod0);
+ }
+
+ private static final Method handleMethod0 = findMethod(FileUploadResource.class,
"handle", byte[].class, Integer.TYPE);
+
+ public void handle(byte[] bytes, int length) throws IOException {
+ invokeMethod(this.control, this, handleMethod0, bytes, length);
+ }
+
+ private static final Method getInputStreamMethod0 =
findMethod(FileUploadResource.class, "getInputStream");
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ try {
+ return invokeMethod(this.control, this, getInputStreamMethod0);
+ } catch (RuntimeException e) {
+ if (e.getCause() instanceof ThrowableWrapper && ((ThrowableWrapper)
e.getCause()).getThrowable() instanceof IOException) {
+ throw (IOException) ((ThrowableWrapper) e.getCause()).getThrowable();
+ } else {
+ throw e;
+ }
+ }
+ }
+
+ private static final Method getSizeMethod0 = findMethod(FileUploadResource.class,
"getSize");
+
+ @Override
+ public long getSize() {
+ return invokeMethod(this.control, this, getSizeMethod0);
+ }
+
+ private static final Method writeMethod0 = findMethod(FileUploadResource.class,
"write", String.class);
+
+ @Override
+ public void write(String fileName) throws IOException {
+ invokeMethod(this.control, this, writeMethod0, fileName);
+ }
+
+ private static final Method deleteMethod0 = findMethod(FileUploadResource.class,
"delete");
+
+ @Override
+ public void delete() throws IOException {
+ invokeMethod(this.control, this, deleteMethod0);
+ }
+
+}
Added: trunk/ui/input/ui/src/test/java/org/richfaces/request/UploadedFile25Test.java
===================================================================
--- trunk/ui/input/ui/src/test/java/org/richfaces/request/UploadedFile25Test.java
(rev 0)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/UploadedFile25Test.java 2011-04-21
13:15:55 UTC (rev 22437)
@@ -0,0 +1,166 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.request;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.easymock.EasyMock;
+import org.easymock.IAnswer;
+import org.jboss.test.faces.mock.MockTestRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.richfaces.exception.FileUploadException;
+
+import com.google.common.collect.LinkedListMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+import com.google.common.io.ByteStreams;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+(a)RunWith(MockTestRunner.class)
+public class UploadedFile25Test {
+
+ private static final String TEST_DATA = "test data";
+
+ private static final IAnswer<InputStream> INPUT_STREAM_SUPPLIER = new
IAnswer<InputStream>() {
+
+ public InputStream answer() throws Throwable {
+ return new ByteArrayInputStream(TEST_DATA.getBytes());
+ }
+ };
+
+ private UploadedFile25 uploadedFile;
+
+ private MockUploadResource uploadResource;
+
+ @Before
+ public void setUp() throws Exception {
+ Multimap<String,String> headers = LinkedListMultimap.<String,
String>create();
+
+ headers.put("content-type", "image/png");
+ headers.put("filename", "x.png");
+ headers.put("x-rftest", "set");
+ headers.put("x-rftest", "of");
+ headers.put("x-rftest", "values");
+
+ uploadResource = new MockUploadResource();
+ uploadedFile = new UploadedFile25("form:fileUpload", uploadResource,
headers);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ uploadResource = null;
+ uploadedFile = null;
+ }
+
+ @Test
+ public void testAttributes() throws Exception {
+ assertEquals("form:fileUpload", uploadedFile.getParameterName());
+ assertEquals("image/png", uploadedFile.getContentType());
+ assertEquals("x.png", uploadedFile.getName());
+ }
+
+ @Test
+ public void testHeaders() throws Exception {
+ assertEquals("image/png",
uploadedFile.getHeader("Content-Type"));
+ assertEquals("set", uploadedFile.getHeader("X-RFTest"));
+ assertNull(uploadedFile.getHeader("X-RFUnknown"));
+
+ assertEquals(Arrays.asList("image/png"),
Lists.newArrayList(uploadedFile.getHeaders("Content-Type")));
+ assertEquals(Arrays.asList("set", "of", "values"),
Lists.newArrayList(uploadedFile.getHeaders("X-RFTest")));
+ assertEquals(Arrays.asList(),
Lists.newArrayList(uploadedFile.getHeaders("X-RFUnknown")));
+
+ Collection<String> sortedHeaderNames =
Sets.newTreeSet(uploadedFile.getHeaderNames());
+ assertEquals(Sets.newLinkedHashSet(Arrays.asList("content-type",
"filename", "x-rftest")), sortedHeaderNames);
+ }
+
+ @Test
+ public void testResource() throws Exception {
+ EasyMock.expect(uploadResource.getSize()).andStubReturn((long)
TEST_DATA.length());
+
EasyMock.expect(uploadResource.getInputStream()).andStubAnswer(INPUT_STREAM_SUPPLIER);
+
+ uploadResource.write(EasyMock.eq("output.png"));
+ EasyMock.expectLastCall();
+
+ uploadResource.delete();
+ EasyMock.expectLastCall();
+
+ uploadResource.getControl().replay();
+
+ assertEquals(TEST_DATA.length(), uploadedFile.getSize());
+ assertNotNull(uploadedFile.getInputStream());
+
+ assertEquals(TEST_DATA, new
String(ByteStreams.toByteArray(uploadedFile.getInputStream()), "US-ASCII"));
+ assertEquals(TEST_DATA, new String(uploadedFile.getData(),
"US-ASCII"));
+
+ uploadedFile.write("output.png");
+ uploadedFile.delete();
+ }
+
+ @Test
+ public void testGetDataExceptions() throws Exception {
+ EasyMock.expect(uploadResource.getSize()).andStubReturn((long) Long.MAX_VALUE);
+
EasyMock.expect(uploadResource.getInputStream()).andStubAnswer(INPUT_STREAM_SUPPLIER);
+
+ uploadResource.getControl().replay();
+
+ try {
+ uploadedFile.getData();
+
+ fail();
+ } catch (FileUploadException e) {
+ //expected - ignore
+ }
+
+ uploadResource.getControl().reset();
+
+ EasyMock.expect(uploadResource.getSize()).andStubReturn(1l);
+ EasyMock.expect(uploadResource.getInputStream()).andStubThrow(new
IOException("No stream available"));
+
+ uploadResource.getControl().replay();
+
+ try {
+ uploadedFile.getData();
+
+ fail();
+ } catch (FileUploadException e) {
+ //expected - ignore
+ assertTrue(e.getCause() instanceof IOException);
+ }
+ }
+}