Rev 1783 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1783 | jmachado | 1 | package pt.estgp.estgweb.domain.test.genson; |
2 | |||
3 | import com.owlike.genson.Genson; |
||
4 | import com.owlike.genson.GensonBuilder; |
||
5 | import junit.framework.TestCase; |
||
6 | import org.json.JSONException; |
||
7 | import org.json.JSONObject; |
||
1827 | jmachado | 8 | import pt.estgp.estgweb.utils.DatesUtils; |
1783 | jmachado | 9 | |
10 | import java.util.Date; |
||
11 | |||
12 | /** |
||
13 | * Created by jorgemachado on 19/06/17. |
||
14 | */ |
||
15 | public class TestFieldsAndMethodsTogether extends TestCase |
||
16 | { |
||
17 | |||
18 | |||
19 | private 5+0%2Fdocs%2Fapi+Date">Date saveDate; |
||
20 | |||
21 | public 5+0%2Fdocs%2Fapi+Date">Date getSaveDate() { |
||
22 | return saveDate; |
||
23 | } |
||
24 | |||
25 | public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date saveDate) { |
||
26 | this.saveDate = saveDate; |
||
27 | } |
||
28 | |||
29 | private int ocorrenciasGetAdulteradoSet; |
||
30 | |||
31 | public int getOcorrenciasGetAdulteradoSet() { |
||
32 | return 3000; |
||
33 | } |
||
34 | |||
35 | public void setOcorrenciasGetAdulteradoSet(int ocorrenciasGetAdulteradoSet) { |
||
36 | this.ocorrenciasGetAdulteradoSet = ocorrenciasGetAdulteradoSet; |
||
37 | } |
||
38 | |||
39 | private int ocorrenciasGetSetNormal; |
||
40 | |||
41 | private int ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia; |
||
42 | |||
43 | private int ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados; |
||
44 | |||
45 | private int ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet; |
||
46 | |||
47 | private boolean valorDevolvidoTrue; |
||
48 | private boolean valorDevolvidoFalse; |
||
49 | |||
50 | public boolean isValorDevolvidoTrue() { |
||
51 | return valorDevolvidoTrue; |
||
52 | } |
||
53 | |||
54 | public boolean isValorDevolvidoFalse() { |
||
55 | return valorDevolvidoFalse; |
||
56 | } |
||
57 | |||
58 | public int getOcorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet() { |
||
59 | return ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet; |
||
60 | } |
||
61 | |||
62 | public void setOcorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet(int ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet) { |
||
63 | this.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet = 100; |
||
64 | } |
||
65 | |||
66 | public int getOcorrenciasGetSetNormal() |
||
67 | { |
||
68 | return ocorrenciasGetSetNormal; |
||
69 | } |
||
70 | |||
71 | public void setOcorrenciasGetSetNormal(int o) |
||
72 | { |
||
73 | ocorrenciasGetSetNormal = o; |
||
74 | } |
||
75 | |||
76 | public int getOcorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia() { |
||
77 | return ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia; |
||
78 | } |
||
79 | |||
80 | |||
81 | private int campoOcultoSemGetNemSet; |
||
82 | |||
83 | private int campoOcultoSemGetNemSetNemComOutroNome; |
||
84 | |||
85 | public int getCampoInvisivel() { |
||
86 | return campoOcultoSemGetNemSet; |
||
87 | } |
||
88 | |||
89 | public void setCampoInvisivel(int campoInvisivel) { |
||
90 | this.campoOcultoSemGetNemSet = campoInvisivel; |
||
91 | } |
||
92 | |||
93 | public void testPriority() throws JSONException { |
||
94 | Genson genson = new GensonBuilder() |
||
95 | .include("ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet") |
||
96 | .include("ocorrenciasGetAdulteradoSet") |
||
97 | .create(); |
||
98 | TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether(); |
||
99 | t.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet=1000; |
||
100 | |||
101 | t.ocorrenciasGetSetNormal = 10; |
||
102 | t.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia = 20; |
||
103 | t.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados = 30; |
||
104 | |||
105 | t.valorDevolvidoTrue = true; |
||
106 | t.valorDevolvidoFalse = false; |
||
107 | |||
108 | t.ocorrenciasGetAdulteradoSet = 1;//tem o get adulterado para devolver 3000 |
||
109 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
110 | |||
111 | JSONObject jsonParsed = new JSONObject(json); |
||
112 | int valor = jsonParsed.getInt("ocorrenciasGetAdulteradoSet"); |
||
113 | //fica com o valor do set adulterado |
||
114 | assertEquals(valor,3000); |
||
115 | |||
116 | TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class); |
||
117 | |||
118 | assertTrue(t2.ocorrenciasGetSetNormal == t.ocorrenciasGetSetNormal); |
||
119 | assertTrue(t2.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia == 0); |
||
120 | assertTrue(t2.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados == 0); |
||
121 | |||
122 | //METODO TEM PRIORIDADE |
||
123 | assertTrue(t2.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet == 100); |
||
124 | |||
125 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoTrue = jsonParsed.getBoolean("valorDevolvidoTrue"); |
||
126 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoFalse = jsonParsed.getBoolean("valorDevolvidoFalse"); |
||
127 | |||
128 | assertTrue(lidoTrue); |
||
129 | assertFalse(lidoFalse); |
||
130 | |||
131 | |||
132 | |||
133 | |||
134 | } |
||
135 | |||
136 | public void testFields1() throws JSONException { |
||
137 | Genson genson = new GensonBuilder() |
||
138 | .useMethods(false) |
||
139 | //.exclude(Object.class) resulta no mesmo porque os campos são privados |
||
140 | // logo estao automaticamente excluidos e' necessario incluir um a um |
||
141 | .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsTogether.class) |
||
142 | .include("ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia",TestFieldsAndMethodsTogether.class) |
||
143 | .include("ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados",TestFieldsAndMethodsTogether.class) |
||
144 | .include("valorDevolvidoTrue",TestFieldsAndMethodsTogether.class) |
||
145 | .include("valorDevolvidoFalse",TestFieldsAndMethodsTogether.class) |
||
146 | .include("ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet",TestFieldsAndMethodsTogether.class) |
||
147 | .include("campoInvisivel",TestFieldsAndMethodsTogether.class) |
||
148 | .create(); |
||
149 | TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether(); |
||
150 | t.ocorrenciasGetSetNormal = 10; |
||
151 | t.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia = 20; |
||
152 | t.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados = 30; |
||
153 | t.valorDevolvidoTrue = true; |
||
154 | t.valorDevolvidoFalse = false; |
||
155 | t.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet = 200; |
||
156 | t.campoOcultoSemGetNemSet = 400; |
||
157 | |||
158 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
159 | |||
160 | JSONObject jsonParsed = new JSONObject(json); |
||
161 | |||
162 | TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class); |
||
163 | |||
164 | assertTrue(t2.ocorrenciasGetSetNormal == t.ocorrenciasGetSetNormal); |
||
165 | //Neste caso como foi deliberadamente declarado o campo o sistema usa ou o metodo ou o campo para fazer a alteração |
||
166 | assertTrue(t2.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia == 20); |
||
167 | //Neste caso variaveis explicitamente declaradas o sistema deve copia-las |
||
168 | assertTrue(t2.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados == 30); |
||
169 | //NEste caso apesar do Set estar a colocar 100 vai usar o valor do campo privado |
||
170 | assertTrue(t2.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet == 200); |
||
171 | |||
172 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoTrue = jsonParsed.getBoolean("valorDevolvidoTrue"); |
||
173 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoFalse = jsonParsed.getBoolean("valorDevolvidoFalse"); |
||
174 | |||
175 | //Os campos mesmo que tenham metodos e esses metodos sejam declarados |
||
176 | //No include do Genson, o include serve apenas para Campos e não para |
||
177 | //metodos, portanto o campo não é copiado se o use metodos estiver a false |
||
178 | //cs campos apenas são copiados se publicos ou incluidos especificamente |
||
179 | assertFalse(t2.campoOcultoSemGetNemSet == 400); |
||
180 | |||
181 | assertTrue(lidoTrue); |
||
182 | assertFalse(lidoFalse); |
||
183 | |||
184 | |||
185 | |||
186 | |||
187 | } |
||
188 | |||
189 | public void testFields() throws JSONException { |
||
190 | Genson genson = new GensonBuilder() |
||
191 | .useMethods(true) //O metodo do campoInvisivel que afeta este campo vai preenche-lo |
||
192 | .useFields(false) |
||
193 | .include("campoOcultoSemGetNemSetNemComOutroNome",TestFieldsAndMethodsTogether.class) |
||
194 | .create(); |
||
195 | TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether(); |
||
196 | t.campoOcultoSemGetNemSet = 400; |
||
197 | t.campoOcultoSemGetNemSetNemComOutroNome = 100; |
||
198 | |||
199 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
200 | |||
201 | JSONObject jsonParsed = new JSONObject(json); |
||
202 | |||
203 | TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class); |
||
204 | |||
205 | //Tem um metodo que usa o valor e o acerta |
||
206 | assertTrue(t2.campoOcultoSemGetNemSet == 400); |
||
207 | //Campos nunca são copiados com o Fields = false logo o valor não passa o assert e' false |
||
208 | assertTrue(t2.campoOcultoSemGetNemSetNemComOutroNome != 100); |
||
209 | |||
210 | |||
211 | |||
212 | |||
213 | } |
||
214 | |||
215 | |||
216 | public void testFields1Herancas() throws JSONException { |
||
217 | Genson genson = new GensonBuilder() |
||
218 | .useMethods(false) |
||
219 | .useClassMetadata(true) |
||
220 | .useRuntimeType(true) |
||
221 | //.exclude(Object.class) resulta no mesmo porque os campos são privados |
||
222 | // logo estao automaticamente excluidos e' necessario incluir um a um |
||
223 | .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsChild.class) |
||
224 | .create(); |
||
225 | TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild(); |
||
226 | t.setOcorrenciasGetSetNormal(10); |
||
227 | |||
228 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
229 | |||
230 | JSONObject jsonParsed = new JSONObject(json); |
||
231 | |||
232 | TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class); |
||
233 | |||
234 | assertTrue(t2.getOcorrenciasGetSetNormal() == t.getOcorrenciasGetSetNormal()); |
||
235 | |||
236 | |||
237 | |||
238 | genson = new GensonBuilder() |
||
239 | .useMethods(false) |
||
240 | .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsTogether.class) |
||
241 | .create(); |
||
242 | t = new TestFieldsAndMethodsChild(); |
||
243 | t.setOcorrenciasGetSetNormal(10); |
||
244 | |||
245 | json = genson.serialize(t); |
||
246 | |||
247 | jsonParsed = new JSONObject(json); |
||
248 | |||
249 | t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class); |
||
250 | |||
251 | assertTrue(t2.getOcorrenciasGetSetNormal() == t.getOcorrenciasGetSetNormal()); |
||
252 | |||
253 | } |
||
254 | |||
255 | public void testFieldsDates() throws JSONException { |
||
256 | Genson genson = new GensonBuilder() |
||
257 | .useFields(true) |
||
258 | .useMethods(false) |
||
259 | .exclude("saveDate",TestFieldsAndMethodsTogether.class) |
||
260 | .include("dateStr", TestFieldsAndMethodsTogether.class) |
||
261 | .create(); |
||
262 | TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild(); |
||
263 | t.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
264 | |||
265 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
266 | |||
267 | TestFieldsAndMethodsChild t2 = genson.deserialize(json,TestFieldsAndMethodsChild.class); |
||
268 | |||
269 | //Tem um metodo que usa o valor e o acerta |
||
270 | assertEquals(t2.getDateStr(),t.getDateStr()); |
||
271 | |||
272 | |||
273 | } |
||
274 | |||
275 | |||
276 | |||
277 | public void testMetodosIncludesEdates() throws JSONException { |
||
278 | Genson genson = new GensonBuilder() |
||
279 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
280 | .useFields(false) |
||
281 | .useMethods(true) |
||
282 | .exclude("ocorrenciasGetSetNormal",TestFieldsAndMethodsTogether.class) |
||
283 | //VAR inexistente apenas existe o metodo |
||
284 | .include("dateStr2",TestFieldsAndMethodsChild.class) |
||
285 | .create(); |
||
286 | TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild(); |
||
287 | t.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
288 | |||
289 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t); |
||
290 | |||
291 | TestFieldsAndMethodsChild t2 = genson.deserialize(json,TestFieldsAndMethodsChild.class); |
||
292 | |||
293 | //Um metodo pode ser excluido, nao pode é ser incluido |
||
294 | assertTrue(t2.getOcorrenciasGetSetNormal() == 0); |
||
295 | //Tem um metodo que usa o valor e o acerta |
||
296 | assertEquals(t2.getDateStr2(),t.getDateStr2()); |
||
297 | |||
1827 | jmachado | 298 | assertEquals(DatesUtils.getYear(t2.getSaveDate()),DatesUtils.getYear(t.getSaveDate())); |
299 | assertEquals(DatesUtils.getMonth(t2.getSaveDate()),DatesUtils.getMonth(t.getSaveDate())); |
||
300 | assertEquals(DatesUtils.getDay(t2.getSaveDate()),DatesUtils.getDay(t.getSaveDate())); |
||
1783 | jmachado | 301 | |
302 | |||
303 | } |
||
304 | } |