commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Watts <nick.a.wa...@gmail.com>
Subject [configuration] JXPathException when setting the default expression engine
Date Thu, 11 Sep 2014 19:37:58 GMT
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?


-- 
Nick Watts
blog: thewonggei.wordpress.com
twitter: @thewonggei

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message