You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/03/02 22:56:41 UTC

incubator-geode git commit: Fix serialization issues

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-949-2 f9b621dc0 -> 9d801e352


Fix serialization issues


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/9d801e35
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/9d801e35
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/9d801e35

Branch: refs/heads/feature/GEODE-949-2
Commit: 9d801e3528f556c470fbca81702ec36b3a4500cc
Parents: f9b621d
Author: Kirk Lund <kl...@apache.org>
Authored: Wed Mar 2 13:47:07 2016 -0800
Committer: Kirk Lund <kl...@apache.org>
Committed: Wed Mar 2 13:47:07 2016 -0800

----------------------------------------------------------------------
 .../security/GemFireSecurityException.java      |  24 ++-
 .../security/GemFireSecurityExceptionTest.java  | 169 +++++++++++++++++++
 .../security/NotAuthorizedExceptionTest.java    | 150 ++++++++++++++--
 3 files changed, 322 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9d801e35/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java b/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
index 441f66d..d099525 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
@@ -32,14 +32,20 @@ import java.io.*;
 public class GemFireSecurityException extends GemFireException {
   private static final long serialVersionUID = 3814254578203076926L;
 
+  private Throwable cause;
+
   /**
    * Constructs instance of <code>SecurityException</code> with error message.
    * 
    * @param message
    *                the error message
    */
-  public GemFireSecurityException(String message) {
-    super(message);
+  public GemFireSecurityException(final String message) {
+    this(message, null);
+  }
+
+  public GemFireSecurityException(final Throwable cause) {
+    this(cause != null ? cause.getMessage() : null, cause);
   }
 
   /**
@@ -51,19 +57,25 @@ public class GemFireSecurityException extends GemFireException {
    * @param cause
    *                a <code>Throwable</code> that is a cause of this exception
    */
-  public GemFireSecurityException(String message, Throwable cause) {
-    super(message, cause);
+  public GemFireSecurityException(final String message, final Throwable cause) {
+    super(message);
+    this.cause = cause;
+  }
+
+  @Override
+  public Throwable getCause() {
+    return (this.cause == this ? null : this.cause);
   }
 
   protected boolean isSerializable(final Object object) {
     if (object == null) {
       return true;
     }
-    return object.getClass().isInstance(Serializable.class);
+    return Serializable.class.isInstance(object);
   }
 
   protected Object getResolvedObj() {
-    if (getCause() != null && getCause().getClass().isInstance(NamingException.class)) {
+    if (getCause() != null && NamingException.class.isInstance(getCause())) {
       return ((javax.naming.NamingException) getCause()).getResolvedObj();
     }
     return null;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9d801e35/geode-core/src/test/java/com/gemstone/gemfire/security/GemFireSecurityExceptionTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/security/GemFireSecurityExceptionTest.java b/geode-core/src/test/java/com/gemstone/gemfire/security/GemFireSecurityExceptionTest.java
new file mode 100644
index 0000000..2c76138
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/security/GemFireSecurityExceptionTest.java
@@ -0,0 +1,169 @@
+/*
+ * 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 com.gemstone.gemfire.security;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TestName;
+
+import javax.naming.NamingException;
+import java.io.NotSerializableException;
+import java.io.Serializable;
+import java.security.Principal;
+
+import static com.googlecode.catchexception.CatchException.*;
+import static org.assertj.core.api.Assertions.*;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Unit tests for {@link GemFireSecurityException}.
+ */
+@Category(UnitTest.class)
+public class GemFireSecurityExceptionTest {
+
+  private String message;
+  private String causeMessage;
+  private Object nonSerializableResolvedObj;
+  private NamingException nonSerializableNamingException;
+  private SerializableObject serializableResolvedObj;
+  private NamingException serializableNamingException;
+
+  @Rule
+  public TestName testName = new TestName();
+
+  @Before
+  public void setUp() throws Exception {
+    this.message = testName.getMethodName() + " message";
+    this.causeMessage = testName.getMethodName() + " cause message";
+
+    this.nonSerializableResolvedObj = new Object();
+    this.nonSerializableNamingException = new NamingException(this.causeMessage);
+    this.nonSerializableNamingException.setResolvedObj(this.nonSerializableResolvedObj);
+
+    this.serializableResolvedObj = new SerializableObject(this.testName.getMethodName());
+    this.serializableNamingException = new NamingException(this.causeMessage);
+    this.serializableNamingException.setResolvedObj(this.serializableResolvedObj);
+
+    assertPreConditions();
+  }
+
+  private void assertPreConditions() {
+    catchException(this).clone(this.nonSerializableNamingException);
+    assertThat((Throwable)caughtException()).isNotNull();
+    assertThat((Throwable)caughtException().getCause()).isInstanceOf(NotSerializableException.class);
+
+    catchException(this).clone(this.serializableNamingException);
+    assertThat((Throwable)caughtException()).isNull();
+
+    assertThat(this.nonSerializableResolvedObj).isNotInstanceOf(Serializable.class);
+
+    catchException(this).clone(this.serializableResolvedObj);
+    assertThat((Throwable)caughtException()).isNull();
+  }
+
+  @Test
+  public void isSerializable() throws Exception {
+    assertThat(GemFireSecurityException.class).isInstanceOf(Serializable.class);
+  }
+
+  @Test
+  public void serializes() throws Exception {
+    GemFireSecurityException instance = new GemFireSecurityException(this.message);
+
+    GemFireSecurityException cloned = (GemFireSecurityException) SerializationUtils.clone(instance);
+
+    assertThat(cloned).hasMessage(this.message);
+  }
+
+  @Test
+  public void serializesWithThrowable() throws Exception {
+    Throwable cause = new Exception(this.causeMessage);
+    GemFireSecurityException instance = new GemFireSecurityException(this.message, cause);
+
+    GemFireSecurityException cloned = (GemFireSecurityException) SerializationUtils.clone(instance);
+
+    assertThat(cloned).hasMessage(this.message).hasCause(cause);
+    assertThat(cloned.getCause()).hasMessage(this.causeMessage);
+  }
+
+  @Test
+  public void serializesWithNonSerializableNamingException() throws Exception {
+    GemFireSecurityException instance = new GemFireSecurityException(this.message, this.nonSerializableNamingException);
+
+    GemFireSecurityException cloned = (GemFireSecurityException) SerializationUtils.clone(instance);
+
+    assertThat(cloned).hasMessage(this.message).hasCause(this.nonSerializableNamingException);
+    NamingException cause = (NamingException) cloned.getCause();
+    assertThat(cause).hasMessage(this.causeMessage);
+    assertThat(cause.getResolvedObj()).isNull();
+  }
+
+  @Test
+  public void serializesWithSerializableNamingException() throws Exception {
+    GemFireSecurityException instance = new GemFireSecurityException(this.message, this.serializableNamingException);
+
+    GemFireSecurityException cloned = (GemFireSecurityException) SerializationUtils.clone(instance);
+
+    assertThat(cloned).hasMessage(this.message).hasCause(this.serializableNamingException);
+    NamingException cause = (NamingException) cloned.getCause();
+    assertThat(cause).hasMessage(this.causeMessage);
+    assertThat(cause.getResolvedObj()).isNotNull().isEqualTo(this.serializableResolvedObj);
+  }
+
+  @Test
+  public void isSerializableReturnsTrueForSerializableClass() throws Exception {
+    assertThat(new GemFireSecurityException("").isSerializable(this.serializableResolvedObj)).isTrue();
+  }
+
+  @Test
+  public void isSerializableReturnsFalseForNonSerializableClass() throws Exception {
+    assertThat(new GemFireSecurityException("").isSerializable(this.nonSerializableResolvedObj)).isFalse();
+  }
+
+  public Object clone(final Serializable object) {
+    return SerializationUtils.clone(object);
+  }
+
+  public static class SerializableObject implements Serializable {
+
+    private String name;
+
+    SerializableObject(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) return true;
+      if (o == null || getClass() != o.getClass()) return false;
+
+      SerializableObject that = (SerializableObject) o;
+
+      return name != null ? name.equals(that.name) : that.name == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+      return name != null ? name.hashCode() : 0;
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9d801e35/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java b/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
index 9391102..162d5ff 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
@@ -18,12 +18,20 @@ package com.gemstone.gemfire.security;
 
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
 import org.apache.commons.lang.SerializationUtils;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.junit.rules.TestName;
 
+import javax.naming.NamingException;
+import javax.security.auth.Subject;
+import java.io.NotSerializableException;
 import java.io.Serializable;
 import java.security.Principal;
 
+import static com.googlecode.catchexception.CatchException.catchException;
+import static com.googlecode.catchexception.CatchException.caughtException;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
@@ -33,43 +41,155 @@ import static org.mockito.Mockito.mock;
 @Category(UnitTest.class)
 public class NotAuthorizedExceptionTest {
 
+  private String message;
+  private String causeMessage;
+  private Object nonSerializableResolvedObj;
+  private NamingException nonSerializableNamingException;
+  private SerializableObject serializableResolvedObj;
+  private NamingException serializableNamingException;
+  private String principalName;
+  private Principal nonSerializablePrincipal;
+  private Principal serializablePrincipal;
+
+  @Rule
+  public TestName testName = new TestName();
+
+  @Before
+  public void setUp() throws Exception {
+    this.message = testName.getMethodName() + " message";
+    this.causeMessage = testName.getMethodName() + " cause message";
+
+    this.nonSerializableResolvedObj = new Object();
+    this.nonSerializableNamingException = new NamingException(this.causeMessage);
+    this.nonSerializableNamingException.setResolvedObj(this.nonSerializableResolvedObj);
+
+    this.serializableResolvedObj = new SerializableObject(this.testName.getMethodName());
+    this.serializableNamingException = new NamingException(this.causeMessage);
+    this.serializableNamingException.setResolvedObj(this.serializableResolvedObj);
+
+    this.principalName = "jsmith";
+    this.nonSerializablePrincipal = mock(Principal.class);
+    this.serializablePrincipal = new SerializablePrincipal(this.principalName);
+
+    assertPreConditions();
+  }
+
+  private void assertPreConditions() {
+    catchException(this).clone(this.nonSerializableNamingException);
+    assertThat((Throwable)caughtException()).isNotNull();
+    assertThat((Throwable)caughtException().getCause()).isInstanceOf(NotSerializableException.class);
+
+    catchException(this).clone(this.serializableNamingException);
+    assertThat((Throwable)caughtException()).isNull();
+
+    assertThat(this.nonSerializableResolvedObj).isNotInstanceOf(Serializable.class);
+
+    catchException(this).clone(this.serializableResolvedObj);
+    assertThat((Throwable)caughtException()).isNull();
+  }
+
   @Test
   public void isSerializable() throws Exception {
     assertThat(NotAuthorizedException.class).isInstanceOf(Serializable.class);
   }
 
   @Test
-  public void canBeSerialized() throws Exception {
-    String message = "my message";
-    NotAuthorizedException instance = new NotAuthorizedException(message);
+  public void serializes() throws Exception {
+    NotAuthorizedException instance = new NotAuthorizedException(this.message);
 
     NotAuthorizedException cloned = (NotAuthorizedException) SerializationUtils.clone(instance);
 
-    assertThat(cloned).hasMessage(message);
+    assertThat(cloned).hasMessage(this.message);
   }
 
   @Test
-  public void canBeSerializedWithThrowable() throws Exception {
-    String message = "my message";
-    Throwable cause = new Exception("the cause");
-    NotAuthorizedException instance = new NotAuthorizedException(message, cause);
+  public void serializesWithThrowable() throws Exception {
+    Throwable cause = new Exception(this.causeMessage);
+    NotAuthorizedException instance = new NotAuthorizedException(this.message, cause);
 
     NotAuthorizedException cloned = (NotAuthorizedException) SerializationUtils.clone(instance);
 
-    assertThat(cloned).hasMessage(message);
+    assertThat(cloned).hasMessage(this.message);
     assertThat(cloned).hasCause(cause);
   }
 
   @Test
-  public void canBeSerializedWithNonSerializablePrincipal() throws Exception {
-    String message = "my message";
-    Principal mockPrincipal = mock(Principal.class);
-    NotAuthorizedException instance = new NotAuthorizedException(message, mockPrincipal);
+  public void serializesWithNonSerializablePrincipal() throws Exception {
+    NotAuthorizedException instance = new NotAuthorizedException(this.message, this.nonSerializablePrincipal);
+    assertThat(instance.getPrincipal()).isNotNull();
 
     NotAuthorizedException cloned = (NotAuthorizedException) SerializationUtils.clone(instance);
 
-    assertThat(cloned).hasMessage(message);
-    //assertThat(cloned.getPrincipal()).isEqualTo(mockPrincipal);
+    assertThat(cloned).hasMessage(this.message);
     assertThat(cloned.getPrincipal()).isNull();
   }
+
+  @Test
+  public void serializesWithSerializablePrincipal() throws Exception {
+    NotAuthorizedException instance = new NotAuthorizedException(this.message, this.serializablePrincipal);
+
+    NotAuthorizedException cloned = (NotAuthorizedException) SerializationUtils.clone(instance);
+
+    assertThat(cloned).hasMessage(this.message);
+    assertThat(cloned.getPrincipal()).isNotNull().isEqualTo(this.serializablePrincipal);
+  }
+
+  public Object clone(final Serializable object) {
+    return SerializationUtils.clone(object);
+  }
+
+  public static class SerializableObject implements Serializable {
+
+    private String name;
+
+    SerializableObject(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) return true;
+      if (o == null || getClass() != o.getClass()) return false;
+
+      SerializableObject that = (SerializableObject) o;
+
+      return name != null ? name.equals(that.name) : that.name == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+      return name != null ? name.hashCode() : 0;
+    }
+  }
+
+  public static class SerializablePrincipal implements Principal, Serializable {
+
+    private String name;
+
+    SerializablePrincipal(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public String getName() {
+      return this.name;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) return true;
+      if (o == null || getClass() != o.getClass()) return false;
+
+      SerializablePrincipal that = (SerializablePrincipal) o;
+
+      return name != null ? name.equals(that.name) : that.name == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+      return name != null ? name.hashCode() : 0;
+    }
+  }
 }