01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.ws.policy.builder.jaxb;
19:
20: import java.io.InputStream;
21:
22: import javax.xml.namespace.QName;
23:
24: import org.w3c.dom.Document;
25: import org.w3c.dom.Element;
26:
27: import org.apache.cxf.helpers.DOMUtils;
28: import org.apache.cxf.test.assertions.foo.FooType;
29: import org.apache.neethi.Assertion;
30: import org.junit.Assert;
31: import org.junit.Test;
32:
33: /**
34: *
35: */
36: public class JaxbAssertionBuilderTest extends Assert {
37:
38: @Test
39: public void testConstructors() throws Exception {
40: QName qn = new QName(
41: "http://cxf.apache.org/test/assertions/foo", "FooType");
42: try {
43: new JaxbAssertionBuilder(
44: "org.apache.cxf.test.assertions.foo.UnknownType",
45: qn);
46: fail("Expected ClassNotFoundException not thrown.");
47: } catch (ClassNotFoundException ex) {
48: // expected
49: }
50: assertNotNull(new JaxbAssertionBuilder(qn));
51: assertNotNull(new JaxbAssertionBuilder(FooType.class.getName(),
52: qn));
53: assertNotNull(new JaxbAssertionBuilder<FooType>(FooType.class,
54: qn));
55: }
56:
57: @Test
58: public void testGetKnownElements() throws Exception {
59: QName qn = new QName(
60: "http://cxf.apache.org/test/assertions/foo", "FooType");
61: JaxbAssertionBuilder<FooType> ab = new JaxbAssertionBuilder<FooType>(
62: FooType.class, qn);
63: assertNotNull(ab);
64: assertEquals(1, ab.getKnownElements().size());
65: assertSame(qn, ab.getKnownElements().iterator().next());
66: }
67:
68: @Test
69: @SuppressWarnings("unchecked")
70: public void testBuild() throws Exception {
71: QName qn = new QName(
72: "http://cxf.apache.org/test/assertions/foo", "FooType");
73: JaxbAssertionBuilder<FooType> ab = new JaxbAssertionBuilder<FooType>(
74: FooType.class, qn);
75: assertNotNull(ab);
76: InputStream is = JaxbAssertionBuilderTest.class
77: .getResourceAsStream("foo.xml");
78: Document doc = DOMUtils.readXml(is);
79: Element elem = (Element) doc.getDocumentElement()
80: .getElementsByTagNameNS(
81: "http://cxf.apache.org/test/assertions/foo",
82: "foo").item(0);
83: Assertion a = ab.build(elem);
84: JaxbAssertion<FooType> jba = JaxbAssertion.cast(a,
85: FooType.class);
86: FooType foo = jba.getData();
87: assertEquals("CXF", foo.getName());
88: assertEquals(2, foo.getNumber().intValue());
89: }
90: }
|