arabica/tests/DOM/test_SAX2DOM.hpp

187 lines
8.3 KiB
C++
Raw Normal View History

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>
2007-09-05 00:55:47 +02:00
#include <DOM/SAX2DOM/SAX2DOM.hpp>
#include <SAX/helpers/CatchErrorHandler.hpp>
2005-10-26 22:52:48 +02:00
template<class string_type, class string_adaptor>
2007-07-19 18:57:04 +02:00
class SAX2DOMTest : public TestCase
2005-10-26 22:52:48 +02:00
{
typedef string_adaptor SA;
2005-10-26 22:52:48 +02:00
public:
2007-07-19 18:57:04 +02:00
SAX2DOMTest(std::string name) :
2005-10-26 22:52:48 +02:00
TestCase(name)
{
2007-07-19 18:57:04 +02:00
} // SAX2DOMTest
2005-10-26 22:52:48 +02:00
void setUp()
{
} // setUp
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> parse(string_type str)
2005-10-26 22:52:48 +02:00
{
std::stringstream ss;
ss << SA::asStdString(str);
2005-10-26 22:52:48 +02:00
Arabica::SAX::InputSource<string_type> is(ss);
2007-09-05 11:49:18 +02:00
Arabica::SAX::CatchErrorHandler<string_type> eh;
2007-09-05 13:47:13 +02:00
Arabica::SAX2DOM::Parser<string_type, string_adaptor> parser;
parser.setErrorHandler(eh);
2005-10-26 22:52:48 +02:00
parser.parse(is);
//if(eh.errorsReported())
// throw std::runtime_error(eh.errors());
2005-10-26 22:52:48 +02:00
return parser.getDocument();
} // parse
void test1()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Element<string_type> d;
Arabica::DOM::Node<string_type> n;
2005-10-26 22:52:48 +02:00
assert(d == 0);
assert(n == 0);
assert(n == d);
} // test1
void test2()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assert(elem.getParentNode() == d);
assert(elem.getOwnerDocument() == d);
} // test2
void test3()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assert(elem.hasAttributes() == true);
assert(elem.getAttribute(SA::construct_from_utf8("attr")) == SA::construct_from_utf8("poop"));
2005-10-26 22:52:48 +02:00
} // test3
void test4()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root><child attr=\"poop\"/></root>"));
Arabica::DOM::Element<string_type> elem = Arabica::DOM::Element<string_type>(d.getDocumentElement().getFirstChild());
assert(SA::construct_from_utf8("child") == elem.getNodeName());
2005-10-26 22:52:48 +02:00
assertEquals(true, elem.hasAttributes());
assert(SA::construct_from_utf8("poop") == elem.getAttribute(SA::construct_from_utf8("attr")));
2005-10-26 22:52:48 +02:00
} // test4
void test5()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assertEquals(true, elem.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
2005-10-26 22:52:48 +02:00
2007-09-05 13:47:13 +02:00
Arabica::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()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<stuff:elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assertEquals(true, elem.hasNamespaceURI());
assert(SA::construct_from_utf8("http://example.com/stuff") == elem.getNamespaceURI());
2005-10-26 22:52:48 +02:00
2007-09-05 13:47:13 +02:00
Arabica::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());
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
2005-10-26 22:52:48 +02:00
} // test6
void test7()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem stuff:attr='something' xmlns:stuff='http://example.com/stuff'/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assertEquals(false, elem.hasNamespaceURI());
2007-09-05 13:47:13 +02:00
Arabica::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());
assert(SA::construct_from_utf8("http://example.com/stuff") == attr.getNamespaceURI());
2005-10-26 22:52:48 +02:00
} // test7
void test8()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
2007-09-05 13:47:13 +02:00
Arabica::DOM::Element<string_type> e2 = Arabica::DOM::Element<string_type>(elem.cloneNode(true));
2005-10-26 22:52:48 +02:00
assert(e2.getOwnerDocument() == d);
assert(e2.getParentNode() == 0);
assert(e2.hasAttributes() == true);
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()
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<string_type> d = parse(SA::construct_from_utf8("<elem attr='something' xmlns:stuff='http://example.com/stuff'/>"));
Arabica::DOM::Element<string_type> elem = d.getDocumentElement();
2005-10-26 22:52:48 +02:00
assertEquals(false, elem.hasNamespaceURI());
2007-09-05 13:47:13 +02:00
Arabica::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()
{
2007-09-05 13:47:13 +02:00
Arabica::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()
{
2007-09-05 13:47:13 +02:00
Arabica::SAX2DOM::Parser<string_type, string_adaptor> parser;
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);
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
parser.parse(SA::construct_from_utf8("<root attr=\"poop\"><child/></root>"));
2005-10-26 22:52:48 +02:00
} // test11
void test12()
{
2007-09-05 13:47:13 +02:00
Arabica::SAX2DOM::Parser<string_type, string_adaptor> parser;
2005-10-26 22:52:48 +02:00
try
{
parser.getFeature(SA::construct_from_utf8("made up name"));
2005-10-26 22:52:48 +02:00
assert(false);
}
2007-09-05 11:49:18 +02:00
catch(const Arabica::SAX::SAXNotRecognizedException&)
2005-10-26 22:52:48 +02:00
{
}
} // test12
};
template<class string_type, class string_adaptor>
2007-07-19 18:57:04 +02:00
TestSuite* SAX2DOMTest_suite()
2005-10-26 22:52:48 +02:00
{
TestSuite *suiteOfTests = new TestSuite;
2007-07-19 18:57:04 +02:00
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test1", &SAX2DOMTest<string_type, string_adaptor>::test1));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test2", &SAX2DOMTest<string_type, string_adaptor>::test2));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test3", &SAX2DOMTest<string_type, string_adaptor>::test3));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test4", &SAX2DOMTest<string_type, string_adaptor>::test4));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test5", &SAX2DOMTest<string_type, string_adaptor>::test5));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test6", &SAX2DOMTest<string_type, string_adaptor>::test6));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test7", &SAX2DOMTest<string_type, string_adaptor>::test7));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test8", &SAX2DOMTest<string_type, string_adaptor>::test8));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test9", &SAX2DOMTest<string_type, string_adaptor>::test9));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test10", &SAX2DOMTest<string_type, string_adaptor>::test10));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test11", &SAX2DOMTest<string_type, string_adaptor>::test11));
suiteOfTests->addTest(new TestCaller<SAX2DOMTest<string_type, string_adaptor> >("test12", &SAX2DOMTest<string_type, string_adaptor>::test12));
2005-10-26 22:52:48 +02:00
return suiteOfTests;
2007-07-19 18:57:04 +02:00
} // SAX2DOMTest_suite
2002-09-03 15:26:08 +02:00
#endif