}
public void testFileAge() throws Exception
{
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference message = new AtomicReference();
final AtomicInteger loopCount = new AtomicInteger(0);
EventCallback callback = new EventCallback()
{
public synchronized void eventReceived(MuleEventContext context, Object component)
{
try
{
logger.info("called " + loopCount.incrementAndGet() + " times");
// without this we may have problems with the many repeats
if (1 == latch.getCount())
{
String o = IOUtils.toString((SftpInputStream) context.getMessage().getPayload());
message.set(o);
latch.countDown();
}
}
catch (Exception e)
{
logger.error(e.getMessage(), e);
}
}
};
MuleClient client = new MuleClient(muleContext);
// Ensure that no other files exists
// cleanupRemoteFtpDirectory(client, INBOUND_ENDPOINT_NAME);
Object component = getComponent("testComponent");
assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
FunctionalTestComponent ftc = (FunctionalTestComponent) component;
assertNotNull(ftc);
ftc.setEventCallback(callback);
// Use one specific filename so that the file is overwritten if necessarily
Map properties = new HashMap();
// properties.put("filename", "fileage-test.tmp");
long startTime = System.currentTimeMillis();
logger.debug("before dispatch");
// Send an file to the SFTP server, which the inbound-endpoint then can pick
// up
client.dispatch(getAddressByEndpoint(client, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, properties);
logger.debug("before retrieve");
latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
// We assume that the total time never should be less than fileAge. That
// means that the fileAge value
// in this test must be rather high
long time = System.currentTimeMillis() - startTime;
int maxTimeDiff = 1000; // Max time diff between localhost and the server.
// Ie. the time can differ up to this and the test
// will be okay. This is used because localhost/developer machine is not
// always synchronized with the server(s)
int expectedMinTime = 2000 - maxTimeDiff;
assertTrue("The total time should never be less the 'fileAge' ms (was " + time + ", expected "
+ expectedMinTime + ")", time > expectedMinTime);
assertEquals(TEST_MESSAGE, message.get());
}