Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / tags / org.gvsig.tools-3.0.13 / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / persistence / case1 / ComplexTest.java

History | View | Annotate | Download (4.93 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.persistence.case1;
25

    
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileOutputStream;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31

    
32
import org.gvsig.tools.ToolsLocator;
33
import org.gvsig.tools.dynobject.DynStruct;
34
import org.gvsig.tools.exception.BaseException;
35
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
36
import org.gvsig.tools.persistence.PersistenceManager;
37
import org.gvsig.tools.persistence.PersistentState;
38
import org.gvsig.tools.persistence.case1.model.MapContext;
39
import org.gvsig.tools.persistence.case1.model.ModelManager;
40
import org.gvsig.tools.persistence.case1.model.Projection;
41
import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered;
42
import org.gvsig.tools.persistence.exception.PersistenceException;
43
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException;
44
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
public class ComplexTest extends AbstractLibraryAutoInitTestCase {
49

    
50
        private static Logger LOG = LoggerFactory.getLogger(ComplexTest.class);
51

    
52
        PersistenceManager manager;
53
        ModelManager modelmgr;
54

    
55
        protected void doSetUp() throws Exception {
56
                this.manager = ToolsLocator.getPersistenceManager();
57
                manager.setAutoValidation(PersistenceManager.MANDATORY);
58
                modelmgr = new ModelManager();
59
                modelmgr.registerPersistence();
60
        }
61

    
62
        protected void tearDown() throws Exception {
63
                super.tearDown();
64
                modelmgr.unregisterPersistence();
65
        }
66

    
67
        public void compareMapContext(MapContext obj1, MapContext obj2) {
68
                assertEquals("MapContext aren't equals, code mismatch", obj1.getCode(),
69
                                obj2.getCode());
70
                assertEquals("MapContext aren't equals, projection mismatch", obj1
71
                                .getProjection().getFullCode(), obj2.getProjection()
72
                                .getFullCode());
73
                assertEquals("MapContext aren't equals, viewport mismatch", obj1
74
                                .getViewPort().getCode(), obj2.getViewPort().getCode());
75
        }
76

    
77
        public void testRegister() {
78
                DynStruct definition = manager.getDefinition(MapContext.class);
79
                assertNotNull("Can't register class", definition);
80
                assertEquals("Registration don't work, name incorrect", "MapContext",
81
                                definition.getName());
82
        }
83

    
84
        public void testGetState() throws PersistenceTypeNotSupportedException,
85
                        PersistenceClassNotRegistered, PersistenceException,
86
                        PersistenceValidateExceptions {
87
                try {
88
                        MapContext obj = new MapContext(new Projection());
89
                        PersistentState state = manager.getState(obj);
90
                        assertNotNull("Can't retrieve state", state);
91
                        assertNotNull("Can't retrieve layers", state.get("layers"));
92
                        assertNotNull("Can't retrieve viewPort", state.get("viewPort"));
93
                } catch (BaseException ex) {
94
                        LOG.error(ex.getMessageStack());
95
                } catch (Exception ex) {
96
                        LOG.error(ex.getMessage());
97
                }
98

    
99
        }
100

    
101
        public void testSetState() throws PersistenceTypeNotSupportedException,
102
                        PersistenceClassNotRegistered, PersistenceException,
103
                        PersistenceValidateExceptions {
104
                MapContext obj1 = new MapContext(new Projection());
105
                PersistentState state = manager.getState(obj1);
106
                assertNotNull("Can't retrieve state", state);
107

    
108
                MapContext obj2 = (MapContext) manager.create(state);
109
                this.compareMapContext(obj1, obj2);
110
        }
111

    
112
        public void testSaveState() throws Exception {
113
                MapContext obj = new MapContext(new Projection());
114
                PersistentState state = manager.getState(obj);
115

    
116
                File f = File.createTempFile("gvsig-test-pers-complex", "A.zip");
117
                OutputStream out = new FileOutputStream(f);
118
                manager.saveState(state, out);
119
                out.close();
120

    
121
        }
122

    
123
        public void testLoadState() throws Exception {
124
                MapContext obj1 = new MapContext(new Projection());
125
                PersistentState state1 = manager.getState(obj1);
126

    
127
                File f = File.createTempFile("gvsig-test-pers-complex", "B.zip");
128
                OutputStream out = new FileOutputStream(f);
129
                manager.saveState(state1, out);
130
                out.close();
131

    
132
                InputStream in = new FileInputStream(f);
133
                PersistentState state2 = manager.loadState(in);
134
                MapContext obj2 = (MapContext) manager.create(state2);
135

    
136
                this.compareMapContext(obj1, obj2);
137

    
138
        }
139

    
140
}