Search in sources :

Example 96 with File

use of java.io.File in project camel by apache.

the class FileConsumerCommitRenameAbsolutePathStrategyTest method setUp.

@Override
protected void setUp() throws Exception {
    deleteDirectory("target/done");
    deleteDirectory("target/reports");
    // use current dir as base as aboslute path
    base = new File("").getAbsolutePath() + "/target";
    super.setUp();
}
Also used : File(java.io.File)

Example 97 with File

use of java.io.File in project camel by apache.

the class FileConsumerCommitRenameStrategyTest method testRenameFileExists.

public void testRenameFileExists() throws Exception {
    // create a file in done to let there be a duplicate file
    File file = new File("target/done");
    file.mkdirs();
    FileWriter fw = new FileWriter("target/done/london.txt");
    try {
        fw.write("I was there once in London");
        fw.flush();
    } finally {
        fw.close();
    }
    MockEndpoint mock = getMockEndpoint("mock:report");
    mock.expectedBodiesReceived("Hello London");
    template.sendBodyAndHeader("file:target/reports", "Hello London", Exchange.FILE_NAME, "london.txt");
    mock.assertIsSatisfied();
    oneExchangeDone.matchesMockWaitTime();
    // content of file should be Hello London
    String content = IOConverter.toString(new File("target/done/london.txt"), null);
    assertEquals("The file should have been renamed replacing any existing files", "Hello London", content);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileWriter(java.io.FileWriter) File(java.io.File)

Example 98 with File

use of java.io.File in project camel by apache.

the class CachedOutputStreamTest method testCachedOutputStreamCustomBufferSize.

public void testCachedOutputStreamCustomBufferSize() throws Exception {
    // double the default buffer size
    context.getStreamCachingStrategy().setBufferSize(8192);
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    assertEquals("we should have a custom buffer size", cos.getStrategyBufferSize(), 8192);
    // make sure things still work after custom buffer size set
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertTrue("The file name should start with cos", files[0].startsWith("cos"));
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the FileInputStreamCache", cache instanceof FileInputStreamCache);
    String temp = toString((InputStream) cache);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    cache.reset();
    temp = toString((InputStream) cache);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    ((InputStream) cache).close();
    files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    exchange.getUnitOfWork().done(exchange);
    files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) File(java.io.File)

Example 99 with File

use of java.io.File in project camel by apache.

the class CachedOutputStreamTest method testCacheStreamToFileAndCloseStream.

public void testCacheStreamToFileAndCloseStream() throws Exception {
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertTrue("The file name should start with cos", files[0].startsWith("cos"));
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the FileInputStreamCache", cache instanceof FileInputStreamCache);
    String temp = toString((InputStream) cache);
    ((InputStream) cache).close();
    files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    exchange.getUnitOfWork().done(exchange);
    try {
        cache.reset();
        // The stream is closed, so the temp file is gone.
        fail("we expect the exception here");
    } catch (Exception exception) {
    // do nothing
    }
    files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) File(java.io.File) IOException(java.io.IOException)

Example 100 with File

use of java.io.File in project camel by apache.

the class FileInputStreamCacheTest method testFileInputStreamCache.

public void testFileInputStreamCache() throws Exception {
    File file = new File(TEST_FILE);
    FileInputStreamCache cache = new FileInputStreamCache(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    cache.writeTo(bos);
    String s = context.getTypeConverter().convertTo(String.class, bos);
    assertNotNull(s);
    assertTrue(s.contains("<firstName>James</firstName>"));
    IOHelper.close(cache, bos);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Aggregations

File (java.io.File)45560 IOException (java.io.IOException)9550 Test (org.junit.Test)9009 FileOutputStream (java.io.FileOutputStream)3388 ArrayList (java.util.ArrayList)3273 FileInputStream (java.io.FileInputStream)2915 InputStream (java.io.InputStream)1783 FileNotFoundException (java.io.FileNotFoundException)1743 URL (java.net.URL)1649 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1478 Test (org.testng.annotations.Test)1411 HashMap (java.util.HashMap)1404 RandomAccessFile (java.io.RandomAccessFile)1154 FileWriter (java.io.FileWriter)1081 Properties (java.util.Properties)1019 ZipFile (java.util.zip.ZipFile)959 JarFile (java.util.jar.JarFile)866 List (java.util.List)850 BufferedReader (java.io.BufferedReader)840 OutputStream (java.io.OutputStream)811