Browse Source

ludbo from json; tests

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
d6e102344e

+ 3
- 3
app/src/main/java/com/rthoni/stssaguenay/dataaccess/RoutesDataAccess.java View File

@@ -1,7 +1,7 @@
1 1
 package com.rthoni.stssaguenay.dataaccess;
2 2
 
3 3
 import com.luticate.utils.business.LuPromise;
4
-import com.luticate.utils.business.LuRequest;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5 5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6 6
 import com.rthoni.stssaguenay.dbo.RoutesDbo;
7 7
 
@@ -11,11 +11,11 @@ import java.util.List;
11 11
  * Created by robin on 9/30/16.
12 12
  */
13 13
 
14
-public class RoutesDataAccess {
14
+public class RoutesDataAccess extends LuDataAccess {
15 15
 
16 16
     public static LuPromise<List<RoutesDbo>> getAll(LuDataAccessConfigDbo config)
17 17
     {
18
-        return LuRequest.get(config, RoutesDbo.PaginatedRoutesDbo.class, "routes")
18
+        return get(config, RoutesDbo.PaginatedRoutesDbo.class, "routes")
19 19
                 .map(new LuPromise.LuConverter<RoutesDbo.PaginatedRoutesDbo, List<RoutesDbo>>() {
20 20
                     @Override
21 21
                     public List<RoutesDbo> convert(RoutesDbo.PaginatedRoutesDbo data) {

+ 3
- 3
app/src/main/java/com/rthoni/stssaguenay/dataaccess/SchedulesDataAccess.java View File

@@ -1,7 +1,7 @@
1 1
 package com.rthoni.stssaguenay.dataaccess;
2 2
 
3 3
 import com.luticate.utils.business.LuPromise;
4
-import com.luticate.utils.business.LuRequest;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5 5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6 6
 import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
7 7
 import com.rthoni.stssaguenay.dbo.RoutesDbo;
@@ -19,7 +19,7 @@ import java.util.List;
19 19
  * Created by robin on 10/1/16.
20 20
  */
21 21
 
22
-public class SchedulesDataAccess {
22
+public class SchedulesDataAccess extends LuDataAccess {
23 23
 
24 24
     public static LuPromise<List<SchedulesDbo>> getMultiple(LuDataAccessConfigDbo config, List<UserFavouriteStopsDbo> favouriteStopDbos, LocalDateTime date, int count)
25 25
     {
@@ -42,7 +42,7 @@ public class SchedulesDataAccess {
42 42
         }
43 43
         map.put("stops", stops.toString());
44 44
 
45
-        return LuRequest.post(config, SchedulesDbo.SchedulesDboArray.class, "schedules/multiple", map).map(new LuPromise.LuConverter<SchedulesDbo.SchedulesDboArray, List<SchedulesDbo>>() {
45
+        return post(config, SchedulesDbo.SchedulesDboArray.class, "schedules/multiple", map).map(new LuPromise.LuConverter<SchedulesDbo.SchedulesDboArray, List<SchedulesDbo>>() {
46 46
             @Override
47 47
             public List<SchedulesDbo> convert(SchedulesDbo.SchedulesDboArray data) {
48 48
                 return data.getData();

+ 3
- 3
app/src/main/java/com/rthoni/stssaguenay/dataaccess/StopsDataAccess.java View File

@@ -1,7 +1,7 @@
1 1
 package com.rthoni.stssaguenay.dataaccess;
2 2
 
3 3
 import com.luticate.utils.business.LuPromise;
4
-import com.luticate.utils.business.LuRequest;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5 5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6 6
 import com.rthoni.stssaguenay.dbo.StopsDbo;
7 7
 
@@ -10,11 +10,11 @@ import java.util.List;
10 10
 /**
11 11
  * Created by robin on 9/29/16.
12 12
  */
13
-public class StopsDataAccess {
13
+public class StopsDataAccess extends LuDataAccess {
14 14
 
15 15
     public static LuPromise<List<StopsDbo>> getAll(LuDataAccessConfigDbo config)
16 16
     {
17
-        return LuRequest.get(config, StopsDbo.PaginatedStopsDbo.class, "stops")
17
+        return get(config, StopsDbo.PaginatedStopsDbo.class, "stops")
18 18
         .map(new LuPromise.LuConverter<StopsDbo.PaginatedStopsDbo, List<StopsDbo>>() {
19 19
             @Override
20 20
             public List<StopsDbo> convert(StopsDbo.PaginatedStopsDbo data) {

+ 4
- 4
app/src/main/java/com/rthoni/stssaguenay/dataaccess/UsersSettingsDataAccess.java View File

@@ -1,7 +1,7 @@
1 1
 package com.rthoni.stssaguenay.dataaccess;
2 2
 
3 3
 import com.luticate.utils.business.LuPromise;
4
-import com.luticate.utils.business.LuRequest;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5 5
 import com.luticate.utils.dbo.LuBoolDbo;
6 6
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
7 7
 import com.luticate.utils.dbo.LuStringDbo;
@@ -17,7 +17,7 @@ import se.simbio.encryption.Encryption;
17 17
  * Created by robin on 10/3/16.
18 18
  */
19 19
 
20
-public class UsersSettingsDataAccess {
20
+public class UsersSettingsDataAccess extends LuDataAccess {
21 21
 
22 22
     public static Encryption getEncryption(String password)
23 23
     {
@@ -38,7 +38,7 @@ public class UsersSettingsDataAccess {
38 38
     {
39 39
         HashMap<String, String> map = new HashMap<>();
40 40
         map.put("id", JSONObject.quote(id));
41
-        return LuRequest.get(config, LuStringDbo.class, "users/settings", map)
41
+        return get(config, LuStringDbo.class, "users/settings", map)
42 42
                 .map(new LuPromise.LuConverter<LuStringDbo, String>() {
43 43
                     @Override
44 44
                     public String convert(LuStringDbo data) {
@@ -52,7 +52,7 @@ public class UsersSettingsDataAccess {
52 52
         HashMap<String, String> map = new HashMap<>();
53 53
         map.put("id", JSONObject.quote(id));
54 54
         map.put("settings", JSONObject.quote(data));
55
-        return LuRequest.post(config, LuBoolDbo.class, "users/settings", map)
55
+        return post(config, LuBoolDbo.class, "users/settings", map)
56 56
                 .map(new LuPromise.LuConverter<LuBoolDbo, LuVoidDbo>() {
57 57
                     @Override
58 58
                     public LuVoidDbo convert(LuBoolDbo data) {

+ 5
- 7
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java View File

@@ -36,7 +36,7 @@ import com.google.android.gms.ads.AdRequest;
36 36
 import com.google.android.gms.ads.AdView;
37 37
 import com.google.android.gms.ads.MobileAds;
38 38
 import com.luticate.utils.business.LuPromise;
39
-import com.luticate.utils.business.LuRequest;
39
+import com.luticate.utils.dataaccess.LuRequest;
40 40
 import com.luticate.utils.dbo.LuVoidDbo;
41 41
 import com.rthoni.stssaguenay.R;
42 42
 import com.rthoni.stssaguenay.business.STSBusiness;
@@ -279,9 +279,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
279 279
         if (requestCode == ADD_FAVOURITE_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
280 280
             UserFavouriteStopsDbo favouriteStopDbo = new UserFavouriteStopsDbo();
281 281
             try {
282
-                JSONObject obj = new JSONObject(data.getStringExtra(StopPickerActivity.FAVOURITE_STOP_EXTRA_NAME));
283
-                favouriteStopDbo.fromJson(obj);
284
-            } catch (JSONException e) {
282
+                favouriteStopDbo.fromString(data.getStringExtra(StopPickerActivity.FAVOURITE_STOP_EXTRA_NAME));
283
+            } catch (Exception e) {
285 284
                 e.printStackTrace();
286 285
                 return;
287 286
             }
@@ -298,9 +297,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
298 297
         else if (requestCode == SEARCH_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
299 298
             final StopsDbo stopDbo = new StopsDbo();
300 299
             try {
301
-                JSONObject obj = new JSONObject(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
302
-                stopDbo.fromJson(obj);
303
-            } catch (JSONException e) {
300
+                stopDbo.fromString(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
301
+            } catch (Exception e) {
304 302
                 e.printStackTrace();
305 303
                 return;
306 304
             }

+ 3
- 0
build.gradle View File

@@ -7,6 +7,9 @@ buildscript {
7 7
         maven {
8 8
             url 'http://dl.bintray.com/amulyakhare/maven'
9 9
         }
10
+        maven {
11
+            url 'https://maven.rthoni.com'
12
+        }
10 13
     }
11 14
     dependencies {
12 15
         classpath 'com.android.tools.build:gradle:2.2.2'

+ 1
- 0
luticateutils/build.gradle View File

@@ -21,5 +21,6 @@ android {
21 21
 dependencies {
22 22
     compile fileTree(dir: 'libs', include: ['*.jar'])
23 23
     testCompile 'junit:junit:4.12'
24
+    testCompile 'org.json:json:20160810'
24 25
     compile 'com.android.volley:volley:1.0.0'
25 26
 }

luticateutils/src/main/java/com/luticate/utils/business/LuBusinessManager.java → luticateutils/src/main/java/com/luticate/utils/business/LuBusiness.java View File

@@ -3,5 +3,5 @@ package com.luticate.utils.business;
3 3
 /**
4 4
  * Created by robin on 11/27/15.
5 5
  */
6
-public class LuBusinessManager {
6
+public abstract class LuBusiness {
7 7
 }

+ 30
- 0
luticateutils/src/main/java/com/luticate/utils/dataaccess/LuDataAccess.java View File

@@ -1,7 +1,37 @@
1 1
 package com.luticate.utils.dataaccess;
2 2
 
3
+import com.luticate.utils.business.LuPromise;
4
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
5
+import com.luticate.utils.dbo.LuDbo;
6
+
7
+import java.util.HashMap;
8
+
3 9
 /**
4 10
  * Created by robin on 11/27/15.
5 11
  */
6 12
 public abstract class LuDataAccess {
13
+
14
+    public static <T extends LuDbo> LuPromise<T> get(final LuDataAccessConfigDbo config, Class<T> type,
15
+                                                     String url, final HashMap<String, String> getParams)
16
+    {
17
+        return LuRequest.get(config, type, url, getParams);
18
+    }
19
+
20
+    public static <T extends LuDbo> LuPromise<T> get(LuDataAccessConfigDbo config, Class<T> type,
21
+                                                     String url)
22
+    {
23
+        return LuRequest.get(config, type, url);
24
+    }
25
+
26
+    public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
27
+                                                      String url, final HashMap<String, String> postParams)
28
+    {
29
+        return LuRequest.post(config, type, url, postParams);
30
+    }
31
+
32
+    public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
33
+                                                      String url)
34
+    {
35
+        return LuRequest.post(config, type, url);
36
+    }
7 37
 }

luticateutils/src/main/java/com/luticate/utils/business/LuRequest.java → luticateutils/src/main/java/com/luticate/utils/dataaccess/LuRequest.java View File

@@ -1,9 +1,8 @@
1
-package com.luticate.utils.business;
1
+package com.luticate.utils.dataaccess;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.net.Uri;
5 5
 
6
-import com.android.volley.AuthFailureError;
7 6
 import com.android.volley.DefaultRetryPolicy;
8 7
 import com.android.volley.Request;
9 8
 import com.android.volley.RequestQueue;
@@ -11,6 +10,7 @@ import com.android.volley.Response;
11 10
 import com.android.volley.VolleyError;
12 11
 import com.android.volley.toolbox.StringRequest;
13 12
 import com.android.volley.toolbox.Volley;
13
+import com.luticate.utils.business.LuPromise;
14 14
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
15 15
 import com.luticate.utils.dbo.LuDbo;
16 16
 

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

@@ -1,23 +1,175 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3
+import org.json.JSONArray;
3 4
 import org.json.JSONException;
4 5
 import org.json.JSONObject;
5 6
 
7
+import java.lang.reflect.Array;
8
+import java.lang.reflect.Field;
9
+import java.lang.reflect.GenericArrayType;
10
+import java.lang.reflect.ParameterizedType;
11
+import java.lang.reflect.Type;
12
+import java.util.AbstractMap;
6 13
 import java.util.HashMap;
14
+import java.util.List;
15
+import java.util.Vector;
7 16
 
8 17
 /**
9 18
  * Created by robin on 11/27/15.
10 19
  */
11 20
 public abstract class LuDbo {
12 21
 
13
-    public void fromString(String data) throws JSONException {
22
+    public void fromString(String data) throws Exception
23
+    {
14 24
         JSONObject json = new JSONObject(data);
15 25
         fromJson(json);
16 26
     }
17 27
 
18
-    public abstract void fromJson(JSONObject json) throws JSONException;
28
+    public static String getFieldFromJson(String jsonName)
29
+    {
30
+        if (!jsonName.startsWith("_")) {
31
+            return String.format("_%s", jsonName);
32
+        }
33
+        return jsonName;
34
+    }
35
+
36
+    public static String getJsonFromField(String fieldName)
37
+    {
38
+        if (fieldName.startsWith("_")) {
39
+            return fieldName.substring(1);
40
+        }
41
+        return fieldName;
42
+    }
43
+
44
+    public static Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
45
+        if (json.isNull(key)) {
46
+            return null;
47
+        }
19 48
 
20
-    public abstract HashMap<String, Object> toArray();
49
+        if (clazz == Byte.class || clazz == byte.class) {
50
+            return (byte) json.getInt(key);
51
+        }
52
+        if (clazz == Character.class || clazz == char.class) {
53
+            return json.getString(key).charAt(0);
54
+        }
55
+        if (clazz == Short.class || clazz == short.class) {
56
+            return (short) json.getInt(key);
57
+        }
58
+        if (clazz == Integer.class || clazz == int.class) {
59
+            return json.getInt(key);
60
+        }
61
+        if (clazz == Long.class || clazz == long.class) {
62
+            return json.getLong(key);
63
+        }
64
+        if (clazz == Float.class || clazz == float.class) {
65
+            return (float) json.getDouble(key);
66
+        }
67
+        if (clazz == Double.class || clazz == double.class) {
68
+            return json.getDouble(key);
69
+        }
70
+        if (clazz == Boolean.class || clazz == boolean.class) {
71
+            return json.getBoolean(key);
72
+        }
73
+        if (clazz == String.class) {
74
+            return json.getString(key);
75
+        }
76
+        if (LuDbo.class.isAssignableFrom(clazz)) {
77
+            LuDbo dbo = (LuDbo) clazz.newInstance();
78
+            dbo.fromJson(json.getJSONObject(key));
79
+            return dbo;
80
+        }
81
+        return null;
82
+    }
83
+
84
+    public static Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
85
+        if (json.isNull(key)) {
86
+            return null;
87
+        }
88
+
89
+        if (clazz == Byte.class || clazz == byte.class) {
90
+            return (byte) json.getInt(key);
91
+        }
92
+        if (clazz == Character.class || clazz == char.class) {
93
+            return json.getString(key).charAt(0);
94
+        }
95
+        if (clazz == Short.class || clazz == short.class) {
96
+            return (short) json.getInt(key);
97
+        }
98
+        if (clazz == Integer.class || clazz == int.class) {
99
+            return json.getInt(key);
100
+        }
101
+        if (clazz == Long.class || clazz == long.class) {
102
+            return json.getLong(key);
103
+        }
104
+        if (clazz == Float.class || clazz == float.class) {
105
+            return (float) json.getDouble(key);
106
+        }
107
+        if (clazz == Double.class || clazz == double.class) {
108
+            return json.getDouble(key);
109
+        }
110
+        if (clazz == Boolean.class || clazz == boolean.class) {
111
+            return json.getBoolean(key);
112
+        }
113
+        if (clazz == String.class) {
114
+            return json.getString(key);
115
+        }
116
+        if (LuDbo.class.isAssignableFrom(clazz)) {
117
+            LuDbo dbo = (LuDbo) clazz.newInstance();
118
+            dbo.fromJson(json.getJSONObject(key));
119
+            return dbo;
120
+        }
121
+        return null;
122
+    }
123
+
124
+    public void fromJson(JSONObject json) throws Exception
125
+    {
126
+        for (Field field : getClass().getDeclaredFields()) {
127
+            String key = getJsonFromField(field.getName());
128
+            Class clazz = field.getType();
129
+            Object value = null;
130
+            if (json.has(key)) {
131
+                if (List.class.isAssignableFrom(clazz)) {
132
+                    ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
133
+                    Class type = (Class) parameterizedType.getActualTypeArguments()[0];
134
+                    List<Object> list = (clazz == List.class ? new Vector<>() : (List<Object>) clazz.newInstance());
135
+                    JSONArray jsonArray = json.getJSONArray(key);
136
+                    for (int i = 0; i < jsonArray.length(); ++i) {
137
+                        list.add(getObjectFromJson(type, jsonArray, i));
138
+                    }
139
+                    value = list;
140
+                }
141
+                else if (clazz.isArray()) {
142
+                    Class type = clazz.getComponentType();
143
+                    JSONArray jsonArray = json.getJSONArray(key);
144
+                    Object array = Array.newInstance(type, jsonArray.length());
145
+                    for (int i = 0; i < jsonArray.length(); ++i) {
146
+                        Array.set(array, i, getObjectFromJson(type, jsonArray, i));
147
+                    }
148
+                    value = array;
149
+                }
150
+//                else if (AbstractMap.class.isAssignableFrom(clazz)) {
151
+//                    ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
152
+//                    Class type = (Class) parameterizedType.getActualTypeArguments()[0];
153
+//                    if (type == String.class) {
154
+//                        type = (Class) parameterizedType.getActualTypeArguments()[1];
155
+//                    }
156
+//                }
157
+                else {
158
+                    value = getObjectFromJson(clazz, json, key);
159
+                }
160
+            }
161
+            field.setAccessible(true);
162
+            field.set(this, value);
163
+            field.setAccessible(false);
164
+        }
165
+    }
166
+
167
+    public HashMap<String, Object> toArray()
168
+    {
169
+        HashMap<String, Object> map = new HashMap<>();
170
+
171
+        return map;
172
+    }
21 173
 
22 174
     public JSONObject toJson()
23 175
     {
@@ -25,7 +177,8 @@ public abstract class LuDbo {
25 177
     }
26 178
 
27 179
     @Override
28
-    public String toString() {
180
+    public String toString()
181
+    {
29 182
         return toJson().toString();
30 183
     }
31 184
 }

+ 0
- 15
luticateutils/src/test/java/com/luticate/lutcateutils/ExampleUnitTest.java View File

@@ -1,15 +0,0 @@
1
-package com.rthoni.utils;
2
-
3
-import org.junit.Test;
4
-
5
-import static org.junit.Assert.*;
6
-
7
-/**
8
- * To work on unit tests, switch the Test Artifact in the Build Variants view.
9
- */
10
-public class ExampleUnitTest {
11
-    @Test
12
-    public void addition_isCorrect() throws Exception {
13
-        assertEquals(4, 2 + 2);
14
-    }
15
-}

+ 32
- 0
luticateutils/src/test/java/com/luticate/utils/NameDboTest.java View File

@@ -0,0 +1,32 @@
1
+package com.luticate.utils;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+import org.junit.Test;
6
+
7
+import static org.junit.Assert.*;
8
+
9
+/**
10
+ * Created by robin on 10/20/16.
11
+ */
12
+
13
+public class NameDboTest {
14
+
15
+    @Test
16
+    public void jsonFromFieldTest()
17
+    {
18
+        assertEquals("byte", LuDbo.getJsonFromField("_byte"));
19
+        assertEquals("anotherVariable", LuDbo.getJsonFromField("_anotherVariable"));
20
+        assertEquals("byte", LuDbo.getJsonFromField("byte"));
21
+        assertEquals("anotherVariable", LuDbo.getJsonFromField("anotherVariable"));
22
+    }
23
+
24
+    @Test
25
+    public void fieldFromJsonTest()
26
+    {
27
+        assertEquals("_byte", LuDbo.getFieldFromJson("byte"));
28
+        assertEquals("_anotherVariable", LuDbo.getFieldFromJson("anotherVariable"));
29
+        assertEquals("_byte", LuDbo.getFieldFromJson("_byte"));
30
+        assertEquals("_anotherVariable", LuDbo.getFieldFromJson("_anotherVariable"));
31
+    }
32
+}

+ 30
- 0
luticateutils/src/test/java/com/luticate/utils/Test2Dbo.java View File

@@ -0,0 +1,30 @@
1
+package com.luticate.utils;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+/**
6
+ * Created by robin on 10/20/16.
7
+ */
8
+
9
+public class Test2Dbo extends LuDbo {
10
+
11
+    protected String _aString;
12
+
13
+    protected int _aInt;
14
+
15
+    public String getaString() {
16
+        return _aString;
17
+    }
18
+
19
+    public void setaString(String aString) {
20
+        _aString = aString;
21
+    }
22
+
23
+    public int getaInt() {
24
+        return _aInt;
25
+    }
26
+
27
+    public void setaInt(int aInt) {
28
+        _aInt = aInt;
29
+    }
30
+}

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

@@ -0,0 +1,228 @@
1
+package com.luticate.utils;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+import java.util.HashMap;
6
+import java.util.List;
7
+
8
+/**
9
+ * Created by robin on 10/20/16.
10
+ */
11
+
12
+public class TestDbo extends LuDbo {
13
+    protected byte _byte;
14
+    protected char _char;
15
+    protected short _short;
16
+    protected int _int;
17
+    protected long _long;
18
+    protected float _float;
19
+    protected double _double;
20
+    protected boolean _boolean;
21
+
22
+    protected Byte _byteObject;
23
+    protected Character _charObject;
24
+    protected Short _shortObject;
25
+    protected Integer _intObject;
26
+    protected Long _longObject;
27
+    protected Float _floatObject;
28
+    protected Double _doubleObject;
29
+    protected Boolean _booleanObject;
30
+
31
+    protected String _string;
32
+
33
+    protected Test2Dbo _test2Dbo;
34
+
35
+    protected Test2Dbo _test2Dbo2;
36
+
37
+    protected List<Integer> _integers;
38
+
39
+    protected int[] _ints;
40
+
41
+    protected List<Test2Dbo> _test2Dbos;
42
+
43
+    protected Test2Dbo[] _dbos;
44
+
45
+    public byte getByte() {
46
+        return _byte;
47
+    }
48
+
49
+    public void setByte(byte aByte) {
50
+        _byte = aByte;
51
+    }
52
+
53
+    public char getChar() {
54
+        return _char;
55
+    }
56
+
57
+    public void setChar(char aChar) {
58
+        _char = aChar;
59
+    }
60
+
61
+    public short getShort() {
62
+        return _short;
63
+    }
64
+
65
+    public void setShort(short aShort) {
66
+        _short = aShort;
67
+    }
68
+
69
+    public int getInt() {
70
+        return _int;
71
+    }
72
+
73
+    public void setInt(int anInt) {
74
+        _int = anInt;
75
+    }
76
+
77
+    public long getLong() {
78
+        return _long;
79
+    }
80
+
81
+    public void setLong(long aLong) {
82
+        _long = aLong;
83
+    }
84
+
85
+    public float getFloat() {
86
+        return _float;
87
+    }
88
+
89
+    public void setFloat(float aFloat) {
90
+        _float = aFloat;
91
+    }
92
+
93
+    public double getDouble() {
94
+        return _double;
95
+    }
96
+
97
+    public void setDouble(double aDouble) {
98
+        _double = aDouble;
99
+    }
100
+
101
+    public boolean isBoolean() {
102
+        return _boolean;
103
+    }
104
+
105
+    public void setBoolean(boolean aBoolean) {
106
+        _boolean = aBoolean;
107
+    }
108
+
109
+    public Byte getByteObject() {
110
+        return _byteObject;
111
+    }
112
+
113
+    public void setByteObject(Byte byteObject) {
114
+        _byteObject = byteObject;
115
+    }
116
+
117
+    public Character getCharObject() {
118
+        return _charObject;
119
+    }
120
+
121
+    public void setCharObject(Character charObject) {
122
+        _charObject = charObject;
123
+    }
124
+
125
+    public Short getShortObject() {
126
+        return _shortObject;
127
+    }
128
+
129
+    public void setShortObject(Short shortObject) {
130
+        _shortObject = shortObject;
131
+    }
132
+
133
+    public Integer getIntObject() {
134
+        return _intObject;
135
+    }
136
+
137
+    public void setIntObject(Integer intObject) {
138
+        _intObject = intObject;
139
+    }
140
+
141
+    public Long getLongObject() {
142
+        return _longObject;
143
+    }
144
+
145
+    public void setLongObject(Long longObject) {
146
+        _longObject = longObject;
147
+    }
148
+
149
+    public Float getFloatObject() {
150
+        return _floatObject;
151
+    }
152
+
153
+    public void setFloatObject(Float floatObject) {
154
+        _floatObject = floatObject;
155
+    }
156
+
157
+    public Double getDoubleObject() {
158
+        return _doubleObject;
159
+    }
160
+
161
+    public void setDoubleObject(Double doubleObject) {
162
+        _doubleObject = doubleObject;
163
+    }
164
+
165
+    public Boolean getBooleanObject() {
166
+        return _booleanObject;
167
+    }
168
+
169
+    public void setBooleanObject(Boolean booleanObject) {
170
+        _booleanObject = booleanObject;
171
+    }
172
+
173
+    public String getString() {
174
+        return _string;
175
+    }
176
+
177
+    public void setString(String string) {
178
+        _string = string;
179
+    }
180
+
181
+    public Test2Dbo getTest2Dbo() {
182
+        return _test2Dbo;
183
+    }
184
+
185
+    public void setTest2Dbo(Test2Dbo test2Dbo) {
186
+        _test2Dbo = test2Dbo;
187
+    }
188
+
189
+    public Test2Dbo getTest2Dbo2() {
190
+        return _test2Dbo2;
191
+    }
192
+
193
+    public void setTest2Dbo2(Test2Dbo test2Dbo2) {
194
+        _test2Dbo2 = test2Dbo2;
195
+    }
196
+
197
+    public List<Integer> getIntegers() {
198
+        return _integers;
199
+    }
200
+
201
+    public void setIntegers(List<Integer> integers) {
202
+        _integers = integers;
203
+    }
204
+
205
+    public int[] getInts() {
206
+        return _ints;
207
+    }
208
+
209
+    public void setInts(int[] ints) {
210
+        _ints = ints;
211
+    }
212
+
213
+    public List<Test2Dbo> getTest2Dbos() {
214
+        return _test2Dbos;
215
+    }
216
+
217
+    public void setTest2Dbos(List<Test2Dbo> test2Dbos) {
218
+        _test2Dbos = test2Dbos;
219
+    }
220
+
221
+    public Test2Dbo[] getDbos() {
222
+        return _dbos;
223
+    }
224
+
225
+    public void setDbos(Test2Dbo[] dbos) {
226
+        _dbos = dbos;
227
+    }
228
+}

+ 109
- 0
luticateutils/src/test/java/com/luticate/utils/TestDboTest.java View File

@@ -0,0 +1,109 @@
1
+package com.luticate.utils;
2
+
3
+import org.json.JSONException;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+/**
9
+ * Created by robin on 10/20/16.
10
+ */
11
+
12
+public class TestDboTest {
13
+
14
+    @Test
15
+    public void deserializeTest() throws Exception {
16
+        TestDbo test = new TestDbo();
17
+        test.fromString("{" +
18
+                "\"byte\": 42," +
19
+                "\"byteObject\": 24," +
20
+                "\"char\": \"h\"," +
21
+                "\"charObject\": \"w\"," +
22
+                "\"short\": 4200," +
23
+                "\"shortObject\": 2400," +
24
+                "\"int\": 420042," +
25
+                "\"intObject\": 240024," +
26
+                "\"long\": 42004200," +
27
+                "\"longObject\": 24002400," +
28
+                "\"float\": 42.24," +
29
+                "\"floatObject\": 24.42," +
30
+                "\"double\": 4200.0024," +
31
+                "\"doubleObject\": 2400.0042," +
32
+                "\"boolean\": true," +
33
+                "\"booleanObject\": false," +
34
+                "\"string\": \"Hello World!\"," +
35
+                "\"test2Dbo\": {" +
36
+                "\"aString\": \"a string\"," +
37
+                "\"aInt\": 4242" +
38
+                "}," +
39
+                "\"integers\": [" +
40
+                "0," +
41
+                "42," +
42
+                "24" +
43
+                "]," +
44
+                "ints: [" +
45
+                "24," +
46
+                "42," +
47
+                "0" +
48
+                "]," +
49
+                "test2Dbos: [" +
50
+                "{" +
51
+                "\"aString\": \"42 42\"," +
52
+                "\"aInt\": 4242" +
53
+                "}" +
54
+                "]," +
55
+                "dbos: [" +
56
+                "{" +
57
+                "\"aString\": \"24 24\"," +
58
+                "\"aInt\": 2424" +
59
+                "}" +
60
+                "]" +
61
+                "}");
62
+        assertEquals(42, test.getByte());
63
+        assertEquals(24, test.getByteObject().byteValue());
64
+        assertEquals('h', test.getChar());
65
+        assertEquals('w', test.getCharObject().charValue());
66
+        assertEquals(4200, test.getShort());
67
+        assertEquals(2400, test.getShortObject().shortValue());
68
+        assertEquals(420042, test.getInt());
69
+        assertEquals(240024, test.getIntObject().intValue());
70
+        assertEquals(42004200, test.getLong());
71
+        assertEquals(24002400, test.getLongObject().longValue());
72
+        assertEquals(42.24, test.getFloat(), 0.1);
73
+        assertEquals(24.42, test.getFloatObject(), 0.1);
74
+        assertEquals(4200.0024, test.getDouble(), 0.1);
75
+        assertEquals(2400.0042, test.getDoubleObject(), 0.1);
76
+        assertEquals(true, test.isBoolean());
77
+        assertEquals(false, test.getBooleanObject());
78
+        assertEquals("Hello World!", test.getString());
79
+
80
+        assertNotNull(test.getTest2Dbo());
81
+        assertEquals("a string", test.getTest2Dbo().getaString());
82
+        assertEquals(4242, test.getTest2Dbo().getaInt());
83
+        assertNull(test.getTest2Dbo2());
84
+
85
+        assertNotNull(test.getIntegers());
86
+        assertEquals(3, test.getIntegers().size());
87
+        assertEquals(0, test.getIntegers().get(0).intValue());
88
+        assertEquals(42, test.getIntegers().get(1).intValue());
89
+        assertEquals(24, test.getIntegers().get(2).intValue());
90
+
91
+        assertNotNull(test.getInts());
92
+        assertEquals(3, test.getInts().length);
93
+        assertEquals(24, test.getInts()[0]);
94
+        assertEquals(42, test.getInts()[1]);
95
+        assertEquals(0, test.getInts()[2]);
96
+
97
+        assertNotNull(test.getTest2Dbos());
98
+        assertEquals(1, test.getTest2Dbos().size());
99
+        assertNotNull(test.getTest2Dbos().get(0));
100
+        assertEquals("42 42", test.getTest2Dbos().get(0).getaString());
101
+        assertEquals(4242, test.getTest2Dbos().get(0).getaInt());
102
+
103
+        assertNotNull(test.getDbos());
104
+        assertEquals(1, test.getDbos().length);
105
+        assertNotNull(test.getDbos()[0]);
106
+        assertEquals("24 24", test.getDbos()[0].getaString());
107
+        assertEquals(2424, test.getDbos()[0].getaInt());
108
+    }
109
+}

Loading…
Cancel
Save