You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2008/06/13 07:03:49 UTC
svn commit: r667341 -
/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java
Author: ivaynberg
Date: Thu Jun 12 22:03:49 2008
New Revision: 667341
URL: http://svn.apache.org/viewvc?rev=667341&view=rev
Log:
remove warning
Modified:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java
Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java?rev=667341&r1=667340&r2=667341&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java Thu Jun 12 22:03:49 2008
@@ -25,9 +25,9 @@
import java.util.HashSet;
import java.util.Set;
-import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.Page;
import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.ITestPageSource;
@@ -79,7 +79,7 @@
// know the path of (e.g. the big DTD this test used previously). This enables
// us to run the test out of a JAR file if need be, and also with an unknown
// running directory (e.g. when run from wicket-parent).
- tmp = writeTestFile(1000);
+ tmp = writeTestFile(1000);
// Let's upload the dtd file. It's large enough to avoid being in memory.
FormTester formtester = tester.newFormTester("form");
@@ -123,94 +123,98 @@
}
}
- public void testFileUploadCanBeValidated() throws IOException
- {
- final Set<IValidatable> validatedComponents = new HashSet<IValidatable>();
-
- final File tmpFile = writeTestFile(1);
- tmpFile.deleteOnExit();
-
- final IValidator testValidator = new IValidator()
- {
- public void validate(IValidatable validatable)
- {
- validatedComponents.add(validatable);
- assertEquals(FileUpload.class, validatable.getValue().getClass());
- FileUpload upload = (FileUpload) validatable.getValue();
- assertEquals(tmpFile.getName(), upload.getClientFileName());
- assertEquals(new String(read(tmpFile)), new String(upload.getBytes()));
- }
- };
- final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();
- page.getForm().visitChildren(FileUploadField.class, new IVisitor<FileUploadField>()
- {
- public Object component(FileUploadField uploadField)
- {
- uploadField.add(testValidator);
- return STOP_TRAVERSAL;
- }
- });
-
- tester.startPage(new ITestPageSource()
- {
- private static final long serialVersionUID = 1L;
-
- public Page<?> getTestPage()
- {
- return page;
- }
- });
-
- FormTester formtester = tester.newFormTester("form");
- formtester.setFile("upload", tmpFile, "text/plain");
- formtester.submit();
- assertEquals(validatedComponents.size(), 1);
- }
-
- private File writeTestFile(int numberOfowsToCreate)
- throws IOException
- {
- File tmp = new File(java.io.File.createTempFile(getClass().getName(), ".txt"));
- OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp));
- for (int i = 0; i < numberOfowsToCreate; i++)
- {
- os.write("test test test test test\n".getBytes());
- }
- os.close();
- return tmp;
- }
-
- private byte[] read(File file)
- {
- try
- {
- return readFile(file);
- } catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- }
-
- private byte[] readFile(File file) throws IOException
- {
- InputStream stream = null;
- byte[] bytes = new byte[0];
- try
- {
- stream = new FileInputStream(file);
- int length = (int) file.length();
- bytes = new byte[length];
- int offset = 0;
- int bytesRead;
-
- while (offset < bytes.length && (bytesRead = stream.read(bytes, offset, bytes.length - offset)) >= 0)
- {
- offset += bytesRead;
- }
- } finally
- {
- stream.close();
- }
- return bytes;
- }
+ public void testFileUploadCanBeValidated() throws IOException
+ {
+ final Set<IValidatable> validatedComponents = new HashSet<IValidatable>();
+
+ final File tmpFile = writeTestFile(1);
+ tmpFile.deleteOnExit();
+
+ final IValidator testValidator = new IValidator()
+ {
+ private static final long serialVersionUID = 1L;
+
+ public void validate(IValidatable validatable)
+ {
+ validatedComponents.add(validatable);
+ assertEquals(FileUpload.class, validatable.getValue().getClass());
+ FileUpload upload = (FileUpload)validatable.getValue();
+ assertEquals(tmpFile.getName(), upload.getClientFileName());
+ assertEquals(new String(read(tmpFile)), new String(upload.getBytes()));
+ }
+ };
+ final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();
+ page.getForm().visitChildren(FileUploadField.class, new IVisitor<FileUploadField>()
+ {
+ public Object component(FileUploadField uploadField)
+ {
+ uploadField.add(testValidator);
+ return STOP_TRAVERSAL;
+ }
+ });
+
+ tester.startPage(new ITestPageSource()
+ {
+ private static final long serialVersionUID = 1L;
+
+ public Page<?> getTestPage()
+ {
+ return page;
+ }
+ });
+
+ FormTester formtester = tester.newFormTester("form");
+ formtester.setFile("upload", tmpFile, "text/plain");
+ formtester.submit();
+ assertEquals(validatedComponents.size(), 1);
+ }
+
+ private File writeTestFile(int numberOfowsToCreate) throws IOException
+ {
+ File tmp = new File(java.io.File.createTempFile(getClass().getName(), ".txt"));
+ OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp));
+ for (int i = 0; i < numberOfowsToCreate; i++)
+ {
+ os.write("test test test test test\n".getBytes());
+ }
+ os.close();
+ return tmp;
+ }
+
+ private byte[] read(File file)
+ {
+ try
+ {
+ return readFile(file);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private byte[] readFile(File file) throws IOException
+ {
+ InputStream stream = null;
+ byte[] bytes = new byte[0];
+ try
+ {
+ stream = new FileInputStream(file);
+ int length = (int)file.length();
+ bytes = new byte[length];
+ int offset = 0;
+ int bytesRead;
+
+ while (offset < bytes.length &&
+ (bytesRead = stream.read(bytes, offset, bytes.length - offset)) >= 0)
+ {
+ offset += bytesRead;
+ }
+ }
+ finally
+ {
+ stream.close();
+ }
+ return bytes;
+ }
}