KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > junit > WildcardTest


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.junit;
18
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.bind.Element;
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.Marshaller;
28 import javax.xml.bind.Unmarshaller;
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.apache.ws.jaxme.WildcardAttribute;
32 import org.apache.ws.jaxme.impl.JMMarshallerImpl;
33 import org.apache.ws.jaxme.impl.OrderedAttributeXMLWriter;
34 import org.apache.ws.jaxme.test.misc.wildcards.AnyAttribute;
35 import org.apache.ws.jaxme.test.misc.wildcards.ListAttribute;
36 import org.apache.ws.jaxme.test.misc.wildcards.ObjectFactory;
37 import org.apache.ws.jaxme.test.misc.wildcards.OtherAttribute;
38 import org.xml.sax.InputSource JavaDoc;
39
40
41 /** <p>Test case for wildcard attributes and elements.</p>
42  */

43 public class WildcardTest extends BaseTestCase {
44     public WildcardTest(String JavaDoc pName) {
45         super(pName);
46     }
47
48     protected JAXBContext getJAXBContext() throws JAXBException {
49         return JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.wildcards");
50     }
51
52     protected String JavaDoc asString(Element pElement) throws JAXBException {
53         Marshaller marshaller = getJAXBContext().createMarshaller();
54         marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
55         marshaller.setProperty(JMMarshallerImpl.JAXME_XML_WRITER, OrderedAttributeXMLWriter.class);
56         StringWriter JavaDoc sw = new StringWriter JavaDoc();
57         marshaller.marshal(pElement, sw);
58         return sw.toString();
59     }
60
61     protected String JavaDoc getMarshalledAnyAttribute() throws JAXBException {
62         ObjectFactory objectFactory = new ObjectFactory();
63         AnyAttribute anyAttribute = objectFactory.createAnyAttribute();
64         anyAttribute.setAnyAttribute(new QName JavaDoc("foo", "bar"), "value 1");
65         anyAttribute.setAnyAttribute(new QName JavaDoc("baz"), "value 2");
66         return asString(anyAttribute);
67     }
68
69     protected String JavaDoc getMarshalledListAttribute() throws JAXBException {
70         ObjectFactory objectFactory = new ObjectFactory();
71         ListAttribute listAttribute = objectFactory.createListAttribute();
72         listAttribute.setAnyAttribute(new QName JavaDoc("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1");
73         listAttribute.setAnyAttribute(new QName JavaDoc("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2");
74         return asString(listAttribute);
75     }
76
77     protected String JavaDoc getMarshalledOtherAttribute() throws JAXBException {
78         ObjectFactory objectFactory = new ObjectFactory();
79         OtherAttribute otherAttribute = objectFactory.createOtherAttribute();
80         otherAttribute.setAnyAttribute(new QName JavaDoc("foo", "bar"), "value 1");
81         otherAttribute.setAnyAttribute(new QName JavaDoc("baz"), "value 2");
82         return asString(otherAttribute);
83     }
84
85     protected Element getUnmarshalledElement(String JavaDoc pMarshalledElement) throws JAXBException {
86         Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
87         return (Element) unmarshaller.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(pMarshalledElement)));
88     }
89
90     public void testMarshalAnyAttribute() throws Exception JavaDoc {
91         String JavaDoc expect = "<ex:AnyAttribute p:bar=\"value 1\" baz=\"value 2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/wildcards\" xmlns:p=\"foo\"/>";
92         String JavaDoc got = getMarshalledAnyAttribute();
93         assertEquals(expect, got);
94     }
95
96     protected void assertEquals(WildcardAttribute[] pExpect, WildcardAttribute[] pGot) {
97         assertEquals(pExpect.length, pGot.length);
98         Map JavaDoc mapGot = new HashMap JavaDoc();
99         for (int i = 0; i < pGot.length; i++) {
100             mapGot.put(pGot[i].getName(), pGot[i].getValue());
101         }
102         if (mapGot.size() < pGot.length) {
103             fail("Expected " + pGot.length + " elements in result Map, got " + mapGot.size());
104         }
105         for (int i = 0; i < pExpect.length; i++) {
106             WildcardAttribute wa = pExpect[i];
107             String JavaDoc value = (String JavaDoc) mapGot.get(wa.getName());
108             if (value == null) {
109                 fail("Expected name " + wa.getName() + " in result Map.");
110             } else {
111                 assertEquals(wa.getValue(), value);
112             }
113         }
114     }
115
116     public void testUnmarshalAnyAttribute() throws Exception JavaDoc {
117         AnyAttribute anyAttribute = (AnyAttribute) getUnmarshalledElement(getMarshalledAnyAttribute());
118         WildcardAttribute[] attrs = anyAttribute.getAnyAttributeArray();
119         assertEquals(new WildcardAttribute[]{
120             new WildcardAttribute(new QName JavaDoc("foo", "bar"), "value 1"),
121             new WildcardAttribute(new QName JavaDoc("baz"), "value 2"),
122         }, attrs);
123     }
124
125     public void testMarshalListAttribute() throws Exception JavaDoc {
126         String JavaDoc expect = "<ex:ListAttribute p:foo=\"value 1\" ex:bar=\"value 2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/wildcards\" xmlns:p=\"http://ws.apache.org/jaxme/test/misc/wildcards/2\"/>";
127         String JavaDoc got = getMarshalledListAttribute();
128         assertEquals(expect, got);
129     }
130
131     public void testUnmarshalListAttribute() throws Exception JavaDoc {
132         ListAttribute listAttribute = (ListAttribute) getUnmarshalledElement(getMarshalledListAttribute());
133         WildcardAttribute[] attrs = listAttribute.getAnyAttributeArray();
134         assertEquals(new WildcardAttribute[]{
135             new WildcardAttribute(new QName JavaDoc("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1"),
136             new WildcardAttribute(new QName JavaDoc("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2")
137         }, attrs);
138     }
139
140     public void testMarshalOtherAttribute() throws Exception JavaDoc {
141         String JavaDoc expect = "<ex:OtherAttribute p:bar=\"value 1\" baz=\"value 2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/wildcards\" xmlns:p=\"foo\"/>";
142         String JavaDoc got = getMarshalledOtherAttribute();
143         assertEquals(expect, got);
144     }
145
146     public void testUnmarshalOtherAttribute() throws Exception JavaDoc {
147         OtherAttribute otherAttribute = (OtherAttribute) getUnmarshalledElement(getMarshalledOtherAttribute());
148         WildcardAttribute[] attrs = otherAttribute.getAnyAttributeArray();
149         assertEquals(new WildcardAttribute[]{
150             new WildcardAttribute(new QName JavaDoc("foo", "bar"), "value 1"),
151             new WildcardAttribute(new QName JavaDoc("baz"), "value 2")
152         }, attrs);
153     }
154 }
155
Popular Tags