Issue Details (XML | Word | Printable)

Key: SFTP-14
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Magnus Larsson
Reporter: Magnus Larsson
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
SFTP Transport Project

Improve support for negative tests

Created: 02/Dec/09 02:04 PM   Updated: 02/Dec/09 05:39 PM
Component/s: Test
Affects Version/s: 2.2.1-RC1
Fix Version/s: 2.2.1-RC2, 2.2.1-RC1

Time Tracking:
Not Specified

Labels:
User impact: Medium
Similar Issues:


 Description  « Hide
Today we call Thread.sleep() to be sure that asynchronous processing has completed.
For negative tests a far better approach is to programmatically register exception listeners that can catch expected exceptions and that immediately after that allows the processing to continue. This improvement will lead to both significantly faster and more robust negative tests.

 All   Comments   Work Log   Change History   Transitions   FishEye      Sort Order: Ascending order - Click to sort in descending order
Magnus Larsson added a comment - 02/Dec/09 05:29 PM - edited
Fixed by adding the following code to AbstractSftpTestCase.java:
final ValueHolder<Exception> exceptionHolder = new ValueHolder<Exception>();
if (expectedFailingConnector != null) {
	// Register an exception-listener on the connector that expects to fail and count down the latch after saving the throwed exception
	muleContext.getRegistry().lookupConnector(expectedFailingConnector).setExceptionListener(new ExceptionListener() {
		public void exceptionThrown(Exception e) {
			exceptionHolder.value = e;
			latch.countDown();
		}
	});
}

We can now write negativetests like:

try {
	executeBaseTest("inboundEndpoint2", "vm://test.upload2", "file2.txt", SEND_SIZE, "receiving2", TIMEOUT, "sftp");
	fail("Should have received an Exception");
} catch (Exception e) {
	assertTrue(e instanceof NotImplementedException);
}

Compared to the old timer-based way, e.g.:

// Send an file to the SFTP server, which the inbound-outboundEndpoint then can pick up
muleClient.dispatch(getAddressByEndpoint(muleClient, INBOUND_ENDPOINT_NAME2), TEST_MESSAGE, fileNameProperties);

// TODO dont know any better way to wait for the above to finish? We cant use the same as SftpFileAgeFunctionalTestCase
// for example since we dont have the TestComponent
Thread.sleep(4000);

verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2, false, true);

muleClient.dispatch(getAddressByEndpoint(muleClient, INBOUND_ENDPOINT_NAME2), TEST_MESSAGE, fileNameProperties);
Thread.sleep(4000);

verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2, true, true);
// Get the exception from mule
Throwable ex = ExceptionListener.getStandardException();
assertNotNull(ex);
assertTrue(ex instanceof IOException);

Magnus Larsson added a comment - 02/Dec/09 05:35 PM
add 2.2.x as fixed version