use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonWithMultipleAttributes.
@Test
public void testToJsonWithMultipleAttributes() {
String xmlStr = "<person id = \"5\"><name cat = \"A\">Jack</name><age>40</age></person>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"person\":{\"@id\":\"5\",\"age\":\"40\",\"name\":{\"@cat\":\"A\",\"#text\":\"Jack\"}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonWithComplexObjectAttributes.
@Test
public void testToJsonWithComplexObjectAttributes() {
String xmlStr = "<bookStore status = \"online\"><storeName>foo</storeName><postalCode>94</postalCode>" + "<isOpen>true</isOpen><address><street>foo</street><city code = \"A\">94</city><country>true" + "</country></address><codes quality=\"b\"><item>4</item><item>8</item><item>9</item></codes>" + "</bookStore>\n";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"bookStore\":{\"@status\":\"online\",\"storeName\":\"foo\",\"postalCode\":\"94\"," + "\"isOpen\":\"true\",\"address\":{\"street\":\"foo\",\"country\":\"true\"," + "\"city\":{\"@code\":\"A\",\"#text\":\"94\"}},\"codes\":{\"@quality\":\"b\"," + "\"item\":[\"4\",\"8\",\"9\"]}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonForSimpleXML.
@Test
public void testToJsonForSimpleXML() {
String xmlStr = "<person><name>Jack</name><age>40</age></person>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"person\":{\"name\":\"Jack\",\"age\":\"40\"}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testCopy.
@Test
public void testCopy() {
BValue[] returns = BRunUtil.invoke(result, "testCopy");
Assert.assertEquals(returns.length, 4);
Assert.assertTrue(returns[0] instanceof BXMLItem);
Assert.assertEquals(returns[0].stringValue(), "<ns0:name xmlns:ns0=\"http://sample.com/test\"><newFname>" + "supun-new</newFname><newMname>thilina-new</newMname><newLname>setunga-new</newLname></ns0:name>");
// Check children of the copied xml
Assert.assertTrue(returns[3] instanceof BXML);
BRefValueArray children = ((BXMLSequence) ((BXML) returns[0]).children()).value();
Assert.assertEquals(children.size(), 3);
Assert.assertEquals(children.get(0).stringValue(), "<newFname>supun-new</newFname>");
Assert.assertEquals(children.get(1).stringValue(), "<newMname>thilina-new</newMname>");
Assert.assertEquals(children.get(2).stringValue(), "<newLname>setunga-new</newLname>");
// is children seq is empty?
Assert.assertSame(returns[1].getClass(), BBoolean.class);
Assert.assertEquals(((BBoolean) returns[1]).booleanValue(), false);
// is children seq is singleton?
Assert.assertSame(returns[2].getClass(), BBoolean.class);
Assert.assertEquals(((BBoolean) returns[2]).booleanValue(), true);
// Check children of the original xml
Assert.assertTrue(returns[3] instanceof BXML);
BRefValueArray originalChildren = ((BXMLSequence) returns[3]).value();
Assert.assertEquals(originalChildren.size(), 2);
Assert.assertEquals(originalChildren.get(0).stringValue(), "<fname xmlns:ns0=\"http://sample.com/test\">supun</fname>");
Assert.assertEquals(originalChildren.get(1).stringValue(), "<lname xmlns:ns0=\"http://sample.com/test\">setunga</lname>");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonForXMLWithThreeLevels.
@Test
public void testToJsonForXMLWithThreeLevels() {
String xmlStr = "<persons><person><test><name>Jack</name><address>wso2</address></test></person></persons>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"persons\":{\"person\":{\"test\":{\"name\":\"Jack\",\"address\":\"wso2\"}}}}");
}
Aggregations