|
| 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