@Test
public void testBindApplication_success() throws Exception {

	TestApplication app1 = new TestApplication();
	app1.setDirectory( this.folder.newFolder());
	app1.getTemplate().setExternalExportsPrefix( "prefix1" );
	ManagedApplication ma1 = new ManagedApplication( app1 );
	TestManagerWrapper.addManagedApplication( this.mngr, ma1 );
	TestApplication app2 = new TestApplication();
	app2.getTemplate().setName( "tpl-other" );
	app2.getTemplate().setExternalExportsPrefix( "tpl-other-prefix" );
	app2.getTemplate().setExternalExportsPrefix( "prefix2" );
	app2.setName( "app-other" );
	// Rename root instances in the second application.
	// This is to make sure messages are sent to the right instances in the right application.
	app2.getMySqlVm().setName( "other-mysql" );
	app2.getTomcatVm().setName( "other-tomcat" );
	app2.setDirectory( this.folder.newFolder());
	ManagedApplication ma2 = new ManagedApplication( app2 );
	TestManagerWrapper.addManagedApplication( this.mngr, ma2 );
	Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size());
	String eep = ma2.getApplication().getTemplate().getExternalExportsPrefix();
	Mockito.verifyZeroInteractions( this.messagingMngr );
	this.mngr.bindOrUnbindApplication( ma1, eep, ma2.getName(), true );
	Assert.assertEquals( 1, ma1.getApplication().getApplicationBindings().size());
	Assert.assertTrue( ma1.getApplication().getApplicationBindings().get( eep ).contains( ma2.getName()));
	// Verify the messaging
	ArgumentCaptor<ManagedApplication> arg0 = ArgumentCaptor.forClass( ManagedApplication.class );
	ArgumentCaptor<Instance> arg1 = ArgumentCaptor.forClass( Instance.class );
	ArgumentCaptor<Message> arg2 = ArgumentCaptor.forClass( Message.class );
	Mockito.verify( this.messagingMngr, Mockito.times( 2 )).sendMessageSafely( arg0.capture(), arg1.capture(), arg2.capture());
	for( ManagedApplication s : arg0.getAllValues()) {
		Assert.assertEquals( ma1, s );
	}
	for( Message m : arg2.getAllValues()) {
		Assert.assertEquals( MsgCmdChangeBinding.class, m.getClass());
		MsgCmdChangeBinding msg = (MsgCmdChangeBinding) m;
		Assert.assertEquals( ma2.getApplication().getTemplate().getExternalExportsPrefix(), msg.getExternalExportsPrefix());
		Assert.assertNotNull( msg.getAppNames());
		Assert.assertEquals( 1, msg.getAppNames().size());
		Assert.assertTrue( msg.getAppNames().contains( ma2.getName()));
	}
	// Messages must be sent to ma1!
	List<Instance> instances = arg1.getAllValues();
	Assert.assertTrue( instances.contains( app1.getMySqlVm()));
	Assert.assertTrue( instances.contains( app1.getTomcatVm()));
}