Statistics
| Revision:

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

History | View | Annotate | Download (9.49 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.FileInputStream;
32
import java.io.IOException;
33
import java.util.List;
34

    
35
import org.gvsig.normalization.algorithm.NormalizationAlgorithm;
36
import org.gvsig.normalization.algorithm.impl.DefaultNormalizationAlgorithm;
37
import org.gvsig.normalization.impl.DefaultNormalizationLibrary;
38
import org.gvsig.normalization.pattern.NormalizationPattern;
39
import org.gvsig.normalization.pattern.impl.DefaultNormalizationPattern;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.library.Library;
42
import org.gvsig.tools.persistence.PersistenceException;
43
import org.gvsig.tools.persistence.PersistenceManager;
44
import org.gvsig.tools.persistence.PersistentState;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
public class TestNormAlgorithm extends org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase {
49

    
50
        private static final Logger log = LoggerFactory
51
                        .getLogger(TestNormAlgorithm.class);
52

    
53
        @Override
54
        protected void doSetUp() throws Exception {
55
                Library normlib = new DefaultNormalizationLibrary();
56
                normlib.initialize();
57
                normlib.postInitialize();
58
        }
59

    
60
        /**
61
         * 
62
         * @throws Exception 
63
         */
64
        public void testSplitChain() throws Exception {
65

    
66
                log.debug("TestNormAlgorithm. Test splits strings");
67

    
68
                log.debug("SubTest 1");
69
                String c1 = ";aaa,,bbb;;;ccc/ddd@eee##;";
70

    
71
                File f1 = new File(
72
                                "resources-test/normalization/patSplitChain.xml");
73

    
74
                assertTrue(f1.exists());
75
                assertNotNull(f1);
76

    
77
                List<String> r1 = parser(f1, c1);
78
                assertNotNull(r1);
79

    
80
                assertEquals("", (String) r1.get(0));
81
                assertEquals("aaa", (String) r1.get(1));
82
                assertEquals("", (String) r1.get(2));
83
                assertEquals("bbb", (String) r1.get(3));
84
                assertEquals("", (String) r1.get(4));
85
                assertEquals("", (String) r1.get(5));
86
                assertEquals("ccc", (String) r1.get(6));
87
                assertEquals("ddd", (String) r1.get(7));
88
                assertEquals("eee", (String) r1.get(8));
89
                assertEquals("", (String) r1.get(9));
90
                assertEquals(";", (String) r1.get(10));
91

    
92
                log.debug("SubTest 2.");
93
                String c2 = "aaa bbb ccc ddd,76 %";
94

    
95
                File f2 = new File(
96
                                "resources-test/normalization/patSplitChain2.xml");
97
                assertTrue(f2.exists());
98
                assertNotNull(f2);
99

    
100
                List<String> r2 = parser(f2, c2);
101
                assertNotNull(r2);
102

    
103
                assertEquals("aaa", (String) r2.get(0));
104
                assertEquals("bbb", (String) r2.get(1));
105
                assertEquals("ccc", (String) r2.get(2));
106
                assertEquals("ddd", (String) r2.get(3));
107
                assertEquals("76", (String) r2.get(4));
108
                assertEquals("%", (String) r2.get(5));
109

    
110
                log.debug("SubTest 3.");
111
                String c3 = "Av;Germanias;25;3;Moncada;Valencia";
112

    
113
                File f3 = new File(
114
                                "resources-test/normalization/testdata/patSplitChain3.xml");
115
                assertTrue(f3.exists());
116
                assertNotNull(f3);
117

    
118
                List<String> r3 = parser(f3, c3);
119
                assertNotNull(r3);
120

    
121
                assertEquals("Av", (String) r3.get(0));
122
                assertEquals("Germanias", (String) r3.get(1));
123
                assertEquals(25, Integer.parseInt((String) r3.get(2)));
124
                assertEquals(3, Integer.parseInt((String) r3.get(3)));
125
                assertEquals("Moncada", (String) r3.get(4));
126
                assertEquals("Valencia", (String) r3.get(5));
127

    
128
                log.debug("SubTest 4.");
129
                String c4 = "Av. Germanias      15  2   Moncada   Valencia    ";
130

    
131
                File f4 = new File(
132
                                "resources-test/normalization/patSplitChain4.xml");
133
                assertTrue(f4.exists());
134
                assertNotNull(f4);
135

    
136
                List<String> r4 = parser(f4, c4);
137
                assertNotNull(r4);
138

    
139
                assertEquals("Av.", ((String) r4.get(0)).trim());
140
                assertEquals("Germanias", ((String) r4.get(1)).trim());
141
                assertEquals(15, Integer.parseInt(((String) r4.get(2)).trim()));
142
                assertEquals(2, Integer.parseInt(((String) r4.get(3)).trim()));
143
                assertEquals("Moncada", ((String) r4.get(4)).trim());
144
                assertEquals("Valencia", ((String) r4.get(5)).trim());
145

    
146
                log.debug("SubTest 5.");
147
                String c5 = "Av;,;Germanias;15;;2;Moncada;Valencia";
148

    
149
                File f5 = new File(
150
                                "resources-test/normalization/patSplitChain5.xml");
151
                assertTrue(f5.exists());
152
                assertNotNull(f5);
153

    
154
                List<String> r5 = parser(f5, c5);
155
                assertNotNull(r5);
156

    
157
                assertEquals("Av", (String) r5.get(0));
158
                assertEquals("Germanias", (String) r5.get(1));
159
                assertEquals(15, Integer.parseInt((String) r5.get(2)));
160
                assertEquals(2, Integer.parseInt((String) r5.get(3)));
161
                assertEquals("Moncada", (String) r5.get(4));
162
                assertEquals("Valencia", (String) r5.get(5));
163

    
164
                log.debug("SubTest 6.");
165
                String c6 = "Av. Germanias 15-2 Moncada (Valencia)";
166

    
167
                File f6 = new File(
168
                                "resources-test/normalization/patSplitChain6.xml");
169
                assertTrue(f6.exists());
170
                assertNotNull(f6);
171

    
172
                List<String> r6 = parser(f6, c6);
173
                assertNotNull(r6);
174

    
175
                assertEquals("Av.", ((String) r6.get(0)).trim());
176
                assertEquals("Germanias", ((String) r6.get(1)).trim());
177
                assertEquals(15, Integer.parseInt(((String) r6.get(2)).trim()));
178
                assertEquals(2, Integer.parseInt(((String) r6.get(3)).trim()));
179
                assertEquals("Moncada", ((String) r6.get(4)).trim());
180
                assertEquals("Valencia", ((String) r6.get(5)).trim());
181

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

    
185
                File f7 = new File(
186
                                "resources-test/normalization/patSplitChain7.xml");
187
                assertTrue(f7.exists());
188
                assertNotNull(f7);
189

    
190
                List<String> r7 = parser(f7, c7);
191
                assertNotNull(r7);
192

    
193
                assertEquals("Juana Aguirre", ((String) r7.get(0)).trim());
194
                assertEquals("Piedras", ((String) r7.get(1)).trim());
195
                assertEquals("No 623", ((String) r7.get(2)).trim());
196
                assertEquals("Piso2", ((String) r7.get(3)).trim());
197
                assertEquals("Dto.4", ((String) r7.get(4)).trim());
198
                assertEquals("C1070AAM", ((String) r7.get(5)).trim());
199
                assertEquals("Capital Federal", ((String) r7.get(6)).trim());
200
                assertEquals("ARGENTINA", ((String) r7.get(7)).trim());
201

    
202
                log.debug("SubTest 8.");
203
                String c8 = "5.548\t5478.254\t0.24578457\t256.21450045";
204

    
205
                File f8 = new File(
206
                                "resources-test/normalization/patSplitChain8.xml");
207
                assertTrue(f8.exists());
208
                assertNotNull(f8);
209

    
210
                List<String> r8 = parser(f8, c8);
211
                assertNotNull(r8);
212

    
213
                assertEquals(5.548d, Double.parseDouble(((String) r8.get(0)).trim()), 0);
214
                assertEquals(5478.254, Double.parseDouble(((String) r8.get(1)).trim()),
215
                                0);
216
                assertEquals(0.24578457, Double
217
                                .parseDouble(((String) r8.get(2)).trim()), 0);
218
                assertEquals(256.21450045, Double.parseDouble(((String) r8.get(3))
219
                                .trim()), 0);
220

    
221
                log.debug("TestNormAlgorithm. Test finished");
222
        }
223

    
224
        /**
225
         * 
226
         */
227
        public void testSplitFixed() {
228

    
229
                log.debug("TestNormAlgorithm. Test tokens fixed");
230

    
231
                String chain = "esto;/es;;; una_prueba;  de un/   split de una cadena_de texto";
232

    
233
                int parts = 4;
234
                boolean join = true;
235
                String[] separators = { " " };
236
                List<String> result = DefaultNormalizationAlgorithm
237
                                .splitChainBySeparators(chain, parts, separators, join);
238
                log.debug("Cadena inicial: " + chain);
239
                for (int i = 0; i < result.size(); i++) {
240
                        log.debug("Subcadena" + i + ": " + (String) result.get(i));
241
                }
242
                assertEquals("esto;/es;;;", result.get(0));
243
                assertEquals("una_prueba;", result.get(1));
244
                assertEquals("de", result.get(2));
245
                assertEquals("un/   split de una cadena_de texto", result.get(3));
246

    
247
                log.debug("TestNormAlgorithm. Test tokens fixed finished");
248

    
249
        }
250

    
251
        /**
252
         * 
253
         */
254
        public void testSplitSeparators() {
255

    
256
                log.debug("TestNormAlgorithm. Test tokens with separators");
257

    
258
                String chain = "esto;/es;;; una_prueba;  de un/   split de una cadena_de texto";
259

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

    
274
                log.debug("TestNormAlgorithm. Test tokens with separators finished");
275

    
276
        }
277

    
278
        /**
279
         * 
280
         * @param f
281
         * @param chain
282
         * @return
283
         * @throws PersistenceException
284
         * @throws IOException
285
         */
286
        private List<String> parser(File f, String chain)
287
                        throws Exception {
288

    
289
                
290

    
291
                NormalizationPattern pat = new DefaultNormalizationPattern();
292
                
293
                ((DefaultNormalizationPattern)pat).loadFromXML(f);
294

    
295
                NormalizationAlgorithm na = new DefaultNormalizationAlgorithm(pat);
296

    
297
                List<String> result = na.splitChain(chain);
298
                return result;
299

    
300
        }
301

    
302
}