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 / BasicSetTest.java

History | View | Annotate | Download (6.25 KB)

1 802 cordinyana
/**
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 73 jjdelcerro
package org.gvsig.tools.persistence;
25
26
import java.util.HashSet;
27
import java.util.Iterator;
28
import java.util.Set;
29
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dataTypes.DataTypes;
32
import org.gvsig.tools.dynobject.DynStruct;
33
import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered;
34
import org.gvsig.tools.persistence.exception.PersistenceException;
35
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException;
36
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions;
37
38
public class BasicSetTest         extends AbstractBasicTest {
39
40
        public static  class MyObject2 implements Persistent {
41
42
                String name;
43
44
                public MyObject2() {
45
                        this.name = "(unknow)";
46
                }
47
48
                MyObject2(String name){
49
                        this.name = name;
50
                }
51
52
                public String getName() {
53
                        return this.name;
54
                }
55
56
                public void loadFromState(PersistentState state)
57
                                throws PersistenceException {
58
                        this.name = state.getString("name");
59
                }
60
61
                public void saveToState(PersistentState state)
62
                                throws PersistenceException {
63
                        state.set("name",this.name);
64
                }
65
66
        }
67
68
        public static  class MyObject implements Persistent {
69
                Set values;
70
                Set persistentValues;
71
72
                public MyObject() {
73
                        this.values = new HashSet();
74
                        this.persistentValues = new HashSet();
75
76
                        for( int i=0; i<10; i++ ) {
77
                                this.values.add( "valor-" + i);
78
                        }
79
80
                        for( int i=0; i<10; i++ ) {
81
                                this.persistentValues.add( new MyObject2("valor-" + i));
82
                        }
83
                }
84
85
                void check() {
86
                        assertNotNull(this.values);
87
                        assertNotNull(this.persistentValues);
88
                        assertEquals("size of list of strings mistmach",10, this.values.size());
89
                        assertEquals("size of list of complex object mistmach",10, this.persistentValues.size());
90
                        Set values = new HashSet();
91
92
                        for( int i=0; i<10; i++ ) {
93
                                String value = "valor-"+i;
94
                                values.add(value);
95
                                assertTrue("value "+ i + " mistmach in list of strings.", this.values.contains(value));
96
                        }
97
                        Iterator it = this.persistentValues.iterator();
98
                        while( it.hasNext() ) {
99
                                MyObject2 obj = (MyObject2) it.next();
100
                                if( ! values.contains(obj.getName()) ) {
101
                                        fail("value '"+ obj.getName() + "' mistmach in list of complex objects.");
102
                                }
103
                                values.remove(obj.getName());
104
                        }
105
                        if( values.size() != 0 ) {
106
                                fail("Unspected values in list of complex objects ");
107
                        }
108
                }
109
110
                public void loadFromState(PersistentState state)
111
                                throws PersistenceException {
112
                        this.values = new HashSet(state.getSet("values"));
113
                        this.persistentValues = new HashSet(state.getSet("persistentValues"));
114
                }
115
116
                public void saveToState(PersistentState state)
117
                                throws PersistenceException {
118
                        state.set("values",this.values);
119
                        state.set("persistentValues",this.persistentValues);
120
                }
121
        }
122
123
124
        protected void register() {
125
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
126
                DynStruct definition2 = manager.addDefinition(
127
                                MyObject2.class,
128
                                "MyObject2BasicSet",
129
                                "MyObject2BasicSet Persistence definition",
130
                                null,
131
                                null
132
                );
133
                definition2.addDynFieldString("name").setMandatory(true);
134
135
                DynStruct definition = manager.addDefinition(
136
                                MyObject.class,
137
                                "MyObjectBasicSet",
138
                                "MyObjectBasicSet Persistence definition",
139
                                null,
140
                                null
141
                );
142 130 jjdelcerro
                definition.addDynFieldSet("values").setMandatory(true).setElementsType(DataTypes.STRING);
143 73 jjdelcerro
                definition.addDynFieldSet("persistentValues").setMandatory(true).setElementsType(definition2);
144
        }
145
146
        protected void unregister() throws PersistenceClassNotRegistered {
147
                manager.unregisterClass("MyObjectBasicSet");
148
                manager.unregisterClass("MyObject2BasicSet");
149
        }
150
151
        public void doSetUp() throws Exception {
152
                super.doSetUp();
153
                register();
154
        }
155
156
        protected void tearDown() throws Exception {
157
                super.tearDown();
158
                unregister();
159
        }
160
161
        //
162
        // =================================================
163
        //
164
165
        protected String getTestId() {
166
                return "basiclist";
167
        }
168
169
        public void testRegister() {
170
                DynStruct definition = manager.getDefinition(MyObject.class);
171
                assertNotNull("Can't register class", definition);
172
                assertEquals(
173
                                "Registration don't work, name incorrect",
174
                                "MyObjectBasicSet",
175
                                definition.getName()
176
                );
177
        }
178
179
        public void testGetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions {
180
                try {
181
                        MyObject obj = new MyObject();
182
                        PersistentState state = manager.getState(obj);
183
                        assertNotNull("Can't retrieve state", state);
184
                        DynStruct definition = state.getDefinition();
185
                        assertNotNull("Can't register class", definition);
186
                        assertEquals(
187
                                        "Registration don't work, name incorrect",
188
                                        "MyObjectBasicSet",
189
                                        definition.getName()
190
                        );
191
                }catch(PersistenceValidateExceptions ex) {
192
                        fail(ex.getMessage());
193
                }
194
         }
195
196
        public void testSetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions {
197
                MyObject obj1 = new MyObject();
198
                PersistentState state = manager.getState(obj1);
199
                assertNotNull("Can't retrieve state", state);
200
201
                MyObject obj2 = (MyObject) manager.create(state);
202
                obj2.check();
203
                obj1.check();
204
        }
205
206
        public void testListSaveState() throws Exception {
207
                MyObject obj = new MyObject();
208
                saveState(obj);
209
        }
210
211
        public void testListLoadState() throws Exception {
212
                MyObject obj1 = new MyObject();
213
                MyObject obj2 = (MyObject) manager.create(this.saveAndRestoreState(obj1));
214
215
                obj2.check();
216
                obj1.check();
217
        }
218
219
220
}