Browse Source

ludbo tojson; tests

tags/v1.0.0
Robin Thoni 8 years ago
parent
commit
891fe1ede6

+ 66
- 4
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

@@ -123,6 +123,59 @@ public abstract class LuDbo {
123 123
         return null;
124 124
     }
125 125
 
126
+    public Object getJsonFromObject(Class clazz, Object obj)
127
+    {
128
+        if (obj == null) {
129
+            return null;
130
+        }
131
+        if (clazz == char.class || clazz == Character.class) {
132
+            return obj.toString();
133
+        }
134
+        if (clazz.isPrimitive() || clazz == Byte.class || clazz == Character.class
135
+                || clazz == Short.class || clazz == Integer.class || clazz == Long.class
136
+                || clazz == Float.class || clazz == Double.class || clazz == Boolean.class
137
+                || clazz == String.class) {
138
+            return obj;
139
+        }
140
+        if (LuDbo.class.isAssignableFrom(clazz)) {
141
+            return ((LuDbo)obj).toArray();
142
+        }
143
+        if (List.class.isAssignableFrom(clazz)) {
144
+            List objList = (List) obj;
145
+            List list = new Vector();
146
+
147
+            for (Object item : objList) {
148
+                list.add(getJsonFromObject(item == null ? null : item.getClass(), item));
149
+            }
150
+
151
+            return list;
152
+        }
153
+        if (AbstractMap.class.isAssignableFrom(clazz)) {
154
+            HashMap objMap = (HashMap) obj;
155
+            HashMap map = new HashMap();
156
+
157
+            for (Object k : objMap.keySet()) {
158
+                if (k.getClass() == String.class) {
159
+                    Object item = objMap.get(k);
160
+                    map.put(k, getJsonFromObject(item == null ? null : item.getClass(), item));
161
+                }
162
+            }
163
+
164
+            return map;
165
+        }
166
+        if (clazz.isArray()) {
167
+            List list = new Vector();
168
+
169
+            for (int i = 0; i < Array.getLength(obj); ++i) {
170
+                Object item = Array.get(obj, i);
171
+                list.add(getJsonFromObject(item == null ? null : item.getClass(), item));
172
+            }
173
+
174
+            return list;
175
+        }
176
+        return null;
177
+    }
178
+
126 179
     public Object getInstance(Class clazz) throws Exception {
127 180
         if (clazz == List.class) {
128 181
             return new Vector<>();
@@ -138,8 +191,8 @@ public abstract class LuDbo {
138 191
         for (Field field : getClass().getDeclaredFields()) {
139 192
             String key = getJsonFromField(field.getName());
140 193
             Class clazz = field.getType();
141
-            Object value = null;
142 194
             if (json.has(key)) {
195
+                Object value = null;
143 196
                 if (List.class.isAssignableFrom(clazz)) {
144 197
                     ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
145 198
                     Class type = (Class) parameterizedType.getActualTypeArguments()[0];
@@ -177,10 +230,10 @@ public abstract class LuDbo {
177 230
                 else {
178 231
                     value = getObjectFromJson(clazz, json, key);
179 232
                 }
233
+                field.setAccessible(true);
234
+                field.set(this, value);
235
+                field.setAccessible(false);
180 236
             }
181
-            field.setAccessible(true);
182
-            field.set(this, value);
183
-            field.setAccessible(false);
184 237
         }
185 238
     }
186 239
 
@@ -191,6 +244,15 @@ public abstract class LuDbo {
191 244
         for (Field field : getClass().getDeclaredFields()) {
192 245
             String key = getJsonFromField(field.getName());
193 246
             Class clazz = field.getType();
247
+            field.setAccessible(true);
248
+            Object obj = null;
249
+            try {
250
+                obj = field.get(this);
251
+            } catch (IllegalAccessException e) {
252
+                e.printStackTrace();
253
+            }
254
+            field.setAccessible(false);
255
+            map.put(key, getJsonFromObject(clazz, obj));
194 256
         }
195 257
 
196 258
         return map;

+ 84
- 0
luticateutils/src/test/java/com/luticate/utils/TestDbo.java View File

@@ -4,6 +4,7 @@ import com.luticate.utils.dbo.LuDbo;
4 4
 
5 5
 import java.io.Serializable;
6 6
 import java.util.AbstractMap;
7
+import java.util.Arrays;
7 8
 import java.util.HashMap;
8 9
 import java.util.List;
9 10
 
@@ -243,4 +244,87 @@ public class TestDbo extends LuDbo implements Serializable {
243 244
     public void setHashMap2(AbstractMap<String, Test2Dbo> hashMap2) {
244 245
         _hashMap2 = hashMap2;
245 246
     }
247
+
248
+    @Override
249
+    public boolean equals(Object o) {
250
+        if (this == o) return true;
251
+        if (o == null || getClass() != o.getClass()) return false;
252
+
253
+        TestDbo testDbo = (TestDbo) o;
254
+
255
+        if (_byte != testDbo._byte) return false;
256
+        if (_char != testDbo._char) return false;
257
+        if (_short != testDbo._short) return false;
258
+        if (_int != testDbo._int) return false;
259
+        if (_long != testDbo._long) return false;
260
+        if (Float.compare(testDbo._float, _float) != 0) return false;
261
+        if (Double.compare(testDbo._double, _double) != 0) return false;
262
+        if (_boolean != testDbo._boolean) return false;
263
+        if (_byteObject != null ? !_byteObject.equals(testDbo._byteObject) : testDbo._byteObject != null)
264
+            return false;
265
+        if (_charObject != null ? !_charObject.equals(testDbo._charObject) : testDbo._charObject != null)
266
+            return false;
267
+        if (_shortObject != null ? !_shortObject.equals(testDbo._shortObject) : testDbo._shortObject != null)
268
+            return false;
269
+        if (_intObject != null ? !_intObject.equals(testDbo._intObject) : testDbo._intObject != null)
270
+            return false;
271
+        if (_longObject != null ? !_longObject.equals(testDbo._longObject) : testDbo._longObject != null)
272
+            return false;
273
+        if (_floatObject != null ? !_floatObject.equals(testDbo._floatObject) : testDbo._floatObject != null)
274
+            return false;
275
+        if (_doubleObject != null ? !_doubleObject.equals(testDbo._doubleObject) : testDbo._doubleObject != null)
276
+            return false;
277
+        if (_booleanObject != null ? !_booleanObject.equals(testDbo._booleanObject) : testDbo._booleanObject != null)
278
+            return false;
279
+        if (_string != null ? !_string.equals(testDbo._string) : testDbo._string != null)
280
+            return false;
281
+        if (_test2Dbo != null ? !_test2Dbo.equals(testDbo._test2Dbo) : testDbo._test2Dbo != null)
282
+            return false;
283
+        if (_test2Dbo2 != null ? !_test2Dbo2.equals(testDbo._test2Dbo2) : testDbo._test2Dbo2 != null)
284
+            return false;
285
+        if (_integers != null ? !_integers.equals(testDbo._integers) : testDbo._integers != null)
286
+            return false;
287
+        if (!Arrays.equals(_ints, testDbo._ints)) return false;
288
+        if (_test2Dbos != null ? !_test2Dbos.equals(testDbo._test2Dbos) : testDbo._test2Dbos != null)
289
+            return false;
290
+        // Probably incorrect - comparing Object[] arrays with Arrays.equals
291
+        if (!Arrays.equals(_dbos, testDbo._dbos)) return false;
292
+        if (_hashMap != null ? !_hashMap.equals(testDbo._hashMap) : testDbo._hashMap != null)
293
+            return false;
294
+        return _hashMap2 != null ? _hashMap2.equals(testDbo._hashMap2) : testDbo._hashMap2 == null;
295
+
296
+    }
297
+
298
+    @Override
299
+    public int hashCode() {
300
+        int result;
301
+        long temp;
302
+        result = (int) _byte;
303
+        result = 31 * result + (int) _char;
304
+        result = 31 * result + (int) _short;
305
+        result = 31 * result + _int;
306
+        result = 31 * result + (int) (_long ^ (_long >>> 32));
307
+        result = 31 * result + (_float != +0.0f ? Float.floatToIntBits(_float) : 0);
308
+        temp = Double.doubleToLongBits(_double);
309
+        result = 31 * result + (int) (temp ^ (temp >>> 32));
310
+        result = 31 * result + (_boolean ? 1 : 0);
311
+        result = 31 * result + (_byteObject != null ? _byteObject.hashCode() : 0);
312
+        result = 31 * result + (_charObject != null ? _charObject.hashCode() : 0);
313
+        result = 31 * result + (_shortObject != null ? _shortObject.hashCode() : 0);
314
+        result = 31 * result + (_intObject != null ? _intObject.hashCode() : 0);
315
+        result = 31 * result + (_longObject != null ? _longObject.hashCode() : 0);
316
+        result = 31 * result + (_floatObject != null ? _floatObject.hashCode() : 0);
317
+        result = 31 * result + (_doubleObject != null ? _doubleObject.hashCode() : 0);
318
+        result = 31 * result + (_booleanObject != null ? _booleanObject.hashCode() : 0);
319
+        result = 31 * result + (_string != null ? _string.hashCode() : 0);
320
+        result = 31 * result + (_test2Dbo != null ? _test2Dbo.hashCode() : 0);
321
+        result = 31 * result + (_test2Dbo2 != null ? _test2Dbo2.hashCode() : 0);
322
+        result = 31 * result + (_integers != null ? _integers.hashCode() : 0);
323
+        result = 31 * result + Arrays.hashCode(_ints);
324
+        result = 31 * result + (_test2Dbos != null ? _test2Dbos.hashCode() : 0);
325
+        result = 31 * result + Arrays.hashCode(_dbos);
326
+        result = 31 * result + (_hashMap != null ? _hashMap.hashCode() : 0);
327
+        result = 31 * result + (_hashMap2 != null ? _hashMap2.hashCode() : 0);
328
+        return result;
329
+    }
246 330
 }

+ 97
- 71
luticateutils/src/test/java/com/luticate/utils/TestDboTest.java View File

@@ -1,6 +1,6 @@
1 1
 package com.luticate.utils;
2 2
 
3
-import org.json.JSONException;
3
+import org.json.JSONObject;
4 4
 import org.junit.Test;
5 5
 
6 6
 import static org.junit.Assert.*;
@@ -11,72 +11,72 @@ import static org.junit.Assert.*;
11 11
 
12 12
 public class TestDboTest {
13 13
 
14
-    @Test
15
-    public void deserializeTest() throws Exception {
16
-        TestDbo test = new TestDbo();
17
-        test.fromString("{" +
18
-                "\"byte\": 42," +
19
-                "\"char\": \"h\"," +
20
-                "\"short\": 4200," +
21
-                "\"int\": 420042," +
22
-                "\"long\": 42004200," +
23
-                "\"float\": 42.24," +
24
-                "\"double\": 4200.0024," +
25
-                "\"boolean\": true," +
26
-
27
-                "\"byteObject\": 24," +
28
-                "\"charObject\": \"w\"," +
29
-                "\"shortObject\": 2400," +
30
-                "\"intObject\": 240024," +
31
-                "\"longObject\": 24002400," +
32
-                "\"floatObject\": 24.42," +
33
-                "\"doubleObject\": 2400.0042," +
34
-                "\"booleanObject\": false," +
35
-
36
-                "\"string\": \"Hello World!\"," +
37
-
38
-                "\"test2Dbo\": {" +
39
-                "\"aString\": \"a string\"," +
40
-                "\"aInt\": 4242" +
41
-                "}," +
42
-
43
-                "\"integers\": [" +
44
-                "0," +
45
-                "42," +
46
-                "24" +
47
-                "]," +
48
-                "ints: [" +
49
-                "24," +
50
-                "42," +
51
-                "0" +
52
-                "]," +
53
-
54
-                "test2Dbos: [" +
55
-                "{" +
56
-                "\"aString\": \"42 42\"," +
57
-                "\"aInt\": 4242" +
58
-                "}" +
59
-                "]," +
60
-                "dbos: [" +
61
-                "{" +
62
-                "\"aString\": \"24 24\"," +
63
-                "\"aInt\": 2424" +
64
-                "}" +
65
-                "]," +
66
-
67
-                "\"hashMap\": {" +
68
-                "\"forty-two\": 42," +
69
-                "\"twenty-four\": 24" +
70
-                "}," +
71
-
72
-                "\"hashMap2\": {" +
73
-                "\"an object\": {" +
74
-                "\"aString\": \"42 24\"," +
75
-                "\"aInt\": 4224" +
76
-                "}" +
77
-                "}" +
78
-
79
-                "}");
14
+    public static String TEST_DBO_JSON =  "{" +
15
+            "\"byte\": 42," +
16
+            "\"char\": \"h\"," +
17
+            "\"short\": 4200," +
18
+            "\"int\": 420042," +
19
+            "\"long\": 42004200," +
20
+            "\"float\": 42.24," +
21
+            "\"double\": 4200.0024," +
22
+            "\"boolean\": true," +
23
+
24
+            "\"byteObject\": 24," +
25
+            "\"charObject\": \"w\"," +
26
+            "\"shortObject\": 2400," +
27
+            "\"intObject\": 240024," +
28
+            "\"longObject\": 24002400," +
29
+            "\"floatObject\": 24.42," +
30
+            "\"doubleObject\": 2400.0042," +
31
+            "\"booleanObject\": false," +
32
+
33
+            "\"string\": \"Hello World!\"," +
34
+
35
+            "\"test2Dbo\": {" +
36
+            "\"aString\": \"a string\"," +
37
+            "\"aInt\": 4242" +
38
+            "}," +
39
+
40
+            "\"integers\": [" +
41
+            "0," +
42
+            "42," +
43
+            "24" +
44
+            "]," +
45
+            "ints: [" +
46
+            "24," +
47
+            "42," +
48
+            "0" +
49
+            "]," +
50
+
51
+            "test2Dbos: [" +
52
+            "{" +
53
+            "\"aString\": \"42 42\"," +
54
+            "\"aInt\": 4242" +
55
+            "}" +
56
+            "]," +
57
+            "dbos: [" +
58
+            "{" +
59
+            "\"aString\": \"24 24\"," +
60
+            "\"aInt\": 2424" +
61
+            "}" +
62
+            "]," +
63
+
64
+            "\"hashMap\": {" +
65
+            "\"forty-two\": 42," +
66
+            "\"twenty-four\": 24" +
67
+            "}," +
68
+
69
+            "\"hashMap2\": {" +
70
+            "\"an object\": {" +
71
+            "\"aString\": \"42 24\"," +
72
+            "\"aInt\": 4224" +
73
+            "}" +
74
+            "}" +
75
+
76
+            "}";
77
+
78
+    public void testTestDbo(TestDbo test)
79
+    {
80 80
         assertEquals(42, test.getByte());
81 81
         assertEquals(24, test.getByteObject().byteValue());
82 82
         assertEquals('h', test.getChar());
@@ -137,14 +137,40 @@ public class TestDboTest {
137 137
         assertNotNull(test.getHashMap2().get("an object"));
138 138
         assertEquals("42 24", test.getHashMap2().get("an object").getaString());
139 139
         assertEquals(4224, test.getHashMap2().get("an object").getaInt());
140
+
141
+        assertEquals(test, test);
142
+    }
143
+
144
+    @Test
145
+    public void deserializeTest() throws Exception {
146
+        TestDbo test = new TestDbo();
147
+        test.fromString(TEST_DBO_JSON);
148
+        testTestDbo(test);
140 149
     }
141 150
 
142 151
     @Test
143
-    public void serializeTest()
152
+    public void serializeTest() throws Exception
144 153
     {
145
-        Test2Dbo dbo = new Test2Dbo();
146
-        dbo.setaInt(2442);
147
-        dbo.setaString("24 42");
154
+        TestDbo test = new TestDbo();
155
+        test.fromString(TEST_DBO_JSON);
156
+        JSONObject json = test.toJson();
157
+
158
+        TestDbo test2 = new TestDbo();
159
+        test2.fromJson(json);
160
+
161
+        testTestDbo(test2);
162
+    }
163
+
164
+    @Test
165
+    public void serializeStringTest() throws Exception
166
+    {
167
+        TestDbo test = new TestDbo();
168
+        test.fromString(TEST_DBO_JSON);
169
+        JSONObject json = test.toJson();
170
+
171
+        TestDbo test2 = new TestDbo();
172
+        test2.fromString(json.toString());
148 173
 
174
+        testTestDbo(test2);
149 175
     }
150 176
 }

Loading…
Cancel
Save