Example usage for org.apache.commons.configuration.reloading FileAlwaysReloadingStrategy FileAlwaysReloadingStrategy

List of usage examples for org.apache.commons.configuration.reloading FileAlwaysReloadingStrategy FileAlwaysReloadingStrategy

Introduction

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

Prototype

FileAlwaysReloadingStrategy

Source Link

Usage

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests collaboration between XMLConfiguration and a reloading strategy.
 *///from ww w.j a va  2 s .  c  om
@Test
public void testReloading() throws Exception {
    assertNotNull(conf.getReloadingStrategy());
    assertTrue(conf.getReloadingStrategy() instanceof InvariantReloadingStrategy);
    PrintWriter out = null;

    try {
        out = new PrintWriter(new FileWriter(testSaveConf));
        out.println("<?xml version=\"1.0\"?><config><test>1</test></config>");
        out.close();
        out = null;
        conf.setFile(testSaveConf);
        FileAlwaysReloadingStrategy strategy = new FileAlwaysReloadingStrategy();
        strategy.setRefreshDelay(100);
        conf.setReloadingStrategy(strategy);
        assertEquals(strategy, conf.getReloadingStrategy());
        assertEquals("Wrong file monitored", testSaveConf.getAbsolutePath(),
                strategy.getMonitoredFile().getAbsolutePath());
        conf.load();
        assertEquals(1, conf.getInt("test"));

        out = new PrintWriter(new FileWriter(testSaveConf));
        out.println("<?xml version=\"1.0\"?><config><test>2</test></config>");
        out.close();
        out = null;

        int value = conf.getInt("test");
        assertEquals("No reloading performed", 2, value);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

@Test
public void testReloadingOOM() throws Exception {
    assertNotNull(conf.getReloadingStrategy());
    assertTrue(conf.getReloadingStrategy() instanceof InvariantReloadingStrategy);
    PrintWriter out = null;//from   w  w  w . j  a v a  2 s .  co  m

    try {
        out = new PrintWriter(new FileWriter(testSaveConf));
        out.println("<?xml version=\"1.0\"?><config><test>1</test></config>");
        out.close();
        out = null;
        conf.setFile(testSaveConf);
        FileAlwaysReloadingStrategy strategy = new FileAlwaysReloadingStrategy();
        strategy.setRefreshDelay(100);
        conf.setReloadingStrategy(strategy);
        conf.load();
        assertEquals(1, conf.getInt("test"));

        for (int i = 1; i < LOOP_COUNT; ++i) {
            assertEquals(1, conf.getInt("test"));
        }
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests a combination of auto save = true and an associated reloading
 * strategy.//from  w w w .  jav  a 2 s.c om
 */
@Test
public void testAutoSaveWithReloadingStrategy() throws ConfigurationException {
    conf.setFile(testSaveConf);
    conf.save();
    conf.setReloadingStrategy(new FileAlwaysReloadingStrategy());
    conf.setAutoSave(true);
    assertEquals("Value not found", "value", conf.getProperty("element"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

@Test
public void testConcurrentGetAndReload() throws Exception {
    // final FileConfiguration config = new
    // PropertiesConfiguration("test.properties");
    final FileConfiguration config = new XMLConfiguration("test.xml");
    config.setReloadingStrategy(new FileAlwaysReloadingStrategy());

    assertTrue("Property not found", config.getProperty("test.short") != null);

    Thread testThreads[] = new Thread[THREAD_COUNT];

    for (int i = 0; i < testThreads.length; ++i) {
        testThreads[i] = new ReloadThread(config);
        testThreads[i].start();// ww w  .ja  va2 s.com
    }

    for (int i = 0; i < LOOP_COUNT; i++) {
        assertTrue("Property not found", config.getProperty("test.short") != null);
    }

    for (int i = 0; i < testThreads.length; ++i) {
        testThreads[i].join();
    }
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Prepares a configuration object for testing a reload operation.
 * /*  ww  w  . j  a va2s.co  m*/
 * @return the initialized configuration
 * @throws ConfigurationException
 *             if an error occurs
 */
private XMLConfiguration setUpReloadTest() throws ConfigurationException {
    removeTestFile();
    conf.save(testSaveConf);
    XMLConfiguration c = new XMLConfiguration(testSaveConf);
    c.setReloadingStrategy(new FileAlwaysReloadingStrategy());
    conf.setProperty("test(0).entity", "newValue");
    conf.save(testSaveConf);
    return c;
}