/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.junit;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.Element;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import org.apache.ws.jaxme.WildcardAttribute;
import org.apache.ws.jaxme.test.misc.wildcards.AnyAttribute;
import org.apache.ws.jaxme.test.misc.wildcards.ListAttribute;
import org.apache.ws.jaxme.test.misc.wildcards.ObjectFactory;
import org.apache.ws.jaxme.test.misc.wildcards.OtherAttribute;
import org.xml.sax.InputSource;
/** <p>Test case for wildcard attributes and elements.</p>
*/
public class WildcardTest extends BaseTestCase {
public WildcardTest(String pName) {
super(pName);
}
protected JAXBContext getJAXBContext() throws JAXBException {
return JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.wildcards");
}
protected String asString(Element pElement) throws JAXBException {
Marshaller marshaller = getJAXBContext().createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(pElement, sw);
return sw.toString();
}
protected String getMarshalledAnyAttribute() throws JAXBException {
ObjectFactory objectFactory = new ObjectFactory();
AnyAttribute anyAttribute = objectFactory.createAnyAttribute();
anyAttribute.setAnyAttribute(new QName("foo", "bar"), "value 1");
anyAttribute.setAnyAttribute(new QName("baz"), "value 2");
return asString(anyAttribute);
}
protected String getMarshalledListAttribute() throws JAXBException {
ObjectFactory objectFactory = new ObjectFactory();
ListAttribute listAttribute = objectFactory.createListAttribute();
listAttribute.setAnyAttribute(new QName("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1");
listAttribute.setAnyAttribute(new QName("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2");
return asString(listAttribute);
}
protected String getMarshalledOtherAttribute() throws JAXBException {
ObjectFactory objectFactory = new ObjectFactory();
OtherAttribute otherAttribute = objectFactory.createOtherAttribute();
otherAttribute.setAnyAttribute(new QName("foo", "bar"), "value 1");
otherAttribute.setAnyAttribute(new QName("baz"), "value 2");
return asString(otherAttribute);
}
protected Element getUnmarshalledElement(String pMarshalledElement) throws JAXBException {
Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
return (Element) unmarshaller.unmarshal(new InputSource(new StringReader(pMarshalledElement)));
}
public void testMarshalAnyAttribute() throws Exception {
String expect = "<ex:AnyAttribute p:bar=\"value 1\" baz=\"value 2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/wildcards\" xmlns:p=\"foo\"/>";
String got = getMarshalledAnyAttribute();
assertStringEquals(expect, got);
}
protected void assertEquals(WildcardAttribute[] pExpect, WildcardAttribute[] pGot) {
assertEquals(pExpect.length, pGot.length);
Map mapGot = new HashMap();
for (int i = 0; i < pGot.length; i++) {
mapGot.put(pGot[i].getName(), pGot[i].getValue());
}
if (mapGot.size() < pGot.length) {
fail("Expected " + pGot.length + " elements in result Map, got " + mapGot.size());
}
for (int i = 0; i < pExpect.length; i++) {
WildcardAttribute wa = pExpect[i];
String value = (String) mapGot.get(wa.getName());
if (value == null) {
fail("Expected name " + wa.getName() + " in result Map.");
} else {
assertEquals(wa.getValue(), value);
}
}
}
public void testUnmarshalAnyAttribute() throws Exception {
AnyAttribute anyAttribute = (AnyAttribute) getUnmarshalledElement(getMarshalledAnyAttribute());
WildcardAttribute[] attrs = anyAttribute.getAnyAttributeArray();
assertEquals(new WildcardAttribute[]{
new WildcardAttribute(new QName("foo", "bar"), "value 1"),
new WildcardAttribute(new QName("baz"), "value 2"),
}, attrs);
}
public void testMarshalListAttribute() throws Exception {
String 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\"/>";
String got = getMarshalledListAttribute();
assertStringEquals(expect, got);
}
public void testUnmarshalListAttribute() throws Exception {
ListAttribute listAttribute = (ListAttribute) getUnmarshalledElement(getMarshalledListAttribute());
WildcardAttribute[] attrs = listAttribute.getAnyAttributeArray();
assertEquals(new WildcardAttribute[]{
new WildcardAttribute(new QName("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1"),
new WildcardAttribute(new QName("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2")
}, attrs);
}
public void testMarshalOtherAttribute() throws Exception {
String expect = "<ex:OtherAttribute p:bar=\"value 1\" baz=\"value 2\" xmlns:ex=\"http://ws.apache.org/jaxme/test/misc/wildcards\" xmlns:p=\"foo\"/>";
String got = getMarshalledOtherAttribute();
assertStringEquals(expect, got);
}
public void testUnmarshalOtherAttribute() throws Exception {
OtherAttribute otherAttribute = (OtherAttribute) getUnmarshalledElement(getMarshalledOtherAttribute());
WildcardAttribute[] attrs = otherAttribute.getAnyAttributeArray();
assertEquals(new WildcardAttribute[]{
new WildcardAttribute(new QName("foo", "bar"), "value 1"),
new WildcardAttribute(new QName("baz"), "value 2")
}, attrs);
}
}