GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-load-xml2.c
1 /***************************************************************************
2  * test-load-xml2.c
3  *
4  * Fri Oct 7 20:51:46 2005
5  * Copyright 2005 Neil Williams
7  ****************************************************************************/
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 /* @file test-load-xml2.c
26  * @brief test the loading of a version-2 gnucash XML file
27  */
28 
29 #include "config.h"
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <glib-object.h>
38 #include <glib/gstdio.h>
39 
40 #include <cashobjects.h>
41 #include <TransLog.h>
42 #include <gnc-engine.h>
43 #include "../gnc-backend-xml.h"
44 #include "../io-gncxml-v2.h"
45 
46 #include <test-stuff.h>
47 #include <unittest-support.h>
48 #include <test-engine-stuff.h>
49 #include <test-file-stuff.h>
50 
51 #define GNC_LIB_NAME "gncmod-backend-xml"
52 
53 static void
54 remove_files_pattern(const char *begining, const char *ending)
55 {
56 }
57 
58 static void
59 remove_locks(const char *filename)
60 {
61  struct stat buf;
62  char *to_remove;
63 
64  {
65  to_remove = g_strdup_printf("%s.LCK", filename);
66  if (g_stat(to_remove, &buf) != -1)
67  {
68  g_unlink(to_remove);
69  }
70  g_free(to_remove);
71  }
72 
73  remove_files_pattern(filename, ".LCK");
74 }
75 
76 static void
77 test_load_file(const char *filename)
78 {
79  QofSession *session;
80  QofBook *book;
81  Account *root;
82  gboolean ignore_lock;
83  gchar *logdomain = "backend.xml";
84  guint loglevel = G_LOG_LEVEL_WARNING;
85  TestErrorStruct check = { loglevel, logdomain, NULL };
86  g_log_set_handler (logdomain, loglevel,
87  (GLogFunc)test_checked_handler, &check);
88 
89  session = qof_session_new();
90 
91  remove_locks(filename);
92 
93  ignore_lock = (g_strcmp0(g_getenv("SRCDIR"), ".") != 0);
94  qof_session_begin(session, filename, ignore_lock, FALSE, TRUE);
95 
96  qof_session_load(session, NULL);
97  book = qof_session_get_book (session);
98 
99  root = gnc_book_get_root_account(book);
100  do_test (gnc_account_get_book (root) == book,
101  "book and root account don't match");
102 
103  do_test_args(qof_session_get_error(session) == ERR_BACKEND_NO_ERR,
104  "session load xml2", __FILE__, __LINE__,
105  "qof error=%d for file [%s]",
106  qof_session_get_error(session), filename);
107  /* Uncomment the line below to generate corrected files */
108  qof_session_save( session, NULL );
109  qof_session_end(session);
110 }
111 
112 int
113 main (int argc, char ** argv)
114 {
115  const char *location = g_getenv("GNC_TEST_FILES");
116  int files_tested = 0;
117  GDir *xml2_dir;
118 
119  qof_init();
120  cashobjects_register();
121  do_test(qof_load_backend_library ("../.libs/", GNC_LIB_NAME),
122  " loading gnc-backend-xml GModule failed");
123 
124  if (!location)
125  {
126  location = "test-files/xml2";
127  }
128 
129  xaccLogDisable();
130 
131  if ((xml2_dir = g_dir_open(location, 0, NULL)) == NULL)
132  {
133  failure("unable to open xml2 directory");
134  }
135  else
136  {
137  const gchar *entry;
138 
139  while ((entry = g_dir_read_name(xml2_dir)) != NULL)
140  {
141  if (g_str_has_suffix(entry, ".gml2"))
142  {
143  gchar *to_open = g_build_filename(location, entry, (gchar*)NULL);
144  if (!g_file_test(to_open, G_FILE_TEST_IS_DIR))
145  {
146  test_load_file(to_open);
147  files_tested++;
148  }
149  g_free(to_open);
150  }
151  }
152  }
153 
154  g_dir_close(xml2_dir);
155 
156  if (files_tested == 0)
157  {
158  failure("handled 0 files in test-load-xml2");
159  }
160 
161  print_test_results();
162  qof_close();
163  exit(get_rv());
164 }
void qof_session_save(QofSession *session, QofPercentageFunc percentage_func)
void xaccLogDisable(void)
Definition: TransLog.c:93
QofBook * qof_session_get_book(const QofSession *session)
QofBackendError qof_session_get_error(QofSession *session)
All type declarations for the whole Gnucash engine.
gboolean qof_load_backend_library(const gchar *directory, const gchar *module_name)
Load a QOF-compatible backend shared library.
API for the transaction logger.
void qof_close(void)
Safely close down the Query Object Framework.
void qof_session_begin(QofSession *session, const char *book_id, gboolean ignore_lock, gboolean create, gboolean force)
void qof_session_end(QofSession *session)
void qof_init(void)
Initialise the Query Object Framework.