Java Code Examples for org.apache.commons.configuration2.BaseHierarchicalConfiguration#addProperty()

The following examples show how to use org.apache.commons.configuration2.BaseHierarchicalConfiguration#addProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a custom provider can be registered.
 */
@Test
public void testCustomBuilderProvider() throws ConfigurationException
{
    final String tagName = "myTestTag";
    final BaseHierarchicalConfiguration dataConf =
            new BaseHierarchicalConfiguration();
    dataConf.addProperty(tagName, Boolean.TRUE);
    final Map<String, Object> attrs = new HashMap<>();
    attrs.put("config-name", BUILDER_NAME);
    attrs.put("config-at", "tests");
    builder.configure(new CombinedBuilderParametersImpl()
            .setDefinitionBuilder(
                    createDefinitionBuilder(createDefinitionConfig(tagName,
                            attrs))).registerProvider(tagName,
                    decl -> new ConstantConfigurationBuilder(
                            dataConf)));
    final CombinedConfiguration cc = builder.getConfiguration();
    assertEquals("Configuration not added", dataConf,
            cc.getConfiguration(BUILDER_NAME));
    assertEquals("Property not set", Boolean.TRUE,
            cc.getProperty("tests." + tagName));
}
 
Example 2
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a configuration with data for testing nested bean declarations
 * including constructor arguments.
 *
 * @return the initialized test configuration
 */
private static BaseHierarchicalConfiguration prepareNestedBeanDeclarations()
{
    final BaseHierarchicalConfiguration config =
            new BaseHierarchicalConfiguration();
    setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
    final String keyCtorArg = KEY + ".config-constrarg";
    setupBeanDeclaration(config, keyCtorArg, CTOR_COMPLEX_ATTRIBUTES,
            CTOR_COMPLEX_VALUES);
    config.addProperty(keyCtorArg + "[@config-class]", "TestClass");
    config.addProperty(keyCtorArg + "(-1)[@config-value]", CTOR_ID);
    config.addProperty(keyCtorArg + "[@config-type]", "long");
    for (int i = 0; i < COMPLEX_PROPS.length; i++)
    {
        setupBeanDeclaration(config, KEY + '.' + COMPLEX_PROPS[i],
                COMPLEX_ATTRIBUTES[i], COMPLEX_VALUES[i]);
        config.addProperty(
                KEY + '.' + COMPLEX_PROPS[i] + "[@config-class]",
                COMPLEX_CLASSES[i]);
    }
    return config;
}
 
Example 3
Source File: MailetConfigImpl.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Set the Avalon Configuration object for the mailet.
 * 
 * @param newConfiguration
 *            the new Configuration for the mailet
 */
public void setConfiguration(Configuration newConfiguration) {
    BaseHierarchicalConfiguration builder = new BaseHierarchicalConfiguration();
    
    // Disable the delimiter parsing. See JAMES-1232
    builder.setListDelimiterHandler(new DisabledListDelimiterHandler());
    Iterator<String> keys = newConfiguration.getKeys();
    while (keys.hasNext()) {
        String key = keys.next();
        String[] values = newConfiguration.getStringArray(key);
        // See JAMES-1177
        // Need to replace ".." with "."
        // See
        // http://commons.apache.org/configuration/userguide-1.2/howto_xml.html
        // Escaping dot characters in XML tags
        key = key.replaceAll("\\.\\.", "\\.");
        
        // Convert array values to a "," delimited string value
        StringBuilder valueBuilder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            valueBuilder.append(values[i]);
            if (i + 1 < values.length) {
                valueBuilder.append(",");
            }
        }
        builder.addProperty(key, valueBuilder.toString());
    }

    configuration = builder;
}
 
Example 4
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests obtaining the bean's properties when reserved attributes are
 * involved. These should be ignored.
 */
@Test
public void testGetBeanPropertiesWithReservedAttributes()
{
    final BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
    setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
    config.addProperty(KEY + "[@config-testattr]", "yes");
    config.addProperty(KEY + "[@config-anothertest]", "this, too");
    final XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
    checkProperties(decl, TEST_PROPS, TEST_VALUES);
}
 
Example 5
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests fetching the bean's class name.
 */
@Test
public void testGetBeanClassName()
{
    final BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
    config.addProperty(KEY + "[@config-class]", getClass().getName());
    final XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
    assertEquals("Wrong class name", getClass().getName(), decl
            .getBeanClassName());
}
 
Example 6
Source File: ListenerConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void getGroupShouldContainsConfiguredValue() {
    String groupName = "Avengers";
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("class", "MyClassName");
    configuration.addProperty("group", groupName);

    ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration);

    assertThat(listenerConfiguration.getGroup()).contains(groupName);
}
 
Example 7
Source File: ListenerConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void getGroupShouldBeEmptyByDefault() {
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("class", "MyClassName");

    ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration);

    assertThat(listenerConfiguration.getGroup()).isEmpty();
}
 
Example 8
Source File: ListenerConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void isAsyncShouldReturnConfiguredValue() {
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("class", "MyClassName");
    configuration.addProperty("async", "false");

    ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration);

    assertThat(listenerConfiguration.isAsync()).contains(false);
}
 
Example 9
Source File: ListenerConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void getClazzShouldReturnTheClassNameFromTheConfiguration() {
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    String expectedClazz = "MyClassName";
    configuration.addProperty("class", expectedClazz);

    ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration);

    assertThat(listenerConfiguration.getClazz()).isEqualTo(expectedClazz);
}
 
Example 10
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests fetching the parameter for the bean factory.
 */
@Test
public void testGetBeanFactoryParameter()
{
    final BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
    config
            .addProperty(KEY + "[@config-factoryParam]",
                    "myFactoryParameter");
    final XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
    assertEquals("Wrong factory parameter", "myFactoryParameter", decl
            .getBeanFactoryParameter());
}
 
Example 11
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests constructing a bean declaration from a key with multiple values.
 * This should cause an exception because keys must be unique.
 */
@Test(expected = ConfigurationRuntimeException.class)
public void testInitFromMultiValueKey()
{
    final BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
    config.addProperty(KEY, "myFirstKey");
    config.addProperty(KEY, "mySecondKey");
    new XMLBeanDeclaration(config, KEY);
}
 
Example 12
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for creating a definition configuration. This method
 * creates a configuration containing a tag referring to a configuration
 * source. The tag has attributes defined by the given map.
 *
 * @param tag the name of the tag to create
 * @param attrs the attributes of this tag
 * @return the definition configuration
 */
protected static BaseHierarchicalConfiguration createDefinitionConfig(final String tag,
        final Map<String, Object> attrs)
{
    final BaseHierarchicalConfiguration defConfig =
            new BaseHierarchicalConfiguration();
    final String prefix = "override." + tag;
    for (final Map.Entry<String, Object> e : attrs.entrySet())
    {
        defConfig.addProperty(prefix + "[@" + e.getKey() + "]",
                e.getValue());
    }
    return defConfig;
}
 
Example 13
Source File: JDBCMailRepositoryTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void init() throws Exception {
    MockFileSystem fs = new MockFileSystem();
    DataSource datasource = getDataSource();
    mailRepository = new JDBCMailRepository();

    BaseHierarchicalConfiguration defaultConfiguration = new BaseHierarchicalConfiguration();
    defaultConfiguration.addProperty("[@destinationURL]", "db://maildb/mr/testrepo");
    defaultConfiguration.addProperty("sqlFile", "file://conf/sqlResources.xml");
    defaultConfiguration.addProperty("[@type]", "MAIL");
    mailRepository.setFileSystem(fs);
    mailRepository.setDatasource(datasource);
    mailRepository.configure(defaultConfiguration);
    mailRepository.init();
}
 
Example 14
Source File: CassandraUsersRepositoryTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
private static UsersRepositoryImpl getUsersRepository(DomainList domainList, boolean enableVirtualHosting) throws Exception {
    CassandraUsersDAO usersDAO = new CassandraUsersDAO(cassandraCluster.getCassandraCluster().getConf());
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("enableVirtualHosting", String.valueOf(enableVirtualHosting));

    UsersRepositoryImpl usersRepository = new UsersRepositoryImpl(domainList, usersDAO);
    usersRepository.configure(configuration);
    return usersRepository;
}
 
Example 15
Source File: RecipientRewriteTableConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void goodConfigurationWithoutMappingShouldHaveANullMappingLevel() throws ConfigurationException {
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("recursiveMapping", "false");
    configuration.addProperty("mappingLimit", 42);
    RecipientRewriteTableConfiguration recipientRewriteTableConfiguration = RecipientRewriteTableConfiguration.fromConfiguration(configuration);

    assertThat(recipientRewriteTableConfiguration.getMappingLimit())
        .isEqualTo(0);
    assertThat(recipientRewriteTableConfiguration.isRecursive())
        .isFalse();
}
 
Example 16
Source File: RecipientRewriteTableConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void negativeLimitShouldThrows() {
    BaseHierarchicalConfiguration configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("recursiveMapping", "true");
    configuration.addProperty("mappingLimit", -1);

    assertThatCode(() -> RecipientRewriteTableConfiguration.fromConfiguration(configuration))
        .isInstanceOf(ConfigurationException.class);
}
 
Example 17
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether interpolation is done on constructor arguments.
 */
@Test
public void testGetInterpolatedConstructorArgs()
{
    final BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
    final String expectedValue = "ctorArg";
    config.addProperty("value", expectedValue);
    setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
    config.addProperty(KEY + ".config-constrarg[@config-value]", "${value}");
    final XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
    final Collection<ConstructorArg> args = decl.getConstructorArgs();
    final ConstructorArg arg = args.iterator().next();
    assertEquals("Wrong interpolated value", expectedValue, arg.getValue());
}
 
Example 18
Source File: FileMailRepositoryTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
protected BaseHierarchicalConfiguration withConfigurationOptions(BaseHierarchicalConfiguration configuration) {
    configuration.addProperty("[@FIFO]", "false");
    configuration.addProperty("[@CACHEKEYS]", "false");
    return configuration;
}
 
Example 19
Source File: FileMailRepositoryTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
protected BaseHierarchicalConfiguration withConfigurationOptions(BaseHierarchicalConfiguration configuration) {
    configuration.addProperty("[@FIFO]", "true");
    configuration.addProperty("[@CACHEKEYS]", "false");
    return configuration;
}
 
Example 20
Source File: TestReloadingCombinedConfigurationBuilderFileBased.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Tests concurrent access to a reloading builder for combined
 * configurations.
 */
@Test
public void testConcurrentGetAndReload() throws Exception
{
    final int threadCount = 4;
    final int loopCount = 100;
    final ReloadingDetectorFactory detectorFactory =
            (handler, params) -> new RandomReloadingDetector();
    final BaseHierarchicalConfiguration defConf = new BaseHierarchicalConfiguration();
    defConf.addProperty("header.result.nodeCombiner[@config-class]",
            MergeCombiner.class.getName());
    defConf.addProperty("header.result.expressionEngine[@config-class]",
            XPathExpressionEngine.class.getName());
    addReloadSource(defConf, "configA.xml");
    addReloadSource(defConf, "configB.xml");
    final Synchronizer sync = new ReadWriteSynchronizer();
    builder.configure(parameters
            .combined()
            .setDefinitionBuilder(new ConstantConfigurationBuilder(defConf))
            .setSynchronizer(sync)
            .registerChildDefaultsHandler(
                    BasicBuilderProperties.class,
                    new CopyObjectDefaultHandler(
                            new BasicBuilderParameters()
                                    .setSynchronizer(sync)))
            .registerChildDefaultsHandler(
                    FileBasedBuilderProperties.class,
                    new CopyObjectDefaultHandler(
                            new FileBasedBuilderParametersImpl()
                                    .setReloadingDetectorFactory(detectorFactory))));

    assertEquals("Wrong initial value", "100", builder.getConfiguration()
            .getString("/property[@name='config']/@value"));

    final Thread testThreads[] = new Thread[threadCount];
    final int failures[] = new int[threadCount];

    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i] = new ReloadThread(builder, failures, i, loopCount);
        testThreads[i].start();
    }

    int totalFailures = 0;
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i].join();
        totalFailures += failures[i];
    }
    assertTrue(totalFailures + " failures Occurred", totalFailures == 0);
}