@Test
  public void testConfigurationEventDeletedNonScope() throws Exception {
    String testPid = SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "abc.def";

    ConfigurationAdmin cm = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(cm.listConfigurations(EasyMock.isA(String.class))).andReturn(null).anyTimes();
    EasyMock.replay(cm);

    SecuredCommandConfigTransformer scct = new SecuredCommandConfigTransformer();
    scct.setConfigAdmin(cm);
    scct.init();

    @SuppressWarnings("unchecked")
    ServiceReference<ConfigurationAdmin> cmRef = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(cmRef);
    ConfigurationEvent event =
        new ConfigurationEvent(cmRef, ConfigurationEvent.CM_DELETED, null, testPid);

    EasyMock.reset(cm);
    // Do not expect any further calls to cm...
    EasyMock.replay(cm);

    scct.configurationEvent(event);
    EasyMock.verify(cm);
  }
  @Test
  public void testConfigurationEventAdded() throws Exception {
    String testPid = SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "test123";
    Configuration conf = EasyMock.createMock(Configuration.class);
    EasyMock.expect(conf.getPid()).andReturn(testPid).anyTimes();
    EasyMock.replay(conf);

    ConfigurationAdmin cm = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(cm.listConfigurations(EasyMock.isA(String.class))).andReturn(null).anyTimes();
    EasyMock.expect(cm.getConfiguration(testPid)).andReturn(conf).anyTimes();
    EasyMock.replay(cm);

    final List<String> generateCalled = new ArrayList<String>();
    SecuredCommandConfigTransformer scct =
        new SecuredCommandConfigTransformer() {
          @Override
          void generateServiceGuardConfig(Configuration config) throws IOException {
            generateCalled.add(config.getPid());
          }
        };
    scct.setConfigAdmin(cm);
    scct.init();

    @SuppressWarnings("unchecked")
    ServiceReference<ConfigurationAdmin> cmRef = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(cmRef);

    ConfigurationEvent event =
        new ConfigurationEvent(cmRef, ConfigurationEvent.CM_UPDATED, null, testPid);

    assertEquals("Precondition", 0, generateCalled.size());
    scct.configurationEvent(event);
    assertEquals(1, generateCalled.size());
    assertEquals(testPid, generateCalled.iterator().next());
  }
  @Test
  public void testConfigurationEventAddedNonCommand() throws Exception {
    ConfigurationAdmin cm = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(cm.listConfigurations(EasyMock.isA(String.class))).andReturn(null).anyTimes();
    EasyMock.replay(cm);

    SecuredCommandConfigTransformer scct = new SecuredCommandConfigTransformer();
    scct.setConfigAdmin(cm);
    scct.init();

    @SuppressWarnings("unchecked")
    ServiceReference<ConfigurationAdmin> cmRef = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(cmRef);
    ConfigurationEvent event =
        new ConfigurationEvent(cmRef, ConfigurationEvent.CM_UPDATED, null, "test123");

    scct.configurationEvent(event);
    EasyMock.verify(cm); // Ensure that this doesn't cause any unwanted calls on ConfigAdmin
  }
  @Test
  public void testConfigurationLocationChangedEventNoEffect() throws Exception {
    String testPid = SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "test123";

    ConfigurationAdmin cm = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(cm.listConfigurations(EasyMock.isA(String.class))).andReturn(null).anyTimes();
    EasyMock.replay(cm);

    SecuredCommandConfigTransformer scct = new SecuredCommandConfigTransformer();
    scct.setConfigAdmin(cm);
    scct.init();

    @SuppressWarnings("unchecked")
    ServiceReference<ConfigurationAdmin> cmRef = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(cmRef);
    ConfigurationEvent event =
        new ConfigurationEvent(cmRef, ConfigurationEvent.CM_LOCATION_CHANGED, null, testPid);

    scct.configurationEvent(event);
    EasyMock.verify(cm); // Ensure that this doesn't cause any unwanted calls on ConfigAdmin
  }
  @Test
  public void testConfigurationEventDeleted() throws Exception {
    String testPid = SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "test123";

    ConfigurationAdmin cm = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(cm.listConfigurations(EasyMock.isA(String.class))).andReturn(null).anyTimes();
    EasyMock.replay(cm);

    SecuredCommandConfigTransformer scct = new SecuredCommandConfigTransformer();
    scct.setConfigAdmin(cm);
    scct.init();

    @SuppressWarnings("unchecked")
    ServiceReference<ConfigurationAdmin> cmRef = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(cmRef);
    ConfigurationEvent event =
        new ConfigurationEvent(cmRef, ConfigurationEvent.CM_DELETED, null, testPid);

    Configuration c1 = EasyMock.createMock(Configuration.class);
    c1.delete();
    EasyMock.expectLastCall().once();
    EasyMock.replay(c1);
    Configuration c2 = EasyMock.createMock(Configuration.class);
    c2.delete();
    EasyMock.expectLastCall().once();
    EasyMock.replay(c2);

    EasyMock.reset(cm);
    EasyMock.expect(
            cm.listConfigurations("(service.pid=org.apache.karaf.service.acl.command.test123.*)"))
        .andReturn(new Configuration[] {c1, c2})
        .once();
    EasyMock.replay(cm);

    scct.configurationEvent(event);

    EasyMock.verify(cm);
    EasyMock.verify(c1);
    EasyMock.verify(c2);
  }