Author: nbelaevski
Date: 2011-04-21 11:55:38 -0400 (Thu, 21 Apr 2011)
New Revision: 22438
Added:
trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadResourcesTest.java
trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadValueParamTest.java
trunk/ui/input/ui/src/test/java/org/richfaces/request/ProgressControlTest.java
Modified:
trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressControl.java
trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressServletInputStream.java
trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java
Log:
RF-10128
Modified: trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressControl.java
===================================================================
--- trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressControl.java 2011-04-21
13:15:55 UTC (rev 22437)
+++ trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressControl.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -34,12 +34,12 @@
private static final String UPLOAD_PROGRESS_PREFIX =
"_richfaces_upload_percents";
+ long totalBytesRead = 0;
+
private Map<String, Object> contextMap;
private String attributeName;
- private long totalBytesRead = 0;
-
private long length;
private byte lastUpdatedPercentValue;
@@ -60,7 +60,7 @@
return 0;
}
- private static String getContextAttributeName(String uploadId) {
+ static String getContextAttributeName(String uploadId) {
return UPLOAD_PROGRESS_PREFIX + uploadId;
}
Modified:
trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressServletInputStream.java
===================================================================
---
trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressServletInputStream.java 2011-04-21
13:15:55 UTC (rev 22437)
+++
trunk/ui/input/ui/src/main/java/org/richfaces/request/ProgressServletInputStream.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -72,5 +72,29 @@
}
return read;
}
+
+ public long skip(long n) throws IOException {
+ return wrappedStream.skip(n);
+ }
+
+ public int available() throws IOException {
+ return wrappedStream.available();
+ }
+
+ public void close() throws IOException {
+ wrappedStream.close();
+ }
+
+ public void mark(int readlimit) {
+ wrappedStream.mark(readlimit);
+ }
+
+ public void reset() throws IOException {
+ wrappedStream.reset();
+ }
+
+ public boolean markSupported() {
+ return wrappedStream.markSupported();
+ }
}
\ No newline at end of file
Added: trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadResourcesTest.java
===================================================================
--- trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadResourcesTest.java
(rev 0)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadResourcesTest.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -0,0 +1,259 @@
+/*
+ * 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.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Random;
+import java.util.UUID;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.io.ByteStreams;
+import com.google.common.io.Closeables;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class FileUploadResourcesTest {
+
+ private FileUploadMemoryResource memoryResource;
+
+ private FileUploadDiscResource discResource;
+
+ private String memoryTempDirectory;
+
+ private String discTempDirectory;
+
+ private String createTempDirectory() {
+ File tmpDir = new File(System.getProperty("java.io.tmpdir"),
UUID.randomUUID().toString());
+ if (!tmpDir.mkdirs()) {
+ fail();
+ }
+
+ return tmpDir.getAbsolutePath();
+ }
+
+ private void eraseDirectory(String location) {
+ File file = new File(location);
+
+ for (File f: file.listFiles()) {
+ f.delete();
+ }
+
+ file.delete();
+ }
+
+ private byte[] getRandomBytes(int size) {
+ byte[] bs = new byte[size];
+ new Random().nextBytes(bs);
+ return bs;
+ }
+
+ private byte[] readFully(InputStream is) throws IOException {
+ try {
+ return ByteStreams.toByteArray(is);
+ } finally {
+ Closeables.closeQuietly(is);
+ }
+ }
+
+ private byte[] readFully(File file) throws IOException {
+ assertNotNull(file);
+ return readFully(new FileInputStream(file));
+ }
+
+ private File getSingleFile(String location) {
+ File root = new File(location);
+
+ File[] files = root.listFiles();
+ switch (files.length) {
+ case 0:
+ return null;
+ case 1:
+ return files[0];
+ default:
+ throw new IllegalStateException("File is not single");
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ memoryTempDirectory = createTempDirectory();
+ discTempDirectory = createTempDirectory();
+
+ memoryResource = new FileUploadMemoryResource("form:memoryUpload",
memoryTempDirectory);
+ memoryResource.create();
+ discResource = new FileUploadDiscResource("form:discUpload",
discTempDirectory);
+ discResource.create();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ eraseDirectory(memoryTempDirectory);
+ eraseDirectory(discTempDirectory);
+
+ memoryTempDirectory = null;
+ discTempDirectory = null;
+
+ memoryResource = null;
+ discResource = null;
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ int length = 5;
+ byte[] bytes = getRandomBytes(length);
+
+ memoryResource.handle(bytes, bytes.length);
+ discResource.handle(bytes, bytes.length);
+
+ memoryResource.complete();
+ discResource.complete();
+
+ assertTrue(memoryResource.isFileParam());
+ assertTrue(discResource.isFileParam());
+
+ InputStream is = null;
+
+ try {
+ is = memoryResource.getInputStream();
+ assertNotNull(is);
+ } finally {
+ Closeables.closeQuietly(is);
+ }
+ try {
+ is = discResource.getInputStream();
+ assertNotNull(is);
+ } finally {
+ Closeables.closeQuietly(is);
+ }
+
+ assertSame(memoryResource, memoryResource.getResource());
+ assertSame(discResource, discResource.getResource());
+
+ assertNull(memoryResource.getValue());
+ assertNull(discResource.getValue());
+
+ assertEquals("form:memoryUpload", memoryResource.getName());
+ assertEquals("form:discUpload", discResource.getName());
+ }
+
+ @Test
+ public void testSmallData() throws Exception {
+ //should be less than 128 - initial buffer size
+ int length = 68;
+ byte[] bytes = getRandomBytes(length);
+
+ memoryResource.handle(bytes, bytes.length);
+ discResource.handle(bytes, bytes.length);
+
+ memoryResource.complete();
+ discResource.complete();
+
+ checkResourcesContent(bytes);
+ }
+
+ @Test
+ public void testLargeData() throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ int count = new Random().nextInt(256) + 256;
+ for (int i = 0; i < count; i++) {
+ int size = new Random().nextInt(512) + 512;
+ byte[] bytes = getRandomBytes(size);
+
+ memoryResource.handle(bytes, bytes.length);
+ discResource.handle(bytes, bytes.length);
+
+ baos.write(bytes);
+ }
+
+ memoryResource.complete();
+ discResource.complete();
+
+ checkResourcesContent(baos.toByteArray());
+ }
+
+ private void checkResourcesContent(byte[] expected) throws IOException {
+ int length = expected.length;
+
+ assertEquals(length, memoryResource.getSize());
+ assertEquals(length, discResource.getSize());
+
+ assertArrayEquals(expected, readFully(memoryResource.getInputStream()));
+ assertArrayEquals(expected, readFully(discResource.getInputStream()));
+
+ assertNull(getSingleFile(memoryTempDirectory));
+ assertArrayEquals(expected, readFully(getSingleFile(discTempDirectory)));
+
+ memoryResource.write("out");
+ discResource.write("out");
+
+ File memoryOutFile = getSingleFile(memoryTempDirectory);
+ File discOutFile = getSingleFile(discTempDirectory);
+
+ assertEquals("out", memoryOutFile.getName());
+ assertEquals("out", discOutFile.getName());
+
+ assertTrue(memoryOutFile.getAbsolutePath().startsWith(memoryTempDirectory));
+ assertTrue(discOutFile.getAbsolutePath().startsWith(discTempDirectory));
+
+ assertArrayEquals(expected, readFully(memoryOutFile));
+ assertArrayEquals(expected, readFully(discOutFile));
+ }
+
+ @Test
+ public void testDelete() throws Exception {
+ int length = 5;
+ byte[] bytes = getRandomBytes(length);
+
+ memoryResource.handle(bytes, bytes.length);
+ discResource.handle(bytes, bytes.length);
+
+ memoryResource.complete();
+ discResource.complete();
+
+ assertNull(getSingleFile(memoryTempDirectory));
+ assertNotNull(getSingleFile(discTempDirectory));
+
+ memoryResource.delete();
+ discResource.delete();
+
+ assertNull(getSingleFile(memoryTempDirectory));
+ assertNull(getSingleFile(discTempDirectory));
+ }
+}
Added:
trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadValueParamTest.java
===================================================================
--- trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadValueParamTest.java
(rev 0)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/FileUploadValueParamTest.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -0,0 +1,115 @@
+/*
+ * 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.assertFalse;
+import static org.junit.Assert.assertNull;
+
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.util.Random;
+
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class FileUploadValueParamTest {
+
+ private FileUploadValueParam param;
+
+ @Before
+ public void setUp() throws Exception {
+ param = new FileUploadValueParam("form:upload", "UTF-8");
+ param.create();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ param = null;
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ param.handle("test".getBytes(), 4);
+ param.complete();
+
+ assertEquals("form:upload", param.getName());
+ assertFalse(param.isFileParam());
+ assertNull(param.getResource());
+ }
+
+ @Test
+ public void testShortParam() throws Exception {
+ byte[] bytes = "test".getBytes();
+ param.handle(bytes, bytes.length);
+ param.complete();
+
+ assertEquals("test", param.getValue());
+ }
+
+ @Test
+ public void testLongParam() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ CharsetEncoder charsetEncoder = Charset.forName("UTF-8").newEncoder();
+
+ for (int i = 0; i < 256; i++) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ int count = new Random().nextInt(128) + 128;
+ while (count != 0) {
+ char c = (char) new Random().nextInt(Character.MAX_VALUE);
+
+ if (charsetEncoder.canEncode(c)) {
+ baos.write(charsetEncoder.encode(CharBuffer.wrap(new
char[]{c})).array());
+ count--;
+ }
+ }
+
+ byte[] bs = baos.toByteArray();
+ param.handle(bs, bs.length);
+ sb.append(new String(bs, "UTF-8"));
+ }
+
+ param.complete();
+ assertEquals(sb.toString(), param.getValue());
+ }
+
+ @Test
+ public void testNullencoding() throws Exception {
+ param = new FileUploadValueParam("form:upload", null);
+ param.create();
+
+ byte[] bytes = "testing...".getBytes();
+ param.handle(bytes, bytes.length - 3);
+
+ param.complete();
+
+ assertEquals("testing", param.getValue());
+ }
+}
Modified: trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java
===================================================================
---
trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java 2011-04-21
13:15:55 UTC (rev 22437)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/MockUploadResource.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -101,7 +101,7 @@
@Override
public long getSize() {
- return invokeMethod(this.control, this, getSizeMethod0);
+ return (Long) invokeMethod(this.control, this, getSizeMethod0);
}
private static final Method writeMethod0 = findMethod(FileUploadResource.class,
"write", String.class);
Added: trunk/ui/input/ui/src/test/java/org/richfaces/request/ProgressControlTest.java
===================================================================
--- trunk/ui/input/ui/src/test/java/org/richfaces/request/ProgressControlTest.java
(rev 0)
+++
trunk/ui/input/ui/src/test/java/org/richfaces/request/ProgressControlTest.java 2011-04-21
15:55:38 UTC (rev 22438)
@@ -0,0 +1,74 @@
+/*
+ * 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 java.util.Map;
+
+import org.junit.Test;
+
+import com.google.common.collect.Maps;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ProgressControlTest {
+
+ private static final String UPLOAD_ID = "testId";
+
+ private static final String ATTRIBUTE_NAME =
ProgressControl.getContextAttributeName(UPLOAD_ID);
+
+ @Test
+ public void testAdvance() throws Exception {
+ Map<String, Object> contextMap = Maps.newHashMap();
+ ProgressControl control = new ProgressControl(contextMap, UPLOAD_ID, 400);
+
+ assertNull(contextMap.get(ATTRIBUTE_NAME));
+ control.advance(1);
+ assertNull(contextMap.get(ATTRIBUTE_NAME));
+
+ control.advance(3);
+ assertEquals((byte) 1, contextMap.get(ATTRIBUTE_NAME));
+
+ control.advance(196);
+ assertEquals((byte) 50, contextMap.get(ATTRIBUTE_NAME));
+
+ control.advance(200);
+ assertEquals((byte) 100, contextMap.get(ATTRIBUTE_NAME));
+ }
+
+ @Test
+ public void testClearProgress() throws Exception {
+ Map<String, Object> contextMap = Maps.newHashMap();
+ ProgressControl control = new ProgressControl(contextMap, UPLOAD_ID, 100);
+
+ assertNull(contextMap.get(ATTRIBUTE_NAME));
+ control.advance(50);
+ assertNotNull(contextMap.get(ATTRIBUTE_NAME));
+ control.clearProgress();
+ assertNull(contextMap.get(ATTRIBUTE_NAME));
+ }
+}