/* * 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" "" "" "" "" "" "" "" "" "" "" "" "" "US" "" "" "\n"; const string customerXML = "\n" "" "jdoe" "
san franciscoca
" "12341000" "67892000" "45673000" "
" "\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 testElement() { { 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; } } int main() { tuscany::cout << "Testing..." << tuscany::endl; tuscany::testReadXML(); tuscany::testWriteXML(); tuscany::testElement(); tuscany::cout << "OK" << tuscany::endl; return 0; }