001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.policy.builder.jaxb;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.apache.cxf.helpers.CastUtils;
026: import org.apache.cxf.test.assertions.foo.FooType;
027: import org.apache.cxf.ws.policy.builder.xml.XmlPrimitiveAssertion;
028: import org.apache.neethi.All;
029: import org.apache.neethi.Constants;
030: import org.apache.neethi.ExactlyOne;
031: import org.apache.neethi.Policy;
032: import org.apache.neethi.PolicyComponent;
033: import org.easymock.classextension.EasyMock;
034: import org.easymock.classextension.IMocksControl;
035: import org.junit.Assert;
036: import org.junit.Test;
037:
038: /**
039: *
040: */
041: public class JaxbAssertionTest extends Assert {
042:
043: @Test
044: public void testBasic() {
045: JaxbAssertion<FooType> assertion = new JaxbAssertion<FooType>();
046: assertNull(assertion.getName());
047: assertNull(assertion.getData());
048: assertTrue(!assertion.isOptional());
049: assertEquals(Constants.TYPE_ASSERTION, assertion.getType());
050: FooType data = new FooType();
051: data.setName("CXF");
052: data.setNumber(2);
053: QName qn = new QName(
054: "http://cxf.apache.org/test/assertions/foo", "FooType");
055: assertion.setName(qn);
056: assertion.setData(data);
057: assertion.setOptional(true);
058: assertSame(qn, assertion.getName());
059: assertSame(data, assertion.getData());
060: assertTrue(assertion.isOptional());
061: assertEquals(Constants.TYPE_ASSERTION, assertion.getType());
062: }
063:
064: @Test
065: public void testEqual() {
066: JaxbAssertion<FooType> assertion = new JaxbAssertion<FooType>();
067: FooType data = new FooType();
068: data.setName("CXF");
069: data.setNumber(2);
070: QName qn = new QName(
071: "http://cxf.apache.org/test/assertions/foo", "FooType");
072: assertion.setName(qn);
073: assertion.setData(data);
074:
075: PolicyComponent pc = new Policy();
076: assertTrue(!assertion.equal(pc));
077: pc = (PolicyComponent) new All();
078: assertTrue(!assertion.equal(pc));
079: pc = (PolicyComponent) new ExactlyOne();
080: assertTrue(!assertion.equal(pc));
081:
082: IMocksControl ctrl = EasyMock.createNiceControl();
083: XmlPrimitiveAssertion xpa = ctrl
084: .createMock(XmlPrimitiveAssertion.class);
085: QName oqn = new QName(
086: "http://cxf.apache.org/test/assertions/blah",
087: "OtherType");
088: EasyMock.expect(xpa.getName()).andReturn(oqn);
089: EasyMock.expect(xpa.getType()).andReturn(
090: Constants.TYPE_ASSERTION);
091:
092: ctrl.replay();
093: assertTrue(!assertion.equal(xpa));
094: ctrl.verify();
095:
096: FooType odata = new FooType();
097: odata.setName(data.getName());
098: odata.setNumber(data.getNumber());
099: JaxbAssertion<FooType> oassertion = new JaxbAssertion<FooType>();
100: oassertion.setData(odata);
101: oassertion.setName(qn);
102: assertTrue(!assertion.equal(oassertion));
103: oassertion.setData(data);
104: assertTrue(assertion.equal(oassertion));
105: assertTrue(assertion.equal(assertion));
106: }
107:
108: @Test
109: public void testNormalise() {
110: JaxbAssertion<FooType> assertion = new JaxbAssertion<FooType>();
111: FooType data = new FooType();
112: data.setName("CXF");
113: data.setNumber(2);
114: QName qn = new QName(
115: "http://cxf.apache.org/test/assertions/foo", "FooType");
116: assertion.setName(qn);
117: assertion.setData(data);
118:
119: JaxbAssertion normalised = (JaxbAssertion) assertion
120: .normalize();
121: assertTrue(normalised.equal(assertion));
122: assertSame(assertion.getData(), normalised.getData());
123:
124: assertion.setOptional(true);
125: PolicyComponent pc = assertion.normalize();
126: assertEquals(Constants.TYPE_POLICY, pc.getType());
127: Policy p = (Policy) pc;
128: Iterator alternatives = p.getAlternatives();
129:
130: int total = 0;
131: for (int i = 0; i < 2; i++) {
132: List<PolicyComponent> pcs = CastUtils.cast(
133: (List<?>) (alternatives.next()),
134: PolicyComponent.class);
135: if (!pcs.isEmpty()) {
136: assertTrue(assertion.equal(pcs.get(0)));
137: total += pcs.size();
138: }
139: }
140: assertTrue(!alternatives.hasNext());
141: assertEquals(1, total);
142: }
143: }
|