Revision 19915

View differences:

trunk/libraries/libDataSourceDBBaseDrivers/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/h2/SHP2H2FeaturesVisitor.java
1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2

  
3
import java.io.IOException;
4
import java.sql.Connection;
5
import java.sql.Date;
6
import java.sql.DriverManager;
7
import java.sql.PreparedStatement;
8
import java.sql.SQLException;
9
import java.sql.Statement;
10

  
11
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2StoreParameters;
12
import org.gvsig.data.vectorial.IFeature;
13
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
14
import org.gvsig.data.vectorial.IFeatureType;
15
import org.gvsig.data.vectorial.visitor.DefaultFeaturesVisitor;
16
import org.gvsig.exceptions.BaseException;
17

  
18
import com.iver.cit.gvsig.fmap.core.IGeometry;
19
import com.iver.cit.gvsig.fmap.drivers.WKBParser2;
20

  
21
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
22
 *
23
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
24
 *
25
 * This program is free software; you can redistribute it and/or
26
 * modify it under the terms of the GNU General Public License
27
 * as published by the Free Software Foundation; either version 2
28
 * of the License, or (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
38
 *
39
 * For more information, contact:
40
 *
41
 *  Generalitat Valenciana
42
 *   Conselleria d'Infraestructures i Transport
43
 *   Av. Blasco Ib??ez, 50
44
 *   46010 VALENCIA
45
 *   SPAIN
46
 *
47
 *      +34 963862235
48
 *   gvsig@gva.es
49
 *      www.gvsig.gva.es
50
 *
51
 *    or
52
 *
53
 *   IVER T.I. S.A
54
 *   Salamanca 50
55
 *   46005 Valencia
56
 *   Spain
57
 *
58
 *   +34 963163400
59
 *   dac@iver.es
60
 */
61
/* CVS MESSAGES:
62
 *
63
 * $Id$
64
 * $Log$
65
 *
66
 */
67
/**
68
 * This visitor is used only to print a feature by console.
69
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
70
 */
71
public class SHP2H2FeaturesVisitor extends DefaultFeaturesVisitor{
72
	private IFeatureType featureType;
73
	private H2StoreParameters h2Params;
74
	private String tableName;
75
	private Connection con;
76

  
77

  
78
	private static int idUnico=0;
79

  
80
	public SHP2H2FeaturesVisitor(IFeatureType featureType,H2StoreParameters h2Params) {
81
		super();
82
		this.featureType = featureType;
83
		this.h2Params=h2Params;
84
		this.tableName = h2Params.tableID();
85

  
86
		try {
87
			Class.forName("org.h2.Driver");
88
			this.con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw());
89
		} catch (ClassNotFoundException e) {
90
			// TODO Auto-generated catch block
91
			e.printStackTrace();
92
		} catch (SQLException e) {
93
			// TODO Auto-generated catch block
94
			e.printStackTrace();
95
		}
96

  
97
	}
98
	public void setTableName(String name){
99
		this.tableName=name;
100
	}
101

  
102
	/* (non-Javadoc)
103
	 * @see org.gvsig.data.vectorial.visitor.DefaultFeaturesVisitor#visit(org.gvsig.data.vectorial.IFeature)
104
	 */
105
	public void visit(IFeature feature) throws BaseException {
106
		WKBParser2 wkbParser = new WKBParser2();
107
		StringBuffer sb=new StringBuffer();
108
		sb.append("INSERT INTO ");
109
		sb.append(this.tableName);
110
		sb.append(" VALUES (");
111
		for (int i=0 ; i<featureType.size() ; i++){
112
			sb.append("?, ");
113
		}
114
		sb.append("?)");
115

  
116
		PreparedStatement pst=null;
117
		try{
118
			pst=con.prepareStatement(sb.toString());
119
			pst.setInt(1, idUnico++);
120

  
121

  
122
			for (int i = 0; i < featureType.size(); i++) {
123

  
124
				IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) featureType
125
				.get(i);
126
				String type = descriptor.getDataType();
127

  
128
				if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)) {
129
					pst.setBoolean(i+2, feature.getBoolean(i));
130
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)) {
131
					pst.setByte(i+2, feature.getByte(i));
132
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)) {
133
					pst.setDate(i+2, new Date(feature.getDate(i).getTime()));
134
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)) {
135
					pst.setDouble(i+2, feature.getDouble(i));
136
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)) {
137
					pst.setFloat(i+2, feature.getFloat(i));
138
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)) {
139
					pst.setInt(i+2, feature.getInt(i));
140
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)) {
141
					pst.setLong(i+2, feature.getLong(i));
142
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)) {
143
					pst.setString(i+2, feature.getString(i));
144
				} else if (type.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
145
					pst.setBytes( i+2,  ((IGeometry)feature.getGeometry(i)).toWKB());
146
				} else {
147
					System.out.print(" ---- " + "TYPE UNKNOWN");
148
				}
149
			}
150
			pst.execute();
151
			System.out.println("* Inserted Feature: "+idUnico);
152
		}
153
		catch(SQLException except){
154
			throw new RuntimeException(except);
155

  
156
		}catch(IOException except){
157
				throw new RuntimeException(except);
158

  
159
		}
160
	}
161

  
162
	public void createStructure() {
163
		System.out.println("******* CreateStructure ********");
164
		StringBuffer sb=new StringBuffer();
165
		sb.append("CREATE TABLE ");
166
//		String s=String.valueOf(System.currentTimeMillis());
167
//		String tableName="TablaPrueba"+s.substring(s.length()-5);
168
		sb.append(h2Params.tableID());
169
		sb.append(" (");
170
		sb.append("id int, ");
171
		for (int i=0 ; i<featureType.size() ; i++){
172

  
173
			IFeatureAttributeDescriptor descriptor=(IFeatureAttributeDescriptor)featureType.get(i);
174
			String type = descriptor.getDataType();
175

  
176
			if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
177
				sb.append(descriptor.getName());
178
				sb.append(" bit, ");
179
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
180
				sb.append(descriptor.getName());
181
				sb.append(" byte, ");
182
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
183
				sb.append(descriptor.getName());
184
				sb.append(" date, ");
185
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
186
				sb.append(descriptor.getName());
187
				sb.append(" double, ");
188
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
189
				sb.append(descriptor.getName());
190
				sb.append(" float, ");
191
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)){
192
				sb.append(descriptor.getName());
193
				sb.append(" int, ");
194
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
195
				sb.append(descriptor.getName());
196
				sb.append(" bigint, ");
197
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
198
				sb.append(descriptor.getName());
199
				sb.append(" varchar, ");
200
			}else if (type.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
201
				sb.append(descriptor.getName());
202
				sb.append(" other, ");
203
			}else {
204
				System.out.print(" ---- " + "TYPE UNKNOWN");
205
			}
206
		}
207
		String createTable=sb.toString();
208
		createTable=createTable.substring(0, createTable.length()-2);
209
		createTable+=")";
210

  
211
		try{
212
			Statement st=this.con.createStatement();
213
			System.out.println("Exec sql:\n"+createTable+"\n\n");
214
			st.execute(createTable);
215
			st.close();
216
		}
217
		catch(SQLException except){
218
			throw new RuntimeException(except);
219

  
220
		}
221
		System.out.println("******* CreateStructure: OK ********");
222
	}
223

  
224
	public void close(){
225
		try {
226
			this.con.close();
227
		} catch (SQLException e) {
228

  
229
		}
230
	}
231
}
trunk/libraries/libDataSourceDBBaseDrivers/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Test.java
1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2

  
3
import java.io.File;
4
import java.sql.Connection;
5
import java.sql.DriverManager;
6
import java.sql.SQLException;
7
import java.sql.Statement;
8
import java.util.Iterator;
9

  
10
import junit.framework.TestCase;
11

  
12
import org.gvsig.data.DataSourceManager;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCTest;
15
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2Store;
16
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2StoreParameters;
17
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.Register;
18
import org.gvsig.data.datastores.vectorial.file.shp.SHPStore;
19
import org.gvsig.data.datastores.vectorial.file.shp.SHPStoreParameters;
20
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP;
21
import org.gvsig.data.exception.CloseException;
22
import org.gvsig.data.exception.InitializeException;
23
import org.gvsig.data.exception.OpenException;
24
import org.gvsig.data.exception.ReadException;
25
import org.gvsig.data.exception.WriteException;
26
import org.gvsig.data.vectorial.Feature;
27
import org.gvsig.data.vectorial.IFeature;
28
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
29
import org.gvsig.data.vectorial.IFeatureCollection;
30
import org.gvsig.data.vectorial.IFeatureStore;
31
import org.gvsig.data.vectorial.IFeatureType;
32
import org.gvsig.data.vectorial.visitor.PrintlnFeaturesVisitor;
33
import org.gvsig.exceptions.BaseException;
34
import org.h2.tools.Server;
35

  
36

  
37
public class H2Test extends JDBCTest {
38

  
39
	private Server h2Server = null;
40
	private String h2Server_argsTCP[]= {"-baseDir",	"."};
41

  
42

  
43

  
44
	private File shpfile = new File(H2Test.class.getResource("data/prueba.shp").getFile());
45

  
46
	public static void main(String[] args) {
47
		junit.textui.TestRunner.run(H2Test.class);
48
	}
49

  
50
	protected void setUp() throws Exception {
51
		super.setUp();
52
		this.startH2Server();
53
	}
54

  
55
	public void startH2Server(){
56
		if (h2Server != null)
57
			throw new RuntimeException("H2 server ya arrancado");
58
		Server myh2Server = null;
59
		try{
60
			myh2Server = Server.createTcpServer(h2Server_argsTCP);
61
			myh2Server.start();
62

  
63
		} catch (Exception e){
64
			throw new RuntimeException(e);
65
		}
66
		h2Server = myh2Server;
67
		System.out.println("\n************************");
68
		System.out.println("* H2 SERVER STARTED!!!! *");
69
		System.out.println("************************\n");
70

  
71
	}
72

  
73
	public void stopH2Server(){
74
		if (h2Server == null)
75
			return;
76
		h2Server.stop();
77
		h2Server.shutdown();
78
		h2Server = null;
79
		System.out.println("\n************************");
80
		System.out.println("* H2 SERVER STOPED!!!! *");
81
		System.out.println("************************\n");
82
	}
83

  
84

  
85
	private void deleteTable(H2StoreParameters h2Params) {
86
		try {
87
			Class.forName("org.h2.Driver");
88
			Connection con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw());
89
			Statement st = con.createStatement();
90
			st.execute("Delete "+ h2Params.tableID());
91
			st.close();
92
			con.close();
93
			System.out.println("* Table " + h2Params.tableID() + " DELETED!!");
94
		} catch (ClassNotFoundException e) {
95
			// TODO Auto-generated catch block
96
			e.printStackTrace();
97
		} catch (SQLException e) {
98
			// TODO Auto-generated catch block
99
			e.printStackTrace();
100
		}
101

  
102

  
103
	}
104

  
105
	public void shp2H2(File shpfile,H2StoreParameters h2Param) {
106
		System.out.println("======= SHP2H2 ==============");
107
		org.gvsig.data.datastores.vectorial.file.shp.Register.selfRegister();
108
		org.gvsig.data.datastores.vectorial.file.dbf.Register.selfRegister();
109

  
110
		DataSourceManager dsm=DataSourceManager.getManager();
111
//		File shpfile= new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile());
112

  
113
//		IDriverParameters dp=dsm.createDriverParameters("shp");
114

  
115

  
116
		IDataStoreParameters dsp=dsm.createDataStoreParameters(SHPStore.DATASTORE_NAME);
117
		((SHPStoreParameters)dsp).setSHPFile(shpfile);
118
		((SHPStoreParameters)dsp).setSHXFile(SHP.getShxFile(shpfile));
119
		((SHPStoreParameters)dsp).setDBFFile(SHP.getDbfFile(shpfile));
120
		IFeatureStore fs=null;
121
		try {
122
			fs = (IFeatureStore)dsm.createDataStore(dsp);
123
		} catch (InitializeException e) {
124
			e.printStackTrace();
125
			fail("Exception:" + e);
126
		}
127

  
128
		try {
129
			fs.open();
130
		} catch (OpenException e2) {
131
			e2.printStackTrace();
132
			fail();
133
		}
134

  
135
		IFeatureType ft= fs.getDefaultFeatureType();
136
		IFeatureCollection featureCollection=null;
137
		try {
138
			featureCollection = (IFeatureCollection)fs.getDataCollection();
139
		} catch (ReadException e2) {
140
			// TODO Auto-generated catch block
141
			e2.printStackTrace();
142
		}
143

  
144
		SHP2H2FeaturesVisitor visitor=new SHP2H2FeaturesVisitor(ft,h2Param);
145
		visitor.createStructure();
146

  
147
		try {
148
			featureCollection.accept(visitor);
149
		} catch (BaseException e1) {
150
			e1.printStackTrace();
151
			System.out.println(e1.getMessageStack());
152
			fail();
153
		}
154

  
155
		try {
156
			visitor.close();
157
			fs.close();
158
		} catch (CloseException e) {
159
			e.printStackTrace();
160
			fail("Exception: "+e);
161
		}
162
		try {
163
			fs.dispose();
164
		} catch (CloseException e) {
165
			// TODO Auto-generated catch block
166
			e.printStackTrace();
167
		}
168

  
169
	}
170

  
171

  
172
	public void testH2_sqlMode() {
173
		System.out.println("======= H2 (sql Mode)==============");
174
		Register.selfRegister();
175

  
176
		DataSourceManager dsm=DataSourceManager.getManager();
177

  
178

  
179
		H2StoreParameters dp=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME);
180

  
181

  
182
		dp.setHost("localhost");
183
		dp.setUser("SA");
184
		dp.setPassw("");
185
		dp.setFields(new String[] {"*"});
186
		dp.setGeometryField("GEOMETRY");
187
		String s=String.valueOf(System.currentTimeMillis());
188
		String tableName="TablaPrueba"+s.substring(s.length()-8);
189
		dp.setTableName(tableName.toUpperCase());
190
		dp.setFieldId(new String[] {"ID"});
191

  
192
		try{
193
			this.shp2H2(shpfile, dp);
194
		} catch (Exception e) {
195
			if (e.getCause() instanceof BaseException){
196
				System.out.println(((BaseException)e).getLocalizedMessageStack());
197
			} else{
198
				e.printStackTrace();
199
			}
200
			fail();
201
		}
202

  
203
		H2StoreParameters dp2=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME);
204

  
205
		dp2.setHost("localhost");
206
		dp2.setUser("SA");
207
		dp2.setPassw("");
208
		dp2.setGeometryField("GEOMETRY");
209
		dp2.setFieldId(new String[] {"ID"});
210
		dp2.setSqlSoure("Select * from " + dp.tableID() );
211

  
212
		storeTest((IDataStoreParameters)dp,null,null,false);
213

  
214
		Exception exc = null;
215

  
216
		IFeatureStore fs=null;
217
		try {
218
			fs = (IFeatureStore)dsm.createDataStore(dp2);
219
		} catch (InitializeException e) {
220
			e.printStackTrace();
221
			fail("Exception:" + e);
222
		}
223
		assertNotNull("Can't create Feature Store", fs);
224

  
225
		try {
226
			fs.open();
227
		} catch (OpenException e2) {
228
			e2.printStackTrace();
229
			fail();
230
		}
231

  
232

  
233
		Iterator it;
234
		IFeatureCollection fc =null;
235

  
236
		try {
237
			fc = (IFeatureCollection)fs.getDataCollection();
238
		} catch (ReadException e1) {
239
			e1.printStackTrace();
240
			fail();
241
		}
242

  
243
		assertEquals(9, fc.size());
244

  
245
		assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
246

  
247
		try {
248
			fs.startEditing();
249
		} catch (ReadException e1) {
250
			exc=e1;
251
		}
252
		assertNotNull("Edition allowed in sqlSource mode",exc);
253

  
254
		exc=null;
255
		try {
256
			fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like 'B%'",null);
257
		} catch (ReadException e1) {
258
			exc=e1;
259
		}
260
		assertNotNull("Filter allowed in sqlSource mode",exc);
261

  
262
		exc=null;
263
		try {
264
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE");
265
		} catch (ReadException e1) {
266
			exc=e1;
267
		}
268
		assertNotNull("Order allowed in sqlSource mode",exc);
269

  
270

  
271
		try {
272
			fs.close();
273
		} catch (CloseException e) {
274
			e.printStackTrace();
275
			fail("Exception:" + e);
276
		}
277

  
278

  
279
		deleteTable(dp);
280

  
281
		System.out.println("======= /H2 (sql Mode) ==============");
282
	}
283

  
284
	public void testH2() {
285
		System.out.println("======= H2 ==============");
286
		Register.selfRegister();
287

  
288
		DataSourceManager dsm=DataSourceManager.getManager();
289

  
290

  
291
		H2StoreParameters dp=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME);
292

  
293

  
294
		dp.setHost("localhost");
295
		dp.setUser("SA");
296
		dp.setPassw("");
297
		dp.setFields(new String[] {"*"});
298
		dp.setGeometryField("GEOMETRY");
299
		String s=String.valueOf(System.currentTimeMillis());
300
		String tableName="TablaPrueba"+s.substring(s.length()-8);
301
		dp.setTableName(tableName.toUpperCase());
302
		dp.setFieldId(new String[] {"ID"});
303

  
304
		try{
305
			this.shp2H2(shpfile, dp);
306
		} catch (Exception e) {
307
			if (e.getCause() instanceof BaseException){
308
				System.out.println(((BaseException)e).getLocalizedMessageStack());
309
			} else{
310
				e.printStackTrace();
311
			}
312
			fail();
313
		}
314

  
315

  
316

  
317

  
318
		storeTest((IDataStoreParameters)dp,null,null,true);
319

  
320
		IFeatureStore fs=null;
321
		try {
322
			fs = (IFeatureStore)dsm.createDataStore(dp);
323
		} catch (InitializeException e) {
324
			e.printStackTrace();
325
			fail("Exception:" + e);
326
		}
327
		assertNotNull("Can't create Feature Store", fs);
328

  
329
		try {
330
			fs.open();
331
		} catch (OpenException e2) {
332
			e2.printStackTrace();
333
			fail();
334
		}
335

  
336
		Iterator it;
337
		IFeatureCollection fc =null;
338
		Comparable v1,v2;
339
		IFeature feature,pfeature;
340
		long count;
341

  
342

  
343
		try {
344
			fc = (IFeatureCollection)fs.getDataCollection();
345
		} catch (ReadException e1) {
346
			e1.printStackTrace();
347
			fail();
348
		}
349

  
350
		assertEquals(9, fc.size());
351

  
352
		try {
353
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null);
354
		} catch (ReadException e1) {
355
			e1.printStackTrace();
356
			fail();
357
		}
358

  
359
		assertEquals(2, fc.size());
360

  
361
		it = fc.iterator();
362
		count=0;
363
		while (it.hasNext()){
364
			feature = (IFeature)it.next();
365
			assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
366
			count++;
367
		}
368
		assertEquals("Iteration error",2,count);
369

  
370

  
371
		try {
372
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC");
373
		} catch (ReadException e1) {
374
			e1.printStackTrace();
375
			fail();
376
		}
377
		assertEquals(9, fc.size());
378
		it = fc.iterator();
379
		count=0;
380
		pfeature = (IFeature)it.next();
381
		count++;
382
		while (it.hasNext()){
383
			feature = (IFeature)it.next();
384
			v1 = (Comparable)pfeature.get("NOMBRE");
385
			v2 = (Comparable)feature.get("NOMBRE");
386
			pfeature=feature;
387
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
388
			count++;
389
		}
390
		assertEquals("Iteration error",9,count);
391

  
392

  
393
		try {
394
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC");
395
		} catch (ReadException e1) {
396
			e1.printStackTrace();
397
			fail();
398
		}
399
		assertEquals(9, fc.size());
400
		it = fc.iterator();
401

  
402
		count=0;
403
		pfeature = (IFeature)it.next();
404
		count++;
405
		while (it.hasNext()){
406
			feature = (IFeature)it.next();
407
			v1 = (Comparable)pfeature.get("NOMBRE");
408
			v2 = (Comparable)feature.get("NOMBRE");
409
			pfeature=feature;
410
			assertTrue("Short error", (v1.compareTo(v1) >= 0));
411
			count++;
412
		}
413
		assertEquals("Iteration error",9,count);
414

  
415

  
416
		try {
417
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE");
418
		} catch (ReadException e1) {
419
			e1.printStackTrace();
420
			fail();
421
		}
422

  
423
		assertEquals(2, fc.size());
424

  
425
		it = fc.iterator();
426

  
427
		count=0;
428
		pfeature = (IFeature)it.next();
429
		assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b"));
430
		count++;
431
		while (it.hasNext()){
432
			feature = (IFeature)it.next();
433
			assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
434
			v1 = (Comparable)pfeature.get("NOMBRE");
435
			v2 = (Comparable)feature.get("NOMBRE");
436
			pfeature=feature;
437
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
438
			count++;
439
		}
440
		assertEquals("Iteration error",2,count);
441

  
442

  
443

  
444
		try {
445
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc");
446
		} catch (ReadException e1) {
447
			e1.printStackTrace();
448
			fail();
449
		}
450
		assertEquals(9, fc.size());
451
		it = fc.iterator();
452
		count=0;
453
		pfeature = (IFeature)it.next();
454
		System.out.println(pfeature.getString("NOMBRE"));
455
		count++;
456
		while (it.hasNext()){
457
			feature = (IFeature)it.next();
458
			v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase();
459
			v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase();
460
			pfeature=feature;
461
			assertTrue("Short error", (v1.compareTo(v2) >= 0));
462
			System.out.println(pfeature.getString("NOMBRE"));
463
			count++;
464
		}
465
		assertEquals("Iteration error",9,count);
466

  
467

  
468

  
469
		/// CON  EDICION
470
		try {
471
			fs.startEditing();
472
		} catch (ReadException e1) {
473
			e1.printStackTrace();
474
			fail();
475
		}
476

  
477

  
478
		IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType());
479
		newFeature.set("NOMBRE","BuRjaSOT");
480
		newFeature.set("TIPO","MUNICIPIO");
481
		fs.insert(newFeature);
482

  
483

  
484
		try {
485
			fc = (IFeatureCollection)fs.getDataCollection();
486
		} catch (ReadException e1) {
487
			e1.printStackTrace();
488
			fail();
489
		}
490

  
491
		assertEquals(10, fc.size());
492

  
493
		try {
494
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null);
495
		} catch (ReadException e1) {
496
			e1.printStackTrace();
497
			fail();
498
		}
499

  
500
		assertEquals(3, fc.size());
501

  
502
		it = fc.iterator();
503
		count=0;
504
		while (it.hasNext()){
505
			feature = (IFeature)it.next();
506
			assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
507
			count++;
508
		}
509
		assertEquals("Iteration error",3,count);
510

  
511

  
512
		try {
513
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC");
514
		} catch (ReadException e1) {
515
			e1.printStackTrace();
516
			fail();
517
		}
518
		assertEquals(10, fc.size());
519
		it = fc.iterator();
520
		count=0;
521
		pfeature = (IFeature)it.next();
522
		count++;
523
		while (it.hasNext()){
524
			feature = (IFeature)it.next();
525
			v1 = (Comparable)pfeature.get("NOMBRE");
526
			v2 = (Comparable)feature.get("NOMBRE");
527
			pfeature=feature;
528
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
529
			count++;
530
		}
531
		assertEquals("Iteration error",10,count);
532

  
533

  
534
		try {
535
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC");
536
		} catch (ReadException e1) {
537
			e1.printStackTrace();
538
			fail();
539
		}
540
		assertEquals(10, fc.size());
541
		it = fc.iterator();
542

  
543
		count=0;
544
		pfeature = (IFeature)it.next();
545
		count++;
546
		while (it.hasNext()){
547
			feature = (IFeature)it.next();
548
			v1 = (Comparable)pfeature.get("NOMBRE");
549
			v2 = (Comparable)feature.get("NOMBRE");
550
			pfeature=feature;
551
			assertTrue("Short error", (v1.compareTo(v1) >= 0));
552
			count++;
553
		}
554
		assertEquals("Iteration error",10,count);
555

  
556

  
557
		try {
558
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE");
559
		} catch (ReadException e1) {
560
			e1.printStackTrace();
561
			fail();
562
		}
563

  
564
		assertEquals(3, fc.size());
565

  
566
		it = fc.iterator();
567

  
568
		count=0;
569
		pfeature = (IFeature)it.next();
570
		assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b"));
571
		count++;
572
		while (it.hasNext()){
573
			feature = (IFeature)it.next();
574
			assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
575
			v1 = (Comparable)pfeature.get("NOMBRE");
576
			v2 = (Comparable)feature.get("NOMBRE");
577
			pfeature=feature;
578
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
579
			count++;
580
		}
581
		assertEquals("Iteration error",3,count);
582

  
583

  
584

  
585
		try {
586
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc");
587
		} catch (ReadException e1) {
588
			e1.printStackTrace();
589
			fail();
590
		}
591
		assertEquals(10, fc.size());
592
		it = fc.iterator();
593
		count=0;
594
		pfeature = (IFeature)it.next();
595
		System.out.println(pfeature.getString("NOMBRE"));
596
		count++;
597
		while (it.hasNext()){
598
			feature = (IFeature)it.next();
599
			v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase();
600
			v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase();
601
			pfeature=feature;
602
			assertTrue("Short error", (v1.compareTo(v2) >= 0));
603
			System.out.println(pfeature.getString("NOMBRE"));
604
			count++;
605
		}
606
		assertEquals("Iteration error",10,count);
607

  
608

  
609
		fs.cancelEditing();
610

  
611

  
612
		//Insertar un elemento
613
		try{
614
			fs.startEditing();
615
		} catch (ReadException e1) {
616
			e1.printStackTrace();
617
			fail();
618
		}
619

  
620
		feature = fs.createDefaultFeature(true);
621
		feature.set("ID", 90000);
622
		feature.set("NOMBRE","BurJASOT");
623
		feature.set("TIPO", "OTRO");
624
		fs.insert(feature);
625

  
626
		try {
627
			fs.finishEditing();
628
		} catch (WriteException e) {
629
			e.printStackTrace();
630
			fail("Exception: "+e);
631
		} catch (ReadException e) {
632
			e.printStackTrace();
633
			fail("Exception: "+e);
634
		}
635

  
636

  
637
		try {
638
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(NOMBRE) Desc");
639
		} catch (ReadException e1) {
640
			e1.printStackTrace();
641
			fail();
642
		}
643
		assertEquals(10, fc.size());
644
		it = fc.iterator();
645
		count=0;
646
		pfeature = (IFeature)it.next();
647
		System.out.println(pfeature.getString("NOMBRE"));
648
		count++;
649
		while (it.hasNext()){
650
			feature = (IFeature)it.next();
651
			v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase();
652
			v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase();
653
			pfeature=feature;
654
			assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0));
655
			System.out.println(pfeature.getString("NOMBRE"));
656
			count++;
657
		}
658
		assertEquals("Iteration error",10,count);
659

  
660

  
661

  
662
		//Actualizacion
663
		try{
664
			fs.startEditing();
665
		} catch (ReadException e1) {
666
			e1.printStackTrace();
667
			fail();
668
		}
669

  
670

  
671
		try {
672
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) = 'burjasot'",null);
673
		} catch (ReadException e1) {
674
			e1.printStackTrace();
675
			fail();
676
		}
677
		assertEquals(1, fc.size());
678
		Feature old =(Feature)fc.iterator().next();
679
		feature = fs.createDefaultFeature(false);
680
		it = fs.getDefaultFeatureType().iterator();
681
		IFeatureAttributeDescriptor attr;
682
		while(it.hasNext()){
683
			 attr = (IFeatureAttributeDescriptor)it.next();
684
//			 if (attr.getDataType() == IFeatureAttributeDescriptor.TYPE_GEOMETRY){
685
//				 feature.set(attr.getName(), old.get(attr.getName()));
686
//			 } else {
687
				 feature.set(attr.getName(), old.get(attr.getName()));
688
//			 }
689
		}
690
		feature.set("NOMBRE", feature.getString("NOMBRE")+"__KK__");
691

  
692
		fs.update(feature, old);
693
		try {
694
			fs.finishEditing();
695
		} catch (WriteException e) {
696
			e.printStackTrace();
697
			fail("Exception: "+e);
698
		} catch (ReadException e) {
699
			e.printStackTrace();
700
			fail("Exception: "+e);
701
		}
702

  
703
		try {
704
			fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like '%__KK__'",null);
705
		} catch (ReadException e1) {
706
			e1.printStackTrace();
707
			fail();
708
		}
709
		assertEquals(1, fc.size());
710

  
711

  
712
		//Eliminacion
713
		try{
714
			fs.startEditing();
715
		} catch (ReadException e1) {
716
			e1.printStackTrace();
717
			fail();
718
		}
719

  
720

  
721
		try {
722
			fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like '%__KK__'",null);
723
		} catch (ReadException e1) {
724
			e1.printStackTrace();
725
			fail();
726
		}
727
		assertEquals(1, fc.size());
728
		fs.delete((IFeature)fc.iterator().next());
729
		try {
730
			fs.finishEditing();
731
		} catch (WriteException e) {
732
			e.printStackTrace();
733
			fail("Exception: "+e);
734
		} catch (ReadException e) {
735
			e.printStackTrace();
736
			fail("Exception: "+e);
737
		}
738

  
739
		try {
740
			fc = (IFeatureCollection)fs.getDataCollection();
741
		} catch (ReadException e1) {
742
			e1.printStackTrace();
743
			fail();
744
		}
745
		assertEquals(9, fc.size());
746

  
747

  
748

  
749

  
750
		try {
751
			fs.close();
752
		} catch (CloseException e) {
753
			e.printStackTrace();
754
			fail("Exception:" + e);
755
		}
756

  
757

  
758
		deleteTable(dp);
759

  
760
		System.out.println("======= /H2 ==============");
761

  
762
	}
763

  
764

  
765
	protected void tearDown() throws Exception {
766
		// TODO Auto-generated method stub
767
		super.tearDown();
768
		this.stopH2Server();
769
	}
770
}
0 771

  

Also available in: Unified diff