|
GnuCash 2.4.99
|
00001 /*************************************************************************** 00002 * test-load-xml2.c 00003 * 00004 * Fri Oct 7 20:51:46 2005 00005 * Copyright 2005 Neil Williams 00006 * linux@codehelp.co.uk 00007 ****************************************************************************/ 00008 /* 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00022 * 02110-1301, USA. 00023 */ 00024 00025 /* @file test-load-xml2.c 00026 * @brief test the loading of a version-2 gnucash XML file 00027 */ 00028 00029 #include "config.h" 00030 #include <stdlib.h> 00031 #include <sys/types.h> 00032 #include <sys/stat.h> 00033 #include <unistd.h> 00034 #include <dirent.h> 00035 #include <string.h> 00036 #include <glib.h> 00037 #include <glib-object.h> 00038 #include <glib/gstdio.h> 00039 00040 #include <cashobjects.h> 00041 #include <TransLog.h> 00042 #include <gnc-engine.h> 00043 #include "../gnc-backend-xml.h" 00044 #include "../io-gncxml-v2.h" 00045 00046 #include <test-stuff.h> 00047 #include <unittest-support.h> 00048 #include <test-engine-stuff.h> 00049 #include <test-file-stuff.h> 00050 00051 #define GNC_LIB_NAME "gncmod-backend-xml" 00052 00053 static void 00054 remove_files_pattern(const char *begining, const char *ending) 00055 { 00056 } 00057 00058 static void 00059 remove_locks(const char *filename) 00060 { 00061 struct stat buf; 00062 char *to_remove; 00063 00064 { 00065 to_remove = g_strdup_printf("%s.LCK", filename); 00066 if (g_stat(to_remove, &buf) != -1) 00067 { 00068 g_unlink(to_remove); 00069 } 00070 g_free(to_remove); 00071 } 00072 00073 remove_files_pattern(filename, ".LCK"); 00074 } 00075 00076 static void 00077 test_load_file(const char *filename) 00078 { 00079 QofSession *session; 00080 QofBook *book; 00081 Account *root; 00082 gboolean ignore_lock; 00083 gchar *logdomain = "GConf"; 00084 guint loglevel = G_LOG_LEVEL_WARNING; 00085 TestErrorStruct check = { loglevel, logdomain, NULL }; 00086 g_log_set_handler (logdomain, loglevel, 00087 (GLogFunc)test_checked_handler, &check); 00088 00089 session = qof_session_new(); 00090 00091 remove_locks(filename); 00092 00093 ignore_lock = (safe_strcmp(g_getenv("SRCDIR"), ".") != 0); 00094 qof_session_begin(session, filename, ignore_lock, FALSE, TRUE); 00095 00096 qof_session_load(session, NULL); 00097 book = qof_session_get_book (session); 00098 00099 root = gnc_book_get_root_account(book); 00100 do_test (gnc_account_get_book (root) == book, 00101 "book and root account don't match"); 00102 00103 do_test_args(qof_session_get_error(session) == ERR_BACKEND_NO_ERR, 00104 "session load xml2", __FILE__, __LINE__, 00105 "qof error=%d for file [%s]", 00106 qof_session_get_error(session), filename); 00107 /* Uncomment the line below to generate corrected files */ 00108 qof_session_save( session, NULL ); 00109 qof_session_end(session); 00110 } 00111 00112 int 00113 main (int argc, char ** argv) 00114 { 00115 const char *location = g_getenv("GNC_TEST_FILES"); 00116 GDir *xml2_dir; 00117 00118 g_type_init(); 00119 qof_init(); 00120 cashobjects_register(); 00121 do_test(qof_load_backend_library ("../.libs/", GNC_LIB_NAME), 00122 " loading gnc-backend-xml GModule failed"); 00123 00124 if (!location) 00125 { 00126 location = "test-files/xml2"; 00127 } 00128 00129 xaccLogDisable(); 00130 00131 if ((xml2_dir = g_dir_open(location, 0, NULL)) == NULL) 00132 { 00133 failure("unable to open xml2 directory"); 00134 } 00135 else 00136 { 00137 const gchar *entry; 00138 00139 while ((entry = g_dir_read_name(xml2_dir)) != NULL) 00140 { 00141 if (g_str_has_suffix(entry, ".gml2")) 00142 { 00143 gchar *to_open = g_build_filename(location, entry, (gchar*)NULL); 00144 if (!g_file_test(to_open, G_FILE_TEST_IS_DIR)) 00145 { 00146 test_load_file(to_open); 00147 } 00148 g_free(to_open); 00149 } 00150 } 00151 } 00152 00153 g_dir_close(xml2_dir); 00154 00155 print_test_results(); 00156 qof_close(); 00157 exit(get_rv()); 00158 }
1.7.4