Java源码示例:org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters
示例1
/**
* Tests whether all occurrences of a given defaults handler can be removed.
*/
@Test
public void testUnregisterDefaultsHandlerAll()
{
final FileBasedDefaultsHandler handler = new FileBasedDefaultsHandler();
manager.registerDefaultsHandler(FileBasedBuilderParameters.class,
handler, XMLBuilderParameters.class);
manager.registerDefaultsHandler(FileBasedBuilderParameters.class,
handler, PropertiesBuilderParameters.class);
manager.unregisterDefaultsHandler(handler);
final XMLBuilderParameters paramsXml = parameters.xml();
manager.initializeParameters(paramsXml);
checkNoDefaultValues(paramsXml.getParameters());
final PropertiesBuilderParameters paramsProps = parameters.properties();
manager.initializeParameters(paramsProps);
checkNoDefaultValues(paramsProps.getParameters());
}
示例2
/**
* Tests whether a specific occurrence of a defaults handler can be removed.
*/
@Test
public void testUnregisterDefaultsHandlerSpecific()
{
final FileBasedDefaultsHandler handler = new FileBasedDefaultsHandler();
manager.registerDefaultsHandler(FileBasedBuilderParameters.class,
handler, XMLBuilderParameters.class);
manager.registerDefaultsHandler(FileBasedBuilderParameters.class,
handler, PropertiesBuilderParameters.class);
manager.unregisterDefaultsHandler(handler,
PropertiesBuilderParameters.class);
final XMLBuilderParameters paramsXml = parameters.xml();
manager.initializeParameters(paramsXml);
checkDefaultValues(paramsXml.getParameters());
final PropertiesBuilderParameters paramsProps = parameters.properties();
manager.initializeParameters(paramsProps);
checkNoDefaultValues(paramsProps.getParameters());
}
示例3
/**
* Tests whether HomeDirectoryLocationStrategy can be properly initialized
* and that it shouldn't throw {@code ConfigurationException} when
* everything is correctly in place. Without the code fix for
* <a href="https://issues.apache.org/jira/browse/CONFIGURATION-634">CONFIGURATION-634</a>,
* this test will throw {@code ConfigurationException}
* @throws IOException Shouldn't happen
* @throws ConfigurationException Shouldn't happen
*/
@Test
public void testFileBasedConfigurationBuilderWithHomeDirectoryLocationStrategy()
throws IOException, ConfigurationException
{
final String folderName = "test";
final String fileName = "sample.properties";
folder.newFolder(folderName);
folder.newFile(folderName + File.separatorChar + fileName);
final FileBasedConfigurationBuilder<FileBasedConfiguration> homeDirConfigurationBuilder =
new FileBasedConfigurationBuilder<>(
PropertiesConfiguration.class);
final PropertiesBuilderParameters homeDirProperties =
new Parameters().properties();
final HomeDirectoryLocationStrategy strategy =
new HomeDirectoryLocationStrategy(
folder.getRoot().getAbsolutePath(), true);
final FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
homeDirConfigurationBuilder.configure(homeDirProperties
.setLocationStrategy(strategy).setBasePath(folderName)
.setListDelimiterHandler(
new DefaultListDelimiterHandler(','))
.setFileName(fileName));
builder.getConfiguration();
}
示例4
/**
*
* @param filename1
* @param filename2
* @param encoding
* @param listDelimiter
* @return
* @throws FileNotFoundException
* @author: tanyaowu
*/
public static PropertiesConfiguration initConfig(String filename1, String filename2, String encoding, char listDelimiter) throws FileNotFoundException {
Parameters _parameters = new Parameters();
PropertiesBuilderParameters parameters = _parameters.properties();
String filename = filename1;
ClassLoader cl = ConfigUtils.class.getClassLoader();
URL url = null;
if (StrUtil.isNotBlank(filename1)) {
url = (cl != null ? cl.getResource(filename1) : ClassLoader.getSystemResource(filename1));
}
if (url == null) {
url = (cl != null ? cl.getResource(filename2) : ClassLoader.getSystemResource(filename2));
if (url == null) {
throw new FileNotFoundException(filename1);
}
filename = filename2;
}
parameters.setFileName(filename);
parameters.setThrowExceptionOnMissing(false);
parameters.setEncoding(encoding);
parameters.setListDelimiterHandler(new DefaultListDelimiterHandler(listDelimiter));
parameters.setReloadingDetectorFactory(new DefaultReloadingDetectorFactory());
parameters.setIncludesAllowed(true);
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(parameters);
try {
PropertiesConfiguration config = builder.getConfiguration();
return config;
} catch (ConfigurationException e) {
log.error(e.toString(), e);
return null;
}
}
示例5
/**
* Initialize property configuration based on a configured configuration file name. The method attempts to load it
* from URL if given config is URL, then load it from class path and then from file system.
*
* @throws MalformedURLException
* if malformed configuration file name
*/
protected void initConfig() throws MalformedURLException {
int urlIndex = configName.indexOf("://");
PropertiesBuilderParameters params = new Parameters().properties();
if (urlIndex > 0) {
params.setURL(new URL(configName));
} else {
URL configResource = getClass().getResource("/" + configName);
if (configResource != null) {
params.setURL(configResource);
} else {
params.setFileName(configName);
}
}
if (refDelay > 0) {
params.setReloadingRefreshDelay(refDelay);
ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
builder.configure(params);
cfgReloadTrigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, refDelay, TimeUnit.MILLISECONDS);
config = builder;
} else {
config = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
config.configure(params);
}
}
示例6
/**
* Tests that default values are only applied if the start class provided at
* registration time matches.
*/
@Test
public void testApplyDefaultsStartClass()
{
manager.registerDefaultsHandler(FileBasedBuilderParameters.class,
new FileBasedDefaultsHandler(), XMLBuilderParameters.class);
final XMLBuilderParameters paramsXml = parameters.xml();
manager.initializeParameters(paramsXml);
Map<String, Object> map = paramsXml.getParameters();
checkDefaultValues(map);
final PropertiesBuilderParameters paramsProps = parameters.properties();
manager.initializeParameters(paramsProps);
map = paramsProps.getParameters();
checkNoDefaultValues(map);
}