cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [1/2] git commit: [CXF-5908] Implemented clone, hashCode, equals and toString method for Claims
Date Fri, 01 Aug 2014 16:18:54 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 11c901b75 -> 573d80171


[CXF-5908] Implemented clone, hashCode, equals and toString method for Claims


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3348030b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3348030b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3348030b

Branch: refs/heads/2.7.x-fixes
Commit: 3348030bdfd6ceeac1d917b59556b3c3dd1a7860
Parents: 11c901b
Author: Jan Bernhardt <jbernhardt@talend.com>
Authored: Sat Jul 26 12:10:18 2014 +0200
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Fri Aug 1 17:10:34 2014 +0100

----------------------------------------------------------------------
 .../java/org/apache/cxf/sts/claims/Claim.java   | 112 +++++++++++++++-
 .../org/apache/cxf/sts/common/ClaimTest.java    | 132 +++++++++++++++++++
 2 files changed, 243 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3348030b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/Claim.java
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/Claim.java b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/Claim.java
index 1d17693..44ee34e 100644
--- a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/Claim.java
+++ b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/Claim.java
@@ -28,7 +28,7 @@ import java.util.List;
 /**
  * This represents a Claim that has been processed by a ClaimsHandler instance.
  */
-public class Claim implements Serializable {
+public class Claim implements Serializable, Cloneable {
 
     /**
      * 
@@ -40,6 +40,27 @@ public class Claim implements Serializable {
     private transient Principal principal;
     private List<String> values = new ArrayList<String>(1);
 
+    public Claim() {
+    }
+    
+    /**
+     * Create a clone of the provided claim.
+     * 
+     * @param claim Claim to be cloned. Value cannot be null.
+     */
+    public Claim(Claim claim) {
+        if (claim == null) {
+            throw new IllegalArgumentException("Claim cannot be null");
+        }
+        if (claim.claimType != null) {
+            claimType = URI.create(claim.claimType.toString());
+        }
+        issuer = claim.issuer;
+        originalIssuer = claim.originalIssuer;
+        values.addAll(claim.values);
+        principal = claim.principal;
+    }
+    
     public String getIssuer() {
         return issuer;
     }
@@ -101,5 +122,94 @@ public class Claim implements Serializable {
         }
         throw new IllegalStateException("Claim has multiple values");
     }
+    
+    @Override
+    public Claim clone() {
+        try {
+            super.clone(); // Checkstyle requires this call
+        } catch (CloneNotSupportedException e) {
+            e.printStackTrace();
+        }
+        return new Claim(this);
+    }
 
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((claimType == null)
+            ? 0
+            : claimType.hashCode());
+        result = prime * result + ((issuer == null)
+            ? 0
+            : issuer.hashCode());
+        result = prime * result + ((originalIssuer == null)
+            ? 0
+            : originalIssuer.hashCode());
+        result = prime * result + ((values == null)
+            ? 0
+            : values.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (!(obj instanceof Claim)) {
+            return false;
+        }
+        Claim other = (Claim)obj;
+        if (claimType == null) {
+            if (other.claimType != null) {
+                return false;
+            }
+        } else if (!claimType.equals(other.claimType)) {
+            return false;
+        }
+        if (issuer == null) {
+            if (other.issuer != null) {
+                return false;
+            }
+        } else if (!issuer.equals(other.issuer)) {
+            return false;
+        }
+        if (originalIssuer == null) {
+            if (other.originalIssuer != null) {
+                return false;
+            }
+        } else if (!originalIssuer.equals(other.originalIssuer)) {
+            return false;
+        }
+        if (values == null) {
+            if (other.values != null) {
+                return false;
+            }
+        } else if (!values.equals(other.values)) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("Claim [values=");
+        builder.append(values);
+        builder.append(", claimType=");
+        builder.append(claimType);
+        builder.append(", issuer=");
+        builder.append(issuer);
+        builder.append(", originalIssuer=");
+        builder.append(originalIssuer);
+        builder.append(", principal=");
+        builder.append(principal);
+        builder.append("]");
+        return builder.toString();
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3348030b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/common/ClaimTest.java
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/common/ClaimTest.java
b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/common/ClaimTest.java
new file mode 100644
index 0000000..fcf2b8b
--- /dev/null
+++ b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/common/ClaimTest.java
@@ -0,0 +1,132 @@
+/**
+ * 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.cxf.sts.common;
+
+import java.net.URI;
+
+import org.apache.cxf.sts.claims.Claim;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ClaimTest extends Assert {
+
+    @Test
+    public void testCloneNull() {
+        try {
+            new Claim(null);
+            fail("IllegalArgumentException was expected");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
+    @Test
+    public void testCloneAllEquals() {
+        Claim claim = new Claim();
+        claim.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"));
+        claim.setIssuer("Issuer");
+        claim.setOriginalIssuer("OrinignalIssuer");
+        claim.addValue("value1");
+        claim.addValue("value2");
+        claim.addValue("value3");
+        Claim clone = claim.clone();
+        assertEquals(claim, clone);
+        assertEquals(claim, new Claim(clone)); // Clone from clone by using clone constructor
+    }
+    
+    @Test
+    public void testCloneTypeOnlySetEquals() {
+        Claim claim = new Claim();
+        claim.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"));
+        Claim clone = claim.clone();
+        assertEquals(claim, clone);
+        assertEquals(claim, new Claim(clone)); // Clone from clone by using clone constructor
+    }
+    
+    @Test
+    public void testCloneValuesOnlySetEquals() {
+        Claim claim = new Claim();
+        claim.addValue("value1");
+        claim.addValue("value2");
+        Claim clone = claim.clone();
+        assertEquals(claim, clone);
+        assertEquals(claim, new Claim(clone)); // Clone from clone by using clone constructor
+    }
+    
+    @Test
+    public void testCloneUnset() {
+        Claim claim = new Claim();
+        Claim clone = claim.clone();
+        assertEquals(claim, clone);
+        assertEquals(claim, new Claim(clone)); // Clone from clone by using clone constructor
+    }
+    
+    @Test
+    public void testCloneAndModifyValuesNotEquals() {
+        Claim claim = new Claim();
+        claim.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"));
+        claim.addValue("value1");
+        claim.addValue("value2");
+        claim.addValue("value3");
+        Claim clone = claim.clone();
+
+        claim.getValues().clear();
+        claim.addValue("value4");
+        assertNotEquals(claim, clone);
+        assertEquals(1, claim.getValues().size());
+        assertEquals(3, clone.getValues().size());
+        assertEquals(claim.getClaimType(), clone.getClaimType());
+        assertNotEquals(claim.getValues(), clone.getValues());
+    }
+    
+    @Test
+    public void testCloneAndModifyTypeNotEquals() {
+        Claim claim = new Claim();
+        claim.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"));
+        claim.setIssuer("Issuer");
+        claim.setOriginalIssuer("OrinignalIssuer");
+        claim.addValue("value1");
+        claim.addValue("value2");
+        claim.addValue("value3");
+        Claim clone = claim.clone();
+        clone.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/value"));
+        assertNotEquals(claim, clone);
+        assertNotEquals(claim.getClaimType(), clone.getClaimType());
+        assertEquals(claim.getValues(), clone.getValues());
+    }
+    
+    @Test
+    public void testCloneAndModifyIssuerNotEquals() {
+        Claim claim = new Claim();
+        claim.setClaimType(URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"));
+        claim.setIssuer("Issuer");
+        claim.setOriginalIssuer("OrinignalIssuer");
+        claim.addValue("value1");
+        claim.addValue("value2");
+        claim.addValue("value3");
+        Claim clone = claim.clone();
+        claim.setIssuer("newIssuer");
+        claim.setOriginalIssuer("newOriginalIssuer");
+        assertNotEquals(claim, clone);
+        assertNotEquals(claim.getIssuer(), clone.getIssuer());
+        assertEquals(claim.getValues(), clone.getValues());
+        assertEquals(claim.getClaimType(), clone.getClaimType());
+    }
+
+}


Mime
View raw message