Example #1
0
  /**
   * Merge configuration dependencies with parent. Child values override parent values.
   *
   * @param parent parent service module
   */
  private void mergeConfigDependencies(ServiceInfo parent) {
    // currently there is no way to remove an inherited config dependency
    List<String> configDependencies = serviceInfo.getConfigDependencies();
    List<String> parentConfigDependencies =
        parent.getConfigDependencies() != null
            ? parent.getConfigDependencies()
            : Collections.<String>emptyList();

    if (configDependencies == null) {
      serviceInfo.setConfigDependencies(parentConfigDependencies);
    } else {
      for (String parentDependency : parentConfigDependencies) {
        if (!configDependencies.contains(parentDependency)) {
          configDependencies.add(parentDependency);
        }
      }
    }
  }
  @Test
  public void testResolve_ConfigDependencies() throws Exception {
    List<String> configDependencies = new ArrayList<String>();
    configDependencies.add("foo");
    configDependencies.add("bar");

    // specified in child only
    ServiceInfo info = new ServiceInfo();
    ServiceInfo parentInfo = new ServiceInfo();
    info.setConfigDependencies(configDependencies);

    ServiceModule service = resolveService(info, parentInfo);
    assertEquals(configDependencies, service.getModuleInfo().getConfigDependencies());

    // specified in parent only
    info.setConfigDependencies(null);
    parentInfo.setConfigDependencies(configDependencies);

    service = resolveService(info, parentInfo);
    assertEquals(configDependencies, service.getModuleInfo().getConfigDependencies());

    // specified in both
    List<String> parentCustomCommands = new ArrayList<String>();
    parentCustomCommands.add("bar");
    parentCustomCommands.add("other");

    info.setConfigDependencies(configDependencies);
    parentInfo.setConfigDependencies(parentCustomCommands);

    service = resolveService(info, parentInfo);
    Collection<String> mergedConfigDependencies = service.getModuleInfo().getConfigDependencies();
    assertEquals(3, mergedConfigDependencies.size());
    assertTrue(mergedConfigDependencies.contains("foo"));
    assertTrue(mergedConfigDependencies.contains("bar"));
    assertTrue(mergedConfigDependencies.contains("other"));

    // not set in either
    info.setConfigDependencies(null);
    parentInfo.setConfigDependencies(null);

    service = resolveService(info, parentInfo);
    assertTrue(service.getModuleInfo().getConfigDependencies().isEmpty());
  }