本文整理匯總了Java中org.apache.commons.configuration.HierarchicalConfiguration.setProperty方法的典型用法代碼示例。如果您正苦於以下問題:Java HierarchicalConfiguration.setProperty方法的具體用法?Java HierarchicalConfiguration.setProperty怎麽用?Java HierarchicalConfiguration.setProperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.configuration.HierarchicalConfiguration
的用法示例。
在下文中一共展示了HierarchicalConfiguration.setProperty方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSave
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void testSave() throws ConfigurationException, IOException {
HierarchicalConfigurationSerializer serializer = new JsonSerializer();
HierarchicalConfiguration configuration = new HierarchicalConfiguration();
configuration.setProperty("type.unicodeString", "€");
configuration.setProperty("type.booleanFalse", false);
configuration.setProperty("type.booleanTrue", true);
configuration.setProperty("type.list", Arrays.asList("1", "2", "3"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
serializer.serialize(configuration, output, false);
String json = IOUtils.toString(new StringReader(output.toString("UTF-8")));
JsonNode tree = mapper.readTree(json);
JsonNode nodeType = tree.get("type");
assertThat(nodeType.get("unicodeString").textValue(), is("€"));
assertThat(nodeType.get("booleanFalse").booleanValue(), is(false));
assertThat(nodeType.get("booleanTrue").booleanValue(), is(true));
ArrayNode listString = (ArrayNode) nodeType.get("list");
assertThat(listString, instanceOf(ArrayNode.class));
assertThat(json.contains("\n"), is(false));
}
示例2: testFormatting
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void testFormatting() throws ConfigurationException, IOException {
HierarchicalConfigurationSerializer serializer = new JsonSerializer();
HierarchicalConfiguration configuration = new HierarchicalConfiguration();
configuration.setProperty("type.unicodeString", "€");
configuration.setProperty("type.booleanFalse", false);
configuration.setProperty("type.booleanTrue", true);
configuration.setProperty("type.list", Arrays.asList("1"));
System.out.println(configuration.getProperty("type.list").getClass().getCanonicalName());
ByteArrayOutputStream output = new ByteArrayOutputStream();
serializer.serialize(configuration, output, true);
String json = IOUtils.toString(new StringReader(output.toString("UTF-8")));
assertThat(json.contains("\n"), is(true));
}
示例3: viaInitializerTest
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void viaInitializerTest() throws Exception {
setData(zookeeper, "test", "\n");
System.setProperty(LIBRARY_PREFIX + ".type", "zookeeper");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.znode", "/config");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.quorum", zookeeperHost);
System.setProperty(LIBRARY_PREFIX + "." + APPNAME_PROPERTY, "test");
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("key", "DEFAULT");
Configuration configuration = ConfigurationManager.configureFromProperties(
defaults, new YamlDeserializer()
);
assertThat(configuration.getString("key"), is("DEFAULT"));
setData(zookeeper, "test", "key: AAA");
TimeUnit.MILLISECONDS.sleep(300);
assertThat(configuration.getString("key"), is("AAA"));
}
示例4: diProviderTestWithDefaults
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void diProviderTestWithDefaults() {
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".type", "classpath");
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".classpath.path", "test.yml");
final HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("type.string", "Not okay");
defaults.setProperty("only.in.defaults", "XXX");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(HierarchicalConfiguration.class).annotatedWith(ConfigurationDefaults.class).toInstance(defaults);
bind(HierarchicalConfigurationDeserializer.class).to(YamlDeserializer.class);
bind(Configuration.class).toProvider(ConfigurationProvider.class);
}
});
Configuration configuration = injector.getInstance(Configuration.class);
// As set in test.yml:
assertThat(configuration.getString("type.string"), is("Okay"));
// Overridden in this test:
assertThat(configuration.getString("only.in.defaults"), is("XXX"));
}
示例5: diModuleTestWithDefaults
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void diModuleTestWithDefaults() {
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".type", "classpath");
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".classpath.path", "test.yml");
final HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("type.string", "Not okay");
defaults.setProperty("only.in.defaults", "XXX");
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new DynamicConfigModule());
bind(HierarchicalConfiguration.class).annotatedWith(ConfigurationDefaults.class).toInstance(defaults);
}
});
Configuration configuration = injector.getInstance(Configuration.class);
// As set in test.yml:
assertThat(configuration.getString("type.string"), is("Okay"));
// Overridden in this test:
assertThat(configuration.getString("only.in.defaults"), is("XXX"));
}
示例6: createControllersConfig
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
public static String createControllersConfig(HierarchicalConfiguration cfg,
HierarchicalConfiguration actualCfg,
String target, String netconfOperation,
String controllerOperation,
List<ControllerInfo> controllers) {
//cfg.getKeys().forEachRemaining(key -> System.out.println(key));
cfg.setProperty("edit-config.target", target);
cfg.setProperty("edit-config.default-operation", netconfOperation);
cfg.setProperty("edit-config.config.capable-switch.id",
parseCapableSwitchId(actualCfg));
cfg.setProperty("edit-config.config.capable-switch." +
"logical-switches.switch.id", parseSwitchId(actualCfg));
List<ConfigurationNode> newControllers = new ArrayList<>();
for (ControllerInfo ci : controllers) {
XMLConfiguration controller = new XMLConfiguration();
controller.setRoot(new HierarchicalConfiguration.Node("controller"));
String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
controller.setProperty("id", id);
controller.setProperty("ip-address", ci.ip());
controller.setProperty("port", ci.port());
controller.setProperty("protocol", ci.type());
newControllers.add(controller.getRootNode());
}
cfg.addNodes("edit-config.config.capable-switch.logical-switches." +
"switch.controllers", newControllers);
XMLConfiguration editcfg = (XMLConfiguration) cfg;
StringWriter stringWriter = new StringWriter();
try {
editcfg.save(stringWriter);
} catch (ConfigurationException e) {
log.error("createControllersConfig()", e);
}
String s = stringWriter.toString()
.replaceAll("<controller>",
"<controller nc:operation=\"" + controllerOperation + "\">");
s = s.replace("<target>" + target + "</target>",
"<target><" + target + "/></target>");
return s;
}
示例7: withDefaultsClasspathTest
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void withDefaultsClasspathTest() throws ConfigurationException {
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".type", "classpath");
System.setProperty(ConfigurationManager.LIBRARY_PREFIX + ".classpath.path", "test.yml");
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("type.string", "Not okay");
defaults.setProperty("only.in.defaults", "XXX");
Configuration configuration = ConfigurationManager.configureFromProperties(
defaults, new YamlDeserializer()
);
assertThat(configuration.getString("type.string"), is("Okay"));
assertThat(configuration.getString("only.in.defaults"), is("XXX"));
}
示例8: modifyConfigurationTest
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void modifyConfigurationTest() {
CombinedConfiguration allConfig = new CombinedConfiguration(new OverrideCombiner());
HierarchicalConfiguration confA = new HierarchicalConfiguration();
HierarchicalConfiguration confB = new HierarchicalConfiguration();
confA.setProperty("a.a", 1);
confB.setProperty("a.a", 2);
allConfig.addConfiguration(confA, "a");
allConfig.addConfiguration(confB, "b");
allConfig.addConfiguration(confB, "d", "b.b");
allConfig.addConfiguration(confB, "c", "c.c");
System.out.println(allConfig.getInt("a.a"));
System.out.println(allConfig.getInt("b.b.a.a"));
System.out.println(allConfig.getInt("c.c.a.a"));
ConcurrentConfiguration concurrentConfiguration = new ConcurrentConfiguration(allConfig);
System.out.println(concurrentConfiguration.getInt("a.a"));
concurrentConfiguration.withConfiguration(cc -> {
cc.clear();
cc.addConfiguration(confB, "b");
cc.addConfiguration(confA, "a");
});
System.out.println(concurrentConfiguration.getInt("a.a"));
}
示例9: testTree
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
@SuppressWarnings("unchecked")
public void testTree() throws Exception {
// Prepare a configuration object to test with.
HierarchicalConfiguration configuration = new HierarchicalConfiguration();
configuration.setProperty("type.unicodeString", "€");
configuration.setProperty("type.booleanFalse", false);
configuration.setProperty("type.booleanTrue", true);
configuration.setProperty("type.list", Arrays.asList("1", "2", "3"));
configuration.setProperty("type.listInt", Arrays.asList(1, 2, 3));
// Perform the test.
Object tree = Objectifier.traverseTreeAndEmit(configuration.getRootNode());
assertThat(tree, instanceOf(Map.class));
Map<String, Object> treeMap = (Map<String, Object>) tree;
assertThat(treeMap.get("type"), instanceOf(Map.class));
Map<String, Object> typeMap = (Map<String, Object>) treeMap.get("type");
assertThat(typeMap.size(), is(5));
String unicodeString = (String) typeMap.get("unicodeString");
assertThat(unicodeString, is("€"));
boolean booleanFalse = (boolean) typeMap.get("booleanFalse");
boolean booleanTrue = (boolean) typeMap.get("booleanTrue");
assertThat(booleanFalse, is(false));
assertThat(booleanTrue, is(true));
List<String> stringList = (List<String>) typeMap.get("list");
assertThat(stringList, is(Arrays.asList("1", "2", "3")));
List<Integer> intList = (List<Integer>) typeMap.get("listInt");
assertThat(intList, is(Arrays.asList(1, 2, 3)));
}
示例10: configurationMonitorTest
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void configurationMonitorTest() throws Exception {
setData(zookeeper, "test", "\n");
System.setProperty(LIBRARY_PREFIX + ".type", "zookeeper");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.znode", "/config");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.quorum", zookeeperHost);
System.setProperty(LIBRARY_PREFIX + "." + APPNAME_PROPERTY, "test");
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("key", "DEFAULT");
Configuration configuration = ConfigurationManager.configureFromProperties(
defaults, new YamlDeserializer()
);
final AtomicInteger count = new AtomicInteger(0);
Precomputed<String> precomputed = Precomputed.monitorByUpdate(
configuration,
config -> {
count.incrementAndGet();
return config.getString("key");
}
);
assertThat(precomputed.get(), is("DEFAULT"));
assertThat(count.get(), is(1));
assertThat(configuration.getString("key"), is("DEFAULT"));
TimeUnit.MILLISECONDS.sleep(300);
assertThat(precomputed.get(), is("DEFAULT"));
assertThat(count.get(), is(1));
setData(zookeeper, "test", "key: AAA");
TimeUnit.MILLISECONDS.sleep(300);
assertThat(count.get(), is(1));
assertThat(configuration.getString("key"), is("AAA"));
assertThat(precomputed.get(), is("AAA"));
assertThat(count.get(), is(2));
assertThat(precomputed.get(), is("AAA"));
assertThat(count.get(), is(2));
}
示例11: disruptedTest
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void disruptedTest() throws Exception {
setData(zookeeper, "test", "\n");
System.setProperty(LIBRARY_PREFIX + ".type", "zookeeper");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.znode", "/config");
System.setProperty(LIBRARY_PREFIX + ".zookeeper.quorum", zookeeperHost);
System.setProperty(LIBRARY_PREFIX + "." + APPNAME_PROPERTY, "test");
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("key", "DEFAULT");
Configuration configuration = ConfigurationManager.configureFromProperties(
defaults, new YamlDeserializer()
);
final AtomicInteger count = new AtomicInteger(0);
Precomputed<String> precomputed = Precomputed.monitorByUpdate(
configuration,
config -> {
count.incrementAndGet();
return config.getString("key");
}
);
assertThat(precomputed.get(), is("DEFAULT"));
assertThat(count.get(), is(1));
assertThat(configuration.getString("key"), is("DEFAULT"));
TimeUnit.MILLISECONDS.sleep(300);
assertThat(precomputed.get(), is("DEFAULT"));
assertThat(count.get(), is(1));
setData(zookeeper, "test", "key: AAA");
TimeUnit.MILLISECONDS.sleep(300);
assertThat(count.get(), is(1));
assertThat(configuration.getString("key"), is("AAA"));
assertThat(precomputed.get(), is("AAA"));
assertThat(count.get(), is(2));
assertThat(precomputed.get(), is("AAA"));
assertThat(count.get(), is(2));
}
示例12: testMultipleFilesConfig
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void testMultipleFilesConfig() throws ConfigurationException, URISyntaxException {
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("outer.def-a", 111);
defaults.setProperty("outer.def-b", 222);
defaults.setProperty("outer.branch-a.x", "x");
final String INPUT = "multiple-files/root.yaml";
URL testUrl = getClass().getResource("/" + INPUT);
final String testYaml = testUrl.toURI().getPath();
Configuration sourceConfiguration = new BaseConfiguration();
InitializedConfiguration ic = ConfigurationManager.fromTheseSettings(
testYaml,
"app",
new FileBasedConfigSource(),
new YamlDeserializer(),
sourceConfiguration,
defaults
);
Configuration configuration = ic.getConfiguration();
// Overridden default.
assertThat(configuration.getInt("outer.def-a"), is(123));
// Test basic include.
assertThat(configuration.getInt("outer.branch-a.a1"), is(1));
assertThat(configuration.getInt("outer.branch-a.a2"), is(2));
// Included config should override the extended one.
assertThat(configuration.getList("outer.branch-a.lst"), contains(4, 5, 6));
// Include in an include.
assertThat(configuration.getInt("outer.branch-a.sub.s1"), is(1234));
assertThat(configuration.getList("outer.branch-a.sub.s2"), contains("a", "b"));
// Include in an extended config.
assertThat(configuration.getInt("outer.branch-c.sub.s1"), is(1234));
assertThat(configuration.getList("outer.branch-c.sub.s2"), contains("a", "b"));
// Inherited from default.
assertThat(configuration.getInt("outer.def-b"), is(222));
}
示例13: testFileMutations
import org.apache.commons.configuration.HierarchicalConfiguration; //導入方法依賴的package包/類
@Test
public void testFileMutations() throws IOException, ConfigurationException, InterruptedException {
Path dir = Files.createTempDirectory("filemutationsit");
Files.copy(getClass().getResourceAsStream("/multiple-files/root.yaml"), dir.resolve("root.yaml"));
Files.copy(getClass().getResourceAsStream("/multiple-files/base1.yaml"), dir.resolve("base1.yaml"));
Files.copy(getClass().getResourceAsStream("/multiple-files/inc1.yaml"), dir.resolve("inc1.yaml"));
Files.copy(getClass().getResourceAsStream("/multiple-files/inc2.yaml"), dir.resolve("inc2.yaml"));
HierarchicalConfiguration defaults = new HierarchicalConfiguration();
defaults.setProperty("outer.def-a", 111);
defaults.setProperty("outer.def-b", 222);
defaults.setProperty("outer.branch-a.x", "x");
Configuration sourceConfiguration = new BaseConfiguration();
InitializedConfiguration ic = ConfigurationManager.fromTheseSettings(
dir.resolve("root.yaml").toString(),
"app",
new FileBasedConfigSource(),
new YamlDeserializer(),
sourceConfiguration,
defaults
);
Configuration configuration = ic.getConfiguration();
assertThat(configuration.getString("outer.branch-a.z"), is(nullValue()));
TimeUnit.SECONDS.sleep(2);
Files.copy(
getClass().getResourceAsStream("/multiple-files/base1-alt1.yaml"),
dir.resolve("base1.yaml"),
StandardCopyOption.REPLACE_EXISTING
);
TimeUnit.SECONDS.sleep(2);
assertThat(configuration.getString("outer.branch-a.z"), is("z"));
Files.delete(dir.resolve("root.yaml"));
TimeUnit.SECONDS.sleep(2);
assertThat(configuration.getString("outer.branch-a.z"), is("z"));
assertThat(configuration.getInt("outer.def-a"), is(123));
Files.copy(getClass().getResourceAsStream("/multiple-files/root.yaml"), dir.resolve("root.yaml"));
TimeUnit.SECONDS.sleep(2);
assertThat(configuration.getString("outer.branch-a.z"), is("z"));
assertThat(configuration.getInt("outer.def-a"), is(123));
FileUtil.deleteDirAndContents(dir);
}