Author: oheger
Date: Fri Feb 7 20:37:00 2014
New Revision: 1565799
URL: http://svn.apache.org/r1565799
Log:
Added a factory class for creating JXPathContext objects.
This class simplifies testing of the XPath expression engine.
Added:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/XPathContextFactory.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestXPathContextFactory.java
Added:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/XPathContextFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/XPathContextFactory.java?rev=1565799&view=auto
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/XPathContextFactory.java
(added)
+++
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/XPathContextFactory.java
Fri Feb 7 20:37:00 2014
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.tree.xpath;
+
+import org.apache.commons.configuration.tree.NodeHandler;
+import org.apache.commons.jxpath.JXPathContext;
+
+/**
+ * <p>
+ * An internally used helper class for creating new XPath context objects.
+ * </p>
+ * <p>
+ * This class is used by {@link XPathExpressionEngine}. It simplifies testing.
+ * </p>
+ *
+ * @version $Id$
+ */
+class XPathContextFactory
+{
+ /**
+ * Creates a new {@code JXPathContext} based on the passed in arguments.
+ *
+ * @param root the root node
+ * @param handler the node handler
+ * @param <T> the type of the nodes to be handled
+ * @return the newly created context
+ */
+ public <T> JXPathContext createContext(T root, NodeHandler<T> handler)
+ {
+ JXPathContext context =
+ JXPathContext.newContext(ConfigurationNodePointerFactory
+ .wrapNode(root, handler));
+ context.setLenient(true);
+ return context;
+ }
+}
Added:
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestXPathContextFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestXPathContextFactory.java?rev=1565799&view=auto
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestXPathContextFactory.java
(added)
+++
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestXPathContextFactory.java
Fri Feb 7 20:37:00 2014
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.tree.xpath;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.InMemoryNodeModel;
+import org.apache.commons.configuration.tree.NodeHandler;
+import org.apache.commons.jxpath.JXPathContext;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code XPathContextFactory}.
+ *
+ * @version $Id$
+ */
+public class TestXPathContextFactory
+{
+ /** The factory to be tested. */
+ private XPathContextFactory factory;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ factory = new XPathContextFactory();
+ }
+
+ /**
+ * Tests whether a correct context is created.
+ */
+ @Test
+ public void testCreateContext()
+ {
+ ImmutableNode node =
+ new ImmutableNode.Builder().name("testRoot").create();
+ NodeHandler<ImmutableNode> handler = new InMemoryNodeModel(node);
+ JXPathContext context = factory.createContext(node, handler);
+
+ assertTrue("No lenient mode", context.isLenient());
+ ConfigurationNodePointerFactory.NodeWrapper<?> wrapper =
+ (ConfigurationNodePointerFactory.NodeWrapper<?>) context
+ .getContextBean();
+ assertSame("Wrong node", node, wrapper.getNode());
+ assertSame("Wrong handler", handler, wrapper.getNodeHandler());
+ }
+}