2002-09-03 15:26:08 +02:00
|
|
|
#ifndef test_SAX_H
|
|
|
|
#define test_SAX_H
|
|
|
|
|
2005-10-26 22:52:48 +02:00
|
|
|
#include "../CppUnit/framework/TestCase.h"
|
|
|
|
#include "../CppUnit/framework/TestSuite.h"
|
|
|
|
#include "../CppUnit/framework/TestCaller.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <DOM/SAX2DOM/SAX2DOM.h>
|
2005-11-17 23:06:50 +01:00
|
|
|
#include <SAX/helpers/CatchErrorHandler.h>
|
2005-10-26 22:52:48 +02:00
|
|
|
|
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
class SAXTest : public TestCase
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
typedef string_adaptor SA;
|
|
|
|
|
2005-10-26 22:52:48 +02:00
|
|
|
public:
|
|
|
|
SAXTest(std::string name) :
|
|
|
|
TestCase(name)
|
|
|
|
{
|
|
|
|
} // SAXTest
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
} // setUp
|
|
|
|
|
|
|
|
DOM::Document<string_type> parse(string_type str)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2005-11-15 17:16:46 +01:00
|
|
|
ss << SA::asStdString(str);
|
2005-10-26 22:52:48 +02:00
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
SAX::basic_InputSource<string_type> is(ss);
|
2005-11-17 23:06:50 +01:00
|
|
|
SAX::CatchErrorHandler<string_type> eh;
|
2005-11-15 17:16:46 +01:00
|
|
|
SAX2DOM::Parser<string_type, string_adaptor> parser;
|
2005-11-17 23:06:50 +01:00
|
|
|
parser.setErrorHandler(eh);
|
2005-10-26 22:52:48 +02:00
|
|
|
parser.parse(is);
|
2005-11-17 23:06:50 +01:00
|
|
|
|
|
|
|
//if(eh.errorsReported())
|
|
|
|
// throw std::runtime_error(eh.errors());
|
|
|
|
|
2005-10-26 22:52:48 +02:00
|
|
|
return parser.getDocument();
|
|
|
|
} // parse
|
|
|
|
|
|
|
|
void test1()
|
|
|
|
{
|
|
|
|
DOM::Element<string_type> d;
|
|
|
|
DOM::Node<string_type> n;
|
|
|
|
assert(d == 0);
|
|
|
|
assert(n == 0);
|
|
|
|
assert(n == d);
|
|
|
|
} // test1
|
|
|
|
|
|
|
|
void test2()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assert(elem.getParentNode() == d);
|
|
|
|
assert(elem.getOwnerDocument() == d);
|
|
|
|
} // test2
|
|
|
|
|
|
|
|
void test3()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assert(elem.hasAttributes() == true);
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(elem.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test3
|
|
|
|
|
|
|
|
void test4()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root><child attr=\"poop\"/></root>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = DOM::Element<string_type>(d.getDocumentElement().getFirstChild());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("child") == elem.getNodeName());
|
2005-10-26 22:52:48 +02:00
|
|
|
assertEquals(true, elem.hasAttributes());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("poop") == elem.getAttribute(SA::construct_from_utf8("attr")));
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test4
|
|
|
|
|
|
|
|
void test5()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assertEquals(true, elem.hasNamespaceURI());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
|
2005-10-26 22:52:48 +02:00
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Attr<string_type> attr = elem.getAttributeNode(SA::construct_from_utf8("attr"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assertEquals(false, attr.hasNamespaceURI());
|
|
|
|
} // test5
|
|
|
|
|
|
|
|
void test6()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assertEquals(true, elem.hasNamespaceURI());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
|
2005-10-26 22:52:48 +02:00
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Attr<string_type> attr = elem.getAttributeNodeNS(SA::construct_from_utf8("http://example.com/stuff"), SA::construct_from_utf8("attr"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assertEquals(true, attr.hasNamespaceURI());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test6
|
|
|
|
|
|
|
|
void test7()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assertEquals(false, elem.hasNamespaceURI());
|
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Attr<string_type> attr = elem.getAttributeNodeNS(SA::construct_from_utf8("http://example.com/stuff"), SA::construct_from_utf8("attr"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assertEquals(true, attr.hasNamespaceURI());
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test7
|
|
|
|
|
|
|
|
void test8()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
|
|
|
|
DOM::Element<string_type> e2 = DOM::Element<string_type>(elem.cloneNode(true));
|
|
|
|
assert(e2.getOwnerDocument() == d);
|
|
|
|
assert(e2.getParentNode() == 0);
|
|
|
|
assert(e2.hasAttributes() == true);
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(e2.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
|
|
|
|
assert(e2.getFirstChild().getNodeName() == SA::construct_from_utf8("child"));
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test8
|
|
|
|
|
|
|
|
void test9()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
DOM::Element<string_type> elem = d.getDocumentElement();
|
|
|
|
assertEquals(false, elem.hasNamespaceURI());
|
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Attr<string_type> attr = elem.getAttributeNode(SA::construct_from_utf8("attr"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assertEquals(false, attr.hasNamespaceURI());
|
|
|
|
} // test9
|
|
|
|
|
|
|
|
void test10()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem stuff:attr='something' poop:attr='fail' xmlns:stuff='http://example.com/stuff'/>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assert(d == 0);
|
|
|
|
} // test10
|
|
|
|
|
|
|
|
void test11()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
SAX2DOM::Parser<string_type, string_adaptor> parser;
|
2005-11-17 23:06:50 +01:00
|
|
|
assert(parser.getFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation")) == false);
|
|
|
|
parser.setFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation"), true);
|
2005-11-15 17:16:46 +01:00
|
|
|
assert(parser.getFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation")) == true);
|
|
|
|
parser.setFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation"), false);
|
|
|
|
assert(parser.getFeature(SA::construct_from_utf8("http://xml.org/sax/features/validation")) == false);
|
2005-10-26 22:52:48 +02:00
|
|
|
|
2005-11-15 17:16:46 +01:00
|
|
|
parser.parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
|
2005-10-26 22:52:48 +02:00
|
|
|
} // test11
|
|
|
|
|
|
|
|
void test12()
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
SAX2DOM::Parser<string_type, string_adaptor> parser;
|
2005-10-26 22:52:48 +02:00
|
|
|
try
|
|
|
|
{
|
2005-11-15 17:16:46 +01:00
|
|
|
parser.getFeature(SA::construct_from_utf8("made up name"));
|
2005-10-26 22:52:48 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
catch(const SAX::SAXNotRecognizedException&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // test12
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
TestSuite* SAXTest_suite()
|
|
|
|
{
|
|
|
|
TestSuite *suiteOfTests = new TestSuite;
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test1", &SAXTest<string_type, string_adaptor>::test1));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test2", &SAXTest<string_type, string_adaptor>::test2));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test3", &SAXTest<string_type, string_adaptor>::test3));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test4", &SAXTest<string_type, string_adaptor>::test4));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test5", &SAXTest<string_type, string_adaptor>::test5));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test6", &SAXTest<string_type, string_adaptor>::test6));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test7", &SAXTest<string_type, string_adaptor>::test7));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test8", &SAXTest<string_type, string_adaptor>::test8));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test9", &SAXTest<string_type, string_adaptor>::test9));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test10", &SAXTest<string_type, string_adaptor>::test10));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test11", &SAXTest<string_type, string_adaptor>::test11));
|
|
|
|
suiteOfTests->addTest(new TestCaller<SAXTest<string_type, string_adaptor> >("test12", &SAXTest<string_type, string_adaptor>::test12));
|
|
|
|
return suiteOfTests;
|
|
|
|
} // SAXTest_suite
|
2002-09-03 15:26:08 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|