1 /*
2  * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #include <stdlib.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <libgda/libgda.h>
22 
23 #define fail(x) g_warning (x)
24 #define fail_if(x,y) if (x) g_warning (y)
25 #define fail_unless(x,y) if (!(x)) g_warning (y)
26 
27 #define CHECK_EXTRA_INFO
28 static gboolean do_test_load_file (const gchar *filename);
29 
30 int
main(int argc,char ** argv)31 main (int argc, char **argv)
32 {
33 	int number_failed = 0;
34 	GDir *dir;
35 	GError *err = NULL;
36 	const gchar *name;
37 
38 	gda_init ();
39 
40 	if (argc == 2) {
41 		if (g_str_has_suffix (argv[1], ".xml")) {
42 			g_print ("Tested: %s\n", argv[1]);
43 			if (!do_test_load_file (argv[1]))
44 				number_failed ++;
45 		}
46 	}
47 	else {
48 		/* data models in tests/providers */
49 		gchar *dirname;
50 		dirname = g_build_filename (CHECK_FILES, "tests", "providers", NULL);
51 		if (!(dir = g_dir_open (dirname, 0, &err))) {
52 #ifdef CHECK_EXTRA_INFO
53 			g_warning ("Could not open directory '%s': %s", dirname,
54 				   err && err->message ? err->message : "No detail");
55 #endif
56 			return EXIT_FAILURE;
57 		}
58 
59 		while ((name = g_dir_read_name (dir))) {
60 			if (g_str_has_suffix (name, ".xml")) {
61 				gchar *full_path = g_build_filename (dirname, name, NULL);
62 				g_print ("Tested: %s\n", full_path);
63 				if (!do_test_load_file (full_path))
64 					number_failed ++;
65 				g_free (full_path);
66 			}
67 		}
68 		g_free (dirname);
69 		g_dir_close (dir);
70 
71 		/* data models in the current dir */
72 		dirname = g_build_filename (CHECK_FILES, "tests", "data-models", NULL);
73 		if (!(dir = g_dir_open (dirname, 0, &err))) {
74 #ifdef CHECK_EXTRA_INFO
75 			g_warning ("Could not open directory '%s': %s", dirname,
76 				   err && err->message ? err->message : "No detail");
77 #endif
78 			return EXIT_FAILURE;
79 		}
80 
81 		while ((name = g_dir_read_name (dir))) {
82 			if (g_str_has_suffix (name, ".xml")) {
83 				gchar *full_path = g_build_filename (dirname, name, NULL);
84 				g_print ("Tested: %s\n", full_path);
85 				if (!do_test_load_file (full_path))
86 					number_failed ++;
87 				g_free (full_path);
88 			}
89 		}
90 		g_free (dirname);
91 		g_dir_close (dir);
92 	}
93 
94 	if (number_failed == 0)
95 		g_print ("Ok.\n");
96 	else
97 		g_print ("%d failed\n", number_failed);
98 
99 	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
100 }
101 
102 static gboolean
do_test_load_file(const gchar * filename)103 do_test_load_file (const gchar *filename)
104 {
105 	GdaDataModel *import;
106 	GSList *errors;
107 	gboolean retval = TRUE;
108 	gchar *export, *contents;
109 
110 	/* make sure we only test data model dumps */
111 	xmlDocPtr doc;
112 	xmlNodePtr root;
113 	doc = xmlParseFile (filename);
114 	if (!doc)
115 		/* abort the test */
116 		return TRUE;
117 	root = xmlDocGetRootElement (doc);
118 	if (strcmp (root->name, "gda_array")) {
119 		/* abort the test */
120 		xmlFreeDoc (doc);
121 		return TRUE;
122 	}
123 	xmlFreeDoc (doc);
124 
125 	import = gda_data_model_import_new_file (filename, TRUE, NULL);
126 
127 	if ((errors = gda_data_model_import_get_errors (GDA_DATA_MODEL_IMPORT (import)))) {
128 #ifdef CHECK_EXTRA_INFO
129 		g_warning ("Could not load file '%s': ", filename);
130 		for (; errors; errors = errors->next)
131 			g_print ("\t%s\n",
132 				 ((GError *)(errors->data))->message ? ((GError *)(errors->data))->message : "No detail");
133 #endif
134 		retval = FALSE;
135 		goto out;
136 	}
137 
138 	export = gda_data_model_export_to_string (import, GDA_DATA_MODEL_IO_DATA_ARRAY_XML, NULL, 0, NULL, 0, NULL);
139 	g_assert (g_file_get_contents (filename, &contents, NULL, NULL));
140 	if (strcmp (export, contents)) {
141 #ifdef CHECK_EXTRA_INFO
142 		g_print ("========== Imported data model ==========\n");
143 		gda_data_model_dump (import, stdout);
144 
145 		g_warning ("Model exported as string differs from loaded data model:");
146 		g_print ("========== Export as string ==========\n%s\n", export);
147 		g_file_set_contents ("err.xml", export, -1, NULL);
148 #endif
149 		retval = FALSE;
150 		goto out;
151 	}
152 	g_free (export);
153 	g_free (contents);
154 
155  out:
156 	g_object_unref (import);
157 	return retval;
158 }
159