Strabon
changeset 901:95427dc12cc4
Every test class creates a temp database (that does not already exists), runs the test and then drops the database.
author | Panayiotis Smeros <psmeros@di.uoa.gr> |
---|---|
date | Thu Mar 21 21:30:20 2013 +0200 (2013-03-21) |
parents | 5a82ff362b8d |
children | 742958932e5a |
files | runtime/src/test/java/eu/earthobservatory/runtime/postgis/TemplateTests.java runtime/src/test/resources/databases.properties |
line diff
1.1 --- a/runtime/src/test/java/eu/earthobservatory/runtime/postgis/TemplateTests.java Wed Mar 20 18:44:35 2013 +0200 1.2 +++ b/runtime/src/test/java/eu/earthobservatory/runtime/postgis/TemplateTests.java Thu Mar 21 21:30:20 2013 +0200 1.3 @@ -12,9 +12,12 @@ 1.4 import java.io.IOException; 1.5 import java.io.InputStream; 1.6 import java.sql.DriverManager; 1.7 +import java.sql.PreparedStatement; 1.8 import java.sql.ResultSet; 1.9 import java.sql.SQLException; 1.10 import java.sql.Statement; 1.11 +import java.sql.Connection; 1.12 +import java.util.ArrayList; 1.13 import java.util.Properties; 1.14 1.15 import org.junit.AfterClass; 1.16 @@ -23,60 +26,78 @@ 1.17 import org.openrdf.rio.RDFHandlerException; 1.18 import org.openrdf.rio.RDFParseException; 1.19 1.20 + 1.21 import eu.earthobservatory.runtime.generaldb.InvalidDatasetFormatFault; 1.22 import eu.earthobservatory.runtime.generaldb.SimpleTests; 1.23 import eu.earthobservatory.runtime.generaldb.Strabon; 1.24 1.25 +import static org.junit.Assert.assertNull; 1.26 + 1.27 /** 1.28 * A set of simple tests on SPARQL query functionality 1.29 * 1.30 * @author George Garbis <ggarbis@di.uoa.gr> 1.31 + * @author Panayiotis Smeros <psmeros@di.uoa.gr> 1.32 */ 1.33 public class TemplateTests { 1.34 1.35 - public static java.sql.Connection conn = null; 1.36 - public static String databaseName = null; 1.37 - 1.38 - public static String jdbcDriver = null; 1.39 + public static String databaseTemplateName = null; 1.40 + public static String defaultUser = null; 1.41 public static String serverName = null; 1.42 public static String username = null; 1.43 public static String password = null; 1.44 public static Integer port = null; 1.45 1.46 + public static Connection conn = null; 1.47 + public static String databaseName = null; 1.48 + 1.49 @BeforeClass 1.50 public static Strabon beforeClass(String inputFile) throws Exception 1.51 { 1.52 + String url=""; 1.53 + ArrayList<String> databases=new ArrayList<String>(); 1.54 + PreparedStatement pst = null; 1.55 + 1.56 // Read properties 1.57 Properties properties = new Properties(); 1.58 InputStream propertiesStream = SimpleTests.class.getResourceAsStream("/databases.properties"); 1.59 properties.load(propertiesStream); 1.60 1.61 + databaseTemplateName = properties.getProperty("postgis.databaseTemplateName");; 1.62 + defaultUser = properties.getProperty("postgis.defaultUser"); 1.63 serverName = properties.getProperty("postgis.serverName"); 1.64 - databaseName = properties.getProperty("postgis.databaseName"); 1.65 - port = Integer.parseInt(properties.getProperty("postgis.port")); 1.66 username = properties.getProperty("postgis.username"); 1.67 password = properties.getProperty("postgis.password"); 1.68 - 1.69 - // Connect to database 1.70 - Class.forName("org.postgresql.Driver"); 1.71 - String url = "jdbc:postgresql://"+serverName+":"+port+"/"+databaseName; 1.72 + port = Integer.parseInt(properties.getProperty("postgis.port")); 1.73 + 1.74 + //Connect to server and create the temp database 1.75 + url = "jdbc:postgresql://"+serverName+":"+port+"/"+defaultUser; 1.76 conn = DriverManager.getConnection(url, username, password); 1.77 - 1.78 -// // Clean database 1.79 - Statement stmt = conn.createStatement(); 1.80 - ResultSet results = stmt.executeQuery("SELECT table_name FROM information_schema.tables WHERE " + 1.81 - "table_schema='public' AND table_name <> 'spatial_ref_sys' " + 1.82 - "AND table_name <> 'geometry_columns' AND table_name <> 'geography_columns' " + 1.83 - "AND table_name <> 'raster_columns' AND table_name <> 'raster_overviews' " + 1.84 - "AND table_name <> 'locked'" 1.85 - ); 1.86 - while (results.next()) { 1.87 - String table_name = results.getString("table_name"); 1.88 - Statement stmt2 = conn.createStatement(); 1.89 - stmt2.executeUpdate("DROP TABLE \""+table_name+"\""); 1.90 - stmt2.close(); 1.91 - } 1.92 - stmt.close(); 1.93 + assertNull(conn.getWarnings()); 1.94 + 1.95 + pst = conn.prepareStatement("SELECT * FROM pg_catalog.pg_database"); 1.96 + ResultSet rs = pst.executeQuery(); 1.97 + 1.98 + while (rs.next()) { 1.99 + databases.add(rs.getString(1)); 1.100 + } 1.101 + rs.close(); 1.102 + pst.close(); 1.103 + 1.104 + databaseName="teststrabon"+(int)(Math.random()*10000); 1.105 + while(databases.contains(databaseName)){ 1.106 + databaseName+="0"; 1.107 + } 1.108 + 1.109 + 1.110 + pst = conn.prepareStatement("CREATE DATABASE "+databaseName+" TEMPLATE " + databaseTemplateName); 1.111 + pst.executeUpdate(); 1.112 + pst.close(); 1.113 + conn.close(); 1.114 + 1.115 + url = "jdbc:postgresql://"+serverName+":"+port+"/"+databaseName; 1.116 + conn = DriverManager.getConnection(url, username, password); 1.117 + assertNull(conn.getWarnings()); 1.118 1.119 Strabon strabon = new eu.earthobservatory.runtime.postgis.Strabon(databaseName, username, password, port, serverName, true); 1.120 1.121 @@ -85,10 +106,22 @@ 1.122 return strabon; 1.123 } 1.124 1.125 + 1.126 @AfterClass 1.127 public static void afterClass(Strabon strabon) throws SQLException 1.128 { 1.129 strabon.close(); 1.130 + 1.131 + //Drop the temp database 1.132 + conn.close(); 1.133 + String url = "jdbc:postgresql://"+serverName+":"+port+"/"+defaultUser; 1.134 + conn = DriverManager.getConnection(url, username, password); 1.135 + assertNull(conn.getWarnings()); 1.136 + 1.137 + PreparedStatement pst = conn.prepareStatement("DROP DATABASE "+databaseName); 1.138 + pst.executeUpdate(); 1.139 + pst.close(); 1.140 + conn.close(); 1.141 } 1.142 1.143 protected static void loadTestData(String inputfile, Strabon strabon) 1.144 @@ -96,6 +129,23 @@ 1.145 { 1.146 strabon.storeInRepo(inputfile, "NTRIPLES"); 1.147 } 1.148 + 1.149 + 1.150 + // Clean database 1.151 +// Statement stmt = conn.createStatement(); 1.152 +// ResultSet results = stmt.executeQuery("SELECT table_name FROM information_schema.tables WHERE " + 1.153 +// "table_schema='public' AND table_name <> 'spatial_ref_sys' " + 1.154 +// "AND table_name <> 'geometry_columns' AND table_name <> 'geography_columns' " + 1.155 +// "AND table_name <> 'raster_columns' AND table_name <> 'raster_overviews' " + 1.156 +// "AND table_name <> 'locked'" 1.157 +// ); 1.158 +// while (results.next()) { 1.159 +// String table_name = results.getString("table_name"); 1.160 +// Statement stmt2 = conn.createStatement(); 1.161 +// stmt2.executeUpdate("DROP TABLE \""+table_name+"\""); 1.162 +// stmt2.close(); 1.163 +// } 1.164 +// stmt.close(); 1.165 1.166 // /** 1.167 // * @throws java.lang.Exception
2.1 --- a/runtime/src/test/resources/databases.properties Wed Mar 20 18:44:35 2013 +0200 2.2 +++ b/runtime/src/test/resources/databases.properties Thu Mar 21 21:30:20 2013 +0200 2.3 @@ -1,5 +1,6 @@ 2.4 # PostGIS 2.5 -postgis.databaseName = strabon-test 2.6 +postgis.databaseTemplateName = template_postgis 2.7 +postgis.defaultUser = postgres 2.8 postgis.serverName = localhost 2.9 postgis.username = postgres 2.10 postgis.password = postgres