1 17 package org.apache.ws.jaxme.junit; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 import java.util.HashMap ; 22 import java.util.Map ; 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 ; 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 ; 39 40 41 43 public class WildcardTest extends BaseTestCase { 44 public WildcardTest(String 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 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 sw = new StringWriter (); 57 marshaller.marshal(pElement, sw); 58 return sw.toString(); 59 } 60 61 protected String getMarshalledAnyAttribute() throws JAXBException { 62 ObjectFactory objectFactory = new ObjectFactory(); 63 AnyAttribute anyAttribute = objectFactory.createAnyAttribute(); 64 anyAttribute.setAnyAttribute(new QName ("foo", "bar"), "value 1"); 65 anyAttribute.setAnyAttribute(new QName ("baz"), "value 2"); 66 return asString(anyAttribute); 67 } 68 69 protected String getMarshalledListAttribute() throws JAXBException { 70 ObjectFactory objectFactory = new ObjectFactory(); 71 ListAttribute listAttribute = objectFactory.createListAttribute(); 72 listAttribute.setAnyAttribute(new QName ("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1"); 73 listAttribute.setAnyAttribute(new QName ("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2"); 74 return asString(listAttribute); 75 } 76 77 protected String getMarshalledOtherAttribute() throws JAXBException { 78 ObjectFactory objectFactory = new ObjectFactory(); 79 OtherAttribute otherAttribute = objectFactory.createOtherAttribute(); 80 otherAttribute.setAnyAttribute(new QName ("foo", "bar"), "value 1"); 81 otherAttribute.setAnyAttribute(new QName ("baz"), "value 2"); 82 return asString(otherAttribute); 83 } 84 85 protected Element getUnmarshalledElement(String pMarshalledElement) throws JAXBException { 86 Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller(); 87 return (Element) unmarshaller.unmarshal(new InputSource (new StringReader (pMarshalledElement))); 88 } 89 90 public void testMarshalAnyAttribute() throws Exception { 91 String 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 got = getMarshalledAnyAttribute(); 93 assertEquals(expect, got); 94 } 95 96 protected void assertEquals(WildcardAttribute[] pExpect, WildcardAttribute[] pGot) { 97 assertEquals(pExpect.length, pGot.length); 98 Map mapGot = new HashMap (); 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 value = (String ) 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 { 117 AnyAttribute anyAttribute = (AnyAttribute) getUnmarshalledElement(getMarshalledAnyAttribute()); 118 WildcardAttribute[] attrs = anyAttribute.getAnyAttributeArray(); 119 assertEquals(new WildcardAttribute[]{ 120 new WildcardAttribute(new QName ("foo", "bar"), "value 1"), 121 new WildcardAttribute(new QName ("baz"), "value 2"), 122 }, attrs); 123 } 124 125 public void testMarshalListAttribute() throws Exception { 126 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\"/>"; 127 String got = getMarshalledListAttribute(); 128 assertEquals(expect, got); 129 } 130 131 public void testUnmarshalListAttribute() throws Exception { 132 ListAttribute listAttribute = (ListAttribute) getUnmarshalledElement(getMarshalledListAttribute()); 133 WildcardAttribute[] attrs = listAttribute.getAnyAttributeArray(); 134 assertEquals(new WildcardAttribute[]{ 135 new WildcardAttribute(new QName ("http://ws.apache.org/jaxme/test/misc/wildcards/2", "foo"), "value 1"), 136 new WildcardAttribute(new QName ("http://ws.apache.org/jaxme/test/misc/wildcards", "bar"), "value 2") 137 }, attrs); 138 } 139 140 public void testMarshalOtherAttribute() throws Exception { 141 String 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 got = getMarshalledOtherAttribute(); 143 assertEquals(expect, got); 144 } 145 146 public void testUnmarshalOtherAttribute() throws Exception { 147 OtherAttribute otherAttribute = (OtherAttribute) getUnmarshalledElement(getMarshalledOtherAttribute()); 148 WildcardAttribute[] attrs = otherAttribute.getAnyAttributeArray(); 149 assertEquals(new WildcardAttribute[]{ 150 new WildcardAttribute(new QName ("foo", "bar"), "value 1"), 151 new WildcardAttribute(new QName ("baz"), "value 2") 152 }, attrs); 153 } 154 } 155 | Popular Tags |