svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / normalization / patterns / TestPersistence.java @ 32479
History | View | Annotate | Download (8.16 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 |
* 2008 Prodevelop S.L. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.normalization.patterns; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.FileOutputStream; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
|
35 |
import junit.framework.TestCase; |
36 |
|
37 |
import org.gvsig.fmap.dal.DALFileLibrary; |
38 |
import org.gvsig.fmap.dal.DALLibrary; |
39 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
40 |
import org.gvsig.normalization.pattern.Element; |
41 |
import org.gvsig.normalization.pattern.Fieldseparator; |
42 |
import org.gvsig.normalization.pattern.Fieldtype; |
43 |
import org.gvsig.normalization.pattern.Infieldseparators; |
44 |
import org.gvsig.normalization.pattern.NormalizationPattern; |
45 |
import org.gvsig.normalization.pattern.Stringvalue; |
46 |
import org.gvsig.normalization.pattern.impl.DefaultElement; |
47 |
import org.gvsig.normalization.pattern.impl.DefaultFieldseparator; |
48 |
import org.gvsig.normalization.pattern.impl.DefaultFieldtype; |
49 |
import org.gvsig.normalization.pattern.impl.DefaultInfieldseparators; |
50 |
import org.gvsig.normalization.pattern.impl.DefaultNormalizationPattern; |
51 |
import org.gvsig.normalization.pattern.impl.DefaultStringvalue; |
52 |
import org.gvsig.tools.ToolsLibrary; |
53 |
import org.gvsig.tools.library.Library; |
54 |
import org.gvsig.utils.XMLEntity; |
55 |
import org.gvsig.utils.XMLException; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
* test persistence
|
61 |
*
|
62 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
63 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
64 |
*
|
65 |
*/
|
66 |
public class TestPersistence extends TestCase { |
67 |
|
68 |
private static final Logger log = LoggerFactory |
69 |
.getLogger(TestPersistence.class); |
70 |
|
71 |
/**
|
72 |
* Constructor
|
73 |
*/
|
74 |
public TestPersistence() {
|
75 |
|
76 |
ToolsLibrary tools = new ToolsLibrary();
|
77 |
tools.initialize(); |
78 |
tools.postInitialize(); |
79 |
|
80 |
Library dlib = new DALLibrary();
|
81 |
dlib.initialize(); |
82 |
dlib.postInitialize(); |
83 |
|
84 |
Library libFile = new DALFileLibrary();
|
85 |
libFile.initialize(); |
86 |
libFile.postInitialize(); |
87 |
|
88 |
Library shpLib = new SHPLibrary();
|
89 |
shpLib.initialize(); |
90 |
shpLib.postInitialize(); |
91 |
} |
92 |
|
93 |
public void setUp() { |
94 |
|
95 |
} |
96 |
|
97 |
public void testSerializing() throws XMLException { |
98 |
|
99 |
NormalizationPattern pat = new DefaultNormalizationPattern();
|
100 |
pat.setPatternname("pruebaParseXML.xml");
|
101 |
pat.setNofirstrows(0);
|
102 |
|
103 |
List<Element> elems = new ArrayList<Element>(); |
104 |
|
105 |
Element elem1 = new DefaultElement(); |
106 |
elem1.setFieldname("campo1");
|
107 |
|
108 |
Fieldtype tipo = new DefaultFieldtype();
|
109 |
Stringvalue strval = new DefaultStringvalue();
|
110 |
strval.setStringvaluewidth(50);
|
111 |
tipo.setStringvalue(strval); |
112 |
elem1.setFieldtype(tipo); |
113 |
|
114 |
elem1.setFieldwidth(30);
|
115 |
|
116 |
Fieldseparator fsep = new DefaultFieldseparator();
|
117 |
fsep.setSemicolonsep(true);
|
118 |
fsep.setJoinsep(false);
|
119 |
elem1.setFieldseparator(fsep); |
120 |
|
121 |
Infieldseparators infsep = new DefaultInfieldseparators();
|
122 |
infsep.setThousandseparator(",");
|
123 |
infsep.setDecimalseparator(".");
|
124 |
infsep.setTextseparator("\"");
|
125 |
elem1.setInfieldseparators(infsep); |
126 |
|
127 |
elem1.setImportfield(true);
|
128 |
|
129 |
elems.add(elem1); |
130 |
|
131 |
Element elem2 = new DefaultElement(); |
132 |
elem2.setFieldname("campo2");
|
133 |
|
134 |
Fieldtype tipo2 = new DefaultFieldtype();
|
135 |
Stringvalue strval2 = new DefaultStringvalue();
|
136 |
strval2.setStringvaluewidth(50);
|
137 |
tipo2.setStringvalue(strval2); |
138 |
elem2.setFieldtype(tipo2); |
139 |
|
140 |
elem2.setFieldwidth(30);
|
141 |
|
142 |
Fieldseparator fsep2 = new DefaultFieldseparator();
|
143 |
fsep2.setSemicolonsep(true);
|
144 |
fsep2.setJoinsep(false);
|
145 |
elem2.setFieldseparator(fsep2); |
146 |
|
147 |
Infieldseparators infsep2 = new DefaultInfieldseparators();
|
148 |
infsep2.setThousandseparator(",");
|
149 |
infsep2.setDecimalseparator(".");
|
150 |
infsep2.setTextseparator("\"");
|
151 |
elem2.setInfieldseparators(infsep2); |
152 |
|
153 |
elem2.setImportfield(true);
|
154 |
|
155 |
elems.add(elem2); |
156 |
|
157 |
pat.setElements(elems); |
158 |
|
159 |
XMLEntity xml = pat.getXMLEntity(); |
160 |
assertNotNull(xml); |
161 |
} |
162 |
|
163 |
public void testParsing() throws XMLException { |
164 |
|
165 |
NormalizationPattern pat = new DefaultNormalizationPattern();
|
166 |
pat.setPatternname("pruebaParseXML.xml");
|
167 |
pat.setNofirstrows(0);
|
168 |
|
169 |
List<Element> elems = new ArrayList<Element>(); |
170 |
|
171 |
Element elem1 = new DefaultElement(); |
172 |
elem1.setFieldname("campo1");
|
173 |
|
174 |
Fieldtype tipo = new DefaultFieldtype();
|
175 |
Stringvalue strval = new DefaultStringvalue();
|
176 |
strval.setStringvaluewidth(50);
|
177 |
tipo.setStringvalue(strval); |
178 |
elem1.setFieldtype(tipo); |
179 |
|
180 |
elem1.setFieldwidth(30);
|
181 |
|
182 |
Fieldseparator fsep = new DefaultFieldseparator();
|
183 |
fsep.setSemicolonsep(true);
|
184 |
fsep.setJoinsep(false);
|
185 |
elem1.setFieldseparator(fsep); |
186 |
|
187 |
Infieldseparators infsep = new DefaultInfieldseparators();
|
188 |
infsep.setThousandseparator(",");
|
189 |
infsep.setDecimalseparator(".");
|
190 |
infsep.setTextseparator("\"");
|
191 |
elem1.setInfieldseparators(infsep); |
192 |
|
193 |
elem1.setImportfield(true);
|
194 |
|
195 |
elems.add(elem1); |
196 |
|
197 |
Element elem2 = new DefaultElement(); |
198 |
elem2.setFieldname("campo2");
|
199 |
|
200 |
Fieldtype tipo2 = new DefaultFieldtype();
|
201 |
Stringvalue strval2 = new DefaultStringvalue();
|
202 |
strval2.setStringvaluewidth(50);
|
203 |
tipo2.setStringvalue(strval2); |
204 |
elem2.setFieldtype(tipo2); |
205 |
|
206 |
elem2.setFieldwidth(30);
|
207 |
|
208 |
Fieldseparator fsep2 = new DefaultFieldseparator();
|
209 |
fsep2.setSemicolonsep(true);
|
210 |
fsep2.setJoinsep(false);
|
211 |
elem2.setFieldseparator(fsep2); |
212 |
|
213 |
Infieldseparators infsep2 = new DefaultInfieldseparators();
|
214 |
infsep2.setThousandseparator(",");
|
215 |
infsep2.setDecimalseparator(".");
|
216 |
infsep2.setTextseparator("\"");
|
217 |
elem2.setInfieldseparators(infsep2); |
218 |
|
219 |
elem2.setImportfield(true);
|
220 |
|
221 |
elems.add(elem2); |
222 |
|
223 |
pat.setElements(elems); |
224 |
|
225 |
XMLEntity xml = pat.getXMLEntity(); |
226 |
|
227 |
// pat 2
|
228 |
NormalizationPattern pat2 = new DefaultNormalizationPattern();
|
229 |
pat2.setXMLEntity(xml); |
230 |
|
231 |
assertEquals(0, pat2.getNofirstrows());
|
232 |
|
233 |
assertEquals("pruebaParseXML.xml", pat2.getPatternname());
|
234 |
assertEquals(0, pat2.getNofirstrows());
|
235 |
|
236 |
Element elem0 = ((Element) pat2.getElements().get(0)); |
237 |
|
238 |
assertEquals("campo1", elem0.getFieldname());
|
239 |
assertEquals(true, elem0.getImportfield());
|
240 |
} |
241 |
|
242 |
public void testParserSerializer() throws Exception { |
243 |
|
244 |
NormalizationPattern pat = new DefaultNormalizationPattern();
|
245 |
|
246 |
File file = new File("./resources-test/normalization/patSplitChain.xml"); |
247 |
|
248 |
assertTrue(file.exists()); |
249 |
|
250 |
// PARSER
|
251 |
((DefaultNormalizationPattern) pat).loadFromXML(file); |
252 |
|
253 |
assertNotNull(pat); |
254 |
assertEquals(11, pat.getElements().size());
|
255 |
assertEquals(0, pat.getNofirstrows());
|
256 |
|
257 |
Element elem1 = pat.getElements().get(0); |
258 |
|
259 |
assertNotNull(elem1); |
260 |
assertEquals("NewField", elem1.getFieldname());
|
261 |
|
262 |
// SERIALIZER
|
263 |
File ftemp = File.createTempFile("tem", "txt"); |
264 |
FileOutputStream out2 = new FileOutputStream(ftemp); |
265 |
|
266 |
XMLEntity xml1 = pat.getXMLEntity(); |
267 |
|
268 |
// PARSER
|
269 |
NormalizationPattern pat3 = new DefaultNormalizationPattern();
|
270 |
|
271 |
pat3.setXMLEntity(xml1); |
272 |
|
273 |
Element elem2 = (Element) pat3.getElements().get(0); |
274 |
|
275 |
assertNotNull(elem2); |
276 |
|
277 |
assertEquals(elem1.getImportfield(), elem2.getImportfield()); |
278 |
assertEquals(elem1.getFieldwidth(), elem2.getFieldwidth()); |
279 |
assertEquals(elem1.getFieldname(), elem2.getFieldname()); |
280 |
assertEquals(elem1.getInfieldseparators().getDecimalseparator(), elem2 |
281 |
.getInfieldseparators().getDecimalseparator()); |
282 |
|
283 |
ftemp.delete(); |
284 |
log.debug("Test finished.....");
|
285 |
|
286 |
} |
287 |
} |