📄 testparser.java
字号:
System.out.println("PostGIS server too old, skipping test on connection " + i + ": " + connection.getCatalog()); } else { System.out.println("Testing on connection " + i + ": " + connection.getCatalog()); try { Geometry sqlGeom = viaSQL(WKT, statement); System.out.println("SQLin : " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after SQL are not equal!"); if (flags == EQUAL10 && serverPostgisMajor < 1) { System.out.println("--- This is expected with PostGIS " + serverPostgisMajor + ".X"); } else { failcount++; } } else { System.out.println("Eq SQL in: yes"); } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } try { Geometry sqlreGeom = viaSQL(parsed, statement); System.out.println("SQLout : " + sqlreGeom.toString()); if (!geom.equals(sqlreGeom)) { System.out.println("--- reparsed Geometries after SQL are not equal!"); if (flags == EQUAL10 && serverPostgisMajor < 1) { System.out.println("--- This is expected with PostGIS " + serverPostgisMajor + ".X"); } else { failcount++; } } else { System.out.println("Eq SQLout: yes"); } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } try { Geometry sqlreGeom = viaPrepSQL(geom, connection); System.out.println("Prepared: " + sqlreGeom.toString()); if (!geom.equals(sqlreGeom)) { System.out.println("--- reparsed Geometries after prepared StatementSQL are not equal!"); if (flags == EQUAL10 && serverPostgisMajor < 1) { System.out.println("--- This is expected with PostGIS " + serverPostgisMajor + ".X"); } else { failcount++; } } else { System.out.println("Eq Prep: yes"); } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } // asEWKT() function is not present on PostGIS 0.X, and the test // is pointless as 0.X uses EWKT as canonical rep so the same // functionality was already tested above. try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = ewktViaSQL(WKT, statement); System.out.println("asEWKT : " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKT SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } // asEWKB() function is not present on PostGIS 0.X. try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = ewkbViaSQL(WKT, statement); System.out.println("asEWKB : " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKB SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } // HexEWKB parsing is not present on PostGIS 0.X. try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = viaSQL(hexNWKT, statement); System.out.println("hexNWKT: " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKB SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = viaSQL(hexXWKT, statement); System.out.println("hexXWKT: " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKB SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } // Canonical binary input is not present before 1.0 try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = binaryViaSQL(NWKT, connection); System.out.println("NWKT: " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKB SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } try { if (serverPostgisMajor >= 1) { Geometry sqlGeom = binaryViaSQL(XWKT, connection); System.out.println("XWKT: " + sqlGeom.toString()); if (!geom.equals(sqlGeom)) { System.out.println("--- Geometries after EWKB SQL are not equal!"); failcount++; } else { System.out.println("equal : yes"); } } } catch (SQLException e) { System.out.println("--- Server side error: " + e.toString()); failcount++; } } statement.close(); } System.out.println("***"); } /** Pass a geometry representation through the SQL server */ private static Geometry viaSQL(String rep, Statement stat) throws SQLException { ResultSet rs = stat.executeQuery("SELECT geometry_in('" + rep + "')"); rs.next(); return ((PGgeometry) rs.getObject(1)).getGeometry(); } /** * Pass a geometry representation through the SQL server via prepared * statement */ private static Geometry viaPrepSQL(Geometry geom, Connection conn) throws SQLException { PreparedStatement prep = conn.prepareStatement("SELECT ?::geometry"); PGgeometry wrapper = new PGgeometry(geom); prep.setObject(1, wrapper, Types.OTHER); ResultSet rs = prep.executeQuery(); rs.next(); PGgeometry resultwrapper = ((PGgeometry) rs.getObject(1)); return resultwrapper.getGeometry(); } /** Pass a geometry representation through the SQL server via EWKT */ private static Geometry ewktViaSQL(String rep, Statement stat) throws SQLException { ResultSet rs = stat.executeQuery("SELECT asEWKT(geometry_in('" + rep + "'))"); rs.next(); String resrep = rs.getString(1); return PGgeometry.geomFromString(resrep); } /** Pass a geometry representation through the SQL server via EWKB */ private static Geometry ewkbViaSQL(String rep, Statement stat) throws SQLException { ResultSet rs = stat.executeQuery("SELECT asEWKB(geometry_in('" + rep + "'))"); rs.next(); byte[] resrep = rs.getBytes(1); return bp.parse(resrep); } /** Pass a EWKB geometry representation through the server */ private static Geometry binaryViaSQL(byte[] rep, Connection conn) throws SQLException { PreparedStatement prep = conn.prepareStatement("SELECT ?::bytea::geometry"); prep.setBytes(1, rep); ResultSet rs = prep.executeQuery(); rs.next(); PGgeometry resultwrapper = ((PGgeometry) rs.getObject(1)); return resultwrapper.getGeometry(); } /** * Connect to the databases * * We use DriverWrapper here. For alternatives, see the DriverWrapper * Javadoc * * @param dbuser * * @see org.postgis.DriverWrapper * */ public static Connection connect(String url, String dbuser, String dbpass) throws SQLException { Connection conn; conn = DriverManager.getConnection(url, dbuser, dbpass); return conn; } public static void loadDrivers() throws ClassNotFoundException { Class.forName("org.postgis.DriverWrapper"); Class.forName("org.postgis.DriverWrapperAutoprobe"); } /** Our apps entry point */ public static void main(String[] args) throws SQLException, ClassNotFoundException { loadDrivers(); PGtokenizer dburls; String dbuser = null; String dbpass = null; if (args.length == 1 && args[0].equalsIgnoreCase("offline")) { System.out.println("Performing only offline tests"); dburls = new PGtokenizer("", ';'); } else if (args.length == 3) { System.out.println("Performing offline and online tests"); dburls = new PGtokenizer(args[0], ';'); dbuser = args[1]; dbpass = args[2]; } else { System.err.println("Usage: java examples/TestParser dburls user pass"); System.err.println(" or: java examples/TestParser offline"); System.err.println(); System.err.println("dburls has one or more jdbc urls separated by ; in the following format"); System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME"); System.exit(1); // Signal the compiler that code flow ends here. return; } Connection[] conns; conns = new Connection[dburls.getSize()]; for (int i = 0; i < dburls.getSize(); i++) { System.out.println("Creating JDBC connection to " + dburls.getToken(i)); conns[i] = connect(dburls.getToken(i), dbuser, dbpass); } System.out.println("Performing tests..."); System.out.println("***"); for (int i = 0; i < testset.length; i++) { test(testset[i][1], conns, testset[i][0]); test(SRIDPREFIX + testset[i][1], conns, testset[i][0]); } System.out.print("cleaning up..."); for (int i = 0; i < conns.length; i++) { conns[i].close(); } System.out.println("Finished, " + failcount + " tests failed!"); System.err.println("Finished, " + failcount + " tests failed!"); System.exit(failcount); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -