Skip to content

Commit

Permalink
Fix postgis search_path leak in test fixture setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bencaradocdavies committed Mar 25, 2016
1 parent 2bffc31 commit a92b206
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
@@ -1,12 +1,12 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014-2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.test.onlineTest.setup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.geoserver.test.onlineTest.support.AbstractReferenceDataSetup;
Expand Down Expand Up @@ -62,7 +62,7 @@ protected Properties createExampleFixture() {

protected void runSqlInsertScript() throws Exception {
DatabaseUtil du = new DatabaseUtil();
ArrayList<String> sqls = du.splitOracleSQLScript(script);
List<String> sqls = du.splitOracleSQLScript(script);
for (String sql : sqls) {
if (sql.startsWith("CALL")) {
String formattedSP = "{" + sql + "}";
Expand Down
@@ -1,12 +1,12 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014-2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.test.onlineTest.setup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.geoserver.test.onlineTest.support.AbstractReferenceDataSetup;
Expand Down Expand Up @@ -40,22 +40,10 @@ public JDBCDataStoreFactory createDataStoreFactory() {

protected void runSqlInsertScript() throws Exception {
DatabaseUtil du = new DatabaseUtil();
ArrayList<String> sqls = du.splitPostgisSQLScript(script);
// run the script as a single shot, going back and forth line
// by line takes forever to run (more than a minute)
String pgScript = rebuildAsSingle(sqls);
this.run(pgScript);
this.setDataVersion(this.scriptVersion);

}

private String rebuildAsSingle(ArrayList<String> sqls) {
StringBuilder sb = new StringBuilder();
for (String sql : sqls) {
sb.append(sql).append("\n");
}

return sb.toString();
List<String> sqls = du.splitPostgisSQLScript(script);
sqls.add("set search_path = public;");
run(du.rebuildAsSingle(sqls));
setDataVersion(scriptVersion);
}

// these private helper class might be useful in the future. feel free to change its access
Expand Down
@@ -1,13 +1,14 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014-2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.test.onlineTest.setup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.geoserver.test.onlineTest.support.AbstractReferenceDataSetup;
import org.geoserver.test.onlineTest.support.DatabaseUtil;
import org.geotools.data.oracle.OracleNGDataStoreFactory;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected Properties createExampleFixture() {

private void runSqlInsertScript() throws Exception {
DatabaseUtil du = new DatabaseUtil();
ArrayList<String> sqls = du.splitOracleSQLScript(script);
List<String> sqls = du.splitOracleSQLScript(script);
for (String sql : sqls) {
if (sql.startsWith("CALL")) {
String formattedSP = "{" + sql + "}";
Expand Down
@@ -1,13 +1,14 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014-2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.test.onlineTest.setup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.geoserver.test.onlineTest.support.AbstractReferenceDataSetup;
import org.geoserver.test.onlineTest.support.DatabaseUtil;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
Expand Down Expand Up @@ -39,12 +40,10 @@ public JDBCDataStoreFactory createDataStoreFactory() {

private void runSqlInsertScript() throws Exception {
DatabaseUtil du = new DatabaseUtil();
ArrayList<String> sqls = du.splitPostgisSQLScript(script);
for (String sql : sqls) {
this.run(sql);
}
this.setDataVersion(this.scriptVersion);

List<String> sqls = du.splitPostgisSQLScript(script);
sqls.add("set search_path = public;");
run(du.rebuildAsSingle(sqls));
setDataVersion(scriptVersion);
}

// these private helper class might be useful in the future. feel free to change its access
Expand Down
@@ -1,4 +1,4 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014-2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
Expand All @@ -11,6 +11,7 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


/**
Expand All @@ -35,7 +36,7 @@ public class DatabaseUtil {
* @param inputStream sql statements
* @return list of SQL statements
*/
public ArrayList<String> splitPostgisSQLScript(InputStream inputStream) throws Exception {
public List<String> splitPostgisSQLScript(InputStream inputStream) throws Exception {

StringBuilder contents = new StringBuilder();

Expand Down Expand Up @@ -104,6 +105,20 @@ public static int countMatches(String str, String sub) {
return count;
}

/**
* Return a list of SQL statements as a single string including a newline after each statement.
*
* @param sqls list of SQL statements
* @return string of statements with newline after each
*/
public String rebuildAsSingle(List<String> sqls) {
StringBuilder sb = new StringBuilder();
for (String sql : sqls) {
sb.append(sql).append("\n");
}
return sb.toString();
}

/**
* sets up the rule such that all ";" between these operators will not be treated as a delimiter
*
Expand Down Expand Up @@ -198,7 +213,7 @@ public void reset() {
* @param inputStream The oracle sql script
* @returnlist of sql statements
*/
public ArrayList<String> splitOracleSQLScript(InputStream inputStream) throws Exception {
public List<String> splitOracleSQLScript(InputStream inputStream) throws Exception {

StringBuilder contents = new StringBuilder();

Expand Down

0 comments on commit a92b206

Please sign in to comment.