Revision 19420 branches/v10/extensions/extPublish/src-test/org/gvsig/publish/util/LocalResourcesTestUtilities.java

View differences:

LocalResourcesTestUtilities.java
42 42
 * @author Jos? Vicente Hig?n (josevicente.higon@iver.es)
43 43
 */
44 44
package org.gvsig.publish.util;
45

  
46

  
47 45
import java.io.BufferedReader;
48 46
import java.io.File;
47
import java.io.FileNotFoundException;
48
import java.io.FileReader;
49 49
import java.io.IOException;
50 50
import java.io.InputStreamReader;
51
import java.net.URL;
51
import java.io.Reader;
52 52
import java.util.ArrayList;
53 53

  
54 54
import org.cresques.cts.IProjection;
55
import org.exolab.castor.xml.MarshalException;
56
import org.exolab.castor.xml.ValidationException;
55 57

  
56 58
import com.hardcode.gdbms.engine.values.Value;
57 59
import com.hardcode.gdbms.engine.values.ValueFactory;
......
66 68
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory;
67 69
import com.iver.cit.gvsig.fmap.drivers.DBException;
68 70
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69 72
import com.iver.cit.gvsig.fmap.drivers.IConnection;
70 73
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
71 74
import com.iver.cit.gvsig.fmap.layers.FLayer;
72 75
import com.iver.cit.gvsig.fmap.layers.FLayers;
73 76
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
77
import com.iver.cit.gvsig.fmap.layers.XMLException;
74 78
import com.iver.cit.gvsig.project.Project;
75 79
import com.iver.cit.gvsig.project.ProjectFactory;
80
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
76 81
import com.iver.cit.gvsig.project.documents.view.ProjectView;
77 82
import com.iver.cit.gvsig.project.documents.view.gui.View;
83
import com.iver.utiles.XMLEntity;
84
import com.iver.utiles.xmlEntity.generate.XmlTag;
78 85

  
79 86
/**
80 87
 * This class represents a factory which can make gvSIG objects like a project,
......
90 97
	/**
91 98
	 * Default constructor
92 99
	 * <p>
93
	 *  it gets the resources from directory "resources"
100
	 *  it gets the resources from directory "resources" which is at the same level as the project extPublish
94 101
	 *  <p>
95 102
	 *  it configure gvSIG drivers path to ../fwAndami/....
96 103
	 */
97 104
	public LocalResourcesTestUtilities()throws Exception{
98 105
		File f = null;		
99
		//gets absolute path
100
		URL url = this.getClass().getResource("../resources");
101
		if (url == null) 
102
			throw new Exception("Can't find resources directory");		
103
		f = new File(url.getFile());
106
		f = new File("../extPublish/resources");
104 107
		if (!f.exists()) 
105
			throw new Exception("Can't find resources dir");
106
		
108
			throw new Exception("Can't find resources dir");		
107 109
		this.basePath = f.getAbsolutePath();
110
		System.out.println("RESOURCES DIRECTORY:" + basePath);
108 111
		//Load the drivers
109
		LayerFactory.setDriversPath("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");
112
		LayerFactory.setDriversPath("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");			
113
	}
110 114
	
111
	
112
	
115
	/**
116
	 * 
117
	 * @return Project with a shape
118
	 */
119
	public Project getProject_shape(){
120
		Project gvProj = ProjectFactory.createProject();
121
		gvProj.setOwner("test_project_owner");
122
		gvProj.setName("test_project_name");
123
		ProjectView view = getView_shape();
124
		gvProj.addDocument(view);
125
		return gvProj;
113 126
	}
114 127
	/**
115
	 * Constructor: gets absolute path to resources directory 
116
	 * @throws Exception 
128
	 * 
129
	 * @return gvSIG view with 3 shapefiles, point, polygon and lines
117 130
	 */
118
	
119
	public LocalResourcesTestUtilities(String basePath){
120
			this.basePath = basePath;
121
			//Load the drivers
122
			LayerFactory.setDriversPath("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers");
123
			//LayerFactory.setDriversPath("/home/jvhigon/eclipse/workspace-v10/_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers/");
131
	public ProjectView getView_shape(){
132
		ProjectView view = createViewFromFile(this.basePath + "/xml/shapes.xml");
133
		return view;
124 134
	}
135
	/**
136
	 * 
137
	 * @return gvSIG view with raster files, jpeg,tiff, ecw.
138
	 */
139
	public ProjectView getView_raster(){
140
		ProjectView view = createViewFromFile(this.basePath + "/xml/raster.xml");
141
		return view;
142
	}
143
	/**
144
	 * 
145
	 * @return gvSIG view with a postgis table (polygons)
146
	 */
147
	public ProjectView getView_postgis(){
148
		ProjectView view = createViewFromFile(this.basePath + "/xml/postgis.xml");
149
		return view;
150
	}
151
	/**
152
	 * 
153
	 * @return gvSIG view with two point shapes
154
	 */
155
	public ProjectView getView_point(){
156
		ProjectView view = createViewFromFile(this.basePath + "/xml/point.xml");
157
		return view;
158
	}
159
	/**
160
	 * 
161
	 * @return gvSIG view with a line shapes
162
	 */
163
	public ProjectView getView_line(){
164
		ProjectView view = createViewFromFile(this.basePath + "/xml/line.xml");
165
		return view;
166
	}
167
	/**
168
	 * 
169
	 * @return gvSIG view with a polygon shape
170
	 */
171
	public ProjectView getView_polygon(){
172
		ProjectView view = createViewFromFile(this.basePath + "/xml/polygon.xml");
173
		return view;
174
	}
175
	/**
176
	 * 
177
	 * @return gvSIG view with a polygon shape labeled
178
	 */
179
	public ProjectView getView_label(){
180
		ProjectView view = createViewFromFile(this.basePath + "/xml/label.xml");
181
		return view;
182
	}
183
	/**
184
	 * 
185
	 * @return gvSIG view with a group of layers (shapes) and a raster
186
	 */
187
	public ProjectView getView_group(){
188
		ProjectView view = createViewFromFile(this.basePath + "/xml/group.xml");
189
		return view;
190
	}
191
	/**
192
	 * 
193
	 * @return gvSIG view with several shapes, each one with a different SRS
194
	 */
195
	public ProjectView getView_srs(){
196
		ProjectView view = createViewFromFile(this.basePath + "/xml/srs.xml");
197
		return view;
198
	}
199
	/**
200
	 * 
201
	 * @return gvSIG view with interval legend (shape of polygons)
202
	 */
203
	public ProjectView getView_intervals(){
204
		ProjectView view = createViewFromFile(this.basePath + "/xml/intervals.xml");
205
		return view;
206
	}
207
	/**
208
	 * 
209
	 * @return gvSIG view unique values legend (shape line)
210
	 */
211
	public ProjectView getView_unique_values(){
212
		ProjectView view = createViewFromFile(this.basePath + "/xml/unique_values.xml");
213
		return view;
214
	}
215
	/**
216
	 * Creates a ProjectView from a XML file
217
	 */
218
	public ProjectView createViewFromFile(String fileName){
219
		ProjectView gvView = new ProjectView();
220
		XMLEntity xmlEntity = createXMLEntityFromFile(fileName);
221
		System.err.println("Creating view from: " + fileName);
222
		try {
223
			gvView.setXMLEntity(xmlEntity);
224
		} catch (XMLException e) {
225
			e.printStackTrace();
226
		} catch (DriverException e) {
227
			e.printStackTrace();
228
		} catch (DriverIOException e) {
229
			e.printStackTrace();
230
		} catch (OpenException e) {
231
			e.printStackTrace();
232
		}
233
		return gvView;
234
	}
235
	/**
236
	 * Creates a XMLEntity from a file located in resources directory
237
	 * @param xml
238
	 * @return
239
	 */
240
	public XMLEntity createXMLEntityFromFile(String fileName){
241
		FileReader xml=null;
242
		try {
243
			//gvp = new FileReader(this.basePath + File.separator + fileName);
244
			xml = new FileReader(fileName);
245
		} catch (FileNotFoundException e1) {
246
			// TODO Auto-generated catch block
247
			e1.printStackTrace();
248
		}
249
		Reader reader = new BufferedReader(xml);
250
		XmlTag tag=null;
251
		try {
252
			tag = (XmlTag) XmlTag.unmarshal(reader);
253
		} catch (MarshalException e) {
254
			e.printStackTrace();
255
		} catch (ValidationException e) {
256
			e.printStackTrace();
257
		}
258
		XMLEntity xmlEntity=new XMLEntity(tag);	
259
		return xmlEntity;
260
	}
125 261
	
262
	
126 263
	/**
127 264
	 * Creates a project
265
	 * @deprecated
128 266
	 */
129 267
	public Project getProject(){
130 268
		Project gvProj = ProjectFactory.createProject();
......
132 270
		gvProj.setName("test_project_name");
133 271
		return gvProj;
134 272
	}
273
//	public Project getProject_gvp(){
274
//		FileReader gvp=null;
275
//		try {
276
//			gvp = new FileReader(this.basePath + File.separator +"project1.gvp");
277
//		} catch (FileNotFoundException e1) {
278
//			// TODO Auto-generated catch block
279
//			e1.printStackTrace();
280
//		}
281
//		Reader reader = new BufferedReader(gvp);
282
//		XmlTag tag=null;
283
//		try {
284
//			tag = (XmlTag) XmlTag.unmarshal(reader);
285
//		} catch (MarshalException e) {
286
//			// TODO Auto-generated catch block
287
//			e.printStackTrace();
288
//		} catch (ValidationException e) {
289
//			// TODO Auto-generated catch block
290
//			e.printStackTrace();
291
//		}
292
//		XMLEntity xml=new XMLEntity(tag);
293
//		Project p=null;
294
//		try {
295
//			p = Project.createFromXML(xml);
296
//		} catch (OpenException e) {
297
//			// TODO Auto-generated catch block
298
//			e.printStackTrace();
299
//		}
300
//		return p;
301
//	}
302
	
303
	
135 304
	/**
136 305
	 * Creates a basic view with one layer
306
	 * @deprecated
137 307
	 */
138 308
	public ProjectView getBasicView(){
139 309
		ProjectView gvView = new ProjectView();
......
149 319
	}
150 320
	/**
151 321
	 * Creates a basic view with two layers
322
	 * @deprecated
152 323
	 */
153 324
	public ProjectView getBasic2layersView(){
154 325
		ProjectView gvView = new ProjectView();
......
166 337
	/**
167 338
	 * Creates a View with a simple layer and a nested layers
168 339
	 * @return
340
	 * @deprecated
169 341
	 */
170 342
	public ProjectView getGroupView(){
171 343
		ProjectView view = this.getBasicView();
......
175 347
	}
176 348
	/**
177 349
	 * Creates a grouped layer
350
	 * @deprecated
178 351
	 */
179 352
	public FLayers getGroupLayer(MapContext mapCtxt){
180 353
		FLayers layers = null;
......
185 358
		return layers;
186 359
	}
187 360
	/**
188
	 * Creates a vectorial layer with two geometries 
361
	 * Creates a vectorial layer with two geometries
362
	 * @deprecated 
189 363
	 */
190 364
	private FLayer getLayerBasic() {
191 365
		// DRIVER DEFINITION (SHAPE TYPE AND FIELDS)
......
240 414
	}
241 415
	/**
242 416
	 * creates a shapefile
417
	 * @deprecated
243 418
	 * @return
244 419
	 */
245 420
	public FLayer getShapePoint(){
......
256 431
		}		
257 432
		return (FLayer)lyr;
258 433
	}
259
	
434
	/**
435
	 * @deprecated
436
	 * @return
437
	 */
260 438
	public FLayer getShapeLine(){		
261 439
		String name = "shp_line";
262 440
		String fileName = this.basePath + "/shp_line.shp";
......
273 451
	}
274 452
	/**
275 453
	 * creates a shape of polygons   
276
	 * @throws Exception 
454
	 * @throws Exception
455
	 * @Deprecated 
277 456
	 */
278 457
	public FLayer getShapePolygon(){		
279 458
		String name = "shp_polygon";
280
		String fileName = this.basePath + "/shp_polygon.shp";
459
		String fileName = this.basePath + "/shp/shp_polygon.shp";
281 460
		String driverName = "gvSIG shp driver";
282 461
		IProjection proj = CRSFactory.getCRS("EPSG:23030");
283 462
		FLayer lyr = null;
......
292 471
	}
293 472
	/**
294 473
	 * creates a test layer postGIS
474
	 * @deprecated
295 475
	 */
296 476
	public FLayer getLayerPostGIS(){
297 477
		try {
......
348 528
	}
349 529
	/**
350 530
	 * Creates a Raster Layer from a ECW
531
	 * @deprecated
351 532
	 */
352 533
	public FLayer getLayerECW(){
353 534
		//RasterDriver driv = new CmsRasterDriver();
354 535
		String name = "orto_ecw";
355
		String fileName = this.basePath + "/orto.ecw";
536
		String fileName = this.basePath + "/raster/orto_23030.ecw";
356 537
		String driverName = "gvSIG Image Driver";
357 538
		FLayer lyr=null;
358 539
		IProjection proj = CRSFactory.getCRS("EPSG:23030");
......
371 552
	 * Create DB PostGIS (publish)
372 553
	 * Preconditions:
373 554
	 * - exists commands createdb, psql
374
	 * - you don't need password to connect with postgres 
555
	 * - you don't need password to connect with the user postgres at localhost
556
	 * - exist a template postgis (with name template_postgis)
557
	 * TODO: pass all the arguments ant create a ProcessBuilder
558
	 *  
375 559
	 */
376
	public void createDBPostGIS(){
560
	public void createDBPostGIS (){
561
		//createDBPostGIS(String user, String password, String dbname, String host, String port)
377 562
		try {
378 563
			BufferedReader br = null;
379 564
			String line = null;
380 565
			String[] commands = null;
381 566
			Process child = null;
567
			 
382 568
		    // Creates the database
383
		    commands = new String[]{"createdb", "-U", "postgres", "publish"};		    
569
		   commands = new String[]{"createdb", "-U", "postgres", "-h","localhost", "-T", "template_postgis", "publish"};			    
384 570
		    child = Runtime.getRuntime().exec(commands);
385 571
		    //Show output
386 572
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
......
390 576
		    	line = br.readLine();
391 577
		    }
392 578
		    // adds plpgsql support
393
		    commands = new String[]{"createlang", "-U", "postgres", "plpgsql", "publish"};		    
394
		    child = Runtime.getRuntime().exec(commands);
395
		    //Show output
396
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
397
		    line = br.readLine();		    
398
		    while (line != null){		    	
399
		    	System.out.println(line);
400
		    	line = br.readLine();
401
		    }
402
		    
579
//		    commands = new String[]{"createlang", "-U", "postgres","-h","localhost", "plpgsql", "publish"};		    
580
//		    child = Runtime.getRuntime().exec(commands);
581
//		    //Show output
582
//		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
583
//		    line = br.readLine();		    
584
//		    while (line != null){		    	
585
//		    	System.out.println(line);
586
//		    	line = br.readLine();
587
//		    }
588
//		    
403 589
		    //load lwpostgis.sql
404
		    commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/lwpostgis.sql"};
405
		    child = Runtime.getRuntime().exec(commands);
590
//		    commands = new String[]{"psql", "-U", "postgres","-h","localhost", "-d", "publish", "-f", this.basePath + "/postgis/lwpostgis.sql"};
591
//		    child = Runtime.getRuntime().exec(commands);
406 592
		    //Show output
407
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
408
		    line = br.readLine();		    
409
		    while (line != null){		    	
410
		    	System.out.println(line);
411
		    	line = br.readLine();
412
		    }
593
//		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
594
//		    line = br.readLine();		    
595
//		    while (line != null){		    	
596
//		    	System.out.println(line);
597
//		    	line = br.readLine();
598
//		    }
413 599
		    //load spatial_ref_sys.sql
414
		    System.out.println("Loading spatial_ref_sys.sql");
415
		    commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/spatial_ref_sys.sql"};
416
		    child = Runtime.getRuntime().exec(commands);
600
//		    System.out.println("Loading spatial_ref_sys.sql");
601
//		    commands = new String[]{"psql", "-U", "postgres","-h","localhost", "-d", "publish", "-f", this.basePath + "/postgis/spatial_ref_sys.sql"};
602
//		    child = Runtime.getRuntime().exec(commands);
417 603
		  //Show output
418
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
419
		    line = br.readLine();		    
420
		    while (line != null){		    	
421
		    	System.out.println(line);
422
		    	line = br.readLine();
423
		    }
604
//		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
605
//		    line = br.readLine();		    
606
//		    while (line != null){		    	
607
//		    	System.out.println(line);
608
//		    	line = br.readLine();
609
//		    }
424 610
		    		    
425 611
		} catch (IOException e) {
426 612
			e.printStackTrace();
......
437 623
			String line = null;
438 624
			
439 625
		    // Execute a command with an argument that contains a space
440
		    String[] commands = new String[]{"dropdb", "-U", "postgres", "publish"};		    
626
		    String[] commands = new String[]{"dropdb", "-U", "postgres","-h","localhost", "publish"};		    
441 627
		    Process child = Runtime.getRuntime().exec(commands);	
442 628
		    //Show output
443 629
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
......
458 644
		try {
459 645
			BufferedReader br = null;
460 646
			String line = null;			
461
		    String[] commands = new String[]{"psql", "-U", "postgres", "-d", "publish", "-f", this.basePath + "/sql_polygon.sql"};		    
647
		    String[] commands = new String[]{"psql", "-U", "postgres","-h", "localhost", "-d", "publish", "-f", this.basePath + "/postgis/sql_polygon.sql"};		    
462 648
		    Process child = Runtime.getRuntime().exec(commands);	
463 649
		    //Show output
464 650
		    br = new BufferedReader(new InputStreamReader(child.getErrorStream()));
......
487 673
		return leyenda;
488 674
	}
489 675
	*/
676
	public FLayer getLayer_tiff(){
677
		ProjectView view = getView_raster();
678
		FLayers layers = view.getMapContext().getLayers();
679
		return layers.getLayer(1);
680
	}
490 681
	
491 682
}

Also available in: Unified diff