cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [2/2] git commit: [CXF-5908] Implemented clone, hashCode, equals and toString method for Claims
Date Fri, 01 Aug 2014 16:00:48 GMT
[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/98f65f29
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/98f65f29
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/98f65f29

Branch: refs/heads/master
Commit: 98f65f2911e063e70e864e7614b062cc124a0f9d
Parents: c019803
Author: Jan Bernhardt <jbernhardt@talend.com>
Authored: Sat Jul 26 11:13:00 2014 +0200
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Fri Aug 1 17:00:33 2014 +0100

----------------------------------------------------------------------
 .../apache/cxf/rt/security/claims/Claim.java    |  92 ++++++++++++++-
 .../cxf/rt/security/claims/ClaimTest.java       | 114 +++++++++++++++++++
 .../apache/cxf/sts/claims/ProcessedClaim.java   |  72 ++++++++++++
 3 files changed, 277 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/98f65f29/rt/security/src/main/java/org/apache/cxf/rt/security/claims/Claim.java
----------------------------------------------------------------------
diff --git a/rt/security/src/main/java/org/apache/cxf/rt/security/claims/Claim.java b/rt/security/src/main/java/org/apache/cxf/rt/security/claims/Claim.java
index bfc6286..c97a8b9 100644
--- a/rt/security/src/main/java/org/apache/cxf/rt/security/claims/Claim.java
+++ b/rt/security/src/main/java/org/apache/cxf/rt/security/claims/Claim.java
@@ -33,7 +33,7 @@ import org.apache.cxf.common.logging.LogUtils;
 /**
  * This represents a Claim.
  */
-public class Claim implements Serializable {
+public class Claim implements Serializable, Cloneable {
     
     private static final long serialVersionUID = 5730726672368086795L;
 
@@ -43,6 +43,25 @@ public class Claim implements Serializable {
     private boolean optional;
     private List<Object> values = new ArrayList<Object>(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.getClaimType() != null) {
+            claimType = URI.create(claim.getClaimType().toString());
+        }
+        optional = claim.isOptional();
+        values.addAll(claim.getValues());
+    }
+
     public URI getClaimType() {
         return claimType;
     }
@@ -96,4 +115,75 @@ public class Claim implements Serializable {
         }
         writer.writeEndElement();
     }
+    
+    @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 + (optional
+            ? 1231
+            : 1237);
+        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 (optional != other.optional) {
+            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(", optional=");
+        builder.append(optional);
+        builder.append("]");
+        return builder.toString();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/98f65f29/rt/security/src/test/java/org/apache/cxf/rt/security/claims/ClaimTest.java
----------------------------------------------------------------------
diff --git a/rt/security/src/test/java/org/apache/cxf/rt/security/claims/ClaimTest.java b/rt/security/src/test/java/org/apache/cxf/rt/security/claims/ClaimTest.java
new file mode 100644
index 0000000..adc814b
--- /dev/null
+++ b/rt/security/src/test/java/org/apache/cxf/rt/security/claims/ClaimTest.java
@@ -0,0 +1,114 @@
+/**
+ * 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.rt.security.claims;
+
+import java.net.URI;
+
+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.setOptional(true);
+        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.setOptional(true);
+        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());
+        assertEquals(claim.isOptional(), clone.isOptional());
+        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.setOptional(true);
+        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.isOptional(), clone.isOptional());
+        assertEquals(claim.getValues(), clone.getValues());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/98f65f29/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/ProcessedClaim.java
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/ProcessedClaim.java
b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/ProcessedClaim.java
index fff7fe6..c99b5f0 100644
--- a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/ProcessedClaim.java
+++ b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/ProcessedClaim.java
@@ -36,6 +36,16 @@ public class ProcessedClaim extends Claim {
     private String originalIssuer;
     private transient Principal principal;
 
+    public ProcessedClaim() {
+    }
+    
+    public ProcessedClaim(ProcessedClaim processedClaim) {
+        super(processedClaim);
+        issuer = processedClaim.issuer;
+        originalIssuer = processedClaim.originalIssuer;
+        principal = processedClaim.principal;
+    }
+
     public String getIssuer() {
         return issuer;
     }
@@ -60,4 +70,66 @@ public class ProcessedClaim extends Claim {
         this.principal = principal;
     }
 
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("ProcessedClaim [");
+        builder.append(super.toString());
+        builder.append(", issuer=");
+        builder.append(issuer);
+        builder.append(", originalIssuer=");
+        builder.append(originalIssuer);
+        builder.append(", principal=");
+        builder.append(principal);
+        builder.append("]");
+        return builder.toString();
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ((issuer == null)
+            ? 0
+            : issuer.hashCode());
+        result = prime * result + ((originalIssuer == null)
+            ? 0
+            : originalIssuer.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof ProcessedClaim)) {
+            return false;
+        }
+        ProcessedClaim other = (ProcessedClaim)obj;
+        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;
+        }
+        return true;
+    }
+    
+    @Override
+    public ProcessedClaim clone() {
+        super.clone(); // Checkstyle requires this call
+        return new ProcessedClaim(this);
+    }
 }


Mime
View raw message