commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <oliver.he...@oliver-heger.de>
Subject Re: [configuration] JXPathException when setting the default expression engine
Date Fri, 12 Sep 2014 13:46:26 GMT
Hi Nick,

you are right, this is indeed a problem in 1.10. 
DefaultConfigurationBuilder extends XMLConfiguration; therefore, setting 
the default expression engine affects the way keys are resolved.

The preferred way to set the expression engine for the combined 
configuration produced by the builder is to declare the desired engine 
in the definition file that is loaded. There is an example in the user's 
guide [1] in the sub section "The header section"

In the upcoming version 2.0, this problem should no longer exist. Here 
different mechanisms are used for setting a default expression engine 
and for constructing a combined configuration. I hope that a first alpha 
release of Commons Configuration 2.0 will be available soon.

So, as the problem will be solved in the next version, it probably does 
not make sense to open a Jira ticket.

Oliver

[1] 
http://commons.apache.org/proper/commons-configuration/userguide/howto_configurationbuilder.html#Configuration_definition_file_reference

On 11.09.2014 21:37, Nick Watts wrote:
> Hello, I've run into a problem that looks to me like a bug. I thought I'd
> get some input on it first though.
>
> Here are two JUnit test cases that both pass with Commons Config 1.10:
>
> import static org.junit.Assert.*;
>
> import org.apache.commons.configuration.Configuration;
> import org.apache.commons.configuration.ConfigurationBuilder;
> import org.apache.commons.configuration.ConfigurationException;
> import org.apache.commons.configuration.DefaultConfigurationBuilder;
> import org.apache.commons.configuration.HierarchicalConfiguration;
> import org.apache.commons.configuration.XMLConfiguration;
> import org.apache.commons.configuration.tree.DefaultExpressionEngine;
> import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
> import org.apache.commons.jxpath.JXPathException;
> import org.junit.Before;
>
> public class Test {
>
>      @Before
>      public void before() {
>          HierarchicalConfiguration.setDefaultExpressionEngine(new
> DefaultExpressionEngine());
>      }
>
>      @org.junit.Test
>      public void testSetExpressionEngineAfterLoad() throws
> ConfigurationException {
>          ConfigurationBuilder builder = new
> DefaultConfigurationBuilder("configuration_builder.xml");
>          ((XMLConfiguration)builder).setBasePath("src/test/resources/");
>          Configuration config = builder.getConfiguration();
>          HierarchicalConfiguration.setDefaultExpressionEngine(new
> XPathExpressionEngine());
>          assertEquals("mail.test.org", config.getString("mail/host"));
>          assertEquals("root@test.org", config.getString("mail/from"));
>          assertEquals("otheruser@test.org", config.getString("mail/to"));
>          assertEquals("thatguy@test.org", config.getString("mail/cc"));
>          assertEquals("", config.getString("mail/bcc"));
>      }
>
>      @org.junit.Test(expected=JXPathException.class)
>      public void testSetExpressionEngineBeforeLoad() {
>          try {
>              HierarchicalConfiguration.setDefaultExpressionEngine(new
> XPathExpressionEngine());
>              ConfigurationBuilder builder = new
> DefaultConfigurationBuilder("configuration_builder.xml");
>              ((XMLConfiguration)builder).setBasePath("src/test/resources/");
>              builder.getConfiguration();
>          } catch (ConfigurationException e) {
>              e.printStackTrace();
>          }
>      }
> }
>
>
> The configuration_builder.xml file:
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <configuration>
>      <override>
>          <xml fileName="dev.config.xml" config-name="env-config" />
>          <xml fileName="config.xml" config-name="default-config" />
>      </override>
> </configuration>
>
>
> the config.xml file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <config>
>      <mail>
>          <host>mail.test.org</host>
>          <from>root@test.org</from>
>          <to>nwatts@test.org</to>
>          <cc/>
>          <bcc/>
>      </mail>
> </config>
>
> the dev.config.xml file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <config>
>      <mail>
>          <to>otheruser@test.org</to>
>          <cc>thatguy@test.org</cc>
>      </mail>
>   </config>
>
>
>
> The possible bug is that the call to
> HierarchicalConfiguration.setDefaultExpressionEngine(new
> XPathExpressionEngine()) must come after the call to
> builder.getConfiguration(). If it precedes builder.getConfiguration() then
> you get this stack trace:
>
> org.apache.commons.jxpath.JXPathException: Invalid XPath:
> '[@systemProperties]'. Syntax error at the beginning of the expression
>      at org.apache.commons.jxpath.ri.Parser.parseExpression(Parser.java:60)
>      at
> org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.compileExpression(JXPathContextReferenceImpl.java:218)
>      at
> org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.iteratePointers(JXPathContextReferenceImpl.java:529)
>      at
> org.apache.commons.jxpath.JXPathContext.selectNodes(JXPathContext.java:654)
>      at
> org.apache.commons.configuration.tree.xpath.XPathExpressionEngine.query(XPathExpressionEngine.java:183)
>      at
> org.apache.commons.configuration.HierarchicalConfiguration.fetchNodeList(HierarchicalConfiguration.java:958)
>      at
> org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.fetchNodeList(AbstractHierarchicalFileConfiguration.java:439)
>      at
> org.apache.commons.configuration.HierarchicalConfiguration.getProperty(HierarchicalConfiguration.java:344)
>      at
> org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.getProperty(AbstractHierarchicalFileConfiguration.java:392)
>      at
> org.apache.commons.configuration.AbstractConfiguration.resolveContainerStore(AbstractConfiguration.java:1171)
>      at
> org.apache.commons.configuration.AbstractConfiguration.getString(AbstractConfiguration.java:1038)
>      at
> org.apache.commons.configuration.AbstractConfiguration.getString(AbstractConfiguration.java:1021)
>      at
> org.apache.commons.configuration.DefaultConfigurationBuilder.initSystemProperties(DefaultConfigurationBuilder.java:793)
>      at
> org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:612)
>      at
> org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:587)
>      at Test.testSetExpressionEngineBeforeLoad(Test.java:40)
>      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>      at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>      at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>      at java.lang.reflect.Method.invoke(Method.java:597)
>      at
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
>      at
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>      at
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
>      at
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>      at
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
>      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
>      at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
>      at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
>      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
>      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
>      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
>      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
>      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
>      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
>      at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
>      at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>      at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
>      at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
>      at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
>      at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
>
> The XPath expression "[@systemProperties]" is hard-coded in the class
> DefaultConfigurationBuilder as the static field named KEY_SYSTEM_PROPS. It
> appears that this hard-coded value means that DefaultConfigurationBuilder
> is not considering that different expression engines can be used. This
> problem is easily solved by just waiting to set the expression engine until
> after the configuration files have been loaded but I'm not sure what
> happens if you have automatic reloading turned on or do other reloading
> down the line. Should this be logged in JIRA as a defect or am I just
> overlooking some information?
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message