我一直在尝试配置一个Java应用程序,特别是我的集成测试,但没有取得任何成功。最初,我有一个单独的XMLConfiguration可以无问题地加载和使用。我的目录设置如下:
src --test ----java ------mypackage ----resources
以前,我在资源目录中有一个myconfig.xml文件。我是这样加载它的:
当我运行集成测试时,我看不到Yay,也看不到Herp和Derp。如果在尝试加载内容之前放置
那些初始化方法只是调用了
src --test ----java ------mypackage ----resources
以前,我在资源目录中有一个myconfig.xml文件。我是这样加载它的:
private static XMLConfiguration configuration = null;
public static void configure() {
try {
configuration = new XMLConfiguration("myconfig.xml");
}
catch (ConfigurationException e) {
System.err.println(e);
}
}
现在我正在尝试让用户通过编写配置文件(位于/etc或/home等位置)来自行配置应用程序,因此我在同一位置创建了一个名为config-test.xml的新文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<!-- Attempt to load configuration provided on the system -->
<xml fileName="/etc/myconf/myconfig.xml" config-optional="true" />
<!-- Load default configuration from the classpath -->
<xml fileName="myconfig.xml" />
</configuration>
/etc/myconf/myconfig.xml文件不存在,但这正是使它可选的全部意义所在。我尝试了几种不同的方法来加载我的配置,包括文档中的方法:http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html。目前我使用的是以下方式:
private static CombinedConfiguration configuration = null;
public static void configure() {
try {
DefaultConfigurationBuilder builder =
new DefaultConfigurationBuilder("config-test.xml");
configuration = builder.getConfiguration(true);
System.err.println("Yay");
}
catch (ConfigurationException e) {
System.err.println("Herp");
}
catch (Exception e) {
System.err.println("Derp");
}
}
当我运行集成测试时,我看不到Yay,也看不到Herp和Derp。如果在尝试加载内容之前放置
println
,它确实会打印,所以configure
被调用了。我从commons配置中得到的输出如下:-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
13:36:57.752 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.753 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.756 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.766 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
13:36:57.795 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.795 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.796 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.800 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16, Time elapsed: 2.589 sec <<< FAILURE!
Results :
Failed tests: initialize(mypackage.ImportTestIT): org/apache/commons/beanutils/PropertyUtils
initialize(mypackage.TransferTestIT): org/apache/commons/beanutils/PropertyUtils
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16
那些初始化方法只是调用了
configure
,然后尝试从加载的配置中获取数据。我已经花费了几个小时在这个问题上,现在真的需要另一双眼睛来帮忙。有什么想法吗?是否有任何我没有提到但会影响此问题的更改?谢谢。
编辑#1(下午1:58): 确保beanutils在类路径上后,我现在得到了一个从堆栈跟踪开始的brutal explosion。Running TestSuite
14:00:16.088 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
14:00:16.089 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
14:00:16.093 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
14:00:16.103 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
14:00:16.178 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml, name is /etc/myconf/myproject/myconfig.xml
14:00:16.179 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file /etc/myconf/myproject/myconfig.xml at file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml: /etc/myconf/myproject/myconfig.xml (No such file or directory)
14:00:16.181 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Load failed for optional configuration xml: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
14:00:16.185 [main] WARN o.a.c.c.DefaultConfigurationBuilder - Internal error
org.apache.commons.configuration.ConfigurationException: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:259) ~[commons-configuration-1.8.jar:1.8]
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:238) ~[commons-configuration-1.8.jar:1.8]
等等等等...但是我现在看到了“耶”。现在我不理解的是,它似乎没有查找我的非可选配置源,或者假设它在我从未告诉它查找的位置。
编辑#2: 我仔细查看了输出,并在commons的投诉中发现它最终似乎加载了正确的文件。我想我可能已经解决了这个问题?也许当我像这样加载它时,我的属性路径会改变...我有一些要尝试的东西。无论如何,我所问的问题已经得到解决。感谢您的帮助。