@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);
  }
  @Test
  public void testTransformation() throws Exception {
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("foo", "a,b,c");
    props.put("bar[/.*[a]+*/]", "d");
    props.put("bar", "e");
    props.put("zar[/.*HiThere*/]", "f");
    props.put("service.pid", SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "abc");
    Configuration commandConfig = mockConfiguration(props);

    Dictionary<String, Object> props2 = new Hashtable<String, Object>();
    props2.put("xxx", "yyy");
    props2.put(
        "service.pid", SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX + "xyz.123");
    Configuration commandConfig2 = mockConfiguration(props2);

    Dictionary<String, Object> props3 = new Hashtable<String, Object>();
    props3.put("test", "toast");
    props3.put("service.pid", "xyz.123");
    Configuration otherConfig = mockConfiguration(props3);

    final Map<String, Configuration> configurations = new HashMap<String, Configuration>();

    ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(
            ca.listConfigurations(
                "(service.pid="
                    + SecuredCommandConfigTransformer.PROXY_COMMAND_ACL_PID_PREFIX
                    + "*)"))
        .andReturn(new Configuration[] {commandConfig, commandConfig2, otherConfig})
        .anyTimes();
    EasyMock.expect(ca.getConfiguration(EasyMock.isA(String.class)))
        .andAnswer(
            new IAnswer<Configuration>() {
              @Override
              public Configuration answer() throws Throwable {
                String pid = (String) EasyMock.getCurrentArguments()[0];
                Configuration c = configurations.get(pid);
                if (c == null) {
                  c = EasyMock.createMock(Configuration.class);

                  // Put some expectations in the various mocks
                  Dictionary<String, Object> m = new Hashtable<String, Object>();
                  if ("org.apache.karaf.service.acl.command.abc.foo".equals(pid)) {
                    m.put(
                        "service.guard", "(&(osgi.command.scope=abc)(osgi.command.function=foo))");
                    m.put("execute", "a,b,c");
                    m.put("foo", "a,b,c");
                  } else if ("org.apache.karaf.service.acl.command.abc.bar".equals(pid)) {
                    m.put(
                        "service.guard", "(&(osgi.command.scope=abc)(osgi.command.function=bar))");
                    m.put("execute[/.*/,/.*[a]+*/]", "d");
                    m.put("execute", "e");
                    m.put("bar[/.*[a]+*/]", "d");
                    m.put("bar", "e");
                  } else if ("org.apache.karaf.service.acl.command.abc.zar".equals(pid)) {
                    m.put(
                        "service.guard", "(&(osgi.command.scope=abc)(osgi.command.function=zar))");
                    m.put("execute[/.*/,/.*HiThere*/]", "f");
                    m.put("zar[/.*HiThere*/]", "f");
                  } else {
                    fail("Unexpected PID: " + pid);
                  }
                  m.put("*", "*");
                  c.update(m);
                  EasyMock.expectLastCall().once();

                  EasyMock.replay(c);
                  configurations.put(pid, c);
                }
                return c;
              }
            })
        .anyTimes();
    EasyMock.replay(ca);

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

    assertEquals(3, configurations.size());

    boolean foundFoo = false;
    boolean foundBar = false;
    boolean foundZar = false;
    for (Map.Entry<String, Configuration> entry : configurations.entrySet()) {
      Configuration c = entry.getValue();
      EasyMock.verify(c);
      if ("org.apache.karaf.service.acl.command.abc.foo".equals(entry.getKey())) {
        foundFoo = true;
      } else if ("org.apache.karaf.service.acl.command.abc.bar".equals(entry.getKey())) {
        foundBar = true;
      } else if ("org.apache.karaf.service.acl.command.abc.zar".equals(entry.getKey())) {
        foundZar = true;
      }
    }

    assertTrue(foundFoo);
    assertTrue(foundBar);
    assertTrue(foundZar);
  }