@Test
public void testFindUsageStatistics() throws Exception {

	// Setup
	TestApplication app = new TestApplication();
	Instance newRootInstance = new Instance( "newRoot" ).component( app.getMySqlVm().getComponent());
	app.getRootInstances().add( newRootInstance );
	String t1 = this.mngr.createTarget( "prop: ok\nid: t1\nhandler: h" );
	String t2 = this.mngr.createTarget( "prop: ok\nid: t2\nhandler: h" );
	String t3 = this.mngr.createTarget( "prop: ok\nid: t3\nhandler: h" );
	this.mngr.associateTargetWith( t1, app, InstanceHelpers.computeInstancePath( app.getMySqlVm()));
	this.mngr.associateTargetWith( t1, app, InstanceHelpers.computeInstancePath( newRootInstance ));
	this.mngr.associateTargetWith( t2, app, null );
	// Checks
	List<TargetUsageItem> items = this.mngr.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.mngr.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.mngr.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	// Mark one as used
	this.mngr.lockAndGetTarget( app, app.getTomcatVm());
	items = this.mngr.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.mngr.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	items = this.mngr.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.mngr.unlockTarget( app, app.getTomcatVm());
	items = this.mngr.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.mngr.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.mngr.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
	// Remove the association for the named instance
	this.mngr.dissociateTargetFrom( app, InstanceHelpers.computeInstancePath( app.getMySqlVm()));
	items = this.mngr.findUsageStatistics( t1 );
	Assert.assertEquals( 1, items.size());
	this.mngr.dissociateTargetFrom( app, InstanceHelpers.computeInstancePath( newRootInstance ));
	items = this.mngr.findUsageStatistics( t1 );
	Assert.assertEquals( 0, items.size());
	items = this.mngr.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.mngr.findUsageStatistics( t3 );
	Assert.assertEquals( 0, items.size());
}