Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / normalization / operations / TestNormAlgorithm.java @ 29385

History | View | Annotate | Download (10.2 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.operations;
29

    
30
import java.io.File;
31
import java.io.FileReader;
32
import java.io.IOException;
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.algorithm.NormalizationAlgorithm;
41
import org.gvsig.normalization.algorithm.impl.DefaultNormalizationAlgorithm;
42
import org.gvsig.normalization.pattern.Patternnormalization;
43
import org.gvsig.normalization.pattern.impl.DefaultPatternnormalization;
44
import org.gvsig.tools.ToolsLibrary;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.locator.Library;
47
import org.gvsig.tools.persistence.PersistenceException;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.xmlentity.XMLEntityManager;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
public class TestNormAlgorithm extends TestCase {
55

    
56
        private static final Logger log = LoggerFactory
57
                        .getLogger(TestNormAlgorithm.class);
58

    
59
        /**
60
         * Constructor
61
         */
62
        public TestNormAlgorithm() {
63

    
64
                ToolsLibrary tools = new ToolsLibrary();
65
                tools.initialize();
66
                ToolsLocator.registerPersistenceManager(XMLEntityManager.class);
67
                tools.postInitialize();
68

    
69
                Library dlib = new DALLibrary();
70
                dlib.initialize();
71
                dlib.postInitialize();
72

    
73
                Library libFile = new DALFileLibrary();
74
                libFile.initialize();
75
                libFile.postInitialize();
76

    
77
                Library shpLib = new SHPLibrary();
78
                shpLib.initialize();
79
                shpLib.postInitialize();
80
        }        
81

    
82
        /**
83
         * 
84
         * @throws PersistenceException
85
         * @throws IOException
86
         */
87
        public void testSplitChain() throws PersistenceException, IOException {
88

    
89
                log.debug("TestNormAlgorithm. Test splits strings");
90

    
91
                log.debug("SubTest 1");
92
                String c1 = ";aaa,,bbb;;;ccc/ddd@eee##;";
93

    
94
                File f1 = new File(
95
                                "test-data/normalization/patSplitChain.xml");
96

    
97
                assertTrue(f1.exists());
98
                assertNotNull(f1);
99

    
100
                List<String> r1 = parser(f1, c1);
101
                assertNotNull(r1);
102

    
103
                assertEquals("", (String) r1.get(0));
104
                assertEquals("aaa", (String) r1.get(1));
105
                assertEquals("", (String) r1.get(2));
106
                assertEquals("bbb", (String) r1.get(3));
107
                assertEquals("", (String) r1.get(4));
108
                assertEquals("", (String) r1.get(5));
109
                assertEquals("ccc", (String) r1.get(6));
110
                assertEquals("ddd", (String) r1.get(7));
111
                assertEquals("eee", (String) r1.get(8));
112
                assertEquals("", (String) r1.get(9));
113
                assertEquals(";", (String) r1.get(10));
114

    
115
                log.debug("SubTest 2.");
116
                String c2 = "aaa bbb ccc ddd,76 %";
117

    
118
                File f2 = new File(
119
                                "test-data/normalization/patSplitChain2.xml");
120
                assertTrue(f2.exists());
121
                assertNotNull(f2);
122

    
123
                List<String> r2 = parser(f2, c2);
124
                assertNotNull(r2);
125

    
126
                assertEquals("aaa", (String) r2.get(0));
127
                assertEquals("bbb", (String) r2.get(1));
128
                assertEquals("ccc", (String) r2.get(2));
129
                assertEquals("ddd", (String) r2.get(3));
130
                assertEquals("76", (String) r2.get(4));
131
                assertEquals("%", (String) r2.get(5));
132

    
133
                log.debug("SubTest 3.");
134
                String c3 = "Av;Germanias;25;3;Moncada;Valencia";
135

    
136
                File f3 = new File(
137
                                "test-data/normalization/testdata/patSplitChain3.xml");
138
                assertTrue(f3.exists());
139
                assertNotNull(f3);
140

    
141
                List<String> r3 = parser(f3, c3);
142
                assertNotNull(r3);
143

    
144
                assertEquals("Av", (String) r3.get(0));
145
                assertEquals("Germanias", (String) r3.get(1));
146
                assertEquals(25, Integer.parseInt((String) r3.get(2)));
147
                assertEquals(3, Integer.parseInt((String) r3.get(3)));
148
                assertEquals("Moncada", (String) r3.get(4));
149
                assertEquals("Valencia", (String) r3.get(5));
150

    
151
                log.debug("SubTest 4.");
152
                String c4 = "Av. Germanias      15  2   Moncada   Valencia    ";
153

    
154
                File f4 = new File(
155
                                "test-data/normalization/patSplitChain4.xml");
156
                assertTrue(f4.exists());
157
                assertNotNull(f4);
158

    
159
                List<String> r4 = parser(f4, c4);
160
                assertNotNull(r4);
161

    
162
                assertEquals("Av.", ((String) r4.get(0)).trim());
163
                assertEquals("Germanias", ((String) r4.get(1)).trim());
164
                assertEquals(15, Integer.parseInt(((String) r4.get(2)).trim()));
165
                assertEquals(2, Integer.parseInt(((String) r4.get(3)).trim()));
166
                assertEquals("Moncada", ((String) r4.get(4)).trim());
167
                assertEquals("Valencia", ((String) r4.get(5)).trim());
168

    
169
                log.debug("SubTest 5.");
170
                String c5 = "Av;,;Germanias;15;;2;Moncada;Valencia";
171

    
172
                File f5 = new File(
173
                                "test-data/normalization/patSplitChain5.xml");
174
                assertTrue(f5.exists());
175
                assertNotNull(f5);
176

    
177
                List<String> r5 = parser(f5, c5);
178
                assertNotNull(r5);
179

    
180
                assertEquals("Av", (String) r5.get(0));
181
                assertEquals("Germanias", (String) r5.get(1));
182
                assertEquals(15, Integer.parseInt((String) r5.get(2)));
183
                assertEquals(2, Integer.parseInt((String) r5.get(3)));
184
                assertEquals("Moncada", (String) r5.get(4));
185
                assertEquals("Valencia", (String) r5.get(5));
186

    
187
                log.debug("SubTest 6.");
188
                String c6 = "Av. Germanias 15-2 Moncada (Valencia)";
189

    
190
                File f6 = new File(
191
                                "test-data/normalization/patSplitChain6.xml");
192
                assertTrue(f6.exists());
193
                assertNotNull(f6);
194

    
195
                List<String> r6 = parser(f6, c6);
196
                assertNotNull(r6);
197

    
198
                assertEquals("Av.", ((String) r6.get(0)).trim());
199
                assertEquals("Germanias", ((String) r6.get(1)).trim());
200
                assertEquals(15, Integer.parseInt(((String) r6.get(2)).trim()));
201
                assertEquals(2, Integer.parseInt(((String) r6.get(3)).trim()));
202
                assertEquals("Moncada", ((String) r6.get(4)).trim());
203
                assertEquals("Valencia", ((String) r6.get(5)).trim());
204

    
205
                log.debug("SubTest 7.");
206
                String c7 = "Juana Aguirre;Piedras;No 623;Piso2;Dto.4;C1070AAM;Capital Federal;ARGENTINA";
207

    
208
                File f7 = new File(
209
                                "test-data/normalization/patSplitChain7.xml");
210
                assertTrue(f7.exists());
211
                assertNotNull(f7);
212

    
213
                List<String> r7 = parser(f7, c7);
214
                assertNotNull(r7);
215

    
216
                assertEquals("Juana Aguirre", ((String) r7.get(0)).trim());
217
                assertEquals("Piedras", ((String) r7.get(1)).trim());
218
                assertEquals("No 623", ((String) r7.get(2)).trim());
219
                assertEquals("Piso2", ((String) r7.get(3)).trim());
220
                assertEquals("Dto.4", ((String) r7.get(4)).trim());
221
                assertEquals("C1070AAM", ((String) r7.get(5)).trim());
222
                assertEquals("Capital Federal", ((String) r7.get(6)).trim());
223
                assertEquals("ARGENTINA", ((String) r7.get(7)).trim());
224

    
225
                log.debug("SubTest 8.");
226
                String c8 = "5.548\t5478.254\t0.24578457\t256.21450045";
227

    
228
                File f8 = new File(
229
                                "test-data/normalization/patSplitChain8.xml");
230
                assertTrue(f8.exists());
231
                assertNotNull(f8);
232

    
233
                List<String> r8 = parser(f8, c8);
234
                assertNotNull(r8);
235

    
236
                assertEquals(5.548d, Double.parseDouble(((String) r8.get(0)).trim()), 0);
237
                assertEquals(5478.254, Double.parseDouble(((String) r8.get(1)).trim()),
238
                                0);
239
                assertEquals(0.24578457, Double
240
                                .parseDouble(((String) r8.get(2)).trim()), 0);
241
                assertEquals(256.21450045, Double.parseDouble(((String) r8.get(3))
242
                                .trim()), 0);
243

    
244
                log.debug("TestNormAlgorithm. Test finished");
245
        }
246

    
247
        /**
248
         * 
249
         */
250
        public void testSplitFixed() {
251

    
252
                log.debug("TestNormAlgorithm. Test tokens fixed");
253

    
254
                String chain = "esto;/es;;; una_prueba;  de un/   split de una cadena_de texto";
255

    
256
                int parts = 4;
257
                boolean join = true;
258
                String[] separators = { " " };
259
                List<String> result = DefaultNormalizationAlgorithm
260
                                .splitChainBySeparators(chain, parts, separators, join);
261
                log.debug("Cadena inicial: " + chain);
262
                for (int i = 0; i < result.size(); i++) {
263
                        log.debug("Subcadena" + i + ": " + (String) result.get(i));
264
                }
265
                assertEquals("esto;/es;;;", result.get(0));
266
                assertEquals("una_prueba;", result.get(1));
267
                assertEquals("de", result.get(2));
268
                assertEquals("un/   split de una cadena_de texto", result.get(3));
269

    
270
                log.debug("TestNormAlgorithm. Test tokens fixed finished");
271

    
272
        }
273

    
274
        /**
275
         * 
276
         */
277
        public void testSplitSeparators() {
278

    
279
                log.debug("TestNormAlgorithm. Test tokens with separators");
280

    
281
                String chain = "esto;/es;;; una_prueba;  de un/   split de una cadena_de texto";
282

    
283
                int parts = 4;
284
                boolean join = true;
285
                String[] separators = { " " };
286
                List<String> result = DefaultNormalizationAlgorithm
287
                                .splitChainBySeparators(chain, parts, separators, join);
288
                System.out.println("Cadena inicial: " + chain);
289
                for (int i = 0; i < result.size(); i++) {
290
                        System.out.println("Subcadena" + i + ": " + (String) result.get(i));
291
                }
292
                assertEquals("esto;/es;;;", result.get(0));
293
                assertEquals("una_prueba;", result.get(1));
294
                assertEquals("de", result.get(2));
295
                assertEquals("un/   split de una cadena_de texto", result.get(3));
296

    
297
                log.debug("TestNormAlgorithm. Test tokens with separators finished");
298

    
299
        }
300

    
301
        /**
302
         * 
303
         * @param f
304
         * @param chain
305
         * @return
306
         * @throws PersistenceException
307
         * @throws IOException
308
         */
309
        private List<String> parser(File f, String chain)
310
                        throws PersistenceException, IOException {
311

    
312
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
313

    
314
                Patternnormalization pat = new DefaultPatternnormalization();
315
                FileReader reader = new FileReader(f);
316
                PersistentState state = manager.loadState(reader);
317
                pat.loadFromState(state);
318
                reader.close();
319

    
320
                NormalizationAlgorithm na = new DefaultNormalizationAlgorithm(pat);
321

    
322
                List<String> result = na.splitChain(chain);
323
                return result;
324

    
325
        }
326

    
327
}