KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > Compare


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test;
22
23 import java.lang.reflect.*;
24
25 import com.db4o.*;
26 import com.db4o.foundation.*;
27
28 public class Compare {
29
30     static boolean hasPublicConstructor(Class JavaDoc a_class) {
31         if (a_class == null || a_class == String JavaDoc.class) {
32             return false;
33         }
34         try {
35             Object JavaDoc o = a_class.newInstance();
36             if (o != null)
37                 return true;
38         } catch (Throwable JavaDoc t) {
39         }
40         return false;
41     }
42     
43     static boolean isPrimitiveOrPrimitiveWrapper(Class JavaDoc clazz){
44         if(clazz.isPrimitive()){
45             return true;
46         }
47         return Platform4.isSimple(clazz);
48     }
49
50     static Object JavaDoc normalizeNArray(Object JavaDoc a_object) {
51         if (Array.getLength(a_object) > 0) {
52             Object JavaDoc first = Array.get(a_object, 0);
53             if (first != null && first.getClass().isArray()) {
54                 int dim[] = arrayDimensions(a_object);
55                 Object JavaDoc all = new Object JavaDoc[arrayElementCount(dim)];
56                 normalizeNArray1(a_object, all, 0, dim, 0);
57                 return all;
58             }
59         }
60         return a_object;
61     }
62
63     public static void compare(
64         com.db4o.ObjectContainer a_con,
65         Object JavaDoc a_Compare,
66         Object JavaDoc a_With,
67         String JavaDoc a_path,
68         Collection4 a_list) {
69         if (!Regression.DEACTIVATE) {
70             a_con.activate(a_With, 1);
71         }
72         
73         
74
75         if (a_list == null) {
76             a_list = new Collection4();
77         }
78         // takes care of repeating calls to the same object
79
if (a_list.containsByIdentity(a_Compare)) {
80             return;
81         }
82
83         a_list.add(a_Compare);
84
85         if (a_path == null || a_path.length() < 1)
86             if (a_Compare != null) {
87                 a_path = a_Compare.getClass().getName() + ":";
88             } else {
89                 if (a_With != null)
90                     a_path = a_With.getClass().getName() + ":";
91             }
92         String JavaDoc path = a_path;
93         if (a_Compare == null){
94             if (a_With == null) {
95                 return;
96             }
97             Regression.addError("1==null:" + path);
98             return;
99         }
100         if (a_With == null) {
101             Regression.addError("2==null:" + path);
102             return;
103         }
104         Class JavaDoc compareClass = a_Compare.getClass();
105         if (!compareClass.isInstance(a_With)) {
106             Regression.addError(
107                 "class!=:" + path + compareClass.getName() + ":" + a_With.getClass().getName());
108             return;
109         }
110         if(isPrimitiveOrPrimitiveWrapper(compareClass)){
111             if(a_Compare.equals(a_With)){
112                 return;
113             }
114             Regression.addError(
115                 "class!=:" + path + compareClass.getName() + ":" + a_With.getClass().getName());
116             return;
117         }
118         
119         Field l_Fields[] = compareClass.getDeclaredFields();
120         for (int i = 0; i < l_Fields.length; i++) {
121             if (storeableField(compareClass, l_Fields[i])) {
122                 Platform4.setAccessible(l_Fields[i]);
123                 try {
124                     path = a_path + l_Fields[i].getName() + ":";
125                     Object JavaDoc l_Compare = l_Fields[i].get(a_Compare);
126                     Object JavaDoc l_With = l_Fields[i].get(a_With);
127                     if (l_Compare == null) {
128                         if (l_With != null) {
129                             Regression.addError("f1==null:" + path);
130                         }
131                     } else if (l_With == null)
132                         Regression.addError("f2==null:" + path);
133                     else if (l_Compare.getClass().isArray()) {
134                         if (!l_With.getClass().isArray()) {
135                             Regression.addError("f2!=array:" + path);
136                         } else {
137                             l_Compare = normalizeNArray(l_Compare);
138                             l_With = normalizeNArray(l_With);
139                             int l_len = Array.getLength(l_Compare);
140                             if (l_len != Array.getLength(l_With)) {
141                                 Regression.addError("arraylen!=:" + path);
142                             } else {
143                                 Class JavaDoc fieldClass = l_Fields[i].getType().getComponentType();
144                                 boolean l_persistentArray =
145                                     hasPublicConstructor(fieldClass) ||
146                                     isPrimitiveOrPrimitiveWrapper(fieldClass);
147                                 for (int j = 0; j < l_len; j++) {
148                                     Object JavaDoc l_ElementCompare = Array.get(l_Compare, j);
149                                     Object JavaDoc l_ElementWith = Array.get(l_With, j);
150                                     if (l_persistentArray){
151                                         compare(
152                                             a_con,
153                                             l_ElementCompare,
154                                             l_ElementWith,
155                                             path,
156                                             a_list);
157                                     } else if (l_ElementCompare == null) {
158                                         if (l_ElementWith != null){
159                                             Regression.addError("1e" + j + "==null:" + path);
160                                         }
161                                     } else if (l_ElementWith == null) {
162                                         Regression.addError("2e" + j + "==null:" + path);
163                                     } else {
164                                         Class JavaDoc elementCompareClass = l_ElementCompare.getClass();
165                                         if (elementCompareClass != l_ElementWith.getClass()){
166                                             Regression.addError(
167                                                 "e"
168                                                     + j
169                                                     + "!=class:"
170                                                     + path
171                                                     + elementCompareClass.toString()
172                                                     + ":"
173                                                     + l_ElementWith.getClass().toString());
174                                         }else if (hasPublicConstructor(elementCompareClass)) {
175                                             compare(
176                                                 a_con,
177                                                 l_ElementCompare,
178                                                 l_ElementWith,
179                                                 path,
180                                                 a_list);
181                                         } else {
182                                             if (!l_ElementCompare.equals(l_ElementWith))
183                                                 Regression.addError(
184                                                     "e"
185                                                         + j
186                                                         + "!=:"
187                                                         + path
188                                                         + l_ElementCompare.toString()
189                                                         + ":"
190                                                         + l_ElementWith.toString());
191                                         }
192                                     }
193                                 }
194
195                             }
196                         }
197                     } else if (hasPublicConstructor(l_Fields[i].getType()))
198                         compare(a_con, l_Compare, l_With, path, a_list);
199                     else if (!l_Compare.equals(l_With))
200                         Regression.addError("!=:" + path);
201                 } catch (Exception JavaDoc e) {
202                     Regression.addError("Exception:" + path);
203                 }
204             }
205         }
206     }
207
208     static int[] arrayDimensions(Object JavaDoc a_object) {
209         int count = 0;
210         for (Class JavaDoc clazz = a_object.getClass(); clazz.isArray(); clazz = clazz.getComponentType())
211             count++;
212
213         int dim[] = new int[count];
214         for (int i = 0; i < count; i++) {
215             dim[i] = Array.getLength(a_object);
216             a_object = Array.get(a_object, 0);
217         }
218
219         return dim;
220     }
221
222     static int normalizeNArray1(
223         Object JavaDoc a_object,
224         Object JavaDoc a_all,
225         int a_next,
226         int a_dim[],
227         int a_index) {
228         if (a_index == a_dim.length - 1) {
229             for (int i = 0; i < a_dim[a_index]; i++)
230                 Array.set(a_all, a_next++, Array.get(a_object, i));
231
232         } else {
233             for (int i = 0; i < a_dim[a_index]; i++)
234                 a_next =
235                     normalizeNArray1(Array.get(a_object, i), a_all, a_next, a_dim, a_index + 1);
236
237         }
238         return a_next;
239     }
240
241     static int arrayElementCount(int a_dim[]) {
242         int elements = a_dim[0];
243         for (int i = 1; i < a_dim.length; i++)
244             elements *= a_dim[i];
245
246         return elements;
247     }
248
249     static String JavaDoc nl() {
250         return System.getProperty("line.separator");
251     }
252     
253     public static boolean storeableField(Class JavaDoc a_class, Field a_field) {
254         return (!Modifier.isStatic(a_field.getModifiers()))
255             && (!Modifier.isTransient(a_field.getModifiers())
256                 & !(a_field.getName().indexOf("$") > -1));
257     }
258
259 }
260
Popular Tags