Statistics
| Revision:

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

History | View | Annotate | Download (3.43 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.io.File;
36
import java.io.FileOutputStream;
37
import java.util.ArrayList;
38
import java.util.Iterator;
39
import java.util.List;
40

    
41
import junit.framework.TestCase;
42

    
43
import org.gvsig.tools.ToolsLibrary;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.Persistent;
47
import org.gvsig.tools.persistence.PersistentState;
48

    
49
/**
50
 * @author jmvivo
51
 *
52
 */
53
public class ZipXMLPersistenceTest extends TestCase {
54

    
55
        /* (non-Javadoc)
56
         * @see junit.framework.TestCase#setUp()
57
         */
58
        protected void setUp() throws Exception {
59
                super.setUp();
60
                ToolsLibrary toolsLib = new ToolsLibrary();
61
                XMLPersistenceLibrary xmlPersistenceLib = new XMLPersistenceLibrary();
62

    
63
                toolsLib.initialize();
64
                xmlPersistenceLib.initialize();
65

    
66
                toolsLib.postInitialize();
67
                xmlPersistenceLib.postInitialize();
68

    
69

    
70
                ToolsLocator.registerPersistenceManager(ZipXMLPersistenceManager.class);
71

    
72
                XMLPersistenceTest_Persistent.registerPersistent();
73
                XMLPersistenceTest_Persistent2.registerPersistent();
74
                XMLPersistenceTest_Persistent_ciclical.registerPersistent();
75
        }
76

    
77
        public void test() throws Exception {
78
                Persistent myPersistent = new XMLPersistenceTest_Persistent();
79

    
80
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
81

    
82
                PersistentState state = manager.getState(myPersistent);
83

    
84
                ByteArrayOutputStream out = new ByteArrayOutputStream(100 * 1024);
85

    
86
                manager.saveState(state, out);
87
                out.flush();
88

    
89
                byte[] bytesOut = out.toByteArray();
90

    
91

    
92
                File file = new File("out.zip");
93

    
94
                FileOutputStream fout = new FileOutputStream(file);
95

    
96
                fout.write(bytesOut);
97

    
98
                fout.flush();
99
                fout.close();
100
                System.out.println("Writed file:" + file.getAbsolutePath());
101

    
102
                ByteArrayInputStream in = new ByteArrayInputStream(bytesOut);
103
                PersistentState stateIn = manager.loadState(in);
104

    
105
                List names = new ArrayList();
106
                List namesIn = new ArrayList();
107

    
108
                Iterator iter = state.getNames();
109
                while (iter.hasNext()) {
110
                        names.add(iter.next());
111
                }
112
                iter = stateIn.getNames();
113
                while (iter.hasNext()) {
114
                        namesIn.add(iter.next());
115
                }
116

    
117
                assertEquals(names.size(), namesIn.size());
118

    
119
                iter = names.iterator();
120
                while (iter.hasNext()) {
121
                        assertTrue(namesIn.contains(iter.next()));
122
                }
123

    
124
                Object loadedPersistent = manager.create(stateIn);
125

    
126
                assertTrue(loadedPersistent instanceof XMLPersistenceTest_Persistent);
127

    
128
                ((XMLPersistenceTest_Persistent) loadedPersistent).checked();
129

    
130

    
131

    
132
        }
133

    
134

    
135
}