?? beantest.java
字號:
package org.codehaus.xfire.aegis.type.basic;import java.beans.PropertyDescriptor;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.Date;import javax.xml.namespace.QName;import org.codehaus.xfire.MessageContext;import org.codehaus.xfire.aegis.AegisBindingProvider;import org.codehaus.xfire.aegis.jdom.JDOMReader;import org.codehaus.xfire.aegis.jdom.JDOMWriter;import org.codehaus.xfire.aegis.stax.ElementReader;import org.codehaus.xfire.aegis.stax.ElementWriter;import org.codehaus.xfire.aegis.type.Configuration;import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;import org.codehaus.xfire.aegis.type.Type;import org.codehaus.xfire.aegis.type.TypeMapping;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.soap.SoapConstants;import org.codehaus.xfire.test.AbstractXFireTest;import org.codehaus.xfire.util.jdom.StaxBuilder;import org.jdom.Document;import org.jdom.Element;public class BeanTest extends AbstractXFireTest{ TypeMapping mapping; private DefaultTypeMappingRegistry reg; public void setUp() throws Exception { super.setUp(); addNamespace("b", "urn:Bean"); addNamespace("a", "urn:anotherns"); addNamespace("xsd", SoapConstants.XSD); addNamespace("xsi", SoapConstants.XSI_NS); reg = new DefaultTypeMappingRegistry(true); mapping = reg.createTypeMapping(true); } public void testBean() throws Exception { BeanType type = new BeanType(); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); // Test reading ElementReader reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean1.xml")); SimpleBean bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("bleh", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); // Test reading with extra elements reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean2.xml")); bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("bleh", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); // test <bleh/> element reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean7.xml")); bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); bean.setBleh("bleh"); // Test writing Element element = new Element("root", "b", "urn:Bean"); Document doc = new Document(element); type.writeObject(bean, new JDOMWriter(element), new MessageContext()); assertValid("/b:root/b:bleh[text()='bleh']", element); assertValid("/b:root/b:howdy[text()='howdy']", element); } public void testBeanWithXsiType() throws Exception { BeanType type = new BeanType(); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); // Test reading ElementReader reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean9.xml")); MessageContext ctx = new MessageContext(); Service s = new Service(); s.setProperty(AegisBindingProvider.READ_XSI_TYPE_KEY, "false"); ctx.setService(s); SimpleBean bean = (SimpleBean) type.readObject(reader, ctx); assertEquals("bleh", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); // Test writing Element element = new Element("root", "b", "urn:Bean"); Document doc = new Document(element); type.writeObject(bean, new JDOMWriter(element), new MessageContext()); assertValid("/b:root/b:bleh[text()='bleh']", element); assertValid("/b:root/b:howdy[text()='howdy']", element); } public void testUnmappedProperty() throws Exception { String ns = "urn:Bean"; BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, ns, false); QName name = new QName(ns, "howdycustom"); info.mapElement("howdy", name); info.setTypeMapping(mapping); assertEquals("howdy", info.getPropertyDescriptorFromMappedName(name).getName()); BeanType type = new BeanType(info); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); ElementReader reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean3.xml")); SimpleBean bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("howdy", bean.getHowdy()); assertNull(bean.getBleh()); reader.getXMLStreamReader().close(); // Test writing Element element = new Element("root", "b", "urn:Bean"); Document doc = new Document(element); type.writeObject(bean, new JDOMWriter(element), new MessageContext()); assertInvalid("/b:root/b:bleh", element); assertValid("/b:root/b:howdycustom[text()='howdy']", element); } public void testAttributeMap() throws Exception { BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean"); info.mapAttribute("howdy", new QName("urn:Bean", "howdy")); info.mapAttribute("bleh", new QName("urn:Bean", "bleh")); info.setTypeMapping(mapping); BeanType type = new BeanType(info); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); ElementReader reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean4.xml")); SimpleBean bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("bleh", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); // Test writing Element element = new Element("root", "b", "urn:Bean"); Document doc = new Document(element); type.writeObject(bean, new JDOMWriter(element), new MessageContext()); assertValid("/b:root[@b:bleh='bleh']", element); assertValid("/b:root[@b:howdy='howdy']", element); Element types = new Element("types", "xsd", SoapConstants.XSD); Element schema = new Element("schema", "xsd", SoapConstants.XSD); types.addContent(schema); doc = new Document(types); type.writeSchema(schema); assertValid("//xsd:complexType[@name='bean']/xsd:attribute[@name='howdy']", schema); assertValid("//xsd:complexType[@name='bean']/xsd:attribute[@name='bleh']", schema); } public void testAttributeMapDifferentNS() throws Exception { BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean"); info.mapAttribute("howdy", new QName("urn:Bean2", "howdy")); info.mapAttribute("bleh", new QName("urn:Bean2", "bleh")); info.setTypeMapping(mapping); BeanType type = new BeanType(info); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); ElementReader reader = new ElementReader(getResourceAsStream("/org/codehaus/xfire/aegis/type/basic/bean8.xml")); SimpleBean bean = (SimpleBean) type.readObject(reader, new MessageContext()); assertEquals("bleh", bean.getBleh()); assertEquals("howdy", bean.getHowdy()); reader.getXMLStreamReader().close(); // Test writing ByteArrayOutputStream bos = new ByteArrayOutputStream(); ElementWriter writer = new ElementWriter(bos, "root", "urn:Bean"); type.writeObject(bean, writer, new MessageContext()); writer.close(); writer.flush(); bos.close(); System.out.println(bos.toString()); StaxBuilder builder = new StaxBuilder(); Document doc = builder.build(new ByteArrayInputStream(bos.toByteArray())); Element element = doc.getRootElement(); addNamespace("b2", "urn:Bean2"); assertValid("/b:root[@b2:bleh='bleh']", element); assertValid("/b:root[@b2:howdy='howdy']", element); } public void testNullProperties() throws Exception { BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean"); info.setTypeMapping(mapping); info.mapAttribute("howdy", new QName("urn:Bean", "howdy")); info.mapElement("bleh", new QName("urn:Bean", "bleh")); BeanType type = new BeanType(info); type.setTypeClass(SimpleBean.class); type.setTypeMapping(mapping); type.setSchemaType(new QName("urn:Bean", "bean")); SimpleBean bean = new SimpleBean(); // Test writing Element element = new Element("root", "b", "urn:Bean"); Document doc = new Document(element); type.writeObject(bean, new JDOMWriter(element), new MessageContext()); assertInvalid("/b:root[@b:howdy]", element); assertValid("/b:root/b:bleh[@xsi:nil='true']", element); Element types = new Element("types", "xsd", SoapConstants.XSD); Element schema = new Element("schema", "xsd", SoapConstants.XSD); types.addContent(schema); doc = new Document(types);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -