Files
gnucash/src/backend/xml/test/test-load-xml2.c

158 lines
4.1 KiB
C
Raw Normal View History

/***************************************************************************
* test-load-xml2.c
*
* Fri Oct 7 20:51:46 2005
* Copyright 2005 Neil Williams
* linux@codehelp.co.uk
****************************************************************************/
/*
* 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 Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/* @file test-load-xml2.c
* @brief test the loading of a version-2 gnucash XML file
*/
#include "config.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#include "cashobjects.h"
#include "TransLog.h"
#include "gnc-engine.h"
#include "gnc-backend-xml.h"
#include "io-gncxml-v2.h"
#include "test-stuff.h"
#include "test-engine-stuff.h"
#include "test-file-stuff.h"
#define GNC_LIB_NAME "gncmod-backend-xml"
static void
remove_files_pattern(const char *begining, const char *ending)
{
}
static void
remove_locks(const char *filename)
{
struct stat buf;
char *to_remove;
{
to_remove = g_strdup_printf("%s.LCK", filename);
if (g_stat(to_remove, &buf) != -1)
{
g_unlink(to_remove);
}
g_free(to_remove);
}
remove_files_pattern(filename, ".LCK");
}
static void
test_load_file(const char *filename)
{
QofSession *session;
QofBook *book;
Account *root;
gboolean ignore_lock;
gchar *logdomain = "GConf";
guint loglevel = G_LOG_LEVEL_WARNING;
TestErrorStruct check = { loglevel, logdomain, NULL };
g_log_set_handler (logdomain, loglevel,
(GLogFunc)test_checked_handler, &check);
session = qof_session_new();
remove_locks(filename);
ignore_lock = (safe_strcmp(g_getenv("SRCDIR"), ".") != 0);
Bug 634392: New sql database raises clobber warning A fairly extensive change, because I changed may_clobber from a global qof function (which passed through qofbackend and fetched up in the actual backends) to a local static in the individual backends which raise a new qof_backend_error, QOF_ABCKEND_STORE_EXISTS. This was necessary to reorder the existence check before the lock, because with the sql backends, locking creates the database... which then would return may_clobber as true, even though it really wasn't. New parameter "force" added to session_begin() functions, and "create_if_nonexistant" is renamed to simply "create". The reason for the latter is that the file/database is created regardless of whether it already exists; that's what the clobber check is about. The new "force" parameter is set to true the second time through, after the user has responded to the clobber dialog indicating that the data should be destroyed. Many of the extraneous changes are just adding the new parameter to the session_begin() calls. gnc-file changes to handle the error in favor of calling the no-longer-existing qof_check_may_clobber_data() call after session_begin(). Two minor changes to gnc_file_do_save_as: gnc_add_history and gnc_hook_run are now called on new_session instead of old_session; this ensures that the new file/database is used at the next startup of gnucash. Second, the filename/url is filtered before displaying the "may_clobber" dialog box to prevent displaying plaintext database passwords. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19798 57a11ea4-9604-0410-9ed3-97b8803252fd
2010-11-11 06:12:20 +00:00
qof_session_begin(session, filename, ignore_lock, FALSE, TRUE);
qof_session_load(session, NULL);
book = qof_session_get_book (session);
root = gnc_book_get_root_account(book);
do_test (gnc_account_get_book (root) == book,
"book and root account don't match");
do_test_args(qof_session_get_error(session) == ERR_BACKEND_NO_ERR,
"session load xml2", __FILE__, __LINE__,
"qof error=%d for file [%s]",
qof_session_get_error(session), filename);
/* Uncomment the line below to generate corrected files */
qof_session_save( session, NULL );
qof_session_end(session);
}
int
main (int argc, char ** argv)
{
const char *location = g_getenv("GNC_TEST_FILES");
GDir *xml2_dir;
g_type_init();
qof_init();
cashobjects_register();
do_test(qof_load_backend_library ("../.libs/", GNC_LIB_NAME),
" loading gnc-backend-xml GModule failed");
if (!location)
{
location = "test-files/xml2";
}
xaccLogDisable();
if ((xml2_dir = g_dir_open(location, 0, NULL)) == NULL)
{
failure("unable to open xml2 directory");
}
else
{
const gchar *entry;
while ((entry = g_dir_read_name(xml2_dir)) != NULL)
{
if (g_str_has_suffix(entry, ".gml2"))
{
gchar *to_open = g_build_filename(location, entry, (gchar*)NULL);
if (!g_file_test(to_open, G_FILE_TEST_IS_DIR))
{
test_load_file(to_open);
}
g_free(to_open);
}
}
}
g_dir_close(xml2_dir);
print_test_results();
qof_close();
exit(get_rv());
}