Example usage for org.apache.commons.configuration BaseConfiguration BaseConfiguration

List of usage examples for org.apache.commons.configuration BaseConfiguration BaseConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.configuration BaseConfiguration BaseConfiguration.

Prototype

BaseConfiguration

Source Link

Usage

From source file:org.lable.oss.dynamicconfig.PrecomputedTest.java

@Test(expected = IllegalArgumentException.class)
public void refuseNullAsKeyList() {
    Precomputed.monitorByKeys(new BaseConfiguration(), configuration -> null, (String[]) null);
}

From source file:org.lable.oss.dynamicconfig.PrecomputedTest.java

@Test
public void timestampTest() {
    Configuration configuration = new BaseConfiguration();
    configuration.setProperty("a", "AAA");
    configuration.setProperty(ConcurrentConfiguration.MODIFICATION_TIMESTAMP, System.nanoTime());
    Precomputed<String> precomputed = Precomputed.monitorByUpdate(configuration,
            config -> config.getString("a"));

    assertThat(precomputed.get(), is("AAA"));

    // Not a monitored value, so no update.
    configuration.setProperty("a", "BBB");
    assertThat(precomputed.get(), is("AAA"));

    // Touch the timestamp so an update will be required.
    configuration.setProperty(ConcurrentConfiguration.MODIFICATION_TIMESTAMP, System.nanoTime());
    assertThat(precomputed.get(), is("BBB"));
}

From source file:org.lable.oss.dynamicconfig.PrecomputedTest.java

@Test
public void nullTest() {
    Configuration configuration = new BaseConfiguration();
    Precomputed<String> precomputed = Precomputed.monitorByKeys(configuration,
            config -> String.valueOf(config.getString("a")) + "-" + String.valueOf(config.getString("b")), "a",
            "b");

    assertThat(precomputed.get(), is("null-null"));

    configuration.setProperty("a", "XXX");
    configuration.setProperty("b", "YYY");
    assertThat(precomputed.get(), is("XXX-YYY"));

    configuration.setProperty("a", null);
    assertThat(precomputed.get(), is("null-YYY"));

    //configuration.setProperty("a", "AAA");
    configuration.setProperty("b", null);
    assertThat(precomputed.get(), is("null-null"));

    configuration.setProperty("b", "XXX");
    assertThat(precomputed.get(), is("null-XXX"));
}

From source file:org.lable.oss.dynamicconfig.provider.FileBasedConfigSourceIT.java

@Test
public void testLoad() throws URISyntaxException, InterruptedException, IOException, ConfigurationException {
    final String INPUT = "test.yml";
    URL testUrl = getClass().getResource("/" + INPUT);
    final String testYaml = testUrl.toURI().getPath();
    FileBasedConfigSource source = new FileBasedConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", testYaml);
    source.configure(config);/*from w  w w  . j  av a 2s .c o  m*/

    HierarchicalConfigurationDeserializer deserializer = new YamlDeserializer();

    final CountDownLatch latch = new CountDownLatch(1);
    Catcher catcher = new Catcher(latch);
    source.load(deserializer, catcher);
    boolean notTimedOut = latch.await(2, TimeUnit.SECONDS);

    assertThat("Expected callback wasn't called within a reasonable time.", notTimedOut, is(true));
    assertThat(source.config.getName(), is(INPUT));
    assertThat(catcher.caughtConfig.getString("type.unicodeString"), is(""));
}

From source file:org.lable.oss.dynamicconfig.provider.FileBasedConfigSourceIT.java

@Ignore
@Test/* w  w w.j  a  v a2 s.c o  m*/
public void testListen() throws URISyntaxException, InterruptedException, IOException, ConfigurationException {
    final String VALUE_A = "key: AAA\n";
    final String VALUE_B = "key: BBB\n";
    final String VALUE_C = "key: CCC\n";

    File configFile = File.createTempFile("configuration", ".yml");
    Files.write(configFile.toPath(), VALUE_A.getBytes());

    FileBasedConfigSource source = new FileBasedConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", configFile.getPath());
    source.configure(config);
    HierarchicalConfigurationDeserializer deserializer = new YamlDeserializer();

    final List<String> results = new ArrayList<>();
    source.listen(deserializer, fresh -> results.add(fresh.getString("key")));

    // Sleep a little bit between file modification to ensure we have a testable sequence of events.
    TimeUnit.MILLISECONDS.sleep(200);
    // Change the contents of the file. Triggers the listener the first time.
    Files.write(configFile.toPath(), VALUE_B.getBytes());
    TimeUnit.MILLISECONDS.sleep(200);
    // Change it again. Triggers the listener a second time.
    Files.write(configFile.toPath(), VALUE_A.getBytes());
    TimeUnit.MILLISECONDS.sleep(200);
    // Remove the file. Has no impact on the listener.
    Files.delete(configFile.toPath());
    TimeUnit.MILLISECONDS.sleep(200);
    // And recreate it. Triggers the listener.
    Files.write(configFile.toPath(), VALUE_C.getBytes());
    TimeUnit.MILLISECONDS.sleep(200);

    assertThat(results.get(0), is("BBB"));
    assertThat(results.get(1), is("AAA"));
    assertThat(results.get(2), is("CCC"));
    assertThat(results.size(), is(3));
}

From source file:org.lable.oss.dynamicconfig.provider.FileBasedConfigSourceTest.java

@Test(expected = ConfigurationException.class)
public void testConfigurationNonExistingFile() throws Exception {
    FileBasedConfigSource source = new FileBasedConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", "bogusPath");
    source.configure(config);//from www . ja v a 2 s. c  o m
}

From source file:org.lable.oss.dynamicconfig.provider.OnClasspathConfigSourceIT.java

@Test
public void testLoad() throws ConfigurationException {
    ConfigChangeListener mockListener = mock(ConfigChangeListener.class);
    ArgumentCaptor<HierarchicalConfiguration> argument = ArgumentCaptor
            .forClass(HierarchicalConfiguration.class);

    OnClasspathConfigSource source = new OnClasspathConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", "testConfigOnClasspath.yml");
    source.configure(config);//from w  ww. ja va 2s  . com

    source.load(new YamlDeserializer(), mockListener);

    verify(mockListener).changed(argument.capture());
    assertThat(argument.getValue().getString("config.string"), is("XXX"));
}

From source file:org.lable.oss.dynamicconfig.provider.OnClasspathConfigSourceTest.java

@Test(expected = ConfigurationException.class)
public void testLoadNoResource() throws ConfigurationException {
    ConfigChangeListener mockListener = mock(ConfigChangeListener.class);
    HierarchicalConfigurationDeserializer mockLoader = mock(HierarchicalConfigurationDeserializer.class);

    OnClasspathConfigSource source = new OnClasspathConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", "bogusPath");
    source.configure(config);/* www .ja  va 2s.com*/

    source.load(mockLoader, mockListener);
}

From source file:org.lable.oss.dynamicconfig.provider.OnClasspathConfigSourceTest.java

@Test(expected = ConfigurationException.class)
public void testLoadFailedLoadConfig() throws ConfigurationException {
    ConfigChangeListener mockListener = mock(ConfigChangeListener.class);
    HierarchicalConfigurationDeserializer mockLoader = mock(HierarchicalConfigurationDeserializer.class);

    when(mockLoader.deserialize(any(InputStream.class))).thenThrow(new ConfigurationException("!"));

    OnClasspathConfigSource source = new OnClasspathConfigSource();
    Configuration config = new BaseConfiguration();
    config.setProperty("path", "dummy.txt");
    source.configure(config);//  w  w  w.  ja va  2s .  c o m
    source.load(mockLoader, mockListener);
}

From source file:org.lable.oss.dynamicconfig.provider.zookeeper.ZookeepersAsConfigSourceIT.java

@BeforeClass
public static void setUp() throws Exception {
    final String clientPort = "21818";
    final String dataDirectory = System.getProperty("java.io.tmpdir");
    zookeeperHost = "localhost:" + clientPort;

    ServerConfig config = new ServerConfig();
    config.parse(new String[] { clientPort, dataDirectory });

    testConfig = new BaseConfiguration();
    testConfig.setProperty("quorum", zookeeperHost);
    testConfig.setProperty("znode", "/config");
    testConfig.setProperty(APPNAME_PROPERTY, "test");

    server = new Thread(new ZooKeeperThread(config));
    server.start();/*from www  .j  a v a 2 s.c  o m*/
}