cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r979764 - in /cxf/trunk/rt/ws/policy/src: main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java main/java/org/apache/cxf/ws/policy/PolicyUtils.java test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java
Date Tue, 27 Jul 2010 16:19:20 GMT
Author: sergeyb
Date: Tue Jul 27 16:19:20 2010
New Revision: 979764

URL: http://svn.apache.org/viewvc?rev=979764&view=rev
Log:
CXF-2912 : Updating EndpointPolicyImpl to ignore empty policies during the update

Modified:
    cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java
    cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/PolicyUtils.java
    cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java

Modified: cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java?rev=979764&r1=979763&r2=979764&view=diff
==============================================================================
--- cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java
(original)
+++ cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/EndpointPolicyImpl.java
Tue Jul 27 16:19:20 2010
@@ -88,8 +88,16 @@ public class EndpointPolicyImpl implemen
     
     public EndpointPolicy updatePolicy(Policy p) {
         EndpointPolicyImpl epi = createEndpointPolicy();
-        Policy np = (Policy)p.normalize(true);
-        epi.setPolicy(getPolicy().merge(np));
+        
+        if (!PolicyUtils.isEmptyPolicy(p)) {
+            Policy normalizedPolicy = (Policy)p.normalize(true);
+            epi.setPolicy(getPolicy().merge(normalizedPolicy));
+        } else {
+            Policy clonedPolicy = new Policy();
+            clonedPolicy.addPolicyComponents(getPolicy().getPolicyComponents());
+            epi.setPolicy(clonedPolicy);
+        }
+        
         epi.checkExactlyOnes();
         epi.finalizeConfig();
         return epi;

Modified: cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/PolicyUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/PolicyUtils.java?rev=979764&r1=979763&r2=979764&view=diff
==============================================================================
--- cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/PolicyUtils.java (original)
+++ cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/PolicyUtils.java Tue Jul
27 16:19:20 2010
@@ -28,6 +28,7 @@ import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.ws.policy.builder.primitive.NestedPrimitiveAssertion;
 import org.apache.neethi.Assertion;
 import org.apache.neethi.Constants;
+import org.apache.neethi.Policy;
 import org.apache.neethi.PolicyComponent;
 import org.apache.neethi.PolicyOperator;
 
@@ -42,6 +43,45 @@ public final class PolicyUtils {
     }  
     
     /**
+     * Checks if a given policy contains no policy components 
+     * or if it has only empty ExactlyOne or All components 
+     * containing no assertions 
+     * 
+     * @param p the policy
+     * @return true if the policy is empty
+     */
+    public static boolean isEmptyPolicy(Policy p) {
+        
+        return isEmptyPolicyOperator(p);
+    }
+    
+    /**
+     * Checks if a given policy operator has no policy components 
+     * or if it has only empty ExactlyOne or All components 
+     * containing no assertions 
+     * 
+     * @param p the policy operator
+     * @return true if this policy operator is empty
+     */
+    public static boolean isEmptyPolicyOperator(PolicyOperator p) {
+        
+        if (p.isEmpty()) {
+            return true;
+        }
+        
+        List components = p.getPolicyComponents();
+        
+        for (Object component : components) {
+            if (!(component instanceof PolicyOperator)
+                || !isEmptyPolicyOperator((PolicyOperator)component)) {
+                return false;
+            }
+        }
+        
+        return true;
+    }
+    
+    /**
      * Determine if a collection of assertions contains a given assertion, using
      * the equal method from the Assertion interface.
      * 

Modified: cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java?rev=979764&r1=979763&r2=979764&view=diff
==============================================================================
--- cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java
(original)
+++ cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/EndpointPolicyImplTest.java
Tue Jul 27 16:19:20 2010
@@ -37,8 +37,10 @@ import org.apache.neethi.All;
 import org.apache.neethi.Constants;
 import org.apache.neethi.ExactlyOne;
 import org.apache.neethi.Policy;
+import org.apache.neethi.PolicyOperator;
 import org.easymock.classextension.EasyMock;
 import org.easymock.classextension.IMocksControl;
+
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -222,6 +224,61 @@ public class EndpointPolicyImplTest exte
         assertTrue("Policy was not merged",
                    n1.equals(aqn1) && n2.equals(aqn2) || n1.equals(aqn2) &&
n2.equals(aqn1));
     }
+
+    @Test
+    public void testUpdatePolicyWithEmptyPolicy() {
+        
+        doTestUpdateWithEmptyPolicy(new Policy());
+    }
+    
+    @Test
+    public void testUpdatePolicyWithEmptyAll() {
+        
+        Policy emptyPolicy = new Policy();
+        emptyPolicy.addPolicyComponent(new All());
+        emptyPolicy.addPolicyComponent(new All());
+        doTestUpdateWithEmptyPolicy(emptyPolicy);
+    }
+    
+    @Test
+    public void testUpdatePolicyWithEmptyExactlyOneAndAll() {
+        
+        Policy emptyPolicy = new Policy();
+        PolicyOperator exactlyOne = new ExactlyOne();
+        exactlyOne.addPolicyComponent(new All());
+        exactlyOne.addPolicyComponent(new All());
+        emptyPolicy.addPolicyComponent(exactlyOne);
+        emptyPolicy.addPolicyComponent(new All());
+        emptyPolicy.addPolicyComponent(new All());
+        doTestUpdateWithEmptyPolicy(emptyPolicy);
+    }    
+    
+    private void doTestUpdateWithEmptyPolicy(Policy emptyPolicy) {
+        Policy p1 = new Policy();
+        QName aqn1 = new QName("http://x.y.z", "a");
+        p1.addAssertion(mockAssertion(aqn1, 5, true));
+        
+        EndpointPolicyImpl epi = new TestEndpointPolicy();
+        control.replay();
+        
+        epi.setPolicy((Policy)p1.normalize(true));
+                
+        Policy ep = epi.updatePolicy(emptyPolicy).getPolicy();
+        
+        List<ExactlyOne> pops = 
+            CastUtils.cast(ep.getPolicyComponents(), ExactlyOne.class);
+        assertEquals("New policy must have 1 top level policy operator", 1, pops.size());
+        List<All> alts = 
+            CastUtils.cast(pops.get(0).getPolicyComponents(), All.class);
+        assertEquals("1 alternatives should be available", 1, alts.size());
+        
+        List<PolicyAssertion> assertions1 = 
+            CastUtils.cast(alts.get(0).getAssertions(), PolicyAssertion.class);
+        assertEquals("1 assertion should be available", 1, assertions1.size());
+        
+        QName n1 = assertions1.get(0).getName();
+        assertTrue("Policy was not merged", n1.equals(aqn1));
+    }
     
     private PolicyAssertion mockAssertion(QName name, int howMany, boolean normalize) {
         PolicyAssertion a = control.createMock(PolicyAssertion.class);        



Mime
View raw message