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