@Test
public void testApplicationDeletion() throws Exception {

	// As we are not very familiar with Quartz behavior,
	// we will use a real Quartz scheduler for tests.
	// We do not mock it.
	Manager manager = Mockito.spy( new Manager());
	manager.configurationMngr().setWorkingDirectory( this.folder.newFolder());
	// Register applications
	final String cmdName = "cmd";
	final Application app1 = new TestApplication().name( "app1" ).directory( this.folder.newFolder());
	final Application app2 = new TestApplication().name( "app2" ).directory( this.folder.newFolder());
	TestManagerWrapper wrapper = new TestManagerWrapper( manager );
	wrapper.addManagedApplication( new ManagedApplication( app1 ));
	wrapper.addManagedApplication( new ManagedApplication( app2 ));
	// Register commands
	manager.commandsMngr().createOrUpdateCommand( app1, cmdName, "" );
	manager.commandsMngr().createOrUpdateCommand( app2, cmdName, "" );
	// Prepare the scheduler
	RoboconfScheduler scheduler = new RoboconfScheduler();
	scheduler.manager = manager;
	Mockito.reset( manager );
	try {
		scheduler.start();
		// Register job properties
		Assert.assertNotNull( scheduler.saveJob( null, "job11", cmdName, "0 0 0 ? 1 *", app1.getName()));
		Assert.assertNotNull( scheduler.saveJob( null, "job12", cmdName, "0 0 0 ? 1 *", app1.getName()));
		Assert.assertNotNull( scheduler.saveJob( null, "job13", cmdName, "0 0 0 ? 1 *", app1.getName()));
		String jobId21 = scheduler.saveJob( null, "job21", cmdName, "0 0 0 ? 1 *", app2.getName());
		Assert.assertNotNull( jobId21  );
		String jobId22 = scheduler.saveJob( null, "job22", cmdName, "0 0 0 ? 1 *", app2.getName());
		Assert.assertNotNull( jobId22  );
		// Now, send signals to the manager listener.
		// Idle ones...
		File schedulerDirectory = scheduler.getSchedulerDirectory();
		Set<JobKey> jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup());
		Assert.assertEquals( 5, jobKeys.size());
		Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size());
		scheduler.dmListener.application( app1, EventType.CREATED );
		jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup());
		Assert.assertEquals( 5, jobKeys.size());
		Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size());
		scheduler.dmListener.application( app1, EventType.CHANGED );
		jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup());
		Assert.assertEquals( 5, jobKeys.size());
		Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size());
		// "app1" was deleted.
		scheduler.dmListener.application( app1, EventType.DELETED );
		jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup());
		Assert.assertEquals( 2, jobKeys.size());
		Assert.assertEquals( 2, Utils.listAllFiles( schedulerDirectory ).size());
		Assert.assertTrue( jobKeys.contains( new JobKey( jobId21, "app2" )));
		Assert.assertTrue( jobKeys.contains( new JobKey( jobId22, "app2" )));
	} finally {
		scheduler.stop();
	}
}