You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2019/02/21 20:56:28 UTC

[geode] branch feature/GEODE-6291 updated: create mapping now attempts to register the pdx meta data. If that fails it then tries to serialize using its own ReflectionBasedAutoSerializer.

This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch feature/GEODE-6291
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-6291 by this push:
     new 617b813  create mapping now attempts to register the pdx meta data. If that fails it then tries to serialize using its own ReflectionBasedAutoSerializer.
617b813 is described below

commit 617b8135c7c72c5a475e6616e74deb288c20c844
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Thu Feb 21 12:55:23 2019 -0800

    create mapping now attempts to register the pdx meta data.
    If that fails it then tries to serialize using its own
    ReflectionBasedAutoSerializer.
---
 .../CreateMappingPreconditionCheckFunction.java    | 17 +++----
 ...CreateMappingPreconditionCheckFunctionTest.java | 57 ++++++----------------
 2 files changed, 20 insertions(+), 54 deletions(-)

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 a12c510..55b556c 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
@@ -39,7 +39,6 @@ import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.internal.jndi.JNDIInvoker;
 import org.apache.geode.management.cli.CliFunction;
 import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
-import org.apache.geode.pdx.PdxSerializable;
 import org.apache.geode.pdx.PdxWriter;
 import org.apache.geode.pdx.ReflectionBasedAutoSerializer;
 import org.apache.geode.pdx.internal.PdxField;
@@ -184,19 +183,14 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<RegionMa
    * @param cache used to generate pdx type
    * @param clazz the class to generate a PdxType for
    * @return the generated PdxType
-   * @throws JdbcException if a PdxType can not be generated
+   * @throws JdbcConnectorException if a PdxType can not be generated
    */
   private PdxType generatePdxTypeForClass(InternalCache cache, TypeRegistry typeRegistry,
       Class<?> clazz) {
     Object object = createInstance(clazz);
-    if (PdxSerializable.class.isAssignableFrom(clazz)) {
-      try {
-        cache.registerPdxMetaData(object);
-      } catch (SerializationException ex) {
-        throw new JdbcConnectorException(
-            "Could not generate a PdxType for the class " + clazz.getName() + " because: " + ex);
-      }
-    } else {
+    try {
+      cache.registerPdxMetaData(object);
+    } catch (SerializationException ex) {
       String className = clazz.getName();
       ReflectionBasedAutoSerializer serializer =
           this.reflectionBasedAutoSerializerFactory.create(className);
@@ -205,7 +199,8 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<RegionMa
       if (!result) {
         throw new JdbcConnectorException(
             "Could not generate a PdxType using the ReflectionBasedAutoSerializer for the class  "
-                + clazz.getName() + ". Check the server log for details.");
+                + clazz.getName() + " after failing to register pdx metadata due to "
+                + ex.getMessage() + ". Check the server log for details.");
       }
     }
     // serialization will leave the type in the registry
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java
index 098b0ce..c3fd216 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java
@@ -55,8 +55,6 @@ import org.apache.geode.connectors.jdbc.internal.configuration.RegionMapping;
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
 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.pdx.ReflectionBasedAutoSerializer;
 import org.apache.geode.pdx.internal.PdxField;
@@ -84,18 +82,11 @@ public class CreateMappingPreconditionCheckFunctionTest {
 
   private CreateMappingPreconditionCheckFunction function;
 
-  public static class NonPdxDummy {
-  }
-
-  public static class PdxClassDummy implements PdxSerializable {
-    @Override
-    public void toData(PdxWriter writer) {}
+  public static class PdxClassDummy {
 
-    @Override
-    public void fromData(PdxReader reader) {}
   }
 
-  public static class PdxClassDummyNoZeroArg extends PdxClassDummy {
+  public static class PdxClassDummyNoZeroArg {
     public PdxClassDummyNoZeroArg(int arg) {}
   }
 
@@ -318,8 +309,7 @@ public class CreateMappingPreconditionCheckFunctionTest {
     when(pdxField1.getFieldType()).thenReturn(FieldType.LONG);
     when(pdxType.getFieldCount()).thenReturn(1);
     when(pdxType.getFields()).thenReturn(Arrays.asList(pdxField1));
-    when(classFactory.loadClass(PDX_CLASS_NAME)).thenReturn(NonPdxDummy.class);
-    when(typeRegistry.getExistingTypeForClass(NonPdxDummy.class)).thenReturn(null)
+    when(typeRegistry.getExistingTypeForClass(PdxClassDummy.class)).thenReturn(null)
         .thenReturn(pdxType);
     ReflectionBasedAutoSerializer reflectionedBasedAutoSerializer =
         mock(ReflectionBasedAutoSerializer.class);
@@ -327,17 +317,20 @@ public class CreateMappingPreconditionCheckFunctionTest {
     when(reflectionedBasedAutoSerializer.toData(any(), same(pdxWriter))).thenReturn(true);
     ReflectionBasedAutoSerializerFactory reflectionBasedAutoSerializerFactory =
         mock(ReflectionBasedAutoSerializerFactory.class);
-    when(reflectionBasedAutoSerializerFactory.create(NonPdxDummy.class.getName()))
+    when(reflectionBasedAutoSerializerFactory.create(PdxClassDummy.class.getName()))
         .thenReturn(reflectionedBasedAutoSerializer);
     PdxWriterFactory pdxWriterFactory = mock(PdxWriterFactory.class);
     when(pdxWriterFactory.create(same(typeRegistry), any())).thenReturn(pdxWriter);
     function = new CreateMappingPreconditionCheckFunction(dataSourceFactory, classFactory,
         reflectionBasedAutoSerializerFactory, pdxWriterFactory,
         tableMetaDataManager);
+    SerializationException ex = new SerializationException("test");
+    doThrow(ex).when(cache).registerPdxMetaData(any());
 
     CliFunctionResult result = function.executeFunction(context);
 
     assertThat(result.isSuccessful()).isTrue();
+    verify(reflectionBasedAutoSerializerFactory).create(PdxClassDummy.class.getName());
     Object[] outputs = (Object[]) result.getResultObject();
     ArrayList<FieldMapping> fieldsMappings = (ArrayList<FieldMapping>) outputs[1];
     assertThat(fieldsMappings).hasSize(1);
@@ -348,7 +341,7 @@ public class CreateMappingPreconditionCheckFunctionTest {
 
 
   @Test
-  public void executeFunctionThrowsGivenNonPdxUsesAndReflectionBasedAutoSerializerThatReturnsFalse()
+  public void executeFunctionThrowsGivenPdxRegistrationFailsAndReflectionBasedAutoSerializerThatReturnsFalse()
       throws Exception {
     Set<String> columnNames = new LinkedHashSet<>(Arrays.asList("col1"));
     when(tableMetaDataView.getColumnNames()).thenReturn(columnNames);
@@ -359,8 +352,7 @@ public class CreateMappingPreconditionCheckFunctionTest {
     when(pdxField1.getFieldType()).thenReturn(FieldType.LONG);
     when(pdxType.getFieldCount()).thenReturn(1);
     when(pdxType.getFields()).thenReturn(Arrays.asList(pdxField1));
-    when(classFactory.loadClass(PDX_CLASS_NAME)).thenReturn(NonPdxDummy.class);
-    when(typeRegistry.getExistingTypeForClass(NonPdxDummy.class)).thenReturn(null)
+    when(typeRegistry.getExistingTypeForClass(PdxClassDummy.class)).thenReturn(null)
         .thenReturn(pdxType);
     ReflectionBasedAutoSerializer reflectionedBasedAutoSerializer =
         mock(ReflectionBasedAutoSerializer.class);
@@ -368,10 +360,13 @@ public class CreateMappingPreconditionCheckFunctionTest {
     when(reflectionedBasedAutoSerializer.toData(any(), same(pdxWriter))).thenReturn(false);
     ReflectionBasedAutoSerializerFactory reflectionBasedAutoSerializerFactory =
         mock(ReflectionBasedAutoSerializerFactory.class);
-    when(reflectionBasedAutoSerializerFactory.create(NonPdxDummy.class.getName()))
+    when(reflectionBasedAutoSerializerFactory.create(PdxClassDummy.class.getName()))
         .thenReturn(reflectionedBasedAutoSerializer);
     PdxWriterFactory pdxWriterFactory = mock(PdxWriterFactory.class);
     when(pdxWriterFactory.create(same(typeRegistry), any())).thenReturn(pdxWriter);
+    SerializationException ex = new SerializationException("test");
+    doThrow(ex).when(cache).registerPdxMetaData(any());
+
     function = new CreateMappingPreconditionCheckFunction(dataSourceFactory, classFactory,
         reflectionBasedAutoSerializerFactory, pdxWriterFactory,
         tableMetaDataManager);
@@ -380,31 +375,7 @@ public class CreateMappingPreconditionCheckFunctionTest {
 
     assertThat(throwable).isInstanceOf(JdbcConnectorException.class)
         .hasMessage(
-            "Could not generate a PdxType using the ReflectionBasedAutoSerializer for the class  org.apache.geode.connectors.jdbc.internal.cli.CreateMappingPreconditionCheckFunctionTest$NonPdxDummy. Check the server log for details.");
-  }
-
-  @Test
-  public void executeFunctionThrowsGivenPdxSerializableAndRegisterThatThrows()
-      throws Exception {
-    Set<String> columnNames = new LinkedHashSet<>(Arrays.asList("col1"));
-    when(tableMetaDataView.getColumnNames()).thenReturn(columnNames);
-    when(tableMetaDataView.isColumnNullable("col1")).thenReturn(false);
-    when(tableMetaDataView.getColumnDataType("col1")).thenReturn(JDBCType.DATE);
-    PdxField pdxField1 = mock(PdxField.class);
-    when(pdxField1.getFieldName()).thenReturn("COL1");
-    when(pdxField1.getFieldType()).thenReturn(FieldType.LONG);
-    when(pdxType.getFieldCount()).thenReturn(1);
-    when(pdxType.getFields()).thenReturn(Arrays.asList(pdxField1));
-    when(typeRegistry.getExistingTypeForClass(PdxClassDummy.class)).thenReturn(null)
-        .thenReturn(pdxType);
-    SerializationException ex = new SerializationException("test");
-    doThrow(ex).when(cache).registerPdxMetaData(any());
-
-    Throwable throwable = catchThrowable(() -> function.executeFunction(context));
-
-    assertThat(throwable).isInstanceOf(JdbcConnectorException.class)
-        .hasMessage(
-            "Could not generate a PdxType for the class org.apache.geode.connectors.jdbc.internal.cli.CreateMappingPreconditionCheckFunctionTest$PdxClassDummy because: org.apache.geode.SerializationException: test");
+            "Could not generate a PdxType using the ReflectionBasedAutoSerializer for the class  org.apache.geode.connectors.jdbc.internal.cli.CreateMappingPreconditionCheckFunctionTest$PdxClassDummy after failing to register pdx metadata due to test. Check the server log for details.");
   }
 
   @Test