svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src-test / org / gvsig / tools / persistence / xml / XMLPersistenceTest.java @ 30619
History | View | Annotate | Download (3.32 KB)
1 |
/* 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 junit.framework.TestCase; |
40 |
|
41 |
import org.gvsig.tools.ToolsLocator; |
42 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
43 |
import org.gvsig.tools.persistence.PersistenceManager; |
44 |
import org.gvsig.tools.persistence.Persistent; |
45 |
import org.gvsig.tools.persistence.PersistentState; |
46 |
|
47 |
/**
|
48 |
* @author jmvivo
|
49 |
*
|
50 |
*/
|
51 |
public class XMLPersistenceTest extends TestCase { |
52 |
|
53 |
/* (non-Javadoc)
|
54 |
* @see junit.framework.TestCase#setUp()
|
55 |
*/
|
56 |
protected void setUp() throws Exception { |
57 |
super.setUp();
|
58 |
|
59 |
new DefaultLibrariesInitializer().fullInitialize();
|
60 |
|
61 |
// ToolsLibrary toolsLib = new ToolsLibrary();
|
62 |
// XMLPersistenceLibrary xmlPersistenceLib = new XMLPersistenceLibrary();
|
63 |
//
|
64 |
// toolsLib.initialize();
|
65 |
// xmlPersistenceLib.initialize();
|
66 |
//
|
67 |
// toolsLib.postInitialize();
|
68 |
// xmlPersistenceLib.postInitialize();
|
69 |
|
70 |
ToolsLocator |
71 |
.registerPersistenceManager(XMLPersistenceManager.class); |
72 |
|
73 |
XMLPersistenceTest_Persistent.registerPersistent(); |
74 |
XMLPersistenceTest_Persistent2.registerPersistent(); |
75 |
XMLPersistenceTest_Persistent_ciclical.registerPersistent(); |
76 |
} |
77 |
|
78 |
public void test() throws Exception { |
79 |
Persistent myPersistent = new XMLPersistenceTest_Persistent();
|
80 |
|
81 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
82 |
|
83 |
PersistentState state = manager.getState(myPersistent); |
84 |
|
85 |
ByteArrayOutputStream out = new ByteArrayOutputStream(100 * 1024); |
86 |
|
87 |
manager.saveState(state, out); |
88 |
out.flush(); |
89 |
|
90 |
// System.out.println(out.toString());
|
91 |
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
92 |
|
93 |
PersistentState stateIn = manager.loadState(in); |
94 |
|
95 |
System.out.println(out.toString());
|
96 |
|
97 |
List names = new ArrayList(); |
98 |
List namesIn = new ArrayList(); |
99 |
|
100 |
Iterator iter = state.getNames();
|
101 |
while (iter.hasNext()) {
|
102 |
names.add(iter.next()); |
103 |
} |
104 |
iter = stateIn.getNames(); |
105 |
while (iter.hasNext()) {
|
106 |
namesIn.add(iter.next()); |
107 |
} |
108 |
|
109 |
assertEquals(names.size(), namesIn.size()); |
110 |
|
111 |
iter = names.iterator(); |
112 |
while (iter.hasNext()) {
|
113 |
assertTrue(namesIn.contains(iter.next())); |
114 |
} |
115 |
|
116 |
Object loadedPersistent = manager.create(stateIn);
|
117 |
|
118 |
assertTrue(loadedPersistent instanceof XMLPersistenceTest_Persistent);
|
119 |
|
120 |
((XMLPersistenceTest_Persistent) loadedPersistent).checked(); |
121 |
|
122 |
|
123 |
|
124 |
} |
125 |
|
126 |
|
127 |
} |