/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ /** * Test XML handling functions. */ #include #include "stream.hpp" #include "string.hpp" #include "list.hpp" #include "value.hpp" #include "element.hpp" #include "xml.hpp" namespace tuscany { const string currencyXML = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " US\n" " \n" "\n"; const string customerXML = "\n" "\n" " jdoe\n" "
\n" " san francisco\n" " ca\n" "
\n" " \n" " 1234\n" " 1000\n" " \n" " \n" " 6789\n" " 2000\n" " \n" " \n" " 4567\n" " 3000\n" " \n" "
\n"; const bool isName(const value& token) { return isTaggedList(token, attribute) && attributeName(token) == "name"; } bool testReadXML() { { istringstream is(customerXML); const list c = readXML(streamList(is)); } { istringstream is(currencyXML); const list c = readXML(streamList(is)); const value composite = car(c); assert(isTaggedList(composite, element)); assert(elementName(composite) == "composite"); assert(attributeValue(car(filter(isName, elementChildren(composite)))) == string("currency")); } return true; } ostream* xmlWriter(const string& s, ostream* os) { (*os) << s; return os; } bool testWriteXML() { { istringstream is(customerXML); const list c = readXML(streamList(is)); ostringstream os; writeXML(xmlWriter, &os, c); assert(str(os) == customerXML); } { istringstream is(currencyXML); const list c = readXML(streamList(is)); ostringstream os; writeXML(xmlWriter, &os, c); assert(str(os) == currencyXML); } return true; } bool testElements() { { const list ad = mklist(mklist("city", string("san francisco")), mklist("state", string("ca"))); const list ac1 = mklist(mklist("id", string("1234")), mklist("balance", 1000)); const list ac2 = mklist(mklist("id", string("6789")), mklist("balance", 2000)); const list ac3 = mklist(mklist("id", string("4567")), mklist("balance", 3000)); { const list c = mklist(mklist("customer", mklist("name", string("jdoe")), cons("address", ad), mklist("account", mklist(ac1, ac2, ac3)))); const list e = valuesToElements(c); const list v = elementsToValues(e); assert(v == c); ostringstream os; writeXML(xmlWriter, &os, e); assert(str(os) == customerXML); } { const list c = mklist(mklist("customer", mklist("name", string("jdoe")), cons("address", ad), cons("account", ac1), cons("account", ac2), cons("account", ac3))); const list e = valuesToElements(c); const list v = elementsToValues(e); ostringstream os; writeXML(xmlWriter, &os, e); assert(str(os) == customerXML); } } { istringstream is(customerXML); const list c = readXML(streamList(is)); const list v = elementsToValues(c); const list e = valuesToElements(v); ostringstream os; writeXML(xmlWriter, &os, e); assert(str(os) == customerXML); } return true; } bool testValues() { { const list l = mklist(list() + "ns1:echoString" + (list() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/services/echo")) + (list() + "text" + string("Hello World!"))); const list e = valuesToElements(l); const failable > lx = writeXML(e); ostringstream os; write(content(lx), os); istringstream is(str(os)); const list x = readXML(streamList(is)); const list v = elementsToValues(x); assert(v == l); } return true; } } int main() { tuscany::cout << "Testing..." << tuscany::endl; tuscany::testReadXML(); tuscany::testWriteXML(); tuscany::testElements(); tuscany::testValues(); tuscany::cout << "OK" << tuscany::endl; return 0; }