This is an automated email from the ASF dual-hosted git repository.
klund pushed a commit to branch feature/GEODE-3824
in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/feature/GEODE-3824 by this
push:
new 1147fe2 WIP XPath usage is broken see XPathTest
1147fe2 is described below
commit 1147fe237f5cca8fa37af44dc44c82f09309ef45
Author: Kirk Lund <[email protected]>
AuthorDate: Mon Nov 20 16:24:30 2017 -0800
WIP XPath usage is broken see XPathTest
---
.../jdbc/internal/JdbcConnectorService.java | 2 +
.../jdbc/internal/cli/CreateConnectionCommand.java | 13 +-
.../internal/cli/CreateConnectionFunction.java | 7 +-
.../xml/JdbcConnectorServiceXmlParser.java | 2 +-
.../jdbc/internal/xml/RegionMappingBuilder.java | 6 +
.../geode.apache.org/schema/jdbc/jdbc-1.0.xsd | 85 ++++++++
.../cli/CreateConnectionCommandDUnitTest.java | 80 +++++++
.../CreateConnectionCommandIntegrationTest.java | 73 +++++++
.../internal/cli/CreateConnectionFunctionTest.java | 9 +-
...onnectorServiceXmlGeneratorIntegrationTest.java | 114 +++++++++-
.../JdbcConnectorServiceXmlIntegrationTest.java | 1 +
.../connectors/jdbc/internal/xml/XPathTest.java | 241 +++++++++++++++++++++
.../internal/configuration/domain/XmlEntity.java | 18 +-
13 files changed, 629 insertions(+), 22 deletions(-)
diff --git
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcConnectorService.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcConnectorService.java
index 9af7aeb..5c86140 100644
---
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcConnectorService.java
+++
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcConnectorService.java
@@ -32,10 +32,12 @@ public class JdbcConnectorService implements
InternalJdbcConnectorService {
private volatile InternalCache cache;
private boolean registered;
+ @Override
public ConnectionConfiguration getConnectionConfig(String connectionName) {
return connectionsByName.get(connectionName);
}
+ @Override
public RegionMapping getMappingForRegion(String regionName) {
return mappingsByRegion.get(regionName);
}
diff --git
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommand.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommand.java
index 799d97f..9e196e1 100644
---
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommand.java
+++
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommand.java
@@ -14,10 +14,12 @@
*/
package org.apache.geode.connectors.jdbc.internal.cli;
+import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
+import org.apache.logging.log4j.Logger;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
@@ -26,6 +28,7 @@ import
org.apache.geode.connectors.jdbc.internal.ConnectionConfigBuilder;
import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.internal.logging.LogService;
import org.apache.geode.management.cli.CliMetaData;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.internal.cli.commands.GfshCommand;
@@ -38,6 +41,7 @@ import
org.apache.geode.management.internal.security.ResourceOperation;
import org.apache.geode.security.ResourcePermission;
public class CreateConnectionCommand implements GfshCommand {
+ private static final Logger logger = LogService.getLogger();
static final String CREATE_CONNECTION = "create jdbc-connection";
static final String CREATE_CONNECTION__HELP = "Create JDBC connection for
JDBC Connector.";
@@ -85,8 +89,15 @@ public class CreateConnectionCommand implements GfshCommand {
ResultCollector<?, ?> resultCollector =
executeFunction(new CreateConnectionFunction(), configuration,
membersToCreateConnectionOn);
+ Object resultCollectorResult = resultCollector.getResult();
+ if (resultCollectorResult instanceof ArrayList) {
+// logger.error("createConnection got Throwable result", (Throwable)
resultCollectorResult);
+// ((Throwable) resultCollectorResult).printStackTrace();
+ logger.error("resultCollectorResult=" + resultCollectorResult);
+ }
+
List<CliFunctionResult> regionCreateResults =
- (List<CliFunctionResult>) resultCollector.getResult();
+ (List<CliFunctionResult>) resultCollectorResult;
AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
TabularResultData tabularResultData =
ResultBuilder.createTabularResultData();
diff --git
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunction.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunction.java
index e732a6a..589373e 100644
---
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunction.java
+++
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunction.java
@@ -62,9 +62,10 @@ public class CreateConnectionFunction implements
Function<ConnectionConfiguratio
InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
service.addOrUpdateConnectionConfig(configuration);
- XmlEntity xmlEntity = new XmlEntity(CacheXml.CACHE, null, null,
- JdbcConnectorServiceXmlGenerator.PREFIX,
JdbcConnectorServiceXmlParser.NAMESPACE,
- ElementType.CONNECTION_SERVICE.getTypeName(), null, null);
+ // this line invokes AbstractExecution.executeFunctionLocally which
throws and catches
+ // underlying Throwable which is provided to lastResult
+ XmlEntity xmlEntity = new XmlEntity(CacheXml.CACHE,
JdbcConnectorServiceXmlGenerator.PREFIX,
+ JdbcConnectorServiceXmlParser.NAMESPACE,
ElementType.CONNECTION_SERVICE.getTypeName());
resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity,
"Created JDBC connection " + configuration.getName() + " on " +
memberNameOrId));
diff --git
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlParser.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlParser.java
index 6af37bd..656bc9c 100644
---
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlParser.java
+++
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlParser.java
@@ -20,7 +20,7 @@ import org.xml.sax.SAXException;
import org.apache.geode.internal.cache.xmlcache.AbstractXmlParser;
public class JdbcConnectorServiceXmlParser extends AbstractXmlParser {
- public static final String NAMESPACE =
"http://geode.apache.org/schema/jdbc-connector";
+ public static final String NAMESPACE = "http://geode.apache.org/schema/jdbc";
static final String NAME = "name";
static final String URL = "url";
static final String USER = "user";
diff --git
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/RegionMappingBuilder.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/RegionMappingBuilder.java
index b4adcaf..48c8543 100644
---
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/RegionMappingBuilder.java
+++
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/xml/RegionMappingBuilder.java
@@ -48,11 +48,17 @@ class RegionMappingBuilder {
return this;
}
+ // TODO: delete withPrimaryKeyInValue(String)
RegionMappingBuilder withPrimaryKeyInValue(String primaryKeyInValue) {
this.primaryKeyInValue = Boolean.parseBoolean(primaryKeyInValue);
return this;
}
+ RegionMappingBuilder withPrimaryKeyInValue(boolean primaryKeyInValue) {
+ this.primaryKeyInValue = primaryKeyInValue;
+ return this;
+ }
+
RegionMappingBuilder withFieldToColumnMapping(String fieldName, String
columnMapping) {
this.fieldToColumnMap.put(fieldName, columnMapping);
return this;
diff --git
a/geode-connectors/src/main/resources/META-INF/services/schemas/geode.apache.org/schema/jdbc/jdbc-1.0.xsd
b/geode-connectors/src/main/resources/META-INF/services/schemas/geode.apache.org/schema/jdbc/jdbc-1.0.xsd
new file mode 100644
index 0000000..1c913dd
--- /dev/null
+++
b/geode-connectors/src/main/resources/META-INF/services/schemas/geode.apache.org/schema/jdbc/jdbc-1.0.xsd
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+<xsd:schema
+ targetNamespace="http://geode.apache.org/schema/jdbc"
+ xmlns:gf="http://geode.apache.org/schema/cache"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ elementFormDefault="qualified"
+ attributeFormDefault="unqualified"
+ version="1.0">
+
+ <xsd:import
+ namespace="http://geode.apache.org/schema/cache"
+
schemaLocation="http://geode.apache.org/schema/cache/cache-1.0.xsd"/>
+
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[
+XML schema for JDBC Connector Service in Geode.
+
+ <cache
+ xmlns="http://geode.apache.org/schema/cache"
+ xmlns:lucene="http://geode.apache.org/schema/jdbc"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://geode.apache.org/schema/cache
+ http://geode.apache.org/schema/cache/cache-1.0.xsd
+ http://geode.apache.org/schema/jdbc
+ http://geode.apache.org/schema/jdbc/jdbc-1.0.xsd"
+ version="1.0">
+
+ ]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:element name="connector-service">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="connection" maxOccurs="unbounded"
minOccurs="0">
+ <xsd:complexType>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute type="xsd:string" name="name"
use="optional"/>
+ <xsd:attribute type="xsd:string" name="url"
use="optional"/>
+ <xsd:attribute type="xsd:string" name="user"
use="optional"/>
+ <xsd:attribute type="xsd:string"
name="password" use="optional"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="region-mapping" maxOccurs="unbounded"
minOccurs="0">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="field-mapping"
maxOccurs="unbounded" minOccurs="0">
+ <xsd:complexType>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute type="xsd:string"
name="field-name" use="optional"/>
+ <xsd:attribute type="xsd:string"
name="column-name" use="optional"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:sequence>
+ <xsd:attribute type="xsd:string"
name="connection-name" use="optional"/>
+ <xsd:attribute type="xsd:string" name="region"
use="optional"/>
+ <xsd:attribute type="xsd:string" name="table"
use="optional"/>
+ <xsd:attribute type="xsd:string" name="pdx-class"
use="optional"/>
+ <xsd:attribute type="xsd:string"
name="primary-key-in-value" use="optional"/>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:sequence>
+ </xsd:complexType>
+ </xsd:element>
+</xsd:schema>
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandDUnitTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandDUnitTest.java
new file mode 100644
index 0000000..c5fbd9c
--- /dev/null
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandDUnitTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import static
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.*;
+import static
org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
+import static org.assertj.core.api.Assertions.*;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Properties;
+
+import org.apache.geode.distributed.internal.InternalLocator;
+import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
+import org.apache.geode.test.dunit.rules.LocatorServerStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.categories.DistributedTest;
+import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.rules.GfshCommandRule;
+import org.apache.geode.test.junit.rules.serializable.SerializableTestName;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(DistributedTest.class)
+public class CreateConnectionCommandDUnitTest {
+
+ @Rule
+ public transient GfshCommandRule gfsh = new GfshCommandRule();
+
+ @Rule
+ public LocatorServerStartupRule startupRule = new LocatorServerStartupRule();
+
+ @Rule
+ public SerializableTestName testName = new SerializableTestName();
+
+ private MemberVM locator;
+ private MemberVM server;
+
+ @Before
+ public void before() throws Exception {
+ locator = startupRule.startLocatorVM(0);
+ server = startupRule.startServerVM(1, locator.getPort());
+
+ gfsh.connectAndVerify(locator);
+ }
+
+ public void connect(MemberVM serverVM) throws Exception {
+ gfsh.connectAndVerify(serverVM.getJmxPort(),
GfshCommandRule.PortType.jmxManager);
+ }
+
+ @Test
+ public void createsConnection() throws Exception {
+ CommandStringBuilder csb = new CommandStringBuilder(CREATE_CONNECTION);
+ csb.addOption(CREATE_CONNECTION__NAME, "name");
+ csb.addOption(CREATE_CONNECTION__URL, "url");
+ csb.addOption(CREATE_CONNECTION__USER, "username");
+ csb.addOption(CREATE_CONNECTION__PASSWORD, "secret");
+ csb.addOption(CREATE_CONNECTION__PARAMS, "param1,param2");
+
+ gfsh.executeAndAssertThat(csb.toString())
+ .statusIsSuccess();
+ }
+}
\ No newline at end of file
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandIntegrationTest.java
new file mode 100644
index 0000000..c3c2818
--- /dev/null
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionCommandIntegrationTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import static
org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
+import org.apache.geode.connectors.jdbc.internal.InternalJdbcConnectorService;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.test.junit.categories.IntegrationTest;
+
+@Category(IntegrationTest.class)
+public class CreateConnectionCommandIntegrationTest {
+
+ private InternalCache cache;
+ private CreateConnectionCommand createConnectionCommand;
+
+ private String name;
+ private String url;
+ private String user;
+ private String password;
+ private String[] params;
+
+ @Before
+ public void setUp() throws Exception {
+ cache = (InternalCache) new
CacheFactory().set(ENABLE_CLUSTER_CONFIGURATION, "true").create();
+ createConnectionCommand = new CreateConnectionCommand();
+
+ name = "name";
+ url = "url";
+ user = "user";
+ password = "password";
+ params = new String[] { "params1", "params2" };
+ }
+
+ @Test
+ public void createsConnectionConfigurationInService() throws Exception {
+ Result result = createConnectionCommand.createConnection(name, url, user,
password, params);
+
+ assertThat(result.getStatus()).isSameAs(Result.Status.OK);
+
+ InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
+ ConnectionConfiguration connectionConfig =
service.getConnectionConfig(name);
+
+ assertThat(connectionConfig).isNotNull();
+ assertThat(connectionConfig.getName()).isEqualTo(name);
+ assertThat(connectionConfig.getUrl()).isEqualTo(url);
+ assertThat(connectionConfig.getUser()).isEqualTo(user);
+ assertThat(connectionConfig.getPassword()).isEqualTo(password);
+ //assertThat(connectionConfig.getParams()).contains(params);
+ }
+}
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunctionTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunctionTest.java
index 2623c8f..6a4f823 100644
---
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunctionTest.java
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateConnectionFunctionTest.java
@@ -14,9 +14,12 @@
*/
package org.apache.geode.connectors.jdbc.internal.cli;
-import static org.assertj.core.api.Assertions.*;
-import static org.mockito.ArgumentMatchers.*;
-import static org.mockito.Mockito.*;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlGeneratorIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlGeneratorIntegrationTest.java
index b5fc108..ba22544 100644
---
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlGeneratorIntegrationTest.java
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlGeneratorIntegrationTest.java
@@ -27,6 +27,7 @@ import static
org.apache.geode.connectors.jdbc.internal.xml.JdbcConnectorService
import static
org.apache.geode.connectors.jdbc.internal.xml.JdbcConnectorServiceXmlParser.TABLE;
import static
org.apache.geode.connectors.jdbc.internal.xml.JdbcConnectorServiceXmlParser.URL;
import static
org.apache.geode.connectors.jdbc.internal.xml.JdbcConnectorServiceXmlParser.USER;
+import static
org.apache.geode.distributed.ConfigurationProperties.CACHE_XML_FILE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
@@ -34,10 +35,14 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.file.Files;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
import org.junit.After;
import org.junit.Before;
@@ -47,6 +52,7 @@ import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@@ -58,7 +64,8 @@ import
org.apache.geode.connectors.jdbc.internal.RegionMapping;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.cache.xmlcache.CacheXml;
import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator;
-import org.apache.geode.management.internal.configuration.domain.XmlEntity;
+import org.apache.geode.internal.cache.xmlcache.CacheXmlParser;
+import org.apache.geode.management.internal.configuration.utils.XmlUtils;
import org.apache.geode.test.junit.categories.IntegrationTest;
@Category(IntegrationTest.class)
@@ -153,25 +160,98 @@ public class
JdbcConnectorServiceXmlGeneratorIntegrationTest {
}
@Test
- public void test() throws Exception {
+ public void generatedXmlWithConnectionConfigurationCanBeParsed() throws
Exception {
InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
ConnectionConfiguration config = new
ConnectionConfigBuilder().withName("name").withUrl("url")
.withUser("username").withPassword("secret").build();
service.addOrUpdateConnectionConfig(config);
+ generateXml();
+
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ cache.close();
+ cache = (InternalCache) new CacheFactory().set(CACHE_XML_FILE,
cacheXml.getAbsolutePath()).create();
+
+ service = cache.getService(InternalJdbcConnectorService.class);
+
+ assertThat(service.getConnectionConfig("name")).isEqualTo(config);
+ }
+ @Test
+ public void generatedXmlWithConnectionConfigurationCanBeXPathed() throws
Exception {
+ InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
+ ConnectionConfiguration config = new
ConnectionConfigBuilder().withName("name").withUrl("url")
+ .withUser("username").withPassword("secret").build();
+ service.addOrUpdateConnectionConfig(config);
generateXml();
- Document document = getCacheXmlDocument();
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ for(String line : Files.readAllLines(cacheXml.toPath())) {
+ System.out.println(line);
+ }
+
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new CacheXmlParser());
+
+ Document document = builder.parse(cacheXml);
+ System.out.println(document.getDocumentElement());
+ XmlUtils.XPathContext xpathContext = new XmlUtils.XPathContext();
+ xpathContext.addNamespace(CacheXml.PREFIX, CacheXml.GEODE_NAMESPACE);
+ xpathContext.addNamespace(PREFIX, NAMESPACE); // TODO: wrap this line with
conditional
+ // Create an XPathContext here
+ XPath xpath = XPathFactory.newInstance().newXPath();
+ xpath.setNamespaceContext(xpathContext);
+ Object result = xpath.evaluate("//cache/jdbc:connector-service", document,
XPathConstants.NODE);
+ //Node element = XmlUtils.querySingleElement(document,
"//cache/jdbc:connector-service", xpathContext);
+ // Must copy to preserve namespaces.
+ System.out.println("RESULT = " + XmlUtils.elementToString((Element)
result));
+ }
- System.out.println("Document: " + document);
- XmlEntity xmlEntity = new XmlEntity(CacheXml.CACHE, null, null,
- JdbcConnectorServiceXmlGenerator.PREFIX,
JdbcConnectorServiceXmlParser.NAMESPACE,
- ElementType.CONNECTION_SERVICE.getTypeName(), null, null);
+ @Test
+ public void generatedXmlWithRegionMappingCanBeParsed() throws Exception {
+ InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
+ RegionMapping mapping = new
RegionMappingBuilder().withRegionName("region").withPdxClassName("class")
+
.withTableName("table").withConnectionConfigName("connection").withPrimaryKeyInValue(true)
+ .withFieldToColumnMapping("field1", "columnMapping1")
+ .withFieldToColumnMapping("field2", "columnMapping2")
+ .build();
+ service.addOrUpdateRegionMapping(mapping);
+ generateXml();
+
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ cache.close();
+ cache = (InternalCache) new CacheFactory().set(CACHE_XML_FILE,
cacheXml.getAbsolutePath()).create();
- String xml = xmlEntity.loadXmlDefinition(document);
+ service = cache.getService(InternalJdbcConnectorService.class);
- System.out.println("Xml: " + xml);
+ assertThat(service.getMappingForRegion("region")).isEqualTo(mapping);
+ }
+
+ @Test
+ public void generatedXmlWithEverythingCanBeParsed() throws Exception {
+ InternalJdbcConnectorService service =
cache.getService(InternalJdbcConnectorService.class);
+ ConnectionConfiguration config = new
ConnectionConfigBuilder().withName("name").withUrl("url")
+ .withUser("username").withPassword("secret").build();
+ service.addOrUpdateConnectionConfig(config);
+ RegionMapping mapping = new
RegionMappingBuilder().withRegionName("region").withPdxClassName("class")
+
.withTableName("table").withConnectionConfigName("connection").withPrimaryKeyInValue(true)
+ .withFieldToColumnMapping("field1", "columnMapping1")
+ .withFieldToColumnMapping("field2", "columnMapping2")
+ .build();
+ service.addOrUpdateRegionMapping(mapping);
+ generateXml();
+
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ cache.close();
+ cache = (InternalCache) new CacheFactory().set(CACHE_XML_FILE,
cacheXml.getAbsolutePath()).create();
+
+ service = cache.getService(InternalJdbcConnectorService.class);
+
+ assertThat(service.getConnectionConfig("name")).isEqualTo(config);
+ assertThat(service.getMappingForRegion("region")).isEqualTo(mapping);
}
private void validatePresenceOfFieldMapping(NodeList elements, String
fieldName,
@@ -204,12 +284,22 @@ public class
JdbcConnectorServiceXmlGeneratorIntegrationTest {
throws IOException, SAXException, ParserConfigurationException {
File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ dbFactory.setNamespaceAware(false);
+ dbFactory.setValidating(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(cacheXml);
document.getDocumentElement().normalize();
return document;
}
+ private void printXml()
+ throws IOException, SAXException, ParserConfigurationException {
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ for (String line : Files.readAllLines(cacheXml.toPath())) {
+ System.out.println(line);
+ }
+ }
+
private void generateXml() throws IOException {
File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
PrintWriter printWriter = new PrintWriter(new FileWriter(cacheXml));
@@ -217,4 +307,10 @@ public class
JdbcConnectorServiceXmlGeneratorIntegrationTest {
printWriter.flush();
}
+ private void writeXml(File cacheXml, String xml) throws IOException {
+ PrintWriter printWriter = new PrintWriter(new FileWriter(cacheXml));
+ printWriter.print(xml);
+ printWriter.flush();
+ }
+
}
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlIntegrationTest.java
index 1582063..4dee67b 100644
---
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlIntegrationTest.java
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/JdbcConnectorServiceXmlIntegrationTest.java
@@ -21,6 +21,7 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.file.Files;
import org.junit.After;
import org.junit.Before;
diff --git
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/XPathTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/XPathTest.java
new file mode 100644
index 0000000..1b08a4e
--- /dev/null
+++
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/xml/XPathTest.java
@@ -0,0 +1,241 @@
+/*
+ * 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.geode.connectors.jdbc.internal.xml;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.Files;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathFactory;
+
+import com.sun.org.apache.xpath.internal.NodeSet;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator;
+import org.apache.geode.internal.cache.xmlcache.CacheXmlParser;
+import org.apache.geode.management.internal.configuration.utils.XmlUtils;
+
+public class XPathTest {
+
+ private InternalCache cache;
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+// @Before
+// public void setup() {
+// cache = (InternalCache) new CacheFactory().create();
+// }
+//
+// @After
+// public void tearDown() {
+// cache.close();
+// }
+
+ @Test
+ public void test() throws Exception {
+ StringBuilder xmlBuilder = new StringBuilder();
+// xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+ xmlBuilder.append("<cache
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://geode.apache.org/schema/cache\"
xsi:schemaLocation=\"http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd\" version=\"1.0\">");
+ xmlBuilder.append("<jdbc:connector-service
xmlns:jdbc=\"http://geode.apache.org/schema/jdbc\">");
+
+// xmlBuilder.append("<cache
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://geode.apache.org/schema/cache\"
xmlns:jdbc=\"http://geode.apache.org/schema/jdbc\"
xsi:schemaLocation=\"http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd\" version=\"1.0\">");
+// xmlBuilder.append("<jdbc:connector-service>");
+
+ xmlBuilder.append("<jdbc:connection name=\"name\" url=\"url\"
user=\"username\" password=\"secret\"/>");
+ xmlBuilder.append("</jdbc:connector-service>");
+ xmlBuilder.append("</cache>");
+
+ System.out.println("xmlBuilder: " + xmlBuilder.toString());
+
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ writeXml(cacheXml, xmlBuilder.toString());
+
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new CacheXmlParser());
+
+ Document document = builder.parse(cacheXml);
+ System.out.println("document: " + document);
+ System.out.println("document.getDocumentElement().getTagName(): " +
document.getDocumentElement().getTagName());
+ //System.out.println("document.getDocumentElement().getTagName(): " +
document.getDocumentElement().getChildNodes());
+ NodeList nodeList = document.getDocumentElement().getChildNodes();
+ print(nodeList);
+
+// XmlUtils.XPathContext xpathContext = new XmlUtils.XPathContext();
+// xpathContext.addNamespace(CacheXml.PREFIX, CacheXml.GEODE_NAMESPACE);
+// xpathContext.addNamespace(PREFIX, NAMESPACE); // TODO: wrap this line
with conditional
+
+ // Create an XPathContext here
+ String expression = "cache";
+ XPath xpath = XPathFactory.newInstance().newXPath();
+// xpath.setNamespaceContext(xpathContext);
+ Object result = xpath.evaluate(expression, document, XPathConstants.NODE);
+
+ //Node element = XmlUtils.querySingleElement(document,
"//cache/jdbc:connector-service", xpathContext);
+ // Must copy to preserve namespaces.
+ if (result == null) {
+ System.out.println("RESULT = " + result);
+ } else {
+ System.out.println("RESULT = " + XmlUtils.elementToString((Element)
result));
+ }
+ }
+
+ @Test
+ public void test2() throws Exception {
+ StringBuilder xmlBuilder = new StringBuilder();
+ xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+// xmlBuilder.append("<catalog
xmlns:journal=\"http://www.w3.org/2001/XMLSchema-Instance\" >");
+// xmlBuilder.append("<journal:journal title=\"XML\" publisher=\"IBM
developerWorks\">");
+ xmlBuilder.append("<catalog>");
+ xmlBuilder.append("<journal:journal
xmlns:journal=\"http://www.w3.org/2001/XMLSchema-Instance\" title=\"XML\"
publisher=\"IBM developerWorks\">");
+ xmlBuilder.append("<article journal:level=\"Intermediate\"
date=\"February-2003\">");
+ xmlBuilder.append("<title>Design XML Schemas Using UML</title>");
+ xmlBuilder.append("<author>Ayesha Malik</author>");
+ xmlBuilder.append("</article>");
+ xmlBuilder.append("</journal:journal>");
+ xmlBuilder.append("<journal title=\"Java Technology\" publisher=\"IBM
developerWorks\">");
+ xmlBuilder.append("<article level=\"Advanced\" date=\"January-2004\">");
+ xmlBuilder.append("<title>Design service-oriented architecture frameworks
with J2EE technology</title>");
+ xmlBuilder.append("<author>Naveen Balani </author>");
+ xmlBuilder.append("</article>");
+ xmlBuilder.append("<article level=\"Advanced\" date=\"October-2003\">");
+ xmlBuilder.append("<title>Advance DAO Programming</title>");
+ xmlBuilder.append("<author>Sean Sullivan </author>");
+ xmlBuilder.append("</article>");
+ xmlBuilder.append("</journal>");
+ xmlBuilder.append("</catalog>");
+
+ System.out.println("xmlBuilder: " + xmlBuilder.toString());
+
+ File fileXml = new File(temporaryFolder.getRoot(), "file.xml");
+ writeXml(fileXml, xmlBuilder.toString());
+
+ XPathFactory factory=XPathFactory.newInstance();
+
+ XPath xPath=factory.newXPath();
+
+ XPathExpression xPathExpression=
xPath.compile("/catalog/journal/article[@date='January-2004']/title");
+
+ InputSource inputSource = new InputSource(new FileInputStream(fileXml));
+
+// String title = xPathExpression.evaluate(inputSource);
+//
+// String publisher = xPath.evaluate("/catalog/journal/@publisher",
inputSource);
+
+ //String expression="/catalog/journal/article";
+
+ String expression="//catalog/journal";
+
+ NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource,
XPathConstants.NODESET);
+
+ NodeList nodeList=(NodeList)nodes;
+ System.out.println(nodeList.getLength());
+ }
+
+ @Test
+ public void test3() throws Exception {
+ StringBuilder xmlBuilder = new StringBuilder();
+ xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+ xmlBuilder.append("<cache>");//
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://geode.apache.org/schema/cache\"
xsi:schemaLocation=\"http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd\" version=\"1.0\">");
+ xmlBuilder.append("<jdbc:connector-service
xmlns:jdbc=\"http://geode.apache.org/schema/jdbc\" name=\"blah\">");
+
+// xmlBuilder.append("<cache
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://geode.apache.org/schema/cache\"
xmlns:jdbc=\"http://geode.apache.org/schema/jdbc\"
xsi:schemaLocation=\"http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd\" version=\"1.0\">");
+// xmlBuilder.append("<jdbc:connector-service>");
+
+ xmlBuilder.append("<jdbc:connection name=\"name\" url=\"url\"
user=\"username\" password=\"secret\"/>");
+ xmlBuilder.append("</jdbc:connector-service>");
+ xmlBuilder.append("</cache>");
+
+ System.out.println("xmlBuilder: " + xmlBuilder.toString());
+
+ File fileXml = new File(temporaryFolder.getRoot(), "file.xml");
+ writeXml(fileXml, xmlBuilder.toString());
+
+ XPathFactory factory=XPathFactory.newInstance();
+
+ XPath xPath=factory.newXPath();
+
+ InputSource inputSource = new InputSource(new FileInputStream(fileXml));
+
+// String title = xPathExpression.evaluate(inputSource);
+//
+// String publisher = xPath.evaluate("/catalog/journal/@publisher",
inputSource);
+
+ //String expression="/catalog/journal/article";
+
+ String expression="//cache/jdbc:connector-service/jdbc:connection";
+
+ NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource,
XPathConstants.NODESET);
+
+ NodeList nodeList=(NodeList)nodes;
+ System.out.println(nodeList.getLength());
+ }
+
+ private void print(NodeList nodeList) {
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ System.out.println("node[" + i + "]:" + nodeList.item(i).getNodeName());
+ if (nodeList.item(i).getChildNodes().getLength() > 0) {
+ print(nodeList.item(i).getChildNodes());
+ }
+ }
+ }
+
+ private void printXml()
+ throws IOException, SAXException, ParserConfigurationException {
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ for (String line : Files.readAllLines(cacheXml.toPath())) {
+ System.out.println(line);
+ }
+ }
+
+ private void writeXml(File cacheXml, String xml) throws IOException {
+ PrintWriter printWriter = new PrintWriter(new FileWriter(cacheXml));
+ printWriter.print(xml);
+ printWriter.flush();
+ }
+
+ private void generateXml() throws IOException {
+ File cacheXml = new File(temporaryFolder.getRoot(), "cache.xml");
+ PrintWriter printWriter = new PrintWriter(new FileWriter(cacheXml));
+ CacheXmlGenerator.generate(cache, printWriter, true, false, false);
+ printWriter.flush();
+ }
+
+}
diff --git
a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/XmlEntity.java
b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/XmlEntity.java
index 38ac3ed..b041a40 100644
---
a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/XmlEntity.java
+++
b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/domain/XmlEntity.java
@@ -148,11 +148,18 @@ public class XmlEntity implements
VersionedDataSerializable {
initializeSearchString(parentKey, parentValue, childPrefix, childKey,
childValue);
}
- // public XmlEntity(final String parentType, final String parentKey, final
String parentValue,
- // final String childPrefix, final String childNamespace, final String
childType,
- // final String childKey, final String childValue) {
- //
- // }
+ public XmlEntity(final String parentType, final String childPrefix, final
String childNamespace, final String childType) {
+ this.parentType = parentType;
+ this.type = childType;
+ this.childPrefix = childPrefix;
+ this.childNamespace = childNamespace;
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("//").append(this.parentType);
+ sb.append("/").append(childPrefix).append(':').append(this.type);
+ this.searchString = sb.toString();
+ this.xmlDefinition = loadXmlDefinition(); //TODO: delete this line
+ }
private void initializeSearchString(final String parentKey, final String
parentValue,
final String childPrefix, final String childKey, final String
childValue) {
@@ -242,6 +249,7 @@ public class XmlEntity implements VersionedDataSerializable
{
if (document != null) {
XPathContext xpathContext = new XPathContext();
xpathContext.addNamespace(prefix, namespace);
+ xpathContext.addNamespace(childPrefix, childNamespace); // TODO: wrap
this line with conditional
// Create an XPathContext here
Node element = XmlUtils.querySingleElement(document, this.searchString,
xpathContext);
// Must copy to preserve namespaces.
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].