Search in sources :

Example 21 with File

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());
}
Also used : MockDomainObjectFileUpload(org.apache.wicket.util.tester.MockFormFileUploadPage.MockDomainObjectFileUpload) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload) MockDomainObjectFileUpload(org.apache.wicket.util.tester.MockFormFileUploadPage.MockDomainObjectFileUpload) Test(org.junit.Test)

Example 22 with File

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));
}
Also used : File(org.apache.wicket.util.file.File) Test(org.junit.Test)

Example 23 with File

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();
}
Also used : File(org.apache.wicket.util.file.File) Test(org.junit.Test)

Example 24 with File

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;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(org.apache.wicket.util.file.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 25 with File

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();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) File(org.apache.wicket.util.file.File) FileInputStream(java.io.FileInputStream)

Aggregations

File (org.apache.wicket.util.file.File)28 Test (org.junit.Test)12 IOException (java.io.IOException)7 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)6 AjaxDownloadBehaviorFromFile (com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile)5 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)5 WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)5 FileInputStream (java.io.FileInputStream)5 MidpointConfiguration (com.evolveum.midpoint.common.configuration.api.MidpointConfiguration)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 RestartResponseException (org.apache.wicket.RestartResponseException)4 FormTester (org.apache.wicket.util.tester.FormTester)4 CommonException (com.evolveum.midpoint.util.exception.CommonException)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 IManageablePage (org.apache.wicket.page.IManageablePage)3 ISerializer (org.apache.wicket.serialize.ISerializer)3 DeflatedJavaSerializer (org.apache.wicket.serialize.java.DeflatedJavaSerializer)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2