Search in sources :

Example 6 with SimpleBean

use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.

the class BeanTest method testAttributeMapDifferentNS.

@Test
public void testAttributeMapDifferentNS() throws Exception {
    defaultContext();
    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("bean8.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    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, getContext());
    writer.close();
    writer.flush();
    bos.close();
    Document doc = StaxUtils.read(new ByteArrayInputStream(bos.toByteArray()));
    Element element = doc.getDocumentElement();
    addNamespace("b2", "urn:Bean2");
    assertValid("/b:root[@b2:bleh='bleh']", element);
    assertValid("/b:root[@b2:howdy='howdy']", element);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter) Document(org.w3c.dom.Document) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 7 with SimpleBean

use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.

the class BeanTest method testNullProperties.

@Test
public void testNullProperties() throws Exception {
    defaultContext();
    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 = writeObjectToElement(type, bean, getContext());
    assertInvalid("/b:root[@b:howdy]", element);
    assertValid("/b:root/b:bleh[@xsi:nil='true']", element);
    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);
    XmlSchemaComplexType stype = (XmlSchemaComplexType) schema.getTypeByName("bean");
    XmlSchemaSequence seq = (XmlSchemaSequence) stype.getParticle();
    boolean howdy = false;
    boolean bleh = false;
    for (int x = 0; x < seq.getItems().size(); x++) {
        XmlSchemaSequenceMember o = seq.getItems().get(x);
        if (o instanceof XmlSchemaElement) {
            XmlSchemaElement a = (XmlSchemaElement) o;
            if ("bleh".equals(a.getName())) {
                bleh = true;
            }
        }
    }
    for (int x = 0; x < stype.getAttributes().size(); x++) {
        XmlSchemaObject o = stype.getAttributes().get(x);
        if (o instanceof XmlSchemaAttribute) {
            XmlSchemaAttribute a = (XmlSchemaAttribute) o;
            if ("howdy".equals(a.getName())) {
                howdy = true;
            }
        }
    }
    assertTrue(howdy);
    assertTrue(bleh);
}
Also used : QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlSchema(org.apache.ws.commons.schema.XmlSchema) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaSequenceMember(org.apache.ws.commons.schema.XmlSchemaSequenceMember) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 8 with SimpleBean

use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.

the class StandaloneWriteTest method testBean.

@Test
public void testBean() throws Exception {
    context = new AegisContext();
    Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
    rootClasses.add(SimpleBean.class);
    context.setRootClasses(rootClasses);
    context.initialize();
    SimpleBean sb = new SimpleBean();
    sb.setCharacter('\u4000');
    sb.setHowdy("doody");
    AegisType sbType = context.getTypeMapping().getType(sb.getClass());
    AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
    StringWriter stringWriter = new StringWriter();
    XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
    writer.write(sb, new QName("urn:meow", "catnip"), false, xmlWriter, sbType);
    xmlWriter.close();
    String xml = stringWriter.toString();
    assertTrue(xml.contains("doody"));
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) AegisContext(org.apache.cxf.aegis.AegisContext) StringType(org.apache.cxf.aegis.type.basic.StringType) AegisType(org.apache.cxf.aegis.type.AegisType) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with SimpleBean

use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.

the class BeanTest method testBean.

@Test
public void testBean() throws Exception {
    defaultContext();
    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("bean1.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test reading with extra elements
    reader = new ElementReader(getResourceAsStream("bean2.xml"));
    bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // test <bleh/> element
    reader = new ElementReader(getResourceAsStream("bean7.xml"));
    bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    bean.setBleh("bleh");
    // Test writing
    Element element = writeObjectToElement(type, bean, getContext());
    assertValid("/b:root/b:bleh[text()='bleh']", element);
    assertValid("/b:root/b:howdy[text()='howdy']", element);
}
Also used : QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 10 with SimpleBean

use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.

the class BeanTest method testCharMappings.

@Test
public void testCharMappings() throws Exception {
    defaultContext();
    BeanType type = (BeanType) mapping.getTypeCreator().createType(SimpleBean.class);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);
    XmlSchemaComplexType btype = (XmlSchemaComplexType) schema.getTypeByName("SimpleBean");
    XmlSchemaSequence seq = (XmlSchemaSequence) btype.getParticle();
    boolean charok = false;
    for (int x = 0; x < seq.getItems().size(); x++) {
        XmlSchemaSequenceMember o = seq.getItems().get(x);
        if (o instanceof XmlSchemaElement) {
            XmlSchemaElement oe = (XmlSchemaElement) o;
            if ("character".equals(oe.getName())) {
                charok = true;
                assertNotNull(oe.getSchemaTypeName());
                assertTrue(oe.isNillable());
                assertEquals(CharacterAsStringType.CHARACTER_AS_STRING_TYPE_QNAME, oe.getSchemaTypeName());
            }
        }
    }
    assertTrue(charok);
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaSequenceMember(org.apache.ws.commons.schema.XmlSchemaSequenceMember) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Aggregations

SimpleBean (org.apache.cxf.aegis.services.SimpleBean)10 Test (org.junit.Test)10 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)8 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)8 XmlElement (javax.xml.bind.annotation.XmlElement)7 QName (javax.xml.namespace.QName)7 Element (org.w3c.dom.Element)7 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)6 XmlSchema (org.apache.ws.commons.schema.XmlSchema)4 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)4 AegisContext (org.apache.cxf.aegis.AegisContext)3 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)3 XmlSchemaSequenceMember (org.apache.ws.commons.schema.XmlSchemaSequenceMember)3 HashSet (java.util.HashSet)2 AegisType (org.apache.cxf.aegis.type.AegisType)2 XmlSchemaAttribute (org.apache.ws.commons.schema.XmlSchemaAttribute)2 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1