geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [geode] branch develop updated: GEODE-6457: Use ClassPathLoader for create jdbc-mapping (#3243)
Date Wed, 27 Feb 2019 17:12:25 GMT
This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new aaa0a1d  GEODE-6457: Use ClassPathLoader for create jdbc-mapping (#3243)
aaa0a1d is described below

commit aaa0a1d17245312b7ee5937fbb8d7129f0282209
Author: Jianxia Chen <jchen@pivotal.io>
AuthorDate: Wed Feb 27 09:12:00 2019 -0800

    GEODE-6457: Use ClassPathLoader for create jdbc-mapping (#3243)
    
    Co-authored-by: Darrel Schneider <dschneider@pivotal.io>
    Co-authored-by: Jianxia Chen <jchen@pivotal.io>
---
 .../cli/CreateMappingCommandDUnitTest.java         | 116 +++++++++++++++++++++
 .../org/apache/geode/internal/ResourcePDX.java     |  62 +++++++++++
 .../CreateMappingPreconditionCheckFunction.java    |   5 +-
 3 files changed, 181 insertions(+), 2 deletions(-)

diff --git a/geode-connectors/src/distributedTest/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingCommandDUnitTest.java
b/geode-connectors/src/distributedTest/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingCommandDUnitTest.java
index 295f5d4..1e3e782 100644
--- a/geode-connectors/src/distributedTest/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingCommandDUnitTest.java
+++ b/geode-connectors/src/distributedTest/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingCommandDUnitTest.java
@@ -26,6 +26,9 @@ import static org.apache.geode.connectors.util.internal.MappingConstants.SYNCHRO
 import static org.apache.geode.connectors.util.internal.MappingConstants.TABLE_NAME;
 import static org.assertj.core.api.Assertions.assertThat;
 
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
 import java.sql.Connection;
 import java.sql.JDBCType;
 import java.sql.SQLException;
@@ -56,16 +59,21 @@ import org.apache.geode.connectors.util.internal.MappingCommandUtils;
 import org.apache.geode.distributed.internal.InternalLocator;
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.internal.jndi.JNDIInvoker;
+import org.apache.geode.management.internal.cli.i18n.CliStrings;
 import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
 import org.apache.geode.pdx.FieldType;
 import org.apache.geode.pdx.PdxReader;
 import org.apache.geode.pdx.PdxSerializable;
 import org.apache.geode.pdx.PdxWriter;
+import org.apache.geode.test.compiler.JarBuilder;
+import org.apache.geode.test.dunit.IgnoredException;
 import org.apache.geode.test.dunit.rules.ClusterStartupRule;
 import org.apache.geode.test.dunit.rules.MemberVM;
 import org.apache.geode.test.junit.categories.JDBCConnectorTest;
 import org.apache.geode.test.junit.rules.GfshCommandRule;
+import org.apache.geode.test.junit.rules.serializable.SerializableTemporaryFolder;
 import org.apache.geode.test.junit.rules.serializable.SerializableTestName;
+import org.apache.geode.util.test.TestUtil;
 
 @Category({JDBCConnectorTest.class})
 public class CreateMappingCommandDUnitTest {
@@ -89,6 +97,9 @@ public class CreateMappingCommandDUnitTest {
   @Rule
   public SerializableTestName testName = new SerializableTestName();
 
+  @Rule
+  public SerializableTemporaryFolder temporaryFolder = new SerializableTemporaryFolder();
+
   private MemberVM locator;
   private MemberVM server1;
   private MemberVM server2;
@@ -488,6 +499,111 @@ public class CreateMappingCommandDUnitTest {
     }
   }
 
+  private static void assertValidResourcePDXMapping(RegionMapping mapping, String tableName)
{
+    assertThat(mapping.getDataSourceName()).isEqualTo("connection");
+    assertThat(mapping.getTableName()).isEqualTo(tableName);
+    assertThat(mapping.getPdxName()).isEqualTo("org.apache.geode.internal.ResourcePDX");
+    assertThat(mapping.getIds()).isEqualTo("id");
+    assertThat(mapping.getCatalog()).isNull();
+    assertThat(mapping.getSchema()).isEqualTo("mySchema");
+    List<FieldMapping> fieldMappings = mapping.getFieldMappings();
+    assertThat(fieldMappings).hasSize(3);
+    assertThat(fieldMappings.get(0))
+        .isEqualTo(new FieldMapping("id", "STRING", "ID", "VARCHAR", false));
+    assertThat(fieldMappings.get(1))
+        .isEqualTo(new FieldMapping("name", "STRING", "NAME", "VARCHAR", true));
+    assertThat(fieldMappings.get(2))
+        .isEqualTo(new FieldMapping("age", "INT", "AGE", "INTEGER", true));
+  }
+
+  private static void assertValidResourcePDXMappingOnServer(RegionMapping mapping,
+      String regionName,
+      boolean synchronous, boolean isParallel, String tableName) {
+    assertValidResourcePDXMapping(mapping, tableName);
+    validateRegionAlteredOnServer(regionName, synchronous);
+    if (!synchronous) {
+      validateAsyncEventQueueCreatedOnServer(regionName, isParallel);
+    }
+  }
+
+  private static void assertValidResourcePDXMappingOnLocator(RegionMapping mapping,
+      String regionName,
+      String groups,
+      boolean synchronous, boolean isParallel, String tableName) {
+    assertValidResourcePDXMapping(mapping, tableName);
+    validateRegionAlteredInClusterConfig(regionName, groups, synchronous);
+    if (!synchronous) {
+      validateAsyncEventQueueCreatedInClusterConfig(regionName, groups, isParallel);
+    }
+  }
+
+  private File loadTestResource(String fileName) throws URISyntaxException {
+    String filePath = TestUtil.getResourcePath(this.getClass(), fileName);
+    assertThat(filePath).isNotNull();
+
+    return new File(filePath);
+  }
+
+  private void deployJar() throws URISyntaxException, IOException {
+    JarBuilder jarBuilder = new JarBuilder();
+    File source = loadTestResource(
+        "/org/apache/geode/internal/ResourcePDX.java");
+
+    File outputJar = new File(temporaryFolder.getRoot(), "output.jar");
+    jarBuilder.buildJar(outputJar, source);
+
+    CommandStringBuilder csb = new CommandStringBuilder(CliStrings.DEPLOY);
+    csb.addOption(CliStrings.JAR, outputJar.getAbsolutePath());
+    gfsh.executeAndAssertThat(csb.toString()).statusIsSuccess();
+  }
+
+  @Test
+  public void createMappingWithoutExistingPdxNameFails() {
+    String region1Name = "region1";
+    setupReplicate(region1Name);
+
+    CommandStringBuilder csb = new CommandStringBuilder(CREATE_MAPPING);
+    csb.addOption(REGION_NAME, region1Name);
+    csb.addOption(DATA_SOURCE_NAME, "connection");
+    csb.addOption(TABLE_NAME, "employeeRegion");
+    csb.addOption(PDX_NAME, "org.apache.geode.internal.ResourcePDX");
+    csb.addOption(ID_NAME, "id");
+    csb.addOption(SCHEMA_NAME, "mySchema");
+    IgnoredException.addIgnoredException(ClassNotFoundException.class);
+
+    gfsh.executeAndAssertThat(csb.toString()).statusIsError()
+        .containsOutput("ClassNotFoundException");
+  }
+
+  @Test
+  public void createMappingWithDeployedPdxClassSucceeds() throws IOException, URISyntaxException
{
+    String region1Name = "region1";
+    setupReplicate(region1Name);
+
+    CommandStringBuilder csb = new CommandStringBuilder(CREATE_MAPPING);
+    csb.addOption(REGION_NAME, region1Name);
+    csb.addOption(DATA_SOURCE_NAME, "connection");
+    csb.addOption(TABLE_NAME, "employeeRegion");
+    csb.addOption(PDX_NAME, "org.apache.geode.internal.ResourcePDX");
+    csb.addOption(ID_NAME, "id");
+    csb.addOption(SCHEMA_NAME, "mySchema");
+    IgnoredException.addIgnoredException(ClassNotFoundException.class);
+
+    deployJar();
+    gfsh.executeAndAssertThat(csb.toString()).statusIsSuccess();
+
+    server1.invoke(() -> {
+      RegionMapping mapping = getRegionMappingFromService(region1Name);
+      assertValidResourcePDXMappingOnServer(mapping, region1Name, false, false, "employeeRegion");
+    });
+
+    locator.invoke(() -> {
+      RegionMapping regionMapping = getRegionMappingFromClusterConfig(region1Name, null);
+      assertValidResourcePDXMappingOnLocator(regionMapping, region1Name, null, false, false,
+          "employeeRegion");
+    });
+  }
+
   private static void assertValidEmployeeMapping(RegionMapping mapping, String tableName)
{
     assertThat(mapping.getDataSourceName()).isEqualTo("connection");
     assertThat(mapping.getTableName()).isEqualTo(tableName);
diff --git a/geode-connectors/src/distributedTest/resources/org/apache/geode/internal/ResourcePDX.java
b/geode-connectors/src/distributedTest/resources/org/apache/geode/internal/ResourcePDX.java
new file mode 100644
index 0000000..55f839d
--- /dev/null
+++ b/geode-connectors/src/distributedTest/resources/org/apache/geode/internal/ResourcePDX.java
@@ -0,0 +1,62 @@
+/*
+ * 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.internal;
+
+import org.apache.geode.pdx.PdxReader;
+import org.apache.geode.pdx.PdxSerializable;
+import org.apache.geode.pdx.PdxWriter;
+
+
+public class ResourcePDX implements PdxSerializable {
+  private String id;
+  private String name;
+  private int age;
+
+  public ResourcePDX() {
+    // nothing
+  }
+
+  public ResourcePDX(String id, String name, int age) {
+    this.id = id;
+    this.name = name;
+    this.age = age;
+  }
+
+  public String getId() {
+    return id;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public int getAge() {
+    return age;
+  }
+
+  @Override
+  public void toData(PdxWriter writer) {
+    writer.writeString("id", this.id);
+    writer.writeString("name", this.name);
+    writer.writeInt("age", this.age);
+  }
+
+  @Override
+  public void fromData(PdxReader reader) {
+    this.id = reader.readString("id");
+    this.name = reader.readString("name");
+    this.age = reader.readInt("age");
+  }
+}
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java
index 55b556c..40f04d1 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java
@@ -35,6 +35,7 @@ import org.apache.geode.connectors.jdbc.internal.TableMetaDataManager;
 import org.apache.geode.connectors.jdbc.internal.TableMetaDataView;
 import org.apache.geode.connectors.jdbc.internal.configuration.FieldMapping;
 import org.apache.geode.connectors.jdbc.internal.configuration.RegionMapping;
+import org.apache.geode.internal.ClassPathLoader;
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.internal.jndi.JNDIInvoker;
 import org.apache.geode.management.cli.CliFunction;
@@ -69,7 +70,7 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<RegionMa
 
   CreateMappingPreconditionCheckFunction() {
     this(dataSourceName -> JNDIInvoker.getDataSource(dataSourceName),
-        className -> Class.forName(className),
+        className -> ClassPathLoader.getLatest().forName(className),
         className -> new ReflectionBasedAutoSerializer(className),
         (typeRegistry, object) -> new PdxWriterImpl(typeRegistry, object, new PdxOutputStream()),
         new TableMetaDataManager());
@@ -78,7 +79,7 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<RegionMa
   // used by java during deserialization
   private void readObject(ObjectInputStream stream) {
     this.dataSourceFactory = dataSourceName -> JNDIInvoker.getDataSource(dataSourceName);
-    this.classFactory = className -> Class.forName(className);
+    this.classFactory = className -> ClassPathLoader.getLatest().forName(className);
     this.reflectionBasedAutoSerializerFactory =
         className -> new ReflectionBasedAutoSerializer(className);
     this.pdxWriterFactory =


Mime
View raw message