From f80a5f0fb75540491cc7796aad27ac7fe91f62de Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Fri, 11 May 2012 01:27:34 +0800 Subject: [PATCH] trying to read properties from entry XML --- libgrive/CMakeLists.txt | 3 +- libgrive/src/drive/Drive.cc | 5 +--- libgrive/src/drive/Entry.cc | 2 ++ libgrive/src/http/ResponseLog.cc | 11 ++++--- libgrive/src/http/ResponseLog.hh | 7 +++-- libgrive/src/xml/Node.cc | 20 ++++++++++++- libgrive/src/xml/Node.hh | 1 + libgrive/src/xml/TreeBuilder.cc | 7 ++++- libgrive/test/UnitTest.cc | 2 ++ libgrive/test/data/entry.xml | 1 + libgrive/test/drive/EntryTest.cc | 50 ++++++++++++++++++++++++++++++++ libgrive/test/drive/EntryTest.hh | 41 ++++++++++++++++++++++++++ libgrive/test/xml/NodeTest.cc | 5 +++- 13 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 libgrive/test/data/entry.xml create mode 100644 libgrive/test/drive/EntryTest.cc create mode 100644 libgrive/test/drive/EntryTest.hh diff --git a/libgrive/CMakeLists.txt b/libgrive/CMakeLists.txt index 3f79a9b..d8c000d 100644 --- a/libgrive/CMakeLists.txt +++ b/libgrive/CMakeLists.txt @@ -43,7 +43,7 @@ file (GLOB LIBGRIVE_SRC src/xml/*.cc ) -add_definitions( -DVERSION="${GRIVE_VERSION}" ) +add_definitions( -DVERSION="${GRIVE_VERSION}" -DTEST_DATA="${libgrive_SOURCE_DIR}/test/data/" ) add_library( grive SHARED ${LIBGRIVE_SRC} ) @@ -75,6 +75,7 @@ IF ( CPPUNIT_FOUND ) # list of test source files here file(GLOB TEST_SRC + test/drive/*.cc test/util/*.cc test/xml/*.cc ) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index f780464..e816c48 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -115,13 +115,10 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href ) void Drive::ConstructDirTree( http::Agent *http ) { http::XmlResponse xml ; - http::ResponseLog log( "dir-", &xml ) ; + http::ResponseLog log( "dir-", ".xml", &xml ) ; http->Get( root_url + "/-/folder?showroot=true&max-results=10", &log, m_http_hdr ) ; -std::ofstream abc( "abc.xml" ) ; -abc << xml.Response()["feed"]["entry"] ; - http::JsonResponse jrsp ; http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ; Json resp = jrsp.Response() ; diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index 800169a..6d7b0bd 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -48,6 +48,8 @@ Entry::Entry( const Json& entry ) Entry::Entry( const xml::Node& n ) { + m_title = n["title"].Value() ; + m_etag = n["@gd:etag"].Value() ; } Entry::Entry( const std::string& title, const std::string& href ) : diff --git a/libgrive/src/http/ResponseLog.cc b/libgrive/src/http/ResponseLog.cc index 23442c1..0e7e631 100644 --- a/libgrive/src/http/ResponseLog.cc +++ b/libgrive/src/http/ResponseLog.cc @@ -25,8 +25,11 @@ namespace gr { namespace http { -ResponseLog::ResponseLog( const std::string& prefix, Receivable *next ) : - m_log( Filename(prefix).c_str() ), +ResponseLog::ResponseLog( + const std::string& prefix, + const std::string& suffix, + Receivable *next ) : + m_log( Filename(prefix, suffix).c_str() ), m_next( next ) { } @@ -43,9 +46,9 @@ void ResponseLog::Clear() m_next->Clear() ; } -std::string ResponseLog::Filename( const std::string& prefix ) +std::string ResponseLog::Filename( const std::string& prefix, const std::string& suffix ) { - return prefix + DateTime::Now().Format( "%H%M%S" ) ; + return prefix + DateTime::Now().Format( "%H%M%S" ) + suffix ; } }} // end of namespace diff --git a/libgrive/src/http/ResponseLog.hh b/libgrive/src/http/ResponseLog.hh index 952e123..a427324 100644 --- a/libgrive/src/http/ResponseLog.hh +++ b/libgrive/src/http/ResponseLog.hh @@ -29,13 +29,16 @@ namespace gr { namespace http { class ResponseLog : public Receivable { public : - ResponseLog( const std::string& prefix, Receivable *next ) ; + ResponseLog( + const std::string& prefix, + const std::string& suffix, + Receivable *next ) ; std::size_t OnData( void *data, std::size_t count ) ; void Clear() ; private : - static std::string Filename( const std::string& prefix ) ; + static std::string Filename( const std::string& prefix, const std::string& suffix ) ; private : std::ofstream m_log ; diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index 83be4c2..77c9c43 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -133,6 +133,11 @@ public : return m_children.end() ; } + std::size_t Size() const + { + return m_children.size() ; + } + std::pair Attr() { return std::make_pair( m_attr.begin(), m_attr.end() ) ; @@ -195,7 +200,10 @@ Node::iterator::iterator( ImplVec::iterator i ) : iterator_adaptor(i) Node::iterator::reference Node::iterator::dereference() const { - return Node( (*base_reference())->AddRef() ) ; + Impl *p = *base_reference() ; + assert( p != 0 ) ; + + return Node( p->AddRef() ) ; } Node::Node() : m_ptr( new Impl ) @@ -380,16 +388,25 @@ std::ostream& Node::PrintChar( std::ostream& os, char c ) Node::iterator Node::begin() const { + assert( m_ptr != 0 ) ; return iterator( m_ptr->Begin() ) ; } Node::iterator Node::end() const { + assert( m_ptr != 0 ) ; return iterator( m_ptr->End() ) ; } +std::size_t Node::size() const +{ + assert( m_ptr != 0 ) ; + return m_ptr->Size() ; +} + Node::Range Node::Attr() const { + assert( m_ptr != 0 ) ; std::pair is = m_ptr->Attr() ; return std::make_pair( iterator(is.first), iterator(is.second) ) ; @@ -397,6 +414,7 @@ Node::Range Node::Attr() const Node::Range Node::Children( const std::string& name ) const { + assert( m_ptr != 0 ) ; std::pair is = m_ptr->Children( name ) ; return std::make_pair( iterator(is.first), iterator(is.second) ) ; diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index 7a0653c..ff09146 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -70,6 +70,7 @@ public : iterator begin() const ; iterator end() const ; + std::size_t size() const ; Range Attr() const ; Range Children( const std::string& name ) const ; diff --git a/libgrive/src/xml/TreeBuilder.cc b/libgrive/src/xml/TreeBuilder.cc index 5debdd0..a4d7684 100644 --- a/libgrive/src/xml/TreeBuilder.cc +++ b/libgrive/src/xml/TreeBuilder.cc @@ -80,8 +80,13 @@ Node TreeBuilder::Parse( const std::string& xml ) Node TreeBuilder::Result() const { + // the node on the stack should be the dummy node with only one child assert( m_impl->stack.size() == 1 ) ; - return m_impl->stack.front() ; + + if ( m_impl->stack.front().size() != 1 ) + throw std::runtime_error( "invalid node" ) ; + + return *m_impl->stack.front().begin() ; } void TreeBuilder::StartElement( void *pvthis, const char *name, const char **attr ) diff --git a/libgrive/test/UnitTest.cc b/libgrive/test/UnitTest.cc index 67952b7..e02239d 100644 --- a/libgrive/test/UnitTest.cc +++ b/libgrive/test/UnitTest.cc @@ -19,6 +19,7 @@ #include +#include "drive/EntryTest.hh" #include "util/DateTimeTest.hh" #include "util/FunctionTest.hh" #include "util/PathTest.hh" @@ -30,6 +31,7 @@ int main( int argc, char **argv ) using namespace grut ; CppUnit::TextUi::TestRunner runner; + runner.addTest( EntryTest::suite( ) ) ; runner.addTest( DateTimeTest::suite( ) ) ; runner.addTest( FunctionTest::suite( ) ) ; runner.addTest( PathTest::suite( ) ) ; diff --git a/libgrive/test/data/entry.xml b/libgrive/test/data/entry.xml new file mode 100644 index 0000000..427ce59 --- /dev/null +++ b/libgrive/test/data/entry.xml @@ -0,0 +1 @@ +https://docs.google.com/feeds/default/private/full2012-05-10T16:38:17.968ZAvailable Documents - me@nestal.netmeme@nestal.net110https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGMl83OEV1ZVc3cUE2012-05-09T16:13:22.401Z2012-05-09T16:13:22.401Z2012-05-09T16:13:22.401Zsnesmeme@nestal.netfolder:0B5KhdsbryVeGMl83OEV1ZVc3cUEmeme@nestal.net0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGeXhVNHpZSVotN1E2012-05-09T16:11:30.497Z2012-05-09T16:12:05.218Z2012-05-09T16:12:06.869ZLocal Sharedmeme@nestal.netfolder:0B5KhdsbryVeGeXhVNHpZSVotN1E2012-05-09T16:12:05.218Zmeme@nestal.net2012-05-09T16:12:06.863Z0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGNzZGdDlXODBiMVU2012-05-07T04:36:37.993Z2012-05-07T04:36:45.478Z2012-05-09T16:11:49.340Zbinmeme@nestal.netfolder:0B5KhdsbryVeGNzZGdDlXODBiMVU2012-05-07T04:36:45.478Zmeme@nestal.net2012-05-09T16:11:49.334Z0https://docs.google.com/feeds/id/folder%3A0B6HkZFGcyk1-c1RvMEctZ1VuTU02012-05-01T17:03:56.733Z2012-05-01T17:04:04.534Z2012-05-06T12:46:05.931ZShared Subfoldermatch065match065@gmail.comfolder:0B6HkZFGcyk1-c1RvMEctZ1VuTU0match065match065@gmail.com2012-05-06T10:48:52.313Z0https://docs.google.com/feeds/id/folder%3A0B6HkZFGcyk1-anBEVlBjb2dockk2012-05-01T16:31:06.465Z2012-05-01T16:31:31.731Z2012-05-06T12:46:04.899ZSharedmatch065match065@gmail.comfolder:0B6HkZFGcyk1-anBEVlBjb2dockk2012-05-01T16:31:31.735Zmatch065match065@gmail.com2012-05-06T11:00:24.103Z0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGRlZuNEU0ckJneGc2012-04-24T17:07:48.335Z2012-04-24T17:33:54.733Z2012-05-09T16:12:09.328ZSAMSUNG_S MEMOmeme@nestal.netfolder:0B5KhdsbryVeGRlZuNEU0ckJneGc2012-04-24T17:33:54.733Zmeme@nestal.net2012-05-09T16:12:09.322Z0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGNEZjdUxzZHl3Sjg2012-04-24T16:55:48.499Z2012-04-24T16:55:48.499Z2012-05-09T16:13:02.326ZROMsmeme@nestal.netfolder:0B5KhdsbryVeGNEZjdUxzZHl3Sjgmeme@nestal.net2012-05-09T16:13:02.321Z0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGWVpLRDdoa1JrRWs2012-04-24T16:55:48.499Z2012-04-24T16:55:48.499Z2012-05-09T16:11:54.889Zsegameme@nestal.netfolder:0B5KhdsbryVeGWVpLRDdoa1JrRWsmeme@nestal.net2012-05-09T16:11:54.881Z0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGQUFSVUgxeVJSd2M2012-04-24T16:55:48.499Z2012-04-24T16:55:48.499Z2012-04-24T16:55:48.499Zn64meme@nestal.netfolder:0B5KhdsbryVeGQUFSVUgxeVJSd2Mmeme@nestal.net0https://docs.google.com/feeds/id/folder%3A0B5KhdsbryVeGQVpPR192QVJ2Uzg2012-04-24T16:54:34.158Z2012-04-24T16:55:02.919Z2012-05-09T16:11:42.580ZPrivatememe@nestal.netfolder:0B5KhdsbryVeGQVpPR192QVJ2Uzg2012-04-24T16:55:02.919Zmeme@nestal.net2012-05-09T16:11:42.575Z0 \ No newline at end of file diff --git a/libgrive/test/drive/EntryTest.cc b/libgrive/test/drive/EntryTest.cc new file mode 100644 index 0000000..f0b17b3 --- /dev/null +++ b/libgrive/test/drive/EntryTest.cc @@ -0,0 +1,50 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "EntryTest.hh" + +#include "Assert.hh" + +#include "drive/Entry.hh" +#include "xml/Node.hh" +#include "xml/TreeBuilder.hh" + +#include + +namespace grut { + +using namespace gr ; + +EntryTest::EntryTest( ) +{ +} + +void EntryTest::TestXml( ) +{ + xml::Node root = xml::TreeBuilder::ParseFile( TEST_DATA "entry.xml" ) ; + + xml::Node::Range entries = root.Children( "entry" ) ; + CPPUNIT_ASSERT( entries.first != entries.second ) ; + + Entry subject( *entries.first ) ; + GRUT_ASSERT_EQUAL( "snes", subject.Title() ) ; + GRUT_ASSERT_EQUAL( "\"WxYPGE8CDyt7ImBk\"", subject.ETag() ) ; +} + +} // end of namespace grut diff --git a/libgrive/test/drive/EntryTest.hh b/libgrive/test/drive/EntryTest.hh new file mode 100644 index 0000000..8d4f7f4 --- /dev/null +++ b/libgrive/test/drive/EntryTest.hh @@ -0,0 +1,41 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include +#include + +namespace grut { + +class EntryTest : public CppUnit::TestFixture +{ +public : + EntryTest( ) ; + + // declare suit function + CPPUNIT_TEST_SUITE( EntryTest ) ; + CPPUNIT_TEST( TestXml ) ; + CPPUNIT_TEST_SUITE_END(); + +private : + void TestXml( ) ; +} ; + +} // end of namespace diff --git a/libgrive/test/xml/NodeTest.cc b/libgrive/test/xml/NodeTest.cc index 64a217c..f1c02eb 100644 --- a/libgrive/test/xml/NodeTest.cc +++ b/libgrive/test/xml/NodeTest.cc @@ -57,7 +57,10 @@ void NodeTest::TestTree( ) void NodeTest::TestParseFile( ) { - Node n = TreeBuilder::Parse( "abc" ) ; + Node n ; + n.AddNode( TreeBuilder::Parse( "abc" ) ) ; + + CPPUNIT_ASSERT_EQUAL( std::string("entry"), n["entry"].Name() ) ; CPPUNIT_ASSERT_EQUAL( std::string("link"), n["entry"]["link"].Name() ) ; CPPUNIT_ASSERT_EQUAL( std::string("q"), n["entry"]["link"]["@href"].Value() ) ;