svn-gvsig-desktop / trunk / extensions / extJDBC / src-test / testPostGIS.java @ 11971
History | View | Annotate | Download (3.43 KB)
1 |
|
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.DriverManager; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
8 |
|
9 |
import junit.framework.TestCase; |
10 |
|
11 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
12 |
import com.hardcode.gdbms.engine.values.NumericValue; |
13 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory; |
14 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
15 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
16 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
17 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
18 |
|
19 |
public class testPostGIS extends TestCase { |
20 |
|
21 |
PostGisDriver driver = new PostGisDriver();
|
22 |
ResultSet rsGood = null; |
23 |
|
24 |
protected void setUp() throws Exception { |
25 |
super.setUp();
|
26 |
try {
|
27 |
String dburl = "jdbc:postgresql://localhost/latin1"; |
28 |
String dbuser = "postgres"; |
29 |
String dbpass = "aquilina"; |
30 |
|
31 |
// String dburl = "jdbc:postgresql://192.168.0.217/postgis";
|
32 |
// String dbuser = "gvsig";
|
33 |
// String dbpass = "";
|
34 |
|
35 |
// String dbtable = "carreteras_lin_5k_t10";
|
36 |
String dbtable = "provin"; // OJO CON LAS MAYUSCULAS!!!!!!! |
37 |
|
38 |
IConnection conn = null;
|
39 |
System.out.println("Creating JDBC connection..."); |
40 |
Class.forName("org.postgresql.Driver"); |
41 |
conn = ConnectionFactory.createConnection(dburl, dbuser, dbpass); |
42 |
|
43 |
((ConnectionJDBC)conn).getConnection().setAutoCommit(false);
|
44 |
|
45 |
DBLayerDefinition lyrDef = new DBLayerDefinition();
|
46 |
lyrDef.setName(dbtable); |
47 |
lyrDef.setTableName(dbtable); |
48 |
lyrDef.setWhereClause("");
|
49 |
String[] fields = {"nom_provin", "gid"}; |
50 |
lyrDef.setFieldNames(fields); |
51 |
lyrDef.setFieldGeometry("the_geom");
|
52 |
lyrDef.setFieldID("gid");
|
53 |
|
54 |
Statement st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
55 |
rsGood = st.executeQuery("SELECT NOM_PROVIN, GID FROM " + dbtable + " ORDER BY GID"); |
56 |
driver.setData(conn, lyrDef); |
57 |
} |
58 |
catch (Exception e){ |
59 |
e.printStackTrace(); |
60 |
} |
61 |
} |
62 |
|
63 |
protected void tearDown() throws Exception { |
64 |
super.tearDown();
|
65 |
driver.close(); |
66 |
} |
67 |
|
68 |
/*
|
69 |
* Test method for
|
70 |
* 'com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver.getFieldValue(long,
|
71 |
* int)'
|
72 |
*/
|
73 |
public void testGetFieldValue() { |
74 |
int pos1 = 1; |
75 |
int pos2 = 30; |
76 |
int pos3 = 15; |
77 |
|
78 |
try {
|
79 |
for (int i=0; i < 40; i++) |
80 |
{ |
81 |
rsGood.absolute(i+1);
|
82 |
System.out.println("GOOD:" + rsGood.getInt(2) + " " + rsGood.getString(1)); |
83 |
|
84 |
String aux = driver.getFieldValue(i, 0).toString(); |
85 |
int id = ((NumericValue) driver.getFieldValue(i, 1)).intValue(); |
86 |
System.out.println(id + " " + aux); |
87 |
} |
88 |
rsGood.absolute(pos1); |
89 |
String str2 = driver.getFieldValue(pos1-1, 0).toString(); |
90 |
String str1 = rsGood.getString(1); |
91 |
|
92 |
System.out.println(str1 + " - " + str2); |
93 |
|
94 |
assertEquals(str1, str2); |
95 |
|
96 |
|
97 |
rsGood.absolute(pos2); |
98 |
str2 = driver.getFieldValue(pos2-1, 0).toString(); |
99 |
str1 = rsGood.getString(1);
|
100 |
|
101 |
|
102 |
int id1 = rsGood.getInt(2); |
103 |
int id2 = ((NumericValue) driver.getFieldValue(pos2-1, 1)).intValue(); |
104 |
System.out.println(id1 + " " + str1 + " - " + id2 + " " + str2); |
105 |
assertEquals(id1, id2); |
106 |
|
107 |
|
108 |
rsGood.absolute(pos3); |
109 |
str2 = driver.getFieldValue(pos3-1, 0).toString(); |
110 |
str1 = rsGood.getString(1);
|
111 |
|
112 |
System.out.println(str1 + " - " + str2); |
113 |
|
114 |
assertEquals(str1, str2); |
115 |
|
116 |
} catch (SQLException e) { |
117 |
// TODO Auto-generated catch block
|
118 |
e.printStackTrace(); |
119 |
} catch (DriverException e) {
|
120 |
// TODO Auto-generated catch block
|
121 |
e.printStackTrace(); |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
} |