1 16 17 package test.encoding; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 22 import javax.xml.namespace.QName ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.axis.Constants; 27 import org.apache.axis.MessageContext; 28 import org.apache.axis.utils.XMLUtils; 29 import org.apache.axis.encoding.SerializationContext; 30 import org.apache.axis.encoding.SerializationContext; 31 import org.apache.axis.encoding.TypeMapping; 32 import org.apache.axis.encoding.TypeMappingRegistry; 33 import org.apache.axis.encoding.ser.BeanDeserializerFactory; 34 import org.apache.axis.encoding.ser.BeanSerializer; 35 import org.apache.axis.encoding.ser.BeanSerializerFactory; 36 import org.apache.axis.server.AxisServer; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.NodeList ; 39 import org.xml.sax.InputSource ; 40 41 44 public class TestDerivatedBeanSerializer extends TestCase { 45 46 QName superTypeQName = new QName ("typeNS", "SuperBean"); 47 QName inheritedTypeQName = new QName ("typeNS", "DerivatedBean"); 48 49 50 StringWriter stringWriter; 51 SerializationContext context; 52 53 57 public TestDerivatedBeanSerializer(String arg0) { 58 super(arg0); 59 } 60 61 64 protected void setUp() throws Exception { 65 super.setUp(); 66 67 stringWriter = new StringWriter (); 69 MessageContext msgContext = new MessageContext(new AxisServer()); 70 context = new SerializationContext(stringWriter, msgContext); 71 72 TypeMappingRegistry reg = context.getTypeMappingRegistry(); 74 TypeMapping tm = (TypeMapping) reg.createTypeMapping(); 75 tm.setSupportedEncodings(new String [] {Constants.URI_DEFAULT_SOAP_ENC}); 76 reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm); 77 78 tm.register(SuperBean.class, superTypeQName, new BeanSerializerFactory(SuperBean.class,superTypeQName), new BeanDeserializerFactory(SuperBean.class,superTypeQName)); 79 tm.register(DerivatedBean.class, inheritedTypeQName, new BeanSerializerFactory(DerivatedBean.class,inheritedTypeQName), new BeanDeserializerFactory(DerivatedBean.class,inheritedTypeQName)); 80 } 81 82 83 95 116 117 131 public void testDerivatedBeanSerialize() throws Exception { 132 BeanSerializer ser = new BeanSerializer(DerivatedBean.class, inheritedTypeQName); 133 134 Object object = new DerivatedBean(); 135 ser.serialize(inheritedTypeQName,null,object,context); 136 137 138 String msgString = stringWriter.toString(); 140 StringReader reader = new StringReader (msgString); 141 Document doc = XMLUtils.newDocument(new InputSource (reader)); 142 143 NodeList nodes = doc.getFirstChild().getChildNodes(); 144 assertEquals("1st Attribute", "zero", nodes.item(0).getLocalName()); 145 assertEquals("2nd Attribute", "one", nodes.item(1).getLocalName()); 146 assertEquals("3rd Attribute", "two", nodes.item(2).getLocalName()); 147 assertEquals("4th Attribute", "three", nodes.item(3).getLocalName()); 148 assertEquals("First Attribute", "four", nodes.item(4).getLocalName()); 149 } 150 151 152 } 153 154 155 156 157 | Popular Tags |