org.apache.commons.configuration2.builder.BasicConfigurationBuilder Java Examples
The following examples show how to use
org.apache.commons.configuration2.builder.BasicConfigurationBuilder.
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: CombinedConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation creates the result builder object * directly, not using reflection. (The reflection-based approach of the * base class does not work here because a combined configuration builder * has constructors with a different signature.) It also performs some * additional initializations. */ @Override protected BasicConfigurationBuilder<? extends Configuration> createBuilder( final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception { CombinedConfigurationBuilder builder; if (decl.isReload()) { builder = new ReloadingCombinedConfigurationBuilder(); } else { builder = new CombinedConfigurationBuilder(); } decl.getConfigurationBuilder().initChildEventListeners(builder); return builder; }
Example #2
Source File: TestFileExtensionConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether the super class is correctly initialized. */ @Test public void testInitSuper() { final FileExtensionConfigurationBuilderProvider provider = new FileExtensionConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), ReloadingFileBasedConfigurationBuilder.class.getName(), MATCH_CLASS, DEF_CLASS, EXT, null); assertEquals("Wrong builder class", BasicConfigurationBuilder.class.getName(), provider.getBuilderClass()); assertEquals("Wrong reloading builder class", ReloadingFileBasedConfigurationBuilder.class.getName(), provider.getReloadingBuilderClass()); assertEquals("Wrong configuration class", DEF_CLASS, provider.getConfigurationClass()); }
Example #3
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests that change listeners registered at sub builders are removed on a * reset. */ @Test public void testRemoveSubBuilderListener() throws ConfigurationException { final Map<String, Object> attrs = new HashMap<>(); prepareSubBuilderTest(attrs); builder.getConfiguration(); final BasicConfigurationBuilder<?> subBuilder = (BasicConfigurationBuilder<?>) builder .getNamedBuilder(BUILDER_NAME); builder.reset(); prepareSubBuilderTest(attrs); final CombinedConfiguration cc = builder.getConfiguration(); final BasicConfigurationBuilder<?> subBuilder2 = (BasicConfigurationBuilder<?>) builder .getNamedBuilder(BUILDER_NAME); assertNotSame("Got the same sub builder", subBuilder, subBuilder2); subBuilder.reset(); assertSame("Configuration was reset", cc, builder.getConfiguration()); }
Example #4
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests a reset of the builder. The configuration instance should be * created anew. */ @Test public void testResetBuilder() throws ConfigurationException { final Map<String, Object> attrs = new HashMap<>(); final BasicConfigurationBuilder<? extends HierarchicalConfiguration<ImmutableNode>> defBuilder = prepareSubBuilderTest(attrs); final CombinedConfiguration cc = builder.getConfiguration(); final ConfigurationBuilder<? extends Configuration> subBuilder = builder.getNamedBuilder(BUILDER_NAME); defBuilder.reset(); final CombinedConfiguration cc2 = builder.getConfiguration(); assertNotSame("No new configuration instance", cc, cc2); final ConfigurationBuilder<? extends Configuration> subBuilder2 = builder.getNamedBuilder(BUILDER_NAME); assertNotSame("No new sub builder instance", subBuilder, subBuilder2); }
Example #5
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether the force-create attribute is taken into account. */ @Test public void testLoadOptionalForceCreate() throws ConfigurationException { final String name = "optionalConfig"; final Map<String, Object> attrs = new HashMap<>(); attrs.put("fileName", "nonExisting.xml"); attrs.put("config-name", name); attrs.put("config-optional", Boolean.TRUE); attrs.put("config-forceCreate", Boolean.TRUE); final BaseHierarchicalConfiguration defConfig = createDefinitionConfig("xml", attrs); final BasicConfigurationBuilder<? extends BaseHierarchicalConfiguration> defBuilder = createDefinitionBuilder(defConfig); builder.configure(new CombinedBuilderParametersImpl() .setDefinitionBuilder(defBuilder)); final CombinedConfiguration cc = builder.getConfiguration(); assertEquals("Wrong number of configurations", 1, cc.getNumberOfConfigurations()); assertTrue("Wrong configuration type", cc.getConfiguration(name) instanceof XMLConfiguration); }
Example #6
Source File: TestCombinedConfigurationBuilderVFS.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests if the base path is correctly evaluated. */ @Test public void testSetConfigurationBasePath() throws ConfigurationException { final File deepDir = new File(ConfigurationAssert.TEST_DIR, "config/deep"); final Map<String, Object> params = new HashMap<>(); params.put("fileName", "test.properties"); final BaseHierarchicalConfiguration defConfig = createDefinitionConfig("properties", params); defConfig.addProperty("override.properties.fileSystem[@config-class]", VFSFileSystem.class.getName()); final BasicConfigurationBuilder<? extends HierarchicalConfiguration<ImmutableNode>> defBuilder = createDefinitionBuilder(defConfig); builder.configure(new CombinedBuilderParametersImpl() .setDefinitionBuilder(defBuilder).setBasePath( deepDir.getAbsolutePath())); final Configuration config = builder.getConfiguration(); assertEquals("Wrong property value", "somevalue", config.getString("somekey")); }
Example #7
Source File: BaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation delegates to some protected methods to * create a new builder instance using reflection and to configure it with * parameter values defined by the passed in {@code BeanDeclaration}. */ @Override public ConfigurationBuilder<? extends Configuration> getConfigurationBuilder( final ConfigurationDeclaration decl) throws ConfigurationException { try { final Collection<BuilderParameters> params = createParameterObjects(); initializeParameterObjects(decl, params); final BasicConfigurationBuilder<? extends Configuration> builder = createBuilder(decl, params); configureBuilder(builder, decl, params); return builder; } catch (final ConfigurationException cex) { throw cex; } catch (final Exception ex) { throw new ConfigurationException(ex); } }
Example #8
Source File: BaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Creates a new, uninitialized instance of the builder class managed by * this provider. This implementation determines the builder class to be * used by delegating to {@code determineBuilderClass()}. It then calls the * constructor expecting the configuration class, the map with properties, * and the<em>allowFailOnInit</em> flag. * * @param decl the current {@code ConfigurationDeclaration} * @param params initialization parameters for the new builder object * @return the newly created builder instance * @throws Exception if an error occurs */ protected BasicConfigurationBuilder<? extends Configuration> createBuilder( final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception { final Class<?> bldCls = ConfigurationUtils.loadClass(determineBuilderClass(decl)); final Class<?> configCls = ConfigurationUtils.loadClass(determineConfigurationClass(decl, params)); final Constructor<?> ctor = bldCls.getConstructor(CTOR_PARAM_TYPES); // ? extends Configuration is the minimum constraint @SuppressWarnings("unchecked") final BasicConfigurationBuilder<? extends Configuration> builder = (BasicConfigurationBuilder<? extends Configuration>) ctor .newInstance(configCls, null, isAllowFailOnInit(decl)); return builder; }
Example #9
Source File: TestBaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Helper method for testing whether the builder's allowFailOnInit flag is * set correctly. * * @param expFlag the expected flag value * @param props the properties to set in the configuration for the * declaration * @throws ConfigurationException if an error occurs */ private void checkAllowFailOnInit(final boolean expFlag, final String... props) throws ConfigurationException { final HierarchicalConfiguration<?> declConfig = setUpConfig(false); for (final String key : props) { declConfig.addProperty(key, Boolean.TRUE); } final ConfigurationDeclaration decl = createDeclaration(declConfig); final BasicConfigurationBuilder<? extends Configuration> builder = (BasicConfigurationBuilder<? extends Configuration>) createProvider() .getConfigurationBuilder(decl); assertEquals("Wrong flag value", expFlag, builder.isAllowFailOnInit()); }
Example #10
Source File: TestFileExtensionConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tries to create an instance without a file extension. */ @Test(expected = IllegalArgumentException.class) public void testInitNoExt() { new FileExtensionConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, MATCH_CLASS, DEF_CLASS, null, null); }
Example #11
Source File: TestFileExtensionConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tries to create an instance without the default configuration class. */ @Test(expected = IllegalArgumentException.class) public void testInitNoDefaultConfigClass() { new FileExtensionConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, MATCH_CLASS, null, EXT, null); }
Example #12
Source File: TestFileExtensionConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tries to create an instance without the matching configuration class. */ @Test(expected = IllegalArgumentException.class) public void testInitNoMatchingConfigClass() { new FileExtensionConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, null, DEF_CLASS, EXT, null); }
Example #13
Source File: TestFileExtensionConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Creates a test instance with default settings. * * @return the test object */ private static FileExtensionConfigurationBuilderProvider setUpProvider() { final FileExtensionConfigurationBuilderProvider provider = new FileExtensionConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, MATCH_CLASS, DEF_CLASS, EXT, null); return provider; }
Example #14
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a reset of one of the sub builders causes the combined * configuration to be re-created. */ @Test public void testReactOnSubBuilderChange() throws ConfigurationException { final Map<String, Object> attrs = new HashMap<>(); prepareSubBuilderTest(attrs); final CombinedConfiguration cc = builder.getConfiguration(); final BasicConfigurationBuilder<?> subBuilder = (BasicConfigurationBuilder<?>) builder .getNamedBuilder(BUILDER_NAME); subBuilder.reset(); assertNotSame("Configuration not newly created", cc, builder.getConfiguration()); }
Example #15
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Prepares a test with a combined configuration that uses a single sub * builder. This method adds some default attributes to the given map, * creates the corresponding definition builder and configures the combined * builder. * * @param attrs the map with attributes * @return the definition builder */ private BasicConfigurationBuilder<? extends HierarchicalConfiguration<ImmutableNode>> prepareSubBuilderTest( final Map<String, Object> attrs) { attrs.put("fileName", TEST_SUB_XML); attrs.put("config-name", BUILDER_NAME); final BaseHierarchicalConfiguration defConfig = createDefinitionConfig("xml", attrs); final BasicConfigurationBuilder<? extends HierarchicalConfiguration<ImmutableNode>> defBuilder = createDefinitionBuilder(defConfig); builder.configure(new CombinedBuilderParametersImpl() .setDefinitionBuilder(defBuilder)); return defBuilder; }
Example #16
Source File: TestCombinedBuilderParametersImpl.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether the definition builder can be set. */ @Test public void testSetDefinitionBuilder() { final CombinedBuilderParametersImpl params = new CombinedBuilderParametersImpl(); assertNull("Got a definition builder", params.getDefinitionBuilder()); final ConfigurationBuilder<XMLConfiguration> builder = new BasicConfigurationBuilder<>( XMLConfiguration.class); assertSame("Wrong result", params, params.setDefinitionBuilder(builder)); assertSame("Builder was not set", builder, params.getDefinitionBuilder()); }
Example #17
Source File: TestBaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests that the collection with parameter classes cannot be modified. */ @Test(expected = UnsupportedOperationException.class) public void testGetParameterClassesModify() { final BaseConfigurationBuilderProvider provider = new BaseConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, PropertiesConfiguration.class.getName(), Arrays.asList(BasicBuilderParameters.class.getName())); provider.getParameterClasses().clear(); }
Example #18
Source File: TestBaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a null collection of parameter classes is handled * correctly. */ @Test public void testInitNoParameterClasses() { final BaseConfigurationBuilderProvider provider = new BaseConfigurationBuilderProvider( BasicConfigurationBuilder.class.getName(), null, PropertiesConfiguration.class.getName(), null); assertTrue("Got parameter classes", provider.getParameterClasses() .isEmpty()); }
Example #19
Source File: TestBaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tries to create an instance without a configuration class. */ @Test(expected = IllegalArgumentException.class) public void testInitNoConfigurationClass() { new BaseConfigurationBuilderProvider(BasicConfigurationBuilder.class.getName(), null, null, null); }
Example #20
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 4 votes |
/** * Tests whether reloading support works for MultiFileConfigurationBuilder. */ @Test public void testMultiTenentConfigurationReloading() throws ConfigurationException, InterruptedException { final CombinedConfiguration config = createMultiFileConfig("testCCMultiTenentReloading.xml"); final File outFile = ConfigurationAssert.getOutFile("MultiFileReloadingTest.xml"); switchToMultiFile(outFile.getAbsolutePath()); final XMLConfiguration reloadConfig = new XMLConfiguration(); final FileHandler handler = new FileHandler(reloadConfig); handler.setFile(outFile); final String key = "test.reload"; reloadConfig.setProperty(key, "no"); handler.save(); try { assertEquals("Wrong property", "no", config.getString(key)); final ConfigurationBuilder<? extends Configuration> childBuilder = builder.getNamedBuilder("clientConfig"); assertTrue("Not a reloading builder", childBuilder instanceof ReloadingControllerSupport); final ReloadingController ctrl = ((ReloadingControllerSupport) childBuilder) .getReloadingController(); ctrl.checkForReloading(null); // initialize reloading final BuilderEventListenerImpl l = new BuilderEventListenerImpl(); childBuilder.addEventListener(ConfigurationBuilderEvent.RESET, l); reloadConfig.setProperty(key, "yes"); handler.save(); int attempts = 10; boolean changeDetected; do { changeDetected = ctrl.checkForReloading(null); if (!changeDetected) { Thread.sleep(1000); handler.save(outFile); } } while (!changeDetected && --attempts > 0); assertTrue("No change detected", changeDetected); assertEquals("Wrong updated property", "yes", builder .getConfiguration().getString(key)); final ConfigurationBuilderEvent event = l.nextEvent(ConfigurationBuilderEvent.RESET); l.assertNoMoreEvents(); final BasicConfigurationBuilder<? extends Configuration> multiBuilder = (BasicConfigurationBuilder<? extends Configuration>) event.getSource(); childBuilder.removeEventListener(ConfigurationBuilderEvent.RESET, l); multiBuilder.resetResult(); l.assertNoMoreEvents(); } finally { assertTrue("Output file could not be deleted", outFile.delete()); } }
Example #21
Source File: BaseConfigurationBuilderProvider.java From commons-configuration with Apache License 2.0 | 3 votes |
/** * Configures a newly created builder instance with its initialization * parameters. This method is called after a new instance was created using * reflection. This implementation passes the parameter objects to the * builder's {@code configure()} method. * * @param builder the builder to be initialized * @param decl the current {@code ConfigurationDeclaration} * @param params the collection with initialization parameter objects * @throws Exception if an error occurs */ protected void configureBuilder( final BasicConfigurationBuilder<? extends Configuration> builder, final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception { builder.configure(params.toArray(new BuilderParameters[params.size()])); }
Example #22
Source File: DatabaseConfigurationTestHelper.java From commons-configuration with Apache License 2.0 | 3 votes |
/** * Creates a configuration instance of the specified class with the given * parameters. * * @param <T> the type of the result configuration * @param configCls the configuration class * @param params the parameters object * @return the newly created configuration instance * @throws ConfigurationException if an error occurs */ public <T extends DatabaseConfiguration> T createConfig(final Class<T> configCls, final DatabaseBuilderParameters params) throws ConfigurationException { return new BasicConfigurationBuilder<>(configCls).configure(params) .getConfiguration(); }
Example #23
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 2 votes |
/** * Creates a configuration builder for the definition configuration which * always returns the passed in definition configuration. * * @param defConfig the definition configuration * @return the definition builder */ protected static BasicConfigurationBuilder<? extends BaseHierarchicalConfiguration> createDefinitionBuilder( final BaseHierarchicalConfiguration defConfig) { return new ConstantConfigurationBuilder(defConfig); }
Example #24
Source File: CombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 2 votes |
/** * Initializes the event listeners of the specified builder from this * object. This method is used to inherit all listeners from a parent * builder. * * @param dest the destination builder object which is to be initialized */ void initChildEventListeners( final BasicConfigurationBuilder<? extends Configuration> dest) { copyEventListeners(dest); }