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

History | View | Annotate | Download (6.18 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
27
import java.util.Date;
28
import java.util.GregorianCalendar;
29
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DynStruct;
32
import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered;
33
import org.gvsig.tools.persistence.exception.PersistenceException;
34
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException;
35
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions;
36
37
public class BasicTypesTest         extends AbstractBasicTest {
38
39
                private static int counter = 0;
40
41
                private static Date getDate() {
42
                        return new GregorianCalendar(2010, 8, 5, 12, 5).getTime();
43
                }
44
45
                public static  class MyObject implements Persistent {
46
                        int a;
47
                        long b;
48
                        double c;
49
                        float d;
50
                        boolean e;
51
                        String f;
52
                        Date g;
53
54
                        public MyObject() {
55
                                float value = (counter/3)*700;
56
57
                                this.a =  (int)value;
58
                                this.b = (long) value ;
59
                                this.c = value;
60
                                this.d = value;
61
                                this.e = (counter%2) == 0;
62
                                this.f = String.valueOf(value);
63
                                this.g= getDate();
64
65
                                counter++;
66
                        }
67
68
                        void check(MyObject other) {
69
                                assertEquals("int value mistmach", this.a, other.a);
70
                                assertEquals("long value mistmach", this.b, other.b);
71
                                assertEquals("double value mistmach", this.c, other.c,0.01);
72
                                assertEquals("float value mistmach", this.d, other.d, 0.01);
73
                                assertEquals("boolean value mistmach", this.e, other.e);
74
                                assertEquals("String value mistmach", this.f, other.f);
75
                                assertTrue("Date value mistmach", getDate().equals(this.g));
76
                        }
77
78
                        public void loadFromState(PersistentState state)
79
                                        throws PersistenceException {
80
                                this.a = state.getInt("a");
81
                                this.b = state.getLong("b");
82
                                this.c = state.getDouble("c");
83
                                this.d = state.getFloat("d");
84
                                this.e = state.getBoolean("e");
85
                                this.f = state.getString("f");
86
                                this.g = (Date) state.get("g");
87
                        }
88
89
                        public void saveToState(PersistentState state)
90
                                        throws PersistenceException {
91
                                state.set("a",a);
92
                                state.set("b",b);
93
                                state.set("c",c);
94
                                state.set("d",d);
95
                                state.set("e",e);
96
                                state.set("f",f);
97
                                state.set("g",g);
98
                        }
99
                }
100
101
                protected void register() {
102
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
103
                        DynStruct definition = manager.addDefinition(
104
                                        MyObject.class,
105
                                        "MyObjectBasicTypes",
106
                                        "MyObjectBasicTypes Persistence definition",
107
                                        null,
108
                                        null
109
                        );
110
                        definition.addDynFieldInt("a").setMandatory(true);
111
                        definition.addDynFieldLong("b").setMandatory(true);
112
                        definition.addDynFieldDouble("c").setMandatory(true);
113
                        definition.addDynFieldFloat("d").setMandatory(true);
114
                        definition.addDynFieldBoolean("e").setMandatory(true);
115
                        definition.addDynFieldString("f").setMandatory(true);
116
                        definition.addDynFieldDate("g").setMandatory(true);
117
                }
118
119
                protected void unregister() throws PersistenceClassNotRegistered {
120
                        manager.unregisterClass("MyObjectBasicTypes");
121
                }
122
123
                public void doSetUp() throws Exception {
124
                        super.doSetUp();
125
                        register();
126
                }
127
128
                protected void tearDown() throws Exception {
129
                        super.tearDown();
130
                        unregister();
131
                }
132
133
                //
134
                // =================================================
135
                //
136
137
                protected String getTestId() {
138
                        return "basictypes";
139
                }
140
141
                public void testRegister() {
142
                        DynStruct definition = manager.getDefinition(MyObject.class);
143
                        assertNotNull("Can't register class", definition);
144
                        assertEquals(
145 385 cmartin
                                        "Registration didn't work, incorrect name.",
146 73 jjdelcerro
                                        "MyObjectBasicTypes",
147
                                        definition.getName()
148
                        );
149 188 jjdelcerro
                        assertEquals(
150 385 cmartin
                                        "Registration didn't work, incorrect namespace.",
151 188 jjdelcerro
                                        PersistenceManager.PERSISTENCE_NAMESPACE,
152
                                        definition.getNamespace()
153
                        );
154 73 jjdelcerro
                }
155
156
                public void testGetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions {
157
                        try {
158
                                MyObject obj = new MyObject();
159
                                PersistentState state = manager.getState(obj);
160
                                assertNotNull("Can't retrieve state", state);
161
                                DynStruct definition = state.getDefinition();
162
                                assertNotNull("Can't register class", definition);
163
                                assertEquals(
164
                                                "Registration don't work, name incorrect",
165
                                                "MyObjectBasicTypes",
166
                                                definition.getName()
167
                                );
168
                        } catch( PersistenceValidateExceptions ex) {
169
                                logger.warn( ex.getMessageStack(), ex);
170
                                throw ex;
171
                        }
172
                }
173
174
                public void testSetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions {
175
                        MyObject obj1 = new MyObject();
176
                        PersistentState state = manager.getState(obj1);
177
                        assertNotNull("Can't retrieve state", state);
178
179
                        MyObject obj2 = (MyObject) manager.create(state);
180
                        obj1.check(obj2);
181
                }
182
183
                public void testSaveState() throws Exception {
184
                        MyObject obj = new MyObject();
185
                        saveState(obj);
186
                }
187
188
                public void testLoadState() throws Exception {
189
                        MyObject obj1 = new MyObject();
190
                        PersistentState state = this.saveAndRestoreState(obj1);
191
                        MyObject obj2 = (MyObject) manager.create(state);
192
193
                        obj1.check(obj2);
194
                }
195
196 639 jjdelcerro
                public void testPutObject() throws Exception {
197
                        MyObject obj = new MyObject();
198
                        saveObject(obj);
199
                }
200
201
                public void testGetObject() throws Exception {
202
                        MyObject obj1 = new MyObject();
203
                        MyObject obj2 = (MyObject) this.saveAndRestoreObject(obj1);
204
205
                        obj1.check(obj2);
206
                }
207
208 73 jjdelcerro
209
}