Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src-test / org / gvsig / tools / persistence / xml / XMLPersistenceTest.java @ 30953

History | View | Annotate | Download (2.97 KB)

1 30060 jmvivo
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27
28
/**
29
 *
30
 */
31
package org.gvsig.tools.persistence.xml;
32
33
import java.io.ByteArrayInputStream;
34
import java.io.ByteArrayOutputStream;
35
import java.util.ArrayList;
36
import java.util.Iterator;
37
import java.util.List;
38
39
import org.gvsig.tools.ToolsLocator;
40 30754 cordinyana
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
41 30060 jmvivo
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.Persistent;
43
import org.gvsig.tools.persistence.PersistentState;
44
45
/**
46
 * @author jmvivo
47
 *
48
 */
49 30754 cordinyana
public class XMLPersistenceTest extends AbstractLibraryAutoInitTestCase {
50 30060 jmvivo
51 30754 cordinyana
        protected void doSetUp() throws Exception {
52 30619 jmvivo
                ToolsLocator
53
                                .registerPersistenceManager(XMLPersistenceManager.class);
54
55
                XMLPersistenceTest_Persistent.registerPersistent();
56
                XMLPersistenceTest_Persistent2.registerPersistent();
57
                XMLPersistenceTest_Persistent_ciclical.registerPersistent();
58 30621 jmvivo
                XMLPersistenceTest_Factory.registerPersistent();
59 30060 jmvivo
        }
60
61
        public void test() throws Exception {
62
                Persistent myPersistent = new XMLPersistenceTest_Persistent();
63
64
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
65
66
                PersistentState state = manager.getState(myPersistent);
67
68
                ByteArrayOutputStream out = new ByteArrayOutputStream(100 * 1024);
69
70
                manager.saveState(state, out);
71
                out.flush();
72
73
                // System.out.println(out.toString());
74
                ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
75
76
                PersistentState stateIn = manager.loadState(in);
77
78 30953 cordinyana
                // System.out.println(out.toString());
79 30060 jmvivo
80
                List names = new ArrayList();
81
                List namesIn = new ArrayList();
82
83
                Iterator iter = state.getNames();
84
                while (iter.hasNext()) {
85
                        names.add(iter.next());
86
                }
87
                iter = stateIn.getNames();
88
                while (iter.hasNext()) {
89
                        namesIn.add(iter.next());
90
                }
91
92
                assertEquals(names.size(), namesIn.size());
93
94
                iter = names.iterator();
95
                while (iter.hasNext()) {
96
                        assertTrue(namesIn.contains(iter.next()));
97
                }
98
99 30207 jmvivo
                Object loadedPersistent = manager.create(stateIn);
100 30060 jmvivo
101
                assertTrue(loadedPersistent instanceof XMLPersistenceTest_Persistent);
102
103
                ((XMLPersistenceTest_Persistent) loadedPersistent).checked();
104
105
106
107
        }
108
109
110
}