Skip to content

Commit e8c317d

Browse files
committed
Initial version to create a standalone one-off test utility which can be used by the OSGeo-Live project to verify
a specified postgis-jdbc.jar functions against a specified PostgreSQL database with PostGIS extensions installed.
1 parent 5fc900d commit e8c317d

File tree

12 files changed

+449
-0
lines changed

12 files changed

+449
-0
lines changed

.idea/compiler.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<module>jdbc</module>
7272
<module>jdbc_jtsparser</module>
7373
<module>postgis-jdbc-java2d</module>
74+
<module>tools</module>
7475
</modules>
7576

7677
<scm>
@@ -96,6 +97,7 @@
9697

9798
<properties>
9899
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
100+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
99101
<java.min.version>1.7</java.min.version>
100102
<maven.min.version>3.3</maven.min.version>
101103
<maven.test.skip>false</maven.test.skip>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
11+
<excludeFolder url="file://$MODULE_DIR$/target" />
12+
</content>
13+
<orderEntry type="inheritedJdk" />
14+
<orderEntry type="sourceFolder" forTests="false" />
15+
<orderEntry type="module" module-name="postgis-jdbc" />
16+
<orderEntry type="library" name="Maven: postgresql:postgresql:9.1-901-1.jdbc4" level="project" />
17+
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.3" level="project" />
18+
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.3" level="project" />
19+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
20+
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.10" level="project" />
21+
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
22+
<orderEntry type="library" scope="TEST" name="Maven: org.beanshell:bsh:2.0b4" level="project" />
23+
</component>
24+
</module>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>tools</artifactId>
9+
<groupId>net.postgis</groupId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>osgeo-postgis-jdbc-test-util</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
17+
<name>osgeo-postgis-jdbc-test-util</name>
18+
<description>
19+
Simple smoke test util for verifying functionality postgis jdbc jar against a postgresql database with posgis
20+
extensions on OSGeo-Live.
21+
</description>
22+
23+
<properties>
24+
<maven.test.skip>true</maven.test.skip>
25+
<testTablePrefix>SMOKE_TEST</testTablePrefix>
26+
<jdbcUrl>jdbc:postgresql://localhost:5432/postgis1</jdbcUrl>
27+
<jdbcUsername>postgis1</jdbcUsername>
28+
<jdbcPassword>postgis1</jdbcPassword>
29+
</properties>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>net.postgis</groupId>
34+
<artifactId>postgis-jdbc</artifactId>
35+
<version>2.2.1-SNAPSHOT</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-shade-plugin</artifactId>
44+
<executions>
45+
<execution>
46+
<id>shade</id>
47+
<phase>package</phase>
48+
<goals>
49+
<goal>shade</goal>
50+
</goals>
51+
<configuration>
52+
<artifactSet>
53+
<excludes>
54+
<exclude>net.postgis:postgis-jdbc</exclude>
55+
</excludes>
56+
</artifactSet>
57+
<transformers>
58+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
59+
<mainClass>net.postgis.osgeo.util.Main</mainClass>
60+
</transformer>
61+
</transformers>
62+
</configuration>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-surefire-plugin</artifactId>
69+
<configuration>
70+
<skip>${maven.test.skip}</skip>
71+
<suiteXmlFiles>
72+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
73+
</suiteXmlFiles>
74+
<systemPropertyVariables>
75+
<testTablePrefix>${testTablePrefix}</testTablePrefix>
76+
<jdbcUrl>${jdbcUrl}</jdbcUrl>
77+
<jdbcUsername>${jdbcUsername}</jdbcUsername>
78+
<jdbcPassword>${jdbcPassword}</jdbcPassword>
79+
</systemPropertyVariables>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
</project>
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package net.postgis.osgeo.util;
2+
3+
import org.postgis.PGbox3d;
4+
import org.postgis.PGgeometry;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.sql.Connection;
9+
import java.sql.DatabaseMetaData;
10+
import java.sql.DriverManager;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Objects;
15+
import java.util.UUID;
16+
17+
18+
/**
19+
* Simple smoke test util for verifying functionality postgis jdbc jar against a postgresql database
20+
* with posgis extensions on OSGeo-Live.
21+
*
22+
* @author Phillip Ross
23+
*/
24+
public class Main {
25+
26+
private static final Logger logger = LoggerFactory.getLogger(Main.class);
27+
28+
private static final String DEFAULT_TEST_TABLE_PREFIX = "SMOKE_TEST";
29+
30+
private static final String JDBC_DRIVER_CLASS_NAME = "org.postgresql.Driver";
31+
32+
private Connection connection = null;
33+
34+
private Statement statement = null;
35+
36+
private String testTableName = null;
37+
38+
39+
public Main(final String jdbcUrl,
40+
final String jdbcUsername,
41+
final String jdbcPassword,
42+
final String testTablePrefix) {
43+
try {
44+
Objects.requireNonNull(jdbcUrl, "A JDBC URL must be specified");
45+
Objects.requireNonNull(jdbcUsername, "A database username must be specified.");
46+
Objects.requireNonNull(jdbcPassword, "A database password must be specified.");
47+
Objects.requireNonNull(testTablePrefix, "Unable to determine test table prefix");
48+
logger.debug("Running test (url/u/p/ttp): {}/{}/{}/{}", jdbcUrl, jdbcUsername, jdbcPassword, testTablePrefix);
49+
setupDatabaseResources(jdbcUrl, jdbcUsername, jdbcPassword);
50+
Objects.requireNonNull(connection, "Unable to continue testing without a connection to the database");
51+
if (testDatatypeRegistration()) {
52+
Objects.requireNonNull(statement, "Unable to continue testing without a statement resource");
53+
if (createTestTable(testTablePrefix)) {
54+
if (testSQL()) {
55+
logger.debug("All tests passed");
56+
}
57+
}
58+
}
59+
closeDatabaseResources();
60+
} catch (Exception e) {
61+
logger.error("Caught exception: {} {}", e.getClass().getName(), e.getMessage());
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
67+
private boolean testSQL() {
68+
final String insertPointSQL = "insert into " + testTableName + " values ('POINT (10 10 10)',1)";
69+
final String insertPolygonSQL = "insert into " + testTableName + " values ('POLYGON ((0 0 0,0 10 0,10 10 0,10 0 0,0 0 0))',2)";
70+
71+
boolean testPass = false;
72+
try {
73+
logger.debug("Inserting point...");
74+
statement.execute(insertPointSQL);
75+
76+
logger.debug("Inserting polygon...");
77+
statement.execute(insertPolygonSQL);
78+
79+
logger.debug("Querying table...");
80+
ResultSet resultSet = statement.executeQuery("select ST_AsText(geom),id from " + testTableName);
81+
while (resultSet.next()) {
82+
Object obj = resultSet.getObject(1);
83+
int id = resultSet.getInt(2);
84+
logger.debug("Row {}: {}", id, obj.toString());
85+
}
86+
testPass = true;
87+
} catch (SQLException se) {
88+
logger.error(
89+
"Caught SQLException attempting to issue SQL to the database: {} {}",
90+
se.getClass().getName(),
91+
se.getMessage()
92+
);
93+
}
94+
return testPass;
95+
}
96+
97+
98+
private boolean createTestTable(final String testTablePrefix) {
99+
testTableName = testTablePrefix + "_" + UUID.randomUUID().toString().replaceAll("-", "");
100+
final String dropSQL = "drop table " + testTableName;
101+
final String createSQL = "create table " + testTableName + " (geom geometry, id int4)";
102+
103+
boolean testPass = false;
104+
logger.debug("Creating table with geometric types...");
105+
boolean tableExists = false;
106+
try {
107+
DatabaseMetaData databaseMetaData = connection.getMetaData();
108+
109+
try (ResultSet resultSet = databaseMetaData.getTables(null, null, testTableName.toLowerCase(), new String[] {"TABLE"})) {
110+
while (resultSet.next()) {
111+
tableExists = true;
112+
}
113+
}
114+
if (tableExists) {
115+
statement.execute(dropSQL);
116+
}
117+
statement.execute(createSQL);
118+
testPass = true;
119+
} catch (SQLException se) {
120+
logger.error(
121+
"Caught SQLException attempting to create a test table: {} {}",
122+
se.getClass().getName(),
123+
se.getMessage()
124+
);
125+
}
126+
return testPass;
127+
}
128+
129+
130+
private boolean testDatatypeRegistration() {
131+
boolean testPass = false;
132+
logger.debug("Adding geometric type entries...");
133+
try {
134+
((org.postgresql.PGConnection)connection).addDataType("geometry", PGgeometry.class);
135+
((org.postgresql.PGConnection)connection).addDataType("box3d", PGbox3d.class);
136+
testPass = true;
137+
} catch (SQLException se) {
138+
logger.error(
139+
"Caught SQLException attempting to register datatypes with PostgreSQL driver: {} {}",
140+
se.getClass().getName(),
141+
se.getMessage()
142+
);
143+
}
144+
return testPass;
145+
}
146+
147+
148+
private void closeDatabaseResources() {
149+
try {
150+
if (statement != null) {
151+
statement.close();
152+
}
153+
} catch (SQLException se) {
154+
logger.error(
155+
"Caught SQLException attempting to close statement resource: {} {}",
156+
se.getClass().getName(),
157+
se.getMessage()
158+
);
159+
}
160+
try {
161+
if (connection != null) {
162+
connection.close();
163+
}
164+
} catch (SQLException se) {
165+
logger.error(
166+
"Caught SQLException attempting to close connection to the database: {} {}",
167+
se.getClass().getName(),
168+
se.getMessage()
169+
);
170+
}
171+
}
172+
173+
174+
private void setupDatabaseResources(final String url, final String username, final String password) {
175+
try {
176+
Class.forName(JDBC_DRIVER_CLASS_NAME);
177+
} catch (ClassNotFoundException e) {
178+
logger.error("Caught exception attempting to load jdbc driver class {}", JDBC_DRIVER_CLASS_NAME);
179+
logger.error("Check your classpath to verify that you've included the postgresql jdbc driver.");
180+
}
181+
try {
182+
connection = DriverManager.getConnection(url, username, password);
183+
statement = connection.createStatement();
184+
} catch (SQLException se) {
185+
logger.error(
186+
"Caught SQLException attempting to setup database resources: {} {}",
187+
se.getClass().getName(),
188+
se.getMessage()
189+
);
190+
}
191+
}
192+
193+
194+
public static void main(final String[] args) {
195+
if (args.length < 3) {
196+
System.out.println("parameters: jdbcUrl jdbcUsername jdbcPassword [testTablePrefix]");
197+
} else {
198+
String jdbcUrl = args[0];
199+
String jdbcUsername = args[1];
200+
String jdbcPassword = args[2];
201+
String testTablePrefix = DEFAULT_TEST_TABLE_PREFIX;
202+
if (args.length > 3) {
203+
testTablePrefix = args[3];
204+
}
205+
new Main(jdbcUrl, jdbcUsername, jdbcPassword, testTablePrefix);
206+
}
207+
}
208+
209+
210+
}
211+
212+
// java -classpath ~/.m2/repository/net/postgis/postgis-jdbc/2.2.1-SNAPSHOT/postgis-jdbc-2.2.1-SNAPSHOT.jar:target/osgeo-postgis-jdbc-test-util-0.0.1-SNAPSHOT.jar net.postgis.osgeo.util.Main jdbc:postgresql://db01:5432/postgis1 postgis1 postgis1 smoke_test

0 commit comments

Comments
 (0)