gvsig-tools / org.gvsig.tools / library / tags / org.gvsig.tools-3.0.13 / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / persistence / BasicTypesArrayTest.java
History | View | Annotate | Download (6.26 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; |
25 |
|
26 |
import java.util.Date; |
27 |
import java.util.GregorianCalendar; |
28 |
|
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dataTypes.DataTypes; |
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 |
|
38 |
public class BasicTypesArrayTest extends AbstractBasicTest { |
39 |
|
40 |
private static int counter = 0; |
41 |
|
42 |
private static Date getDate(int day) { |
43 |
return new GregorianCalendar(2010, 8, 5, 12, day).getTime(); |
44 |
} |
45 |
|
46 |
|
47 |
public static class MyObjectArray implements Persistent { |
48 |
int a[] ; |
49 |
long b[]; |
50 |
double c[]; |
51 |
float d[]; |
52 |
boolean e[]; |
53 |
String f[]; |
54 |
Date g[]; |
55 |
|
56 |
public MyObjectArray() {
|
57 |
float value = (counter/3)*700; |
58 |
|
59 |
this.a = new int[] { (int)value, (int)value, (int)value } ; |
60 |
this.b = new long[] { (long)value, (long)value, (long)value } ; |
61 |
this.c = new double[] { value, value, value }; |
62 |
this.d = new float[] { value, value, value }; |
63 |
this.e = new boolean[] { (counter%2) == 0, (counter%2) == 0, (counter%2) == 0 }; |
64 |
this.f = new String[] { String.valueOf(value),String.valueOf(value),String.valueOf(value) }; |
65 |
this.g = new Date[] { getDate(10), getDate(11), getDate(20) }; |
66 |
counter++; |
67 |
} |
68 |
|
69 |
void check(MyObjectArray other) {
|
70 |
for( int i=0; i<this.a.length; i++ ) { |
71 |
assertEquals("int value["+i+"] mistmach", this.a[i], other.a[i]); |
72 |
assertEquals("long value["+i+"] mistmach", this.b[i], other.b[i]); |
73 |
assertEquals("double value["+i+"] mistmach", this.c[i], other.c[i],0.01); |
74 |
assertEquals("float value["+i+"] mistmach", this.d[i], other.d[i], 0.01); |
75 |
assertEquals("boolean value["+i+"] mistmach", this.e[i], other.e[i]); |
76 |
assertEquals("String value["+i+"] mistmach", this.f[i], other.f[i]); |
77 |
assertTrue("Date value["+i+"] mistmach", this.f[i].equals( other.f[i]) ); |
78 |
} |
79 |
} |
80 |
|
81 |
public void loadFromState(PersistentState state) |
82 |
throws PersistenceException {
|
83 |
this.a = state.getIntArray("a"); |
84 |
this.b = state.getLongArray("b"); |
85 |
this.c = state.getDoubleArray("c"); |
86 |
this.d = state.getFloatArray("d"); |
87 |
this.e = state.getBooleanArray("e"); |
88 |
this.f = state.getStringArray("f"); |
89 |
this.g = state.getDateArray("g"); |
90 |
} |
91 |
|
92 |
public void saveToState(PersistentState state) |
93 |
throws PersistenceException {
|
94 |
state.set("a",a);
|
95 |
state.set("b",b);
|
96 |
state.set("c",c);
|
97 |
state.set("d",d);
|
98 |
state.set("e",e);
|
99 |
state.set("f",f);
|
100 |
state.set("g",g);
|
101 |
} |
102 |
} |
103 |
|
104 |
|
105 |
protected void registerArray() { |
106 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
107 |
DynStruct definition = manager.addDefinition( |
108 |
MyObjectArray.class, |
109 |
"MyObjectBasicTypesArray",
|
110 |
"MyObjectBasicTypesArray Persistence definition",
|
111 |
null,
|
112 |
null
|
113 |
); |
114 |
definition.addDynFieldArray("a").setMandatory(true).setElementsType(DataTypes.INT); |
115 |
definition.addDynFieldArray("b").setMandatory(true).setElementsType(DataTypes.LONG); |
116 |
definition.addDynFieldArray("c").setMandatory(true).setElementsType(DataTypes.DOUBLE); |
117 |
definition.addDynFieldArray("d").setMandatory(true).setElementsType(DataTypes.FLOAT); |
118 |
definition.addDynFieldArray("e").setMandatory(true).setElementsType(DataTypes.BOOLEAN); |
119 |
definition.addDynFieldArray("f").setMandatory(true).setElementsType(DataTypes.STRING); |
120 |
definition.addDynFieldArray("g").setMandatory(true).setElementsType(DataTypes.DATE); |
121 |
} |
122 |
|
123 |
protected void unregister() throws PersistenceClassNotRegistered { |
124 |
manager.unregisterClass("MyObjectBasicTypesArray");
|
125 |
} |
126 |
|
127 |
public void doSetUp() throws Exception { |
128 |
super.doSetUp();
|
129 |
registerArray(); |
130 |
} |
131 |
|
132 |
protected void tearDown() throws Exception { |
133 |
super.tearDown();
|
134 |
unregister(); |
135 |
} |
136 |
|
137 |
protected String getTestId() { |
138 |
return "basicarray"; |
139 |
} |
140 |
|
141 |
public void testRegister() { |
142 |
DynStruct definition = manager.getDefinition(MyObjectArray.class); |
143 |
assertNotNull("Can't register class", definition);
|
144 |
assertEquals( |
145 |
"Registration don't work, name incorrect",
|
146 |
"MyObjectBasicTypesArray",
|
147 |
definition.getName() |
148 |
); |
149 |
} |
150 |
|
151 |
public void testGetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions { |
152 |
MyObjectArray obj = new MyObjectArray();
|
153 |
PersistentState state = manager.getState(obj); |
154 |
assertNotNull("Can't retrieve state", state);
|
155 |
DynStruct definition = state.getDefinition(); |
156 |
assertNotNull("Can't register class", definition);
|
157 |
assertEquals( |
158 |
"Registration don't work, name incorrect",
|
159 |
"MyObjectBasicTypesArray",
|
160 |
definition.getName() |
161 |
); |
162 |
} |
163 |
|
164 |
public void testSetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions { |
165 |
MyObjectArray obj1 = new MyObjectArray();
|
166 |
PersistentState state = manager.getState(obj1); |
167 |
assertNotNull("Can't retrieve state", state);
|
168 |
|
169 |
MyObjectArray obj2 = (MyObjectArray) manager.create(state); |
170 |
obj1.check(obj2); |
171 |
} |
172 |
|
173 |
public void testSaveState() throws Exception { |
174 |
MyObjectArray obj = new MyObjectArray();
|
175 |
saveState(obj); |
176 |
} |
177 |
|
178 |
public void testLoadState() throws Exception { |
179 |
MyObjectArray obj1 = new MyObjectArray();
|
180 |
MyObjectArray obj2 = (MyObjectArray) manager.create(this.saveAndRestoreState(obj1));
|
181 |
|
182 |
obj1.check(obj2); |
183 |
} |
184 |
|
185 |
|
186 |
} |