Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

ElementStreamTest.cpp

00001 //============================================================================
00002 // Project:       Jabber Universal Document Objects (Judo)
00003 // Filename:      ElementStreamTest.cpp
00004 // Description:   judo::ElementStream unit tests
00005 // Created at:    Tue Jul  3 15:59:14 2001
00006 // Modified at:   Mon Jul 30 18:07:08 2001
00007 //
00008 //   License:
00009 // 
00010 // The contents of this file are subject to the Jabber Open Source License
00011 // Version 1.0 (the "License").  You may not copy or use this file, in either
00012 // source code or executable form, except in compliance with the License.  You
00013 // may obtain a copy of the License at http://www.jabber.com/license/ or at
00014 // http://www.opensource.org/.  
00015 //
00016 // Software distributed under the License is distributed on an "AS IS" basis,
00017 // WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
00018 // for the specific language governing rights and limitations under the
00019 // License.
00020 //
00021 //   Copyrights
00022 //
00023 // Portions created by or assigned to Jabber.com, Inc. are 
00024 // Copyright (c) 1999-2001 Jabber.com, Inc.  All Rights Reserved.  
00025 // 
00026 // $Id: ElementStreamTest.cpp,v 1.2 2002/07/13 19:30:22 temas Exp $
00027 //============================================================================
00028 
00029 #include "judo.hpp"
00030 #include "judo_test.hpp"
00031 using namespace judo;
00032 
00033 #include <iostream>
00034 using namespace std;
00035 
00036 Test* ElementStreamTest::getTestSuite()
00037 {
00038     TestSuite* s = new TestSuite();
00039     s->addTest(new TestCaller<ElementStreamTest>("testing construction",
00040                                              &ElementStreamTest::construct));
00041     s->addTest(new TestCaller<ElementStreamTest>("push",
00042                                              &ElementStreamTest::push));
00043     s->addTest(new TestCaller<ElementStreamTest>("parseAtOnce",
00044                                              &ElementStreamTest::parseAtOnce));
00045     return s;
00046 }
00047 
00048 list<Element*> G_results;
00049 
00050 class ElementStreamTestImpl
00051     : public ElementStreamEventListener
00052 {
00053 public:
00054     ElementStreamTestImpl()
00055         : _stream(this)
00056         {}
00057 
00058     void onDocumentStart(Element* t)
00059         {
00060             G_results.push_back(t);
00061         }
00062     void onElement(Element* t)
00063         {
00064             G_results.push_back(t);
00065         }
00066     void onDocumentEnd()
00067         {
00068             for_each(G_results.begin(), G_results.end(), _releaseElement);
00069             G_results.clear();
00070         }
00071     ElementStream _stream;
00072 private:
00073     static void _releaseElement(Element* t)
00074         {
00075             delete t;
00076         }
00077 
00078 };
00079 
00080 void ElementStreamTest::construct()
00081 {
00082     ElementStreamTestImpl es;
00083 
00084     Assert(es._stream._document_started == false);
00085     Assert(es._stream._document_ended == false);
00086 }
00087 
00088 void ElementStreamTest::push()
00089 {
00090     Assert(G_results.empty());
00091 
00092     ElementStreamTestImpl es;
00093 
00094     try 
00095     {    
00096         // Push the root element in
00097         es._stream.push("<root>", 6);
00098         Assert(es._stream._document_started == true);
00099         Assert(G_results.size() == 1);
00100         Assert(G_results.back()->toString() == "<root/>");
00101 
00102         // Do a partial push of a top-level packet
00103         es._stream.push("<message to='dizzy@j.org'><body>Hello", 37);
00104         Assert(es._stream._element_stack.size() == 2);
00105         Assert(G_results.size() == 1);
00106 
00107         // Complete the push of the top-level packet
00108         es._stream.push("</body></message>", 17);
00109         Assert(es._stream._element_stack.size() == 0);
00110         Assert(G_results.size() == 2);
00111         Assert(G_results.back()->toString() == "<message to='dizzy@j.org'><body>Hello</body></message>");
00112 
00113         // Push another packet on
00114         es._stream.push("<presence/>", 11);
00115         Assert(es._stream._element_stack.size() == 0);
00116         Assert(G_results.size() == 3);
00117         Assert(G_results.back()->toString() == "<presence/>");
00118 
00119         // Close the document
00120         es._stream.push("</root>", 7);
00121         Assert(es._stream._document_ended == true);
00122         Assert(G_results.empty());
00123     } catch (ElementStream::exception::ParserError& e)
00124     {
00125         cerr << "Parser error: " << e.getMessage() << endl;
00126         throw;
00127     }
00128 }
00129 
00130 void ElementStreamTest::parseAtOnce()
00131 {
00132     // Standard test
00133     const char* data1 = "<root>foobar<tag1/>somedata</root>";
00134     Element* e = ElementStream::parseAtOnce(data1);
00135     Assert(e->toString() == "<root>foobar<tag1/>somedata</root>");
00136     delete e;
00137 
00138     // Incomplete test
00139     bool successful2 = false;
00140     const char* data2 = "<root><tag1>foadsf</tag1><tag2>";
00141     try
00142     {
00143         e = ElementStream::parseAtOnce(data2);
00144     } catch (ElementStream::exception::IncompleteParse& parse_ex) {
00145         successful2 = true;
00146     }
00147     Assert(successful2 == true);
00148 
00149     // Invalid XML test
00150     bool successful3 = false;
00151     const char* data3 = "<root></tag1></root>";
00152     try
00153     {
00154         e = ElementStream::parseAtOnce(data3);
00155     } catch (ElementStream::exception::ParserError& parse_ex) {
00156         successful3 = true;
00157     }
00158     Assert(successful3 == true);
00159     
00160 }

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3