@Test
  public void testLoadUpdateAndRemove() throws Exception {

    // Create an application
    TestApplication app = new TestApplication();
    app.setDirectory(this.folder.newFolder());

    // Create rule files
    File autonomicRulesDir = new File(app.getDirectory(), Constants.PROJECT_DIR_RULES_AUTONOMIC);
    Assert.assertTrue(autonomicRulesDir.mkdir());

    File f = new File(autonomicRulesDir, "rule1.invalid-ext");
    Assert.assertTrue(f.createNewFile());

    f = new File(autonomicRulesDir, "rule2" + Constants.FILE_EXT_RULE);
    Utils.writeStringInto("rule \"test1\"\nwhen event1 then cmd1 end", f);

    f = new File(autonomicRulesDir, "rule3" + Constants.FILE_EXT_RULE);
    Utils.writeStringInto("rule \"test2\"\nwhen event2 then invalid syntax", f);

    // Load the rules
    Assert.assertEquals(0, this.autonomicMngr.appNameToContext.size());
    this.autonomicMngr.loadApplicationRules(app);
    Assert.assertEquals(1, this.autonomicMngr.appNameToContext.size());

    AutonomicApplicationContext ctx = this.autonomicMngr.appNameToContext.get(app.getName());
    Assert.assertNotNull(ctx);
    Assert.assertEquals(1, ctx.ruleNameToRule.size());
    Assert.assertNotNull(ctx.ruleNameToRule.get("test1"));

    // Update the invalid rule
    Utils.writeStringInto("rule \"test2\"\nwhen event2 then cmd2 end", f);
    this.autonomicMngr.refreshApplicationRules(app, "rule3");
    Assert.assertEquals(1, this.autonomicMngr.appNameToContext.size());

    Assert.assertEquals(2, ctx.ruleNameToRule.size());
    Assert.assertNotNull(ctx.ruleNameToRule.get("test1"));
    Assert.assertNotNull(ctx.ruleNameToRule.get("test2"));

    // Reload the first one
    Rule oldRule1 = ctx.ruleNameToRule.remove("test1");
    this.autonomicMngr.refreshApplicationRules(app, "rule2" + Constants.FILE_EXT_RULE);

    Assert.assertEquals(1, this.autonomicMngr.appNameToContext.size());
    Assert.assertEquals(2, ctx.ruleNameToRule.size());
    Assert.assertNotNull(ctx.ruleNameToRule.get("test1"));
    Assert.assertNotNull(ctx.ruleNameToRule.get("test2"));
    Assert.assertNotSame(oldRule1, ctx.ruleNameToRule.get("test1"));

    // Unload the rules
    this.autonomicMngr.unloadApplicationRules(app);
    Assert.assertEquals(0, this.autonomicMngr.appNameToContext.size());
  }
  @Test
  public void testRefreshRules_unknownRule() throws Exception {

    // Create an application
    TestApplication app = new TestApplication();
    app.setDirectory(this.folder.newFolder());

    // Create the autonomic directory
    File autonomicRulesDir = new File(app.getDirectory(), Constants.PROJECT_DIR_RULES_AUTONOMIC);
    Assert.assertTrue(autonomicRulesDir.mkdir());

    // Load the rules
    Assert.assertEquals(0, this.autonomicMngr.appNameToContext.size());
    this.autonomicMngr.loadApplicationRules(app);
    Assert.assertEquals(1, this.autonomicMngr.appNameToContext.size());

    AutonomicApplicationContext ctx = this.autonomicMngr.appNameToContext.get(app.getName());
    Assert.assertNotNull(ctx);
    Assert.assertEquals(0, ctx.ruleNameToRule.size());

    // Try to refresh an invalid rule
    this.autonomicMngr.refreshApplicationRules(app, "unknown");
    Assert.assertEquals(0, ctx.ruleNameToRule.size());
  }