Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xml toString() error for modified name space attributes #41541

Merged
merged 11 commits into from
Nov 2, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private String getXmlNsUriPrefix(Map<String, String> nsPrefixMap, String uri) {
}

private void writeAttributes(HashSet<String> curNSSet, Map<String, String> attributeMap) throws XMLStreamException {
String defaultNS = xmlStreamWriter.getNamespaceContext().getNamespaceURI(XMLNS);
String defaultNS = xmlStreamWriter.getNamespaceContext().getNamespaceURI("");
for (Map.Entry<String, String> attributeEntry : attributeMap.entrySet()) {
String key = attributeEntry.getKey();
int closingCurlyPos = key.lastIndexOf('}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -178,7 +179,7 @@ public void testObjectLevelXML() {
public void xmlWithDefaultNamespaceToString() {
Object returns = BRunUtil.invoke(literalWithNamespacesResult, "XMLWithDefaultNamespaceToString");
Assert.assertEquals(returns.toString(),
"<Order xmlns=\"http://acme.company\" xmlns:acme=\"http://acme.company\">\n" +
"<Order xmlns=\"http://acme.company\" xmlns:acme=\"http://acme.company.nondefault\">\n" +
" <OrderLines>\n" +
" <OrderLine acme:lineNo=\"334\" itemCode=\"334-2\"/>\n" +
" </OrderLines>\n" +
Expand All @@ -199,21 +200,25 @@ public void testXMLSerialize() {
"<foo xmlns=\"http://wso2.com/\">hello</foo>");
}

@Test
public void testXmlLiteralUsingXmlNamespacePrefix() {
BRunUtil.invoke(literalWithNamespacesResult, "testXmlLiteralUsingXmlNamespacePrefix");
}

@Test
public void testXMLToString() {
BXml xml = XmlFactory.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<!DOCTYPE foo [<!ELEMENT foo ANY ><!ENTITY data \"Example\" >]><foo>&data;</foo>");
Assert.assertEquals(xml.toString(), "<foo>Example</foo>");
}

@Test
public void testXmlInterpolationWithQuery() {
BRunUtil.invoke(literalWithNamespacesResult, "testXmlInterpolationWithQuery");
@Test (dataProvider = "xmlValueFunctions")
public void testXmlStrings(String functionName) {
BRunUtil.invoke(literalWithNamespacesResult, functionName);
}

@DataProvider(name = "xmlValueFunctions")
private String[] xmlValueFunctions() {
return new String[]{
"testXmlLiteralUsingXmlNamespacePrefix",
"testXmlInterpolationWithQuery",
"testAddAttributeToDefaultNS"
};
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function getXML() returns xml {
}

function XMLWithDefaultNamespaceToString() returns string {
xml x = xml `<Order xmlns="http://acme.company" xmlns:acme="http://acme.company">
xml x = xml `<Order xmlns="http://acme.company" xmlns:acme="http://acme.company.nondefault">
<OrderLines>
<OrderLine acme:lineNo="334" itemCode="334-2"></OrderLine>
</OrderLines>
Expand Down Expand Up @@ -294,3 +294,21 @@ function testXmlInterpolationWithQuery() returns error? {
}
}
}

function testAddAttributeToDefaultNS() {
xml x1 = xml `<root xmlns="http://sample.com/wso2/c1" xmlns:ns3="http://sample.com/wso2/f"></root>`;
var xAttr = let var x2 = <'xml:Element>x1 in x2.getAttributes();
//adding attribute with default namespace
xAttr["{http://sample.com/wso2/c1}foo1"] = "bar1";
string s = x1.toString();
string expectedStr = "<root xmlns=\"http://sample.com/wso2/c1\" xmlns:ns3=\"http://sample.com/wso2/f\" foo1=\"bar1\"/>";
HindujaB marked this conversation as resolved.
Show resolved Hide resolved
if (s != expectedStr) {
panic error("Assertion error", expected = expectedStr, found=s);
}
s = xAttr.toString();
expectedStr = "{\"{http://www.w3.org/2000/xmlns/}xmlns\":\"http://sample.com/wso2/c1\"," +
"\"{http://www.w3.org/2000/xmlns/}ns3\":\"http://sample.com/wso2/f\",\"{http://sample.com/wso2/c1}foo1\":\"bar1\"}";
HindujaB marked this conversation as resolved.
Show resolved Hide resolved
if (s != expectedStr) {
panic error("Assertion error", expected = expectedStr, found=s);
}
HindujaB marked this conversation as resolved.
Show resolved Hide resolved
}
HindujaB marked this conversation as resolved.
Show resolved Hide resolved
Loading