Package org.apache.wicket.util.file

Examples of org.apache.wicket.util.file.File


  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();
  }
View Full Code Here


      {
        return page;
      }
    });

    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
      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();
      }
    }
  }
View Full Code Here

  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>()
View Full Code Here

    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());
    }
View Full Code Here

  {
    final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();

    tester.startPage(page);

    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
      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();
      }
    }
  }
View Full Code Here

   */
  public void testFileUploadCanBeValidated() throws IOException
  {
    final Set<IValidatable<?>> validatedComponents = new HashSet<IValidatable<?>>();

    final File tmpFile = writeTestFile(1);
    tmpFile.deleteOnExit();

    final IValidator<List<FileUpload>> testValidator = new IValidator<List<FileUpload>>()
    {
      private static final long serialVersionUID = 1L;

      public void validate(IValidatable<List<FileUpload>> validatable)
      {
        validatedComponents.add(validatable);
        assertEquals(ArrayList.class, validatable.getValue().getClass());
        List<FileUpload> uploads = validatable.getValue();
        FileUpload upload = uploads.get(0);
        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, Void>()
View Full Code Here

    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());
    }
View Full Code Here

    assertNotNull(domainObject);
    assertNull(domainObject.getText());


    FormTester formTester = tester.newFormTester("form");
    formTester.setFile("file", new File("pom.xml"), "text/xml");
    formTester.setValue("text", "Mock value");
    formTester.submit();


    assertNotNull(domainObject);
View Full Code Here

    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();

View Full Code Here

  }

  @Override
  public FileResourceStream getReference()
  {
    FileResourceStream fileResourceStream = new FileResourceStream(new File(fileName));
    restoreResourceStream(fileResourceStream);
    return fileResourceStream;
  }
View Full Code Here

TOP

Related Classes of org.apache.wicket.util.file.File

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.