From 61c01019f749f80e51c24a21e44ddb640ff601c3 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Thu, 24 May 2012 17:47:26 +0800 Subject: [PATCH] added more test cases and some minor refactoring --- libgrive/src/drive/Drive.cc | 20 +++++++++--------- libgrive/src/drive/Drive.hh | 1 + libgrive/src/drive/Resource.cc | 10 +++++++++ libgrive/src/drive/Resource.hh | 2 ++ libgrive/src/drive/ResourceTree.cc | 22 +++++++++++++++++--- libgrive/src/drive/State.cc | 8 ++++++- libgrive/src/drive/State.hh | 5 +++-- libgrive/test/data/test_dir1.state | 1 + libgrive/test/data/test_dir1/folder1/abc.txt | 1 + libgrive/test/drive/StateTest.cc | 14 ++++++++++--- 10 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 libgrive/test/data/test_dir1.state create mode 100644 libgrive/test/data/test_dir1/folder1/abc.txt diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index e812b4c..ede0f57 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -93,22 +93,22 @@ Drive::Drive( OAuth2& auth ) : if ( (*i)["content"] == "" ) continue ; - Entry file( *i ) ; - if ( file.Kind() != "folder" ) + Entry entry( *i ) ; + if ( entry.Kind() != "folder" ) { - Resource *p = m_state.FindFolderByHref( file.ParentHref() ) ; - if ( file.Filename().empty() ) - Log( "file \"%1%\" is a google document, ignored", file.Title(), log::info ) ; + Resource *parent = m_state.FindByHref( entry.ParentHref() ) ; + if ( entry.Filename().empty() ) + Log( "file \"%1%\" is a google document, ignored", entry.Title(), log::info ) ; - else if ( file.ParentHref().empty() || p == 0 || !p->IsInRootTree() ) - Log( "file \"%1%\" parent doesn't exist, ignored", file.Title(), log::info ) ; + else if ( parent == 0 || !parent->IsInRootTree() ) + Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::info ) ; - else if ( p != 0 && !p->IsFolder() ) + else if ( parent != 0 && !parent->IsFolder() ) Log( "entry %1% has parent %2% which is not a folder, ignored", - file.Title(), p->Name(), log::info ) ; + entry.Title(), parent->Name(), log::info ) ; else - m_state.FromRemote( file ) ; + m_state.FromRemote( entry ) ; } } diff --git a/libgrive/src/drive/Drive.hh b/libgrive/src/drive/Drive.hh index 82cc6a6..b92ad19 100644 --- a/libgrive/src/drive/Drive.hh +++ b/libgrive/src/drive/Drive.hh @@ -48,6 +48,7 @@ public : private : void ConstructDirTree( http::Agent *http ) ; + void file(); private : OAuth2& m_auth ; diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index 137879a..ccae08d 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -154,11 +154,13 @@ std::string Resource::ResourceID() const const Resource* Resource::Parent() const { + assert( m_parent == 0 || m_parent->IsFolder() ) ; return m_parent ; } Resource* Resource::Parent() { + assert( m_parent == 0 || m_parent->IsFolder() ) ; return m_parent ; } @@ -182,6 +184,7 @@ void Resource::Swap( Resource& coll ) m_entry.Swap( coll.m_entry ) ; std::swap( m_parent, coll.m_parent ) ; m_child.swap( coll.m_child ) ; + std::swap( m_state, coll.m_state ) ; } bool Resource::IsFolder() const @@ -395,6 +398,13 @@ std::ostream& operator<<( std::ostream& os, Resource::State s ) return os << state[s] ; } +std::string Resource::StateStr() const +{ + std::ostringstream ss ; + ss << m_state ; + return ss.str() ; +} + } // end of namespace namespace std diff --git a/libgrive/src/drive/Resource.hh b/libgrive/src/drive/Resource.hh index 978c45e..6648f42 100644 --- a/libgrive/src/drive/Resource.hh +++ b/libgrive/src/drive/Resource.hh @@ -83,6 +83,8 @@ public : iterator end() const ; std::size_t size() const ; + std::string StateStr() const ; + private : /// State of the resource. indicating what to do with the resource enum State diff --git a/libgrive/src/drive/ResourceTree.cc b/libgrive/src/drive/ResourceTree.cc index 02adb7f..80e4033 100644 --- a/libgrive/src/drive/ResourceTree.cc +++ b/libgrive/src/drive/ResourceTree.cc @@ -94,6 +94,8 @@ ResourceTree& ResourceTree::operator=( const ResourceTree& fs ) Resource* ResourceTree::FindByHref( const std::string& href ) { + // for the resource that not yet have href (e.g. not yet fetched from server) + // their href will be empty. if ( href.empty() ) return 0 ; @@ -113,9 +115,23 @@ const Resource* ResourceTree::FindByHref( const std::string& href ) const /// container. It traverses the tree instead. Resource* ResourceTree::FindByPath( const fs::path& path ) { - // not yet implemented - assert( false ) ; - return false ; + Resource *current = m_root ; + for ( fs::path::iterator i = path.begin() ; i != path.end() && current != 0 ; ++i ) + { + Trace( "path it = %1%", *i ) ; + + // current directory + if ( *i == "." ) + continue ; + + else if ( *i == ".." ) + current = current->Parent() ; + + else + current = current->FindChild( i->filename().string() ) ; + } + + return current ; } /// Reinsert should be called when the ID/HREF were updated diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index 26aac2d..4700cbd 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -176,11 +176,17 @@ bool State::Update( const Entry& e ) return false ; } -Resource* State::FindFolderByHref( const std::string& href ) +Resource* State::FindByHref( const std::string& href ) { return m_res.FindByHref( href ) ; } +Resource* State::Find( const fs::path& path ) +{ + return m_res.FindByPath( path ) ; +} + + State::iterator State::begin() { return m_res.begin() ; diff --git a/libgrive/src/drive/State.hh b/libgrive/src/drive/State.hh index f211ca2..92d4672 100644 --- a/libgrive/src/drive/State.hh +++ b/libgrive/src/drive/State.hh @@ -48,8 +48,9 @@ public : std::string ChangeStamp() const ; void ChangeStamp( const std::string& cs ) ; - Resource* FindFolderByHref( const std::string& href ) ; - Resource* FindFolderByID( const std::string& id ) ; + Resource* FindByHref( const std::string& href ) ; + Resource* FindByID( const std::string& id ) ; + Resource* Find( const fs::path& path ) ; iterator begin() ; iterator end() ; diff --git a/libgrive/test/data/test_dir1.state b/libgrive/test/data/test_dir1.state new file mode 100644 index 0000000..67b5d1b --- /dev/null +++ b/libgrive/test/data/test_dir1.state @@ -0,0 +1 @@ +{ "change_stamp": "", "rtree": { "name": ".", "id": "folder:root", "href": "https:\/\/docs.google.com\/feeds\/default\/private\/full\/folder%3Aroot", "md5": "", "kind": "folder", "mtime": { "sec": 0, "nsec": 0 }, "child": [ { "name": "entry.xml", "id": "", "href": "", "md5": "c0742c0a32b2c909b6f176d17a6992d0", "kind": "file", "mtime": { "sec": 1336796872, "nsec": 404985662 }, "child": [ ] } ] } } \ No newline at end of file diff --git a/libgrive/test/data/test_dir1/folder1/abc.txt b/libgrive/test/data/test_dir1/folder1/abc.txt new file mode 100644 index 0000000..f2ba8f8 --- /dev/null +++ b/libgrive/test/data/test_dir1/folder1/abc.txt @@ -0,0 +1 @@ +abc \ No newline at end of file diff --git a/libgrive/test/drive/StateTest.cc b/libgrive/test/drive/StateTest.cc index 9771d06..0dcee65 100644 --- a/libgrive/test/drive/StateTest.cc +++ b/libgrive/test/drive/StateTest.cc @@ -22,6 +22,7 @@ #include "Assert.hh" #include "drive/State.hh" +#include "util/Log.hh" #include @@ -35,9 +36,16 @@ StateTest::StateTest( ) void StateTest::TestSync( ) { - State s( ".grive_state" ) ; - s.FromLocal( TEST_DATA ) ; - s.Write( "" ) ; + State s( TEST_DATA "/test_dir1.state" ) ; + Resource *r = s.Find( "./folder1/abc.txt" ) ; + CPPUNIT_ASSERT( r != 0 ) ; + GRUT_ASSERT_EQUAL( r->Name(), "abc.txt" ) ; + + Trace( "state %1% = %2%", r->Name(), r->StateStr() ) ; + + // load directory + s.FromLocal( TEST_DATA "/test_dir1" ) ; + } } // end of namespace grut