Author: andreasmyth
Date: Wed Mar 28 00:14:48 2007
New Revision: 523198
URL: http://svn.apache.org/viewvc?view=rev&rev=523198
Log:
* Implement Intersection according to section 4.5 in
http://www.w3.org/TR/2006/WD-ws-policy-20061117.
* Add interface to compute compatible assertions and implement for the generic
assertion types.
* Implement Assertion equal for the generic assertion types (in case of
NestedPrimitiveAssertions taking into account the nested policies.
* Fixed Normalisation for NestedPrimitiveAssertions.
Added:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
(with props)
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
(with props)
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/AssertionBuilder.java
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertion.java
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilder.java
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/PrimitiveAssertionBuilder.java
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionTest.java
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilderTest.java
incubator/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/policy/AddressingAssertionBuilder.java
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/PolicyExtensionsTest.java
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/AssertionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/AssertionBuilder.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/AssertionBuilder.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/AssertionBuilder.java
Wed Mar 28 00:14:48 2007
@@ -33,10 +33,10 @@
* given xml element.
* Domain Policy authors write custom AssertionBuilders to build Assertions
for
* domain specific assertions.
- * Note that assertions can include nested policy expressions. To do so, they
can
- * obtain other AssertionBuilders by accessing the AssertionBuilderRegistry as
a
- * Bus extension, hence the registry is not passes as an argument here.
- *
+ * Note that assertions can include nested policy expressions. To build these,
+ * it may be necessary to obtain other AssertionBuilders.
+ * Concrete implementations should access the AssertionBuilderRegistry as a
+ * Bus extension, so the registry need not passed as an argument here.
*/
public interface AssertionBuilder {
@@ -56,4 +56,11 @@
* @return collection of QNames of known schema types
*/
Collection<QName> getKnownElements();
+
+
+ /**
+ * Returns a new assertion that is compatible with the two specified
+ * assertions or null if no compatible assertion can be built.
+ */
+ Assertion buildCompatible(Assertion a, Assertion b);
}
Added:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java?view=auto&rev=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
(added)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
Wed Mar 28 00:14:48 2007
@@ -0,0 +1,163 @@
+/**
+ * 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.ws.policy;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.neethi.All;
+import org.apache.neethi.Assertion;
+import org.apache.neethi.ExactlyOne;
+import org.apache.neethi.Policy;
+
+/**
+ * This class contains methods dealing with policy intersection.
+ * Intersection of two assertions, i.e. computation if a compatible assertion,
+ * is domain specific and relies on AssertionBuilder.buildCompatible.
+ * See Section 4.5 in http://www.w3.org/TR/2006/WD-ws-policy-20061117.
+ */
+public class Intersector {
+
+ private AssertionBuilderRegistry assertionBuilderRegistry;
+ private boolean strict;
+
+ public Intersector(AssertionBuilderRegistry abr) {
+ assertionBuilderRegistry = abr;
+ strict = true;
+ }
+
+ public boolean isStrict() {
+ return strict;
+ }
+
+ public void setStrict(boolean s) {
+ strict = s;
+ }
+
+ boolean compatibleAssertions(Assertion a1, Assertion a2) {
+ AssertionBuilder ab = assertionBuilderRegistry.get(a1.getName());
+ if (null == ab) {
+ return false;
+ }
+ return null != ab.buildCompatible(a1, a2);
+ }
+
+ boolean compatibleAlternatives(Collection<Assertion> alt1,
Collection<Assertion> alt2) {
+ if (alt1.isEmpty() || alt2.isEmpty()) {
+ return true;
+ }
+ if (strict) {
+ for (Assertion a1 : alt1) {
+ if (null == findCompatibleAssertion(a1, alt2)) {
+ return false;
+ }
+ }
+ for (Assertion a2 : alt2) {
+ if (null == findCompatibleAssertion(a2, alt1)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ // Lax intersection not supported as neethi does not support Ignorable
yet.
+ throw new UnsupportedOperationException("Lax intersection of
assertions is not supported "
+ + "because the Ignorable
attribute is not supported.");
+ }
+
+ boolean compatiblePolicies(Policy p1, Policy p2) {
+ Iterator i1 = p1.getAlternatives();
+ while (i1.hasNext()) {
+ Collection<Assertion> alt1 = CastUtils.cast((Collection)i1.next(),
Assertion.class);
+ Iterator i2 = p2.getAlternatives();
+ while (i2.hasNext()) {
+ Collection<Assertion> alt2 =
CastUtils.cast((Collection)i2.next(), Assertion.class);
+ if (compatibleAlternatives(alt1, alt2)) {
+ return true;
+ }
+ }
+ System.out.println("p1 and p2 are incompatible because p2 has no
alternative compatible with "
+ + alt1);
+ return false;
+ }
+ return true;
+ }
+
+ public Assertion intersect(Assertion a1, Assertion a2) {
+ AssertionBuilder ab = assertionBuilderRegistry.get(a1.getName());
+ if (null == ab) {
+ return null;
+ }
+ return ab.buildCompatible(a1, a2);
+ }
+
+ public Collection<Assertion> intersect(Collection<Assertion> alt1,
+ Collection<Assertion>
alt2) {
+ if (!compatibleAlternatives(alt1, alt2)) {
+ return null;
+ }
+ Collection<Assertion> intersection = new ArrayList<Assertion>();
+ intersection.addAll(alt1);
+ intersection.addAll(alt2);
+ return intersection;
+ }
+
+ public Policy intersect(Policy p1, Policy p2) {
+ if (!compatiblePolicies(p1, p2)) {
+ return null;
+ }
+
+ Policy compatible = new Policy();
+ ExactlyOne eo = new ExactlyOne();
+
+ Iterator i1 = p1.getAlternatives();
+ while (i1.hasNext()) {
+ List<Assertion> alt1 = CastUtils.cast((List)i1.next(),
Assertion.class);
+ Iterator i2 = p2.getAlternatives();
+ while (i2.hasNext()) {
+ List<Assertion> alt2 = CastUtils.cast((List)i2.next(),
Assertion.class);
+ if (compatibleAlternatives(alt1, alt2)) {
+ All all = new All();
+ all.addPolicyComponents(alt1);
+ all.addPolicyComponents(alt2);
+ eo.addPolicyComponent(all);
+ }
+ }
+ }
+
+ if (!eo.isEmpty()) {
+ compatible.addPolicyComponent(eo);
+ }
+
+ return compatible;
+ }
+
+ private Assertion findCompatibleAssertion(Assertion assertion,
Collection<Assertion> alt) {
+ for (Assertion a : alt) {
+ Assertion compatible = intersect(assertion, a);
+ if (null != compatible) {
+ return compatible;
+ }
+ }
+ return null;
+ }
+}
Propchange:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/Intersector.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertion.java
Wed Mar 28 00:14:48 2007
@@ -22,8 +22,8 @@
import javax.xml.namespace.QName;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
-
import org.apache.neethi.Assertion;
+import org.apache.neethi.PolicyComponent;
/**
@@ -48,6 +48,15 @@
return data;
}
+ @Override
+ public boolean equal(PolicyComponent policyComponent) {
+ if (!super.equal(policyComponent)) {
+ return false;
+ }
+ JaxbAssertion<T> other = (JaxbAssertion<T>)policyComponent;
+ return data.equals(other.data);
+ }
+
protected Assertion cloneMandatory() {
JaxbAssertion<T> a = new JaxbAssertion<T>(getName(), false);
a.setData(data);
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
Wed Mar 28 00:14:48 2007
@@ -119,4 +119,10 @@
return supportedTypes;
}
+ public Assertion buildCompatible(Assertion a, Assertion b) {
+ return null;
+ }
+
+
+
}
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertion.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertion.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertion.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertion.java
Wed Mar 28 00:14:48 2007
@@ -93,11 +93,15 @@
Iterator alternatives = normalisedNested.getAlternatives();
while (alternatives.hasNext()) {
All all = new All();
- all.addPolicyComponent(super.cloneMandatory());
List<Assertion> alternative =
CastUtils.cast((List)alternatives.next(), Assertion.class);
NestedPrimitiveAssertion a = new
NestedPrimitiveAssertion(getName(), false);
a.nested = new Policy();
- a.nested.addPolicyComponents(alternative);
+ ExactlyOne nea = new ExactlyOne();
+ a.nested.addPolicyComponent(nea);
+ All na = new All();
+ nea.addPolicyComponent(na);
+ na.addPolicyComponents(alternative);
+ all.addPolicyComponent(a);
ea.addPolicyComponent(all);
}
return p;
@@ -106,4 +110,19 @@
public Policy getNested() {
return nested;
}
+
+ @Override
+ public boolean equal(PolicyComponent policyComponent) {
+ if (!super.equal(policyComponent)) {
+ return false;
+ }
+ NestedPrimitiveAssertion other =
(NestedPrimitiveAssertion)policyComponent;
+ return getNested().equal(other.getNested());
+ }
+
+ protected void setNested(Policy n) {
+ nested = n;
+ }
+
+
}
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilder.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilder.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilder.java
Wed Mar 28 00:14:48 2007
@@ -21,22 +21,62 @@
import org.w3c.dom.Element;
+import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
+import org.apache.cxf.ws.policy.Intersector;
import org.apache.cxf.ws.policy.PolicyBuilder;
import org.apache.neethi.Assertion;
+import org.apache.neethi.Policy;
public class NestedPrimitiveAssertionBuilder extends PrimitiveAssertionBuilder
{
private PolicyBuilder builder;
+ private AssertionBuilderRegistry assertionBuilderRegistry;
public void setPolicyBuilder(PolicyBuilder b) {
builder = b;
}
+ public void setAssertionBuilderRegistry(AssertionBuilderRegistry abr) {
+ assertionBuilderRegistry = abr;
+ }
+
@Override
public Assertion build(Element elem) {
return new NestedPrimitiveAssertion(elem, builder);
}
+
+ @Override
+ /**
+ * If the nested policies in both assertions are empty, the compatible
policy
+ * .
+ * The compatible policy is optional iff both assertions are optional.
+ */
+ public Assertion buildCompatible(Assertion a, Assertion b) {
+ if (!getKnownElements().contains(a.getName()) ||
!a.getName().equals(b.getName())) {
+ return null;
+ }
+
+ if (null == assertionBuilderRegistry) {
+ return null;
+ }
+
+
+ NestedPrimitiveAssertion na = (NestedPrimitiveAssertion)a;
+ NestedPrimitiveAssertion nb = (NestedPrimitiveAssertion)b;
+
+ Intersector intersector = new Intersector(assertionBuilderRegistry);
+
+ Policy nested = intersector.intersect(na.getNested(), nb.getNested());
+ if (null == nested) {
+ return null;
+ }
+
+ NestedPrimitiveAssertion compatible =
+ new NestedPrimitiveAssertion(a.getName(), a.isOptional() &&
b.isOptional());
+ compatible.setNested(nested);
+
+ return compatible;
+ }
-
}
Modified:
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/PrimitiveAssertionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/PrimitiveAssertionBuilder.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/PrimitiveAssertionBuilder.java
(original)
+++
incubator/cxf/trunk/api/src/main/java/org/apache/cxf/ws/policy/builder/primitive/PrimitiveAssertionBuilder.java
Wed Mar 28 00:14:48 2007
@@ -44,4 +44,15 @@
public void setKnownElements(Collection<QName> k) {
knownElements = k;
}
+
+ /**
+ * If the two assertions are equal, they are also compatible.
+ * The compatible policy is optional iff both assertions are optional.
+ */
+ public Assertion buildCompatible(Assertion a, Assertion b) {
+ if (knownElements.contains(a.getName()) &&
a.getName().equals(b.getName())) {
+ return new PrimitiveAssertion(a.getName(), a.isOptional() &&
b.isOptional());
+ }
+ return null;
+ }
}
Added:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java?view=auto&rev=523198
==============================================================================
---
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
(added)
+++
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
Wed Mar 28 00:14:48 2007
@@ -0,0 +1,83 @@
+/**
+ * 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.ws.policy;
+
+import java.util.Collections;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
+import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
+import org.apache.neethi.Policy;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class IntersectorTest extends Assert {
+
+ private static final QName NAME1 = new QName("http://x.y.z", "a");
+ private static final QName NAME2 = new QName("http://x.y.z", "a");
+
+ private IMocksControl control = EasyMock.createNiceControl();
+ private Intersector intersector;
+ private AssertionBuilderRegistry reg;
+ private PrimitiveAssertionBuilder pab1;
+ private PrimitiveAssertionBuilder pab2;
+
+ @Before
+ public void setUp() {
+ reg = control.createMock(AssertionBuilderRegistry.class);
+ intersector = new Intersector(reg);
+ pab1 = new PrimitiveAssertionBuilder();
+ pab1.setKnownElements(Collections.singleton(NAME1));
+ pab2 = new PrimitiveAssertionBuilder();
+ pab2.setKnownElements(Collections.singleton(NAME2));
+ }
+
+ @Test
+ public void testCompatiblePoliciesBothEmpty() {
+ Policy p1 = new Policy();
+ Policy p2 = new Policy();
+ assertTrue(intersector.compatiblePolicies(p1, p2));
+ }
+
+ @Test
+ public void testCompatiblePoliciesOneEmpty() {
+ Policy p1 = new Policy();
+ Policy p2 = new Policy();
+ p2.addPolicyComponent(new PrimitiveAssertion(NAME1));
+ assertTrue(intersector.compatiblePolicies(p1, p2));
+ }
+
+ @Test
+ public void testIntersectPoliciesBothEmpty() {
+ Policy p1 = new Policy();
+ Policy p2 = new Policy();
+ Policy p = intersector.intersect(p1, p2);
+ assertNotNull(p);
+ // control.replay();
+ }
+
+}
Propchange:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/IntersectorTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionTest.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionTest.java
(original)
+++
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionTest.java
Wed Mar 28 00:14:48 2007
@@ -93,7 +93,7 @@
JaxbAssertion<FooType> oassertion = new JaxbAssertion<FooType>();
oassertion.setData(odata);
oassertion.setName(qn);
- assertTrue(!assertion.equal(xpa));
+ assertTrue(!assertion.equal(oassertion));
oassertion.setData(data);
assertTrue(assertion.equal(assertion));
Modified:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilderTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilderTest.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilderTest.java
(original)
+++
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionBuilderTest.java
Wed Mar 28 00:14:48 2007
@@ -24,14 +24,14 @@
import java.util.Collections;
import javax.xml.namespace.QName;
-
import org.w3c.dom.Element;
import junit.framework.TestCase;
-
import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
import org.apache.cxf.ws.policy.PolicyBuilder;
import org.apache.cxf.ws.policy.PolicyException;
+import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;
import org.easymock.classextension.EasyMock;
import org.easymock.classextension.IMocksControl;
@@ -44,17 +44,22 @@
private static final String TEST_NAMESPACE =
"http://www.w3.org/2007/01/addressing/metadata";
private static final QName TEST_NAME1 = new QName(TEST_NAMESPACE,
"Addressing");
+ private static final QName TEST_NAME2 = new QName(TEST_NAMESPACE,
"AnonymousResponses");
+ private static final QName TEST_NAME3 = new QName(TEST_NAMESPACE,
"NonAnonymousResponses");
private NestedPrimitiveAssertionBuilder npab;
private IMocksControl control;
private PolicyBuilder builder;
+ private AssertionBuilderRegistry reg;
public void setUp() {
control = EasyMock.createNiceControl();
npab = new NestedPrimitiveAssertionBuilder();
npab.setKnownElements(Collections.singletonList(TEST_NAME1));
builder = control.createMock(PolicyBuilder.class);
- npab.setPolicyBuilder(builder);
+ npab.setPolicyBuilder(builder);
+ reg = control.createMock(AssertionBuilderRegistry.class);
+ npab.setAssertionBuilderRegistry(reg);
}
public void tearDown() {
@@ -89,6 +94,67 @@
assertSame(nested, npc.getNested());
assertTrue(npc.isOptional());
control.verify();
+ }
+
+ public void testBuildCompatibleNoRegistry() {
+ npab.setAssertionBuilderRegistry(null);
+ Policy[] policies = NestedPrimitiveAssertionTest.buildTestPolicies();
+ Assertion a = (Assertion)policies[4].getFirstPolicyComponent();
+ assertNull("Should not have been able to build compatible policy.",
npab.buildCompatible(a, a));
+ }
+
+ public void testCompatibleWithSelf() {
+ Policy[] policies = NestedPrimitiveAssertionTest.buildTestPolicies();
+ EasyMock.expect(reg.get(TEST_NAME1)).andReturn(npab).anyTimes();
+ PrimitiveAssertionBuilder ab1 = new PrimitiveAssertionBuilder();
+ ab1.setKnownElements(Collections.singleton(TEST_NAME2));
+ PrimitiveAssertionBuilder ab2 = new PrimitiveAssertionBuilder();
+ ab2.setKnownElements(Collections.singleton(TEST_NAME3));
+ EasyMock.expect(reg.get(TEST_NAME2)).andReturn(ab1).anyTimes();
+ EasyMock.expect(reg.get(TEST_NAME3)).andReturn(ab2).anyTimes();
+
+ control.replay();
+ Assertion a = (Assertion)policies[2].getFirstPolicyComponent();
+ Assertion compatible = npab.buildCompatible(a, a);
+ assertNotNull("assertion in policy 2 should be compatible with
itself.", compatible);
+ control.verify();
+ }
+
+
+ public void testBuildCompatible() {
+ Policy[] policies = NestedPrimitiveAssertionTest.buildTestPolicies();
+ EasyMock.expect(reg.get(TEST_NAME1)).andReturn(npab).anyTimes();
+ PrimitiveAssertionBuilder ab1 = new PrimitiveAssertionBuilder();
+ ab1.setKnownElements(Collections.singleton(TEST_NAME2));
+ PrimitiveAssertionBuilder ab2 = new PrimitiveAssertionBuilder();
+ ab2.setKnownElements(Collections.singleton(TEST_NAME3));
+ EasyMock.expect(reg.get(TEST_NAME2)).andReturn(ab1).anyTimes();
+ EasyMock.expect(reg.get(TEST_NAME3)).andReturn(ab2).anyTimes();
+
+ control.replay();
+ for (int i = 0; i < policies.length; i++) {
+ Assertion a = (Assertion)policies[i].getFirstPolicyComponent();
+ Assertion compatible = npab.buildCompatible(a, a);
+ assertNotNull("assertion in policy " + i + " should be compatible
with itself.", compatible);
+ }
+
+ for (int i = 1; i < 5; i++) {
+ Assertion a = (Assertion)policies[0].getFirstPolicyComponent();
+ Assertion b = (Assertion)policies[i].getFirstPolicyComponent();
+ Assertion compatible = npab.buildCompatible(a, b);
+ assertNotNull("assertion in policy 0 should be compatible with
assertion in policy " + i + ".",
+ compatible);
+ }
+
+ for (int i = 2; i < 5; i++) {
+ Assertion a = (Assertion)policies[1].getFirstPolicyComponent();
+ Assertion b = (Assertion)policies[i].getFirstPolicyComponent();
+ Assertion compatible = npab.buildCompatible(a, b);
+ assertNotNull("assertion in policy " + 1 + " should be compatible
with assertion in policy i.",
+ compatible);
+ }
+ control.verify();
+
}
Element getElement(String data) throws Exception {
Added:
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java?view=auto&rev=523198
==============================================================================
---
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
(added)
+++
incubator/cxf/trunk/api/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
Wed Mar 28 00:14:48 2007
@@ -0,0 +1,111 @@
+/**
+ * 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.ws.policy.builder.primitive;
+
+import javax.xml.namespace.QName;
+
+import org.apache.neethi.Assertion;
+import org.apache.neethi.ExactlyOne;
+import org.apache.neethi.Policy;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ *
+ */
+public class NestedPrimitiveAssertionTest extends Assert {
+
+ private static final String TEST_NAMESPACE =
"http://www.w3.org/2007/01/addressing/metadata";
+ private static final QName TEST_NAME1 = new QName(TEST_NAMESPACE,
"Addressing");
+ private static final QName TEST_NAME2 = new QName(TEST_NAMESPACE,
"AnonymousResponses");
+ private static final QName TEST_NAME3 = new QName(TEST_NAMESPACE,
"NonAnonymousResponses");
+
+ private Policy[] policies;
+
+
+ @Before
+ public void setUp() {
+ policies = buildTestPolicies();
+ }
+
+ @Test
+ public void testEqual() {
+ Assertion other = new PrimitiveAssertion(new QName("abc"));
+ for (int i = 0; i < policies.length; i++) {
+ Assertion a = (Assertion)policies[i].getFirstPolicyComponent();
+ assertTrue("Assertion " + i + " should equal itself.",
a.equal(a));
+ assertTrue("Assertion " + i + " should not equal other.",
!a.equal(other));
+ for (int j = i + 1; j < policies.length; j++) {
+ Assertion b = (Assertion)policies[j].getFirstPolicyComponent();
+ if (j == 1) {
+ assertTrue("Assertion " + i + " should equal " + j + ".",
a.equal(b));
+ } else {
+ assertTrue("Assertion " + i + " unexpectedly equals
assertion " + j + ".", !a.equal(b));
+ }
+ }
+ }
+ }
+
+ protected static Policy[] buildTestPolicies() {
+ Policy[] p = new Policy[5];
+ int i = 0;
+
+ p[i] = new Policy();
+ NestedPrimitiveAssertion a = new NestedPrimitiveAssertion(TEST_NAME1,
true);
+ Policy nested = new Policy();
+ a.setNested(nested);
+ p[i++].addPolicyComponent(a);
+
+ p[i] = new Policy();
+ a = new NestedPrimitiveAssertion(TEST_NAME1, false);
+ nested = new Policy();
+ a.setNested(nested);
+ p[i++].addPolicyComponent(a);
+
+ p[i] = new Policy();
+ a = new NestedPrimitiveAssertion(TEST_NAME1, false);
+ nested = new Policy();
+ a.setNested(nested);
+ nested.addPolicyComponent(new PrimitiveAssertion(TEST_NAME2, true));
+ nested.addPolicyComponent(new PrimitiveAssertion(TEST_NAME3, true));
+ p[i++].addPolicyComponent(a);
+
+ p[i] = new Policy();
+ a = new NestedPrimitiveAssertion(TEST_NAME1, false);
+ nested = new Policy();
+ a.setNested(nested);
+ ExactlyOne eo = new ExactlyOne();
+ nested.addPolicyComponent(eo);
+ eo.addPolicyComponent(new PrimitiveAssertion(TEST_NAME2));
+ eo.addPolicyComponent(new PrimitiveAssertion(TEST_NAME3));
+ p[i++].addPolicyComponent(a);
+
+ p[i] = new Policy();
+ a = new NestedPrimitiveAssertion(TEST_NAME1, false);
+ nested = new Policy();
+ a.setNested(nested);
+ nested.addPolicyComponent(new PrimitiveAssertion(TEST_NAME3));
+ p[i++].addPolicyComponent(a);
+
+ return p;
+ }
+}
Modified:
incubator/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/policy/AddressingAssertionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/policy/AddressingAssertionBuilder.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/policy/AddressingAssertionBuilder.java
(original)
+++
incubator/cxf/trunk/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/policy/AddressingAssertionBuilder.java
Wed Mar 28 00:14:48 2007
@@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import javax.xml.namespace.QName;
@@ -29,10 +30,13 @@
import org.apache.cxf.Bus;
import org.apache.cxf.ws.policy.AssertionBuilder;
+import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
import org.apache.cxf.ws.policy.PolicyBuilder;
import org.apache.cxf.ws.policy.PolicyConstants;
import org.apache.cxf.ws.policy.builder.primitive.NestedPrimitiveAssertion;
+import
org.apache.cxf.ws.policy.builder.primitive.NestedPrimitiveAssertionBuilder;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
+import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
import org.apache.neethi.Assertion;
/**
@@ -82,6 +86,21 @@
return KNOWN;
}
-
+ public Assertion buildCompatible(Assertion a, Assertion b) {
+ QName qn = a.getName();
+ if (MetadataConstants.ADDRESSING_ASSERTION_QNAME.equals(qn)) {
+ NestedPrimitiveAssertionBuilder npab = new
NestedPrimitiveAssertionBuilder();
+
npab.setKnownElements(Collections.singleton(MetadataConstants.ADDRESSING_ASSERTION_QNAME));
+
npab.setAssertionBuilderRegistry(bus.getExtension(AssertionBuilderRegistry.class));
+ return npab.buildCompatible(a, b);
+ } else if (MetadataConstants.ANON_RESPONSES_ASSERTION_QNAME.equals(qn)
+ ||
MetadataConstants.NON_ANON_RESPONSES_ASSERTION_QNAME.equals(qn)) {
+
+ PrimitiveAssertionBuilder pab = new PrimitiveAssertionBuilder();
+ pab.setKnownElements(Collections.singleton(qn));
+ return pab.buildCompatible(a, b);
+ }
+ return null;
+ }
}
Modified:
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/PolicyExtensionsTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/PolicyExtensionsTest.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/PolicyExtensionsTest.java
(original)
+++
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/PolicyExtensionsTest.java
Wed Mar 28 00:14:48 2007
@@ -121,7 +121,10 @@
public Collection<QName> getKnownElements() {
return knownElements;
- }
+ }
+ public Assertion buildCompatible(Assertion a, Assertion b) {
+ return null;
+ }
}
public static class TestPolicyInterceptorProvider extends
AbstractAttributedInterceptorProvider
Modified:
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java?view=diff&rev=523198&r1=523197&r2=523198
==============================================================================
---
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
(original)
+++
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/builder/primitive/NestedPrimitiveAssertionTest.java
Wed Mar 28 00:14:48 2007
@@ -149,7 +149,11 @@
assertTrue("Normalised version of policy defined in compact" + i
+ ".xml does not match expected version defined in
normalised" + i + ".xml",
PolicyComparator.compare(expectedNormalisedPolicy,
normalisedPolicy));
-
- }
+ }
+ }
+
+ @Test
+ public void testCompatible() {
+
}
}