Search in sources :

Example 6 with FormTester

use of org.apache.wicket.util.tester.FormTester in project wicket by apache.

the class FileUploadFieldTest method testEmptyFieldAsBrowserSendsIt.

/**
 * https://issues.apache.org/jira/browse/WICKET-6270
 */
@Test
public void testEmptyFieldAsBrowserSendsIt() throws Exception {
    tester.startPage(TestValidationPage.class);
    FileUploadField uploadField = (FileUploadField) tester.getComponentFromLastRenderedPage("form:upload");
    uploadField.add(new IValidator<List<FileUpload>>() {

        @Override
        public void validate(IValidatable<List<FileUpload>> validatable) {
            // must check during validation, since the uploads are nullified on detach
            assertEquals(0, validatable.getValue().size());
        }
    });
    FormTester formtester = tester.newFormTester("form");
    formtester.setFile("upload", null, "text/xml");
    formtester.submit();
    tester.assertNoErrorMessage();
}
Also used : FormTester(org.apache.wicket.util.tester.FormTester) List(java.util.List) Test(org.junit.Test)

Example 7 with FormTester

use of org.apache.wicket.util.tester.FormTester in project wicket by apache.

the class FileUploadFieldTest method internalDetach.

/**
 * Test that detach closes the streams
 *
 * @throws IOException
 *             '
 */
@Test
public void internalDetach() throws IOException {
    tester.startPage(MockPageWithFormAndUploadField.class);
    File tmp = null;
    try {
        // Write out a large text file. We need to make this file reasonably sizable,
        // because things get handled using input streams, and we want to check to make
        // sure they're closed properly if we abort mid-request.
        // We create a temp file because we don't want to depend on a file we might not
        // 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);
        // Let's upload the dtd file. It's large enough to avoid being in memory.
        FormTester formtester = tester.newFormTester("form");
        formtester.setFile("upload", tmp, "text/plain");
        formtester.submit();
        // Get the file upload
        MockPageWithFormAndUploadField page = (MockPageWithFormAndUploadField) tester.getLastRenderedPage();
        FileUpload fileUpload = page.getFileUpload();
        assertNotNull(fileUpload);
        // Get an input stream from the file upload
        InputStream is = fileUpload.getInputStream();
        // We should be able to read a byte
        assertTrue(is.read() != -1);
        fileUpload.closeStreams();
        // The input stream should be closed so we shouldn't be able to read any more bytes
        try {
            is.read();
            fail("The input stream should be closed so we shouldn't be able to read any more bytes");
        } catch (IOException e) {
        // Expected
        } catch (Exception e) {
            fail();
        }
    } finally {
        if (tmp != null && tmp.exists()) {
            tmp.delete();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormTester(org.apache.wicket.util.tester.FormTester) IOException(java.io.IOException) File(org.apache.wicket.util.file.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with FormTester

use of org.apache.wicket.util.tester.FormTester in project wicket by apache.

the class FileUploadFieldTest method fileUploadCanBeValidated.

/**
 * @throws IOException
 */
@Test
public void fileUploadCanBeValidated() throws IOException {
    tester.startPage(TestValidationPage.class);
    // creating the file expected by form validators
    File tmpFile = writeTestFile(1);
    tmpFile.deleteOnExit();
    FormTester formtester = tester.newFormTester("form");
    formtester.setFile("upload", tmpFile, "text/plain");
    formtester.submit();
    TestValidationPage page = (TestValidationPage) tester.getLastRenderedPage();
    assertFalse(page.getForm().hasError());
}
Also used : FormTester(org.apache.wicket.util.tester.FormTester) File(org.apache.wicket.util.file.File) Test(org.junit.Test)

Example 9 with FormTester

use of org.apache.wicket.util.tester.FormTester in project wicket by apache.

the class FileUploadFieldTest method testEmptyField.

/**
 * https://issues.apache.org/jira/browse/WICKET-5691
 */
@Test
public void testEmptyField() throws Exception {
    tester.startPage(TestValidationPage.class);
    FileUploadField uploadField = (FileUploadField) tester.getComponentFromLastRenderedPage("form:upload");
    uploadField.add(new IValidator<List<FileUpload>>() {

        @Override
        public void validate(IValidatable<List<FileUpload>> validatable) {
            // must check during validation, since the uploads are nullified on detach
            assertEquals(0, validatable.getValue().size());
        }
    });
    FormTester formtester = tester.newFormTester("form");
    formtester.submit();
    tester.assertNoErrorMessage();
}
Also used : FormTester(org.apache.wicket.util.tester.FormTester) List(java.util.List) Test(org.junit.Test)

Example 10 with FormTester

use of org.apache.wicket.util.tester.FormTester in project wicket by apache.

the class FileUploadTest method writeToTempFile.

/**
 * @see <a href="https://issues.apache.org/jira/browse/WICKET-3715">WICKET-3715</a>
 * @throws IOException
 */
@Test
public void writeToTempFile() throws IOException {
    tester.startPage(TestPage.class);
    File tmp = null;
    try {
        tmp = FileUploadFieldTest.writeTestFile(1);
        FormTester formtester = tester.newFormTester("form");
        formtester.setFile("upload", tmp, "text/plain");
        formtester.submit();
        TestPage page = (TestPage) tester.getLastRenderedPage();
        assertNotNull(page.testFile);
    } finally {
        if (tmp != null && tmp.exists()) {
            tmp.delete();
        }
    }
}
Also used : FormTester(org.apache.wicket.util.tester.FormTester) File(org.apache.wicket.util.file.File) Test(org.junit.Test)

Aggregations

FormTester (org.apache.wicket.util.tester.FormTester)207 Test (org.junit.Test)122 Test (org.junit.jupiter.api.Test)54 Component (org.apache.wicket.Component)50 WicketTester (org.apache.wicket.util.tester.WicketTester)14 AbstractInitializedGuiIntegrationTest (com.evolveum.midpoint.web.AbstractInitializedGuiIntegrationTest)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 Test (org.testng.annotations.Test)9 List (java.util.List)8 ListModel (org.apache.wicket.model.util.ListModel)7 ArrayList (java.util.ArrayList)6 File (org.apache.wicket.util.file.File)4 OrgType (com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType)3 ButtonAjaxBehavior (com.googlecode.wicket.jquery.ui.widget.dialog.ButtonAjaxBehavior)3 LocalDate (java.time.LocalDate)3 Cookie (javax.servlet.http.Cookie)3 Page (org.apache.wicket.Page)3 LocalDateConverter (org.apache.wicket.util.convert.converter.LocalDateConverter)3 Ignore (org.junit.Ignore)3 RoleType (com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType)2