use of org.apache.wicket.util.file.File in project wicket by apache.
the class FormTesterTest method addBinaryFile.
/**
* Test that the user can use
* {@link FormTester#setFile(String, org.apache.wicket.util.file.File, String)} to test that
* upload to a FileUploadField works.
*/
@Test
public void addBinaryFile() {
tester.startPage(MockFormFileUploadPage.class);
MockFormFileUploadPage page = (MockFormFileUploadPage) tester.getLastRenderedPage();
MockDomainObjectFileUpload domainObject = page.getDomainObject();
assertNull(page.getFileUpload());
assertNotNull(domainObject);
assertNull(domainObject.getText());
FormTester formTester = tester.newFormTester("form");
formTester.setFile("file", new File(getBasedir() + "src/test/java/org/apache/wicket/util/tester/bg.jpg"), "image/jpeg");
formTester.setValue("text", "Mock value");
formTester.submit();
assertNotNull(domainObject);
assertNotNull(domainObject.getText());
assertEquals("Mock value", domainObject.getText());
FileUpload fileUpload = page.getFileUpload();
assertNotNull(fileUpload);
assertTrue("uploaded content does not have the right size, expected 428, got " + fileUpload.getBytes().length, fileUpload.getBytes().length == 428);
assertEquals("bg.jpg", fileUpload.getClientFileName());
assertEquals("image/jpeg", fileUpload.getContentType());
}
use of org.apache.wicket.util.file.File in project wicket by apache.
the class FileUploadErrorTest method testSubmit_NotValidTextFieldValue_FileUploaded.
/**
* FileUpload is filled on submit: Validation reports too short TextField input.
*/
@Test
public void testSubmit_NotValidTextFieldValue_FileUploaded() {
formTester.setValue(textFieldId, "te");
formTester.setFile(fileUploadId, new File(testUploadFilePath), "UTF-8");
formTester.submit();
tester.assertErrorMessages(String.format("The value of '%1$s' is not between 3 and 10 characters long.", textFieldId));
}
use of org.apache.wicket.util.file.File in project wicket by apache.
the class FileUploadErrorTest method testSubmit_RequiredFileUpload_Ok.
@Test
public void testSubmit_RequiredFileUpload_Ok() {
((FileUploadField) tester.getLastRenderedPage().get("form:" + fileUploadId)).setRequired(true);
formTester.setValue(textFieldId, "test value");
formTester.setFile(fileUploadId, new File(testUploadFilePath), "UTF-8");
formTester.submit();
tester.assertNoErrorMessage();
}
use of org.apache.wicket.util.file.File in project wicket by apache.
the class FileUploadFieldTest method writeTestFile.
/**
* @param numberOfowsToCreate
* @return test file
* @throws IOException
*/
public static File writeTestFile(int numberOfowsToCreate) throws IOException {
File tmp = new File(java.io.File.createTempFile(TEST_FILE_NAME, ".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;
}
use of org.apache.wicket.util.file.File in project wicket by apache.
the class ZipResourceStream method zipDir.
/**
* Recursive method for zipping the contents of a directory including nested directories.
*
* @param dir
* dir to be zipped
* @param out
* ZipOutputStream to write to
* @param path
* Path to nested dirs (used in resursive calls)
* @param recursive
* If true, all subdirs will be zipped as well
* @throws IOException
*/
private static void zipDir(final File dir, final ZipOutputStream out, final String path, final boolean recursive) throws IOException {
Args.notNull(dir, "dir");
Args.isTrue(dir.isDirectory(), "Not a directory: '{}'", dir);
String[] files = dir.list();
int BUFFER = 2048;
BufferedInputStream origin;
byte[] data = new byte[BUFFER];
if (files != null) {
for (String file : files) {
if (log.isDebugEnabled()) {
log.debug("Adding: '{}'", file);
}
File f = new File(dir, file);
if (f.isDirectory()) {
if (recursive) {
zipDir(f, out, path + f.getName() + "/", recursive);
}
} else {
out.putNextEntry(new ZipEntry(path + f.getName()));
FileInputStream fi = new FileInputStream(f);
origin = new BufferedInputStream(fi, BUFFER);
try {
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
} finally {
origin.close();
}
}
}
}
if ("".equals(path)) {
out.close();
}
}
Aggregations