@Test
public void testFindUsageStatistics() throws Exception {

	// Setup
	String t1 = (String) this.resource.createOrUpdateTarget( "id: t1\nhandler: h\nprop: ok", null ).getEntity();
	Assert.assertNotNull( t1 );
	String t2 = (String) this.resource.createOrUpdateTarget( "id: t2\nhandler: h\nprop: ok", null ).getEntity();
	Assert.assertNotNull( t2 );
	String t3 = (String) this.resource.createOrUpdateTarget( "id: t3\nhandler: h\nprop: ok", null ).getEntity();
	Assert.assertNotNull( t3 );
	TestApplication app = new TestApplication();
	app.setDirectory( this.folder.newFolder());
	this.managerWrapper.addManagedApplication( new ManagedApplication( app ));
	this.resource.associateTarget( app.getName(), null, InstanceHelpers.computeInstancePath( app.getMySqlVm()), t1, true );
	this.resource.associateTarget( app.getName(), null, null, t2, true );
	// Checks
	List<TargetUsageItem> items = this.resource.findUsageStatistics( t1 );
	Assert.assertEquals( 1, items.size());
	TargetUsageItem item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t2 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	// Mark one as used
	this.manager.targetsMngr().lockAndGetTarget( app, app.getTomcatVm());
	items = this.resource.findUsageStatistics( t1 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	items = this.resource.findUsageStatistics( t2 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertTrue( item.isReferencing());
	// The change is here!
	Assert.assertTrue( item.isUsing());
	// Release it
	this.manager.targetsMngr().unlockTarget( app, app.getTomcatVm());
	items = this.resource.findUsageStatistics( t1 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t2 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	// Remove the association for the named instance
	this.resource.associateTarget( app.getName(), null, InstanceHelpers.computeInstancePath( app.getMySqlVm()), null, false );
	items = this.resource.findUsageStatistics( t1 );
	Assert.assertEquals( 0, items.size());
	items = this.resource.findUsageStatistics( t2 );
	Assert.assertEquals( 1, items.size());
	item = items.get( 0 );
	Assert.assertEquals( app.getName(), item.getName());
	Assert.assertNull( item.getVersion());
	Assert.assertFalse( item.isUsing());
	Assert.assertTrue( item.isReferencing());
	items = this.resource.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
}