Search in sources :

Example 1 with IFileCleaner

use of org.apache.wicket.util.file.IFileCleaner in project wicket by apache.

the class FileUploadTest method getInputStream.

/**
 * Test that when getting an input stream a new input stream is returned every time.
 *
 * Also test that the inputstream is saved internally for later closing.
 *
 * @throws Exception
 */
@Test
public void getInputStream() throws Exception {
    final IFileCleaner fileUploadCleaner = new FileCleaner();
    DiskFileItemFactory itemFactory = new DiskFileItemFactory() {

        @Override
        public FileCleaningTracker getFileCleaningTracker() {
            return new FileCleanerTrackerAdapter(fileUploadCleaner);
        }
    };
    FileItem fileItem = itemFactory.createItem("dummyFieldName", "text/java", false, "FileUploadTest.java");
    // Initialize the upload
    fileItem.getOutputStream();
    // Get the internal list out
    Field inputStreamsField = FileUpload.class.getDeclaredField("inputStreamsToClose");
    inputStreamsField.setAccessible(true);
    FileUpload fileUpload = new FileUpload(fileItem);
    List<?> inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    InputStream is1 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(1, inputStreams.size());
    InputStream is2 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(2, inputStreams.size());
    assertNotSame(is1, is2);
    // Ok lets close all the streams
    try {
        fileUpload.closeStreams();
    } catch (Exception e) {
        fail();
    }
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    fileUploadCleaner.destroy();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Field(java.lang.reflect.Field) FileCleanerTrackerAdapter(org.apache.wicket.util.file.FileCleanerTrackerAdapter) InputStream(java.io.InputStream) List(java.util.List) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) FileCleaner(org.apache.wicket.util.file.FileCleaner) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with IFileCleaner

use of org.apache.wicket.util.file.IFileCleaner in project wicket by apache.

the class WebApplication method internalDestroy.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
 */
@Override
public void internalDestroy() {
    // destroy the resource watcher
    IModificationWatcher resourceWatcher = getResourceSettings().getResourceWatcher(false);
    if (resourceWatcher != null) {
        resourceWatcher.destroy();
    }
    IFileCleaner fileCleaner = getResourceSettings().getFileCleaner();
    if (fileCleaner != null) {
        fileCleaner.destroy();
    }
    super.internalDestroy();
}
Also used : IModificationWatcher(org.apache.wicket.util.watch.IModificationWatcher) IFileCleaner(org.apache.wicket.util.file.IFileCleaner)

Aggregations

IFileCleaner (org.apache.wicket.util.file.IFileCleaner)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 FileCleaner (org.apache.wicket.util.file.FileCleaner)1 FileCleanerTrackerAdapter (org.apache.wicket.util.file.FileCleanerTrackerAdapter)1 IModificationWatcher (org.apache.wicket.util.watch.IModificationWatcher)1 Test (org.junit.Test)1