[glom] Simplify test_selfhosting_new_empty.



commit 4cf4b7bde940893f60428c3cdfdbe34d803e56d4
Author: Murray Cumming <murrayc murrayc com>
Date:   Sun Apr 25 13:05:34 2010 +0200

    Simplify test_selfhosting_new_empty.
    
    * glom/frame_glom.cc: Move setup_connection_pool_from_document() to:
    * glom/libglom/connectionpool.[h|cc]: Added setup_from_document().
    * tests/test_selfhosting_new_empty/test_selfhosting_new_empty.cc: Use the
    new function here, removing code to explicitly set the backend.

 ChangeLog                                          |    9 +
 glom/frame_glom.cc                                 |   69 +-------
 glom/libglom/connectionpool.cc                     |   50 ++++++-
 glom/libglom/connectionpool.h                      |    5 +
 .../test_selfhosting_new_empty.cc                  |  181 ++++++++++++++++++++
 5 files changed, 249 insertions(+), 65 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3d6a2ff..7e1d2da 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2010-04-25  Murray Cumming  <murrayc murrayc com>
+
+	Simplify test_selfhosting_new_empty.
+	
+	* glom/frame_glom.cc: Move setup_connection_pool_from_document() to:
+	* glom/libglom/connectionpool.[h|cc]: Added setup_from_document().
+	* tests/test_selfhosting_new_empty/test_selfhosting_new_empty.cc: Use the 
+	new function here, removing code to explicitly set the backend.
+
 2010-04-23  Murray Cumming  <murrayc murrayc com>
 
 	Add a document-loading test and self-hosting test
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index a50c95e..2a8e75b 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -34,10 +34,6 @@
 #include <libglom/connectionpool_backends/postgres_self.h>
 #endif
 
-#ifdef GLOM_ENABLE_SQLITE
-# include <libglom/connectionpool_backends/sqlite.h>
-#endif
-
 #ifndef GLOM_ENABLE_CLIENT_ONLY
 #include <glom/mode_design/users/dialog_groups_list.h>
 #include <glom/mode_design/dialog_database_preferences.h>
@@ -1712,9 +1708,9 @@ void Frame_Glom::do_menu_developer_fields(Gtk::Window& parent, const Glib::ustri
     add_view(m_pDialog_Fields);
   }
 
-  // Some database backends (SQLite) require the table to change no longer
-  // being in use when changing the records, so clear the database usage
-  // here. We reshow everything in on_developer_dialog_hide anyway.
+  // Some database backends (SQLite) require the table to change to no longer
+  // be in use when changing the records, so we stop the database usage
+  // here. We reshow everything in on_developer_dialog_hide() anyway.
   show_no_table();
 
   // Remember the old table name so that we re-show the previous table as
@@ -1932,61 +1928,6 @@ void Frame_Glom::on_developer_dialog_hide()
 }
 #endif // !GLOM_ENABLE_CLIENT_ONLY
 
-namespace
-{
-  void setup_connection_pool_from_document(Document* document)
-  {
-    ConnectionPool* connection_pool = ConnectionPool::get_instance();
-    switch(document->get_hosting_mode())
-    {
-#ifdef GLOM_ENABLE_POSTGRESQL
-
-#ifndef GLOM_ENABLE_CLIENT_ONLY
-    case Document::HOSTING_MODE_POSTGRES_SELF:
-      {
-        ConnectionPoolBackends::PostgresSelfHosted* backend = new ConnectionPoolBackends::PostgresSelfHosted;
-        backend->set_self_hosting_data_uri(document->get_connection_self_hosted_directory_uri());
-        connection_pool->set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
-      }
-      break;
-#endif //GLOM_ENABLE_CLIENT_ONLY
-
-    case Document::HOSTING_MODE_POSTGRES_CENTRAL:
-      {
-        ConnectionPoolBackends::PostgresCentralHosted* backend = new ConnectionPoolBackends::PostgresCentralHosted;
-        backend->set_host(document->get_connection_server());
-        backend->set_port(document->get_connection_port());
-        backend->set_try_other_ports(document->get_connection_try_other_ports());
-        connection_pool->set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
-      }
-      break;
-#endif //GLOM_ENABLE_POSTGRESQL
-
-#ifdef GLOM_ENABLE_SQLITE
-    case Document::HOSTING_MODE_SQLITE:
-      {
-        ConnectionPoolBackends::Sqlite* backend = new ConnectionPoolBackends::Sqlite;
-        backend->set_database_directory_uri(document->get_connection_self_hosted_directory_uri());
-        connection_pool->set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
-      }
-      break;
-#endif // GLOM_ENABLE_SQLITE
-
-    default:
-      //on_document_load() should have checked for this already, informing the user.
-      std::cerr << "Glom: setup_connection_pool_from_document(): Unhandled hosting mode: " << document->get_hosting_mode() << std::endl;
-      g_assert_not_reached();
-      break;
-    }
-
-    // Might be overwritten later when actually attempting a connection:
-    connection_pool->set_user(document->get_connection_user());
-    connection_pool->set_database(document->get_connection_database());
-
-    connection_pool->set_ready_to_connect();
-  }
-}
-
 #ifndef GLOM_ENABLE_CLIENT_ONLY
 void Frame_Glom::on_connection_initialize_progress()
 {
@@ -2105,7 +2046,7 @@ bool Frame_Glom::connection_request_password_and_choose_new_database_name()
     return false;
 
   ConnectionPool* connection_pool = ConnectionPool::get_instance();
-  setup_connection_pool_from_document(document);
+  connection_pool->setup_from_document(document);
 
   if(!m_pDialogConnection)
   {
@@ -2383,7 +2324,7 @@ bool Frame_Glom::connection_request_password_and_attempt(bool& database_not_foun
 
   //Start a self-hosted server if necessary:
   ConnectionPool* connection_pool = ConnectionPool::get_instance();
-  setup_connection_pool_from_document(document);
+  connection_pool->setup_from_document(document);
   if(!connection_pool->startup( sigc::mem_fun(*this, &Frame_Glom::on_connection_startup_progress) ))
     return false;
 
diff --git a/glom/libglom/connectionpool.cc b/glom/libglom/connectionpool.cc
index 6152b89..13b522d 100644
--- a/glom/libglom/connectionpool.cc
+++ b/glom/libglom/connectionpool.cc
@@ -24,7 +24,10 @@
 #include <libglom/document/document.h>
 #include <libglom/utils.h>
 //#include <libgdamm/connectionevent.h>
-#include <glibmm/i18n.h>
+
+#include <libglom/connectionpool_backends/postgres_central.h>
+#include <libglom/connectionpool_backends/postgres_self.h>
+# include <libglom/connectionpool_backends/sqlite.h>
 
 #ifdef G_OS_WIN32
 # include <windows.h>
@@ -35,6 +38,9 @@
 
 #include <signal.h> //To catch segfaults
 
+#include <glibmm/i18n.h>
+
+
 #ifndef G_OS_WIN32
 static EpcProtocol publish_protocol = EPC_PROTOCOL_HTTPS;
 #endif
@@ -123,6 +129,48 @@ ConnectionPool* ConnectionPool::get_instance()
   }
 }
 
+void ConnectionPool::setup_from_document(const Document* document)
+{
+  switch(document->get_hosting_mode())
+  {
+  case Document::HOSTING_MODE_POSTGRES_SELF:
+    {
+      ConnectionPoolBackends::PostgresSelfHosted* backend = new ConnectionPoolBackends::PostgresSelfHosted;
+      backend->set_self_hosting_data_uri(document->get_connection_self_hosted_directory_uri());
+      set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
+    }
+    break;
+  case Document::HOSTING_MODE_POSTGRES_CENTRAL:
+    {
+      ConnectionPoolBackends::PostgresCentralHosted* backend = new ConnectionPoolBackends::PostgresCentralHosted;
+      backend->set_host(document->get_connection_server());
+      backend->set_port(document->get_connection_port());
+      backend->set_try_other_ports(document->get_connection_try_other_ports());
+      set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
+    }
+    break;
+  case Document::HOSTING_MODE_SQLITE:
+    {
+      ConnectionPoolBackends::Sqlite* backend = new ConnectionPoolBackends::Sqlite;
+      backend->set_database_directory_uri(document->get_connection_self_hosted_directory_uri());
+      set_backend(std::auto_ptr<ConnectionPool::Backend>(backend));
+    }
+    break;
+
+  default:
+    //on_document_load() should have checked for this already, informing the user.
+    std::cerr << "Glom: setup_connection_pool_from_document(): Unhandled hosting mode: " << document->get_hosting_mode() << std::endl;
+    g_assert_not_reached();
+    break;
+  }
+
+  // Might be overwritten later when actually attempting a connection:
+  set_user(document->get_connection_user());
+  set_database(document->get_connection_database());
+
+  set_ready_to_connect();
+}
+  
 void ConnectionPool::delete_instance()
 {
   if(m_instance)
diff --git a/glom/libglom/connectionpool.h b/glom/libglom/connectionpool.h
index ac1426f..f285534 100644
--- a/glom/libglom/connectionpool.h
+++ b/glom/libglom/connectionpool.h
@@ -99,6 +99,11 @@ public:
    */
   static ConnectionPool* get_instance();
 
+  /** Make the ConnectionPool use the correct backend, with the necessary details,
+   * as required by the document.
+   */
+  void setup_from_document(const Document* document);
+
   /// Delete the singleton so it doesn't show up as leaked memory in, for instance, valgrind.
   static void delete_instance();
   
diff --git a/tests/test_selfhosting_new_empty/test_selfhosting_new_empty.cc b/tests/test_selfhosting_new_empty/test_selfhosting_new_empty.cc
new file mode 100644
index 0000000..5e6d9c8
--- /dev/null
+++ b/tests/test_selfhosting_new_empty/test_selfhosting_new_empty.cc
@@ -0,0 +1,181 @@
+/* Glom
+ *
+ * Copyright (C) 2010 Openismus GmbH
+ *
+ * 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; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <libglom/document/document.h>
+#include <libglom/connectionpool.h>
+#include <libglom/connectionpool_backends/postgres_self.h>
+#include <giomm/file.h>
+#include <libglom/init.h>
+
+static void on_initialize_progress()
+{
+  std::cout << "Database initialization progress" << std::endl;
+}
+
+static void on_startup_progress()
+{
+  std::cout << "Database startup progress" << std::endl;
+}
+
+static void on_cleanup_progress()
+{
+  std::cout << "Database cleanup progress" << std::endl;
+}
+
+/** Delete a directory, if it exists, and its contents.
+ * Unlike g_file_delete(), this does not fail if the directory is not empty.
+ */
+static bool delete_directory(const Glib::RefPtr<Gio::File>& directory)
+{
+  if(!(directory->query_exists()))
+    return true;
+
+  //(Recursively) Delete any child files and directories,
+  //so we can delete this directory.
+  Glib::RefPtr<Gio::FileEnumerator> enumerator = directory->enumerate_children();
+  
+  Glib::RefPtr<Gio::FileInfo> info = enumerator->next_file();
+  while(info)
+  {
+    Glib::RefPtr<Gio::File> child = directory->get_child(info->get_name());
+    bool removed_child = false;
+    if(child->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
+      removed_child = delete_directory(child);
+    else
+      removed_child = child->remove();
+
+    if(!removed_child)
+       return false;
+    
+    info = enumerator->next_file();
+  }
+
+  //Delete the actual directory:
+  if(!directory->remove())
+    return false;
+  
+  return true;
+}
+
+/** Delete a directory, if it exists, and its contents.
+ * Unlike g_file_delete(), this does not fail if the directory is not empty.
+ */
+static bool delete_directory(const std::string& uri)
+{
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);
+  return delete_directory(file);
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  // Get a URI for a test file:
+  Glib::ustring uri;
+
+  #ifdef GLIBMM_EXCEPTIONS_ENABLED
+  try
+  {
+    const std::string path = 
+       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED, 
+         "example_music_collection.glom");
+    uri = Glib::filename_to_uri(path);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "Exception from Glib::filename_to_uri(): " << ex.what();
+    return EXIT_FAILURE;
+  }
+  #else
+  std::auto_ptr<Glib::Error> ex;
+  uri = Glib::filename_to_uri("/usr/share/glom/doc/examples/example_music_collection.glom", ex);
+  #endif
+
+  //std::cout << "URI=" << uri << std::endl;
+
+
+  // Load the document:
+  Glom::Document document;
+  document.set_file_uri(uri);
+  int failure_code = 0;
+  const bool test = document.load(failure_code);
+  //std::cout << "Document load result=" << test << std::endl;
+
+  if(!test)
+  {
+    std::cerr << "Document::load() failed with failure_code=" << failure_code << std::endl;
+    return EXIT_FAILURE;
+  }
+  
+  g_assert(document.get_is_example_file());;
+
+  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
+  
+  //Save a copy, specifying the path to file in a directory:
+  //For instance, /tmp/testfileglom/testfile.glom");
+  const std::string temp_filename = "testglom";
+  const std::string temp_filepath_dir = Glib::build_filename(Glib::get_tmp_dir(), 
+    temp_filename);
+  const std::string temp_filepath = Glib::build_filename(temp_filepath_dir, temp_filename);
+  
+  //Make sure that the file does not exist yet:
+  {
+    const Glib::ustring uri = Glib::filename_to_uri(temp_filepath_dir);
+    delete_directory(uri);
+  }
+
+  //Save the example as a real file:
+  const Glib::ustring file_uri = Glib::filename_to_uri(temp_filepath);
+  document.set_file_uri(file_uri);
+  
+  document.set_hosting_mode(Glom::Document::HOSTING_MODE_POSTGRES_SELF);
+  document.set_is_example_file(false);
+  const bool saved = document.save();
+  g_assert(saved);
+
+  //Specify the backend and backend-specific details to be used by the connectionpool.
+  connection_pool->setup_from_document(&document);
+
+  const Glib::ustring password = "testpassword";
+  const Glib::ustring user = "testuser";
+  connection_pool->set_user(user);
+  connection_pool->set_password(password);
+
+  //Create the self-hosting files:
+  const Glom::ConnectionPool::InitErrors initialized_errors = connection_pool->initialize( sigc::ptr_fun(&on_initialize_progress) );
+  g_assert(initialized_errors == Glom::ConnectionPool::Backend::INITERROR_NONE);
+
+  //Start self-hosting:
+  //TODO: Let this happen automatically on first connection?
+  const bool started = connection_pool->startup( sigc::ptr_fun(&on_startup_progress) );
+  g_assert(started);
+  
+  connection_pool->cleanup( sigc::ptr_fun(&on_cleanup_progress) );  
+
+  //Make sure the directory is removed at the end:
+  {
+    const Glib::ustring uri = Glib::filename_to_uri(temp_filepath_dir);
+    delete_directory(uri);
+  }
+  
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]