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);
}
use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.
the class BeanTest method testByteMappings.
@Test
public void testByteMappings() 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 littleByteOk = false;
boolean bigByteOk = 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 ("littleByte".equals(oe.getName())) {
littleByteOk = true;
assertNotNull(oe.getSchemaTypeName());
assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
} else if ("bigByte".equals(oe.getName())) {
bigByteOk = true;
assertNotNull(oe.getSchemaTypeName());
assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
}
}
}
assertTrue(littleByteOk);
assertTrue(bigByteOk);
SimpleBean bean = new SimpleBean();
bean.setBigByte(Byte.valueOf((byte) 0xfe));
bean.setLittleByte((byte) 0xfd);
Element element = writeObjectToElement(type, bean, getContext());
Byte bb = Byte.valueOf((byte) 0xfe);
String bbs = bb.toString();
assertValid("/b:root/bz:bigByte[text()='" + bbs + "']", element);
// Test reading
ElementReader reader = new ElementReader(getResourceAsStream("byteBeans.xml"));
bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals(-5, bean.getLittleByte());
assertEquals(25, bean.getBigByte().byteValue());
reader.getXMLStreamReader().close();
}
use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.
the class BeanTest method testBeanWithXsiType.
@Test
public void testBeanWithXsiType() 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("bean9.xml"));
Context ctx = getContext();
ctx.getGlobalContext().setReadXsiTypes(false);
SimpleBean bean = (SimpleBean) type.readObject(reader, ctx);
assertEquals("bleh", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
Element element = writeObjectToElement(type, bean, getContext());
assertValid("/b:root/b:bleh[text()='bleh']", element);
assertValid("/b:root/b:howdy[text()='howdy']", element);
}
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);
}
use of org.apache.cxf.aegis.services.SimpleBean in project cxf by apache.
the class BeanTest method testAttributeMap.
@Test
public void testAttributeMap() throws Exception {
defaultContext();
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("bean4.xml"));
SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals("bleh", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
// Test writing
Element element = writeObjectToElement(type, bean, getContext());
assertValid("/b:root[@b:bleh='bleh']", element);
assertValid("/b:root[@b:howdy='howdy']", element);
XmlSchema schema = newXmlSchema("urn:Bean");
type.writeSchema(schema);
XmlSchemaComplexType stype = (XmlSchemaComplexType) schema.getTypeByName("bean");
boolean howdy = false;
boolean bleh = false;
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;
}
if ("bleh".equals(a.getName())) {
bleh = true;
}
}
}
assertTrue(howdy);
assertTrue(bleh);
}
Aggregations