Skip to content

Commit

Permalink
Refactor the Java acceptance test.
Browse files Browse the repository at this point in the history
This patch pulls out Java code in separate standalone .java files.
  • Loading branch information
knz committed Feb 22, 2016
1 parent 3873c14 commit 8c06cec
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
16 changes: 16 additions & 0 deletions acceptance/java/TestConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.sql.*;

public class TestConnection {
public static Connection getConnection() {
Class.forName("org.postgresql.Driver");

String DB_URL = "jdbc:postgresql://";
DB_URL += System.getenv("PGHOST") + ":" + System.getenv("PGPORT");
DB_URL += "/?ssl=true";
DB_URL += "&sslcert=" + System.getenv("PGSSLCERT");
DB_URL += "&sslkey=key.pk8";
DB_URL += "&sslrootcert=/certs/ca.crt";
DB_URL += "&sslfactory=org.postgresql.ssl.jdbc4.LibPQFactory";
return DriverManager.getConnection(DB_URL);
}
}
27 changes: 27 additions & 0 deletions acceptance/java/TestPlaceholders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.sql.*;

public class TestStatements {
public static void runTests(Connection conn) throws Exception {
PreparedStatement stmt = conn.prepareStatement("SELECT 1, 2 > ?, ?::int, ?::string, ?::string, ?::string, ?::string, ?::string");
stmt.setInt(1, 3);
stmt.setInt(2, 3);
stmt.setBoolean(3, true);
stmt.setLong(4, -4L);
stmt.setFloat(5, 5.31f);
stmt.setDouble(6, -6.21d);
stmt.setShort(7, (short)7);

ResultSet rs = stmt.executeQuery();
rs.next();
int a = rs.getInt(1);
boolean b = rs.getBoolean(2);
int c = rs.getInt(3);
String d = rs.getString(4);
String e = rs.getString(5);
String f = rs.getString(6);
String g = rs.getString(7);
String h = rs.getString(8);
if (a != 1 || b != false || c != 3 || !d.equals("true") || !e.equals("-4") || !f.startsWith("5.3") || !g.startsWith("-6.2") || !h.equals("7")) {
throw new Exception("unexpected: round-trip of constants via SELECT does not preserve their value");
}
}
12 changes: 12 additions & 0 deletions acceptance/java/TestPlaceholdersFail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.sql.*;

public class TestStatements {
public static void runTests(Connection conn) throws Exception {
PreparedStatement stmt = conn.prepareStatement("SELECT 1, 2 > ?, ?::int");
stmt.setInt(1, 3);
stmt.setString(2, "a"); // invalid because wrong type

ResultSet rs = stmt.executeQuery();
rs.next();
}
}
8 changes: 8 additions & 0 deletions acceptance/java/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.sql.*;

public class main {
public static void main(String[] args) throws Exception {
Connection conn = TestConnection.getConnection();
TestStatements.runTests(conn)
}
}
15 changes: 15 additions & 0 deletions acceptance/java/runjava.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
set -uex

cd acceptance/java/

# See: https://basildoncoder.com/blog/postgresql-jdbc-client-certificates.html
openssl pkcs8 -topk8 -inform PEM -outform DER -in /certs/node.client.key -out key.pk8 -nocrypt

export PATH=$PATH:/usr/lib/jvm/java-1.7-openjdk/bin

cp -f "$1" TestConnection.java
cp -f "$2" TestStatements.java

javac main.java TestConnection.java TestStatements.java
java -cp /postgres.jar:. main
62 changes: 4 additions & 58 deletions acceptance/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,10 @@

package acceptance

import (
"strings"
"testing"
)
import "testing"

func TestDockerJava(t *testing.T) {
testDockerSuccess(t, "java", []string{"/bin/sh", "-c", strings.Replace(java, "%v", "Int(2, 3)", 1)})
testDockerFail(t, "java", []string{"/bin/sh", "-c", strings.Replace(java, "%v", `String(2, "a")`, 1)})
baseCmd := []string{"/bin/sh", "-c", "acceptance/java/runjava.sh"}
testDockerSuccess(t, "java", append(baseCmd, "TestConnection.java", "TestPlaceholders.java"))
testDockerFail(t, "java", append(baseCmd, "TestConnection.java", "TestPlaceholdersFail.java"))
}

const java = `
set -e
cat > main.java << 'EOF'
import java.sql.*;
public class main {
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
String DB_URL = "jdbc:postgresql://";
DB_URL += System.getenv("PGHOST") + ":" + System.getenv("PGPORT");
DB_URL += "/?ssl=true";
DB_URL += "&sslcert=" + System.getenv("PGSSLCERT");
DB_URL += "&sslkey=key.pk8";
DB_URL += "&sslrootcert=/certs/ca.crt";
DB_URL += "&sslfactory=org.postgresql.ssl.jdbc4.LibPQFactory";
Connection conn = DriverManager.getConnection(DB_URL);
PreparedStatement stmt = conn.prepareStatement("SELECT 1, 2 > ?, ?::int, ?::string, ?::string, ?::string, ?::string, ?::string");
stmt.setInt(1, 3);
stmt.set%v;
stmt.setBoolean(3, true);
stmt.setLong(4, -4L);
stmt.setFloat(5, 5.31f);
stmt.setDouble(6, -6.21d);
stmt.setShort(7, (short)7);
ResultSet rs = stmt.executeQuery();
rs.next();
int a = rs.getInt(1);
boolean b = rs.getBoolean(2);
int c = rs.getInt(3);
String d = rs.getString(4);
String e = rs.getString(5);
String f = rs.getString(6);
String g = rs.getString(7);
String h = rs.getString(8);
if (a != 1 || b != false || c != 3 || !d.equals("true") || !e.equals("-4") || !f.startsWith("5.3") || !g.startsWith("-6.2") || !h.equals("7")) {
throw new Exception("unexpected");
}
}
}
EOF
# See: https://basildoncoder.com/blog/postgresql-jdbc-client-certificates.html
openssl pkcs8 -topk8 -inform PEM -outform DER -in /certs/node.client.key -out key.pk8 -nocrypt
export PATH=$PATH:/usr/lib/jvm/java-1.7-openjdk/bin
javac main.java
java -cp /postgres.jar:. main
`

0 comments on commit 8c06cec

Please sign in to comment.