Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src-test / com / iver / utiles / TestXMLEntityPersistenceManager.java @ 28076

History | View | Annotate | Download (14.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: TestXMLEntity.java 10602 2007-03-05 11:15:50Z jaume $
45
* $Log$
46
* Revision 1.7  2007-03-05 11:15:43  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.6  2007/03/05 10:03:12  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2007/03/05 09:00:11  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.4  2007/03/02 13:35:56  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.3  2007/03/02 13:27:32  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.2  2007/03/02 13:24:53  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1  2007/03/02 13:23:50  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1  2006/08/29 06:18:17  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.utiles;
73

    
74
import java.io.BufferedInputStream;
75
import java.io.BufferedOutputStream;
76
import java.io.FileInputStream;
77
import java.io.FileNotFoundException;
78
import java.io.FileOutputStream;
79
import java.io.IOException;
80
import java.io.InputStreamReader;
81
import java.io.OutputStreamWriter;
82
import java.io.Reader;
83
import java.io.UnsupportedEncodingException;
84
import java.io.Writer;
85
import java.util.ArrayList;
86
import java.util.Iterator;
87

    
88
import junit.framework.TestCase;
89

    
90
import org.gvsig.tools.IverUtilesLibrary;
91
import org.gvsig.tools.ToolsLibrary;
92
import org.gvsig.tools.ToolsLocator;
93
import org.gvsig.tools.locator.Library;
94
import org.gvsig.tools.locator.LocatorException;
95
import org.gvsig.tools.persistence.PersistenceException;
96
import org.gvsig.tools.persistence.PersistenceManager;
97
import org.gvsig.tools.persistence.Persistent;
98
import org.gvsig.tools.persistence.PersistentState;
99

    
100
public class TestXMLEntityPersistenceManager extends TestCase {
101
        private XMLEntity x1, x2, x3, x4;
102
        private XMLEntity gvp1;
103
        private XMLEntity gvp2;
104
        private ArrayList list;
105
        private String string1;
106
        private String string2;
107
        private String string3;
108
        private String string4;
109
        private String string5;
110
        private int integer1;
111
        private int integer2;
112
        private int integer3;
113
        private long long1;
114
        private long long2;
115
        private double double1;
116
        private double double2;
117
        private double double3;
118
        private double double4;
119
        private double double5;
120
        private boolean bool1;
121
        private boolean bool2;
122
        private MyPersistentObject myPersObj;
123
        private final String ENCODING = "UTF-8";
124
        private MyPersistentContainer container1, container2;
125
        
126

    
127
        public void setUp() throws Exception {
128
                super.setUp();
129
                Library toolsLib = new ToolsLibrary();
130
                toolsLib.initialize();
131
                
132
                Library utilsLib = new IverUtilesLibrary();
133
                utilsLib.initialize();
134
                
135
                toolsLib.postInitialize();
136
                utilsLib.postInitialize();
137

    
138
                string1 = "This is my first$$ String to check";
139
                string2 = "An very long long long asdasfsafffffffffasfasasdffffffsf"
140
                        + "long long long long long asdfaaaddddddddddddddddddddddddasdsd"
141
                        + "fbkdfjger gsdf glkwerqwsdvsi?omodsgt string with special"
142
                        + "characters and etc etc asdfasdfwqfwefwfasdfasfasfasfasfasfasfas"
143
                        + "asfasfasfasfasfasfasfasfdasdsdfsafsafasfasfsafasfasfasfasfasfas"
144
                        + "safasfasfasdfkb sdfd  ertge ertwgfwrt43 fbgdsf gw wfwf sdgas  "
145
                        + "asfwr324rf635r sdgsa swe3er 6546               sfasdfsafas asfa"
146
                        + "asdfasfasf435hdhge sfsd g dfsafasfsa as";
147
                string3 = "pero perico pero";
148
                string4 = "piripi pi";
149
                string5 = "lepe lepero";
150

    
151
                integer1 = 9323823;
152
                integer2 = -234234234;
153
                integer3 = 0;
154
                
155
                double1 = 23234.234;
156
                double2 = -674633.2423423;
157
                double3 = 0.0;
158
                double4 = 0.23432445634;
159
                double5 = -0.2342323;
160
                
161
                bool1 = true;
162
                bool2 = false;
163
                
164
                myPersObj = new MyPersistentObject();
165
        }
166

    
167
        protected void tearDown() throws Exception {
168
                super.tearDown();
169
                list = null;
170
                container1 = null;
171
                container2 = null;
172
        }
173

    
174
        public void testMarshall1() {
175
                marshall1();
176
                
177
                try {
178
                        Reader test = getReader("/tmp/persistence-test.xml");
179
                        Reader sample = getReader("testdata/persistence-test.xml");
180
                        char[] array1 = new char[1024];
181
                        char[] array2 = new char[1024];
182
                        while (sample.read(array1)!=-1) {
183
                                test.read(array2);
184
                                for (int i=0; i<array1.length; i++) {
185
                                        assertEquals(array1[i], array2[i]);
186
                                }
187
                        }
188
                        
189
                } catch (UnsupportedEncodingException e) {
190
                        e.printStackTrace();
191
                        fail(e.getMessage());
192
                } catch (FileNotFoundException e) {
193
                        e.printStackTrace();
194
                        fail(e.getMessage());
195
                } catch (IOException e) {
196
                        e.printStackTrace();
197
                        fail(e.getMessage());
198
                }
199
        }
200

    
201
        public Reader getReader(String fileName) throws UnsupportedEncodingException, FileNotFoundException {
202
                return new InputStreamReader(
203
                                new BufferedInputStream(
204
                                                new FileInputStream(fileName)), ENCODING) {
205
                };
206
        }
207

    
208
        public Writer getWriter(String fileName) throws UnsupportedEncodingException, FileNotFoundException{
209
                return new OutputStreamWriter(
210
                                new BufferedOutputStream(
211
                                                new FileOutputStream(fileName)), ENCODING) {
212
                };
213
        }
214
        
215
        public void testMarshall2() {
216
                marshall2();
217
                
218
                try {
219
                        Reader test = getReader("/tmp/persistence-test2.xml");
220
                        Reader sample = getReader("testdata/persistence-test2.xml");
221
                        char[] array1 = new char[1024];
222
                        char[] array2 = new char[1024];
223
                        while (sample.read(array1)!=-1) {
224
                                test.read(array2);
225
                                for (int i=0; i<array1.length; i++) {
226
                                        assertEquals(array1[i], array2[i]);
227
                                }
228
                        }
229
                } catch (UnsupportedEncodingException e) {
230
                        e.printStackTrace();
231
                        fail(e.getMessage());
232
                } catch (FileNotFoundException e) {
233
                        e.printStackTrace();
234
                        fail(e.getMessage());
235
                } catch (IOException e) {
236
                        e.printStackTrace();
237
                        fail(e.getMessage());
238
                }
239
        }
240

    
241
        private void initList() {
242
                if (list==null) {
243
                        list = new ArrayList();
244
                        list.add(string1);
245
                        list.add(string2);
246
                        list.add(new Integer(integer1));
247
                        list.add(new Integer(integer2));
248
                        list.add(new Integer(integer3));
249
                        list.add(new Boolean(bool1));
250
                        list.add(new Boolean(bool2));
251
                        list.add(myPersObj);
252
                        initContainer2();
253
                        list.add(container2);
254
                }
255
        }
256

    
257
        private void marshall1() {
258
                initList();
259
                
260
                MyPersistentList originalList = new MyPersistentList(list);
261
                
262
                try {
263
                        PersistentState state = ToolsLocator.getPersistenceManager().getState(originalList);
264
                        Writer writer = getWriter("/tmp/persistence-test.xml");
265
                        state.save(writer);
266
                } catch (FileNotFoundException e) {
267
                        e.printStackTrace();
268
                        fail(e.getMessage());
269
                } catch (LocatorException e) {
270
                        e.printStackTrace();
271
                        fail(e.getMessage());
272
                } catch (PersistenceException e) {
273
                        e.printStackTrace();
274
                        fail(e.getMessage());
275
                } catch (UnsupportedEncodingException e) {
276
                        e.printStackTrace();
277
                        fail(e.getMessage());
278
                }
279
        }
280

    
281
        public void testUnmarshall1() {
282
                try {
283
                        initList();
284
                        Reader reader = getReader("testdata/persistence-test.xml");
285
                        PersistentState state = ToolsLocator.getPersistenceManager().loadState(reader);
286
                        Iterator it = state.getIterator("myList");
287
                        int i = 0;
288
                        while (it.hasNext()) {
289
                                Object obj = it.next();
290
                                if (i<list.size()) {
291
                                        assertEquals(obj, list.get(i++));
292
                                }
293
                                else {
294
                                        fail("Iterator size is shorter than original list");
295
                                }
296
                        }
297
                        if (i!=list.size()) {
298
                                fail("Iterator size is longer than original list");
299
                        }
300
                        
301
                } catch (UnsupportedEncodingException e) {
302
                        e.printStackTrace();
303
                        fail(e.getMessage());
304
                } catch (FileNotFoundException e) {
305
                        e.printStackTrace();
306
                        fail(e.getMessage());
307
                } catch (LocatorException e) {
308
                        e.printStackTrace();
309
                        fail(e.getMessage());
310
                } catch (PersistenceException e) {
311
                        e.printStackTrace();
312
                        fail(e.getMessage());
313
                }
314
        }
315

    
316
        public void testUnmarshall2() {
317
                try {
318
                        initContainer1();
319
                        Reader reader = getReader("testdata/persistence-test2.xml");
320
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
321
                        PersistentState state = manager.loadState(reader);
322
                        Object obj = manager.create(state);
323
                        assertEquals(container1, obj);
324
                        
325
                } catch (UnsupportedEncodingException e) {
326
                        e.printStackTrace();
327
                        fail(e.getMessage());
328
                } catch (FileNotFoundException e) {
329
                        e.printStackTrace();
330
                        fail(e.getMessage());
331
                } catch (LocatorException e) {
332
                        e.printStackTrace();
333
                        fail(e.getMessage());
334
                } catch (PersistenceException e) {
335
                        e.printStackTrace();
336
                        fail(e.getMessage());
337
                }
338
        }
339

    
340
        private void initContainer2() {
341
                if (container2==null) {
342
                        container2 = new MyPersistentContainer();
343
                        container2.setValue1(myPersObj);
344
                        container2.setValue2(string4);
345
                        container2.setValue3(string5);
346
                }
347
        }
348

    
349
        private void initContainer1() {
350
                if (container1==null) {
351
                        initList();
352
                        container1 = new MyPersistentContainer();
353
                        container1.setValue1(string3);
354
                        container1.setValue2(new MyPersistentList(list));
355
                        container1.setValue3(myPersObj);
356
                }
357
        }
358

    
359
        private void marshall2() {
360
                initContainer1();        
361
                        try {
362
                                PersistentState state = ToolsLocator.getPersistenceManager().getState(container1);
363
                                Writer writer = getWriter("/tmp/persistence-test2.xml");
364
                                state.save(writer);
365
                        } catch (FileNotFoundException e) {
366
                                e.printStackTrace();
367
                                fail(e.getMessage());
368
                        } catch (LocatorException e) {
369
                                e.printStackTrace();
370
                                fail(e.getMessage());
371
                        } catch (PersistenceException e) {
372
                                e.printStackTrace();
373
                                fail(e.getMessage());
374
                        } catch (UnsupportedEncodingException e) {
375
                                e.printStackTrace();
376
                                fail(e.getMessage());
377
                        }
378
        }
379

    
380
        public void testIterator() {
381
                list = new ArrayList();
382
                list.add(string1);
383
                list.add(string2);
384
                list.add(new Integer(integer1));
385
                list.add(new Integer(integer2));
386
                list.add(new Integer(integer3));
387
                list.add(new Boolean(bool1));
388
                list.add(new Boolean(bool2));
389
                list.add(myPersObj);
390
                
391
                MyPersistentList originalList = new MyPersistentList(list);
392
                try {
393
                        PersistentState state = ToolsLocator.getPersistenceManager().getState(originalList);
394
                        MyPersistentList resultList = new MyPersistentList();
395
                        resultList.setState(state);
396
                        checkEquals(originalList, resultList);
397
                         
398
                } catch (LocatorException e) {
399
                        // TODO Auto-generated catch block
400
                        e.printStackTrace();
401
                        fail(e.getMessage());
402
                } catch (PersistenceException e) {
403
                        // TODO Auto-generated catch block
404
                        e.printStackTrace();
405
                        fail(e.getMessage());
406
                }
407
        }
408

    
409
    public void checkEquals(MyPersistentList originalList, MyPersistentList resultList) {
410
            if (originalList == null) {
411
                    if (resultList == null) {
412
                            return;
413
                    }
414
                    else {
415
                            fail("List1 is null");
416
                    }
417
            }
418
            else if (resultList == null) {
419
                    fail("List2 is null");
420
            }
421

    
422
            Iterator iterator1 = originalList.iterator();
423
            Iterator iterator2 = resultList.iterator();
424
            while(iterator1.hasNext() && iterator2.hasNext()) {
425
                Object o1 = iterator1.next();
426
                Object o2 = iterator2.next();
427
                if (!(o1==null ? o2==null : o1.equals(o2))) {
428
                        failNotEquals("Items are not equal", o1, o2);
429
                }
430
            }
431
            if (iterator1.hasNext() || iterator2.hasNext()) {
432
                    fail("Lists have different length");
433
            }
434
    }
435

    
436
        public static class MyPersistentList implements Persistent {
437
                public ArrayList list;
438
                public MyPersistentList() {
439
                        
440
                }
441
                public MyPersistentList(ArrayList list) {
442
                        this.list = list;
443
                }
444

    
445
                public void saveToState(PersistentState state)
446
                                throws PersistenceException {
447
                        state.set("myList", list.iterator());
448
                        
449
                }
450

    
451
                public void setState(PersistentState state) throws PersistenceException {
452
                        Iterator it = state.getIterator("myList");
453
                        list = new ArrayList();
454
                        while (it.hasNext()) {
455
                                list.add(it.next());
456
                        }
457
                }
458
                @Override
459
                public boolean equals(Object obj) {
460
                        if (obj instanceof MyPersistentList) {
461
                                return list.equals(((MyPersistentList)obj).list);
462
                        }
463
                        return false;
464
                }
465
                
466
                public Iterator iterator() {
467
                        return list.iterator();
468
                }
469
        }
470
        
471
        public static class MyPersistentObject implements Persistent {
472
                private String value = "Sopa de cabrita";
473
                private int intValue = 20090625;
474

    
475
                public void saveToState(PersistentState state)
476
                                throws PersistenceException {
477
                        state.set("miValor", value);
478
                        state.set("numero", intValue);
479
                        
480
                }
481

    
482
                public String getValue() {
483
                        return value;
484
                }
485

    
486
                public void setValue(String val) {
487
                        value = val;
488
                }
489

    
490
                public void setState(PersistentState state) throws PersistenceException {
491
                        value = state.getString("miValor");
492
                        intValue = state.getInt("numero");
493
                }
494

    
495
                public boolean equals(Object obj) {
496
                        if (obj instanceof MyPersistentObject) {
497
                                MyPersistentObject myPers = (MyPersistentObject) obj;
498
                                return value.equals(myPers.getValue());
499
                        }
500
                        return false;
501
                }
502
        }
503

    
504
        public static class MyPersistentContainer implements Persistent {
505
                private Object value1;
506
                private Object value2;
507
                private Object value3;
508

    
509
                public void saveToState(PersistentState state)
510
                                throws PersistenceException {
511
                        state.set("miObjetito1", value1);
512
                        state.set("miObjetito2", value2);
513
                        state.set("miObjetito3", value3);
514
                        
515
                }
516
                public Object getValue1() {
517
                        return value1;
518
                }
519
                public void setValue1(Object val) {
520
                        value1 = val;
521
                }
522
                public Object getValue2() {
523
                        return value2;
524
                }
525
                public void setValue2(Object val) {
526
                        value2 = val;
527
                }
528
                public Object getValue3() {
529
                        return value3;
530
                }
531
                public void setValue3(Object val) {
532
                        value3 = val;
533
                }
534

    
535
                public void setState(PersistentState state) throws PersistenceException {
536
                        value1 = state.get("miObjetito1");
537
                        value2 = state.get("miObjetito2");
538
                        value3 = state.get("miObjetito3");
539
                }
540

    
541
                public boolean equals(Object obj) {
542
                        if (obj instanceof MyPersistentContainer) {
543
                                MyPersistentContainer myPers = (MyPersistentContainer) obj;
544
                                if (!value1.equals(myPers.getValue1())) {
545
                                        return false;
546
                                }
547
                                if (!value2.equals(myPers.getValue2())) {
548
                                        return false;
549
                                }
550
                                if (!value3.equals(myPers.getValue3())) {
551
                                        return false;
552
                                }
553
                                else {
554
                                        return true;
555
                                }
556
                        }
557
                        return false;
558
                }
559
                
560
        }
561
}