提交 223328b2 authored 作者: Thomas Mueller's avatar Thomas Mueller

Slightly improve code coverage

上级 91bf5c5f
...@@ -115,7 +115,7 @@ public class PgServerThread implements Runnable { ...@@ -115,7 +115,7 @@ public class PgServerThread implements Runnable {
return dataIn.readInt(); return dataIn.readInt();
} }
private int readShort() throws IOException { private short readShort() throws IOException {
return dataIn.readShort(); return dataIn.readShort();
} }
...@@ -143,7 +143,7 @@ public class PgServerThread implements Runnable { ...@@ -143,7 +143,7 @@ public class PgServerThread implements Runnable {
byte[] data = DataUtils.newBytes(len); byte[] data = DataUtils.newBytes(len);
dataInRaw.readFully(data, 0, len); dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len)); dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switchBlock: switch (x) { switch (x) {
case 0: case 0:
server.trace("Init"); server.trace("Init");
int version = readInt(); int version = readInt();
...@@ -269,7 +269,7 @@ public class PgServerThread implements Runnable { ...@@ -269,7 +269,7 @@ public class PgServerThread implements Runnable {
} }
} catch (Exception e) { } catch (Exception e) {
sendErrorResponse(e); sendErrorResponse(e);
break switchBlock; break;
} }
int resultCodeCount = readShort(); int resultCodeCount = readShort();
portal.resultColumnFormat = new int[resultCodeCount]; portal.resultColumnFormat = new int[resultCodeCount];
...@@ -509,11 +509,11 @@ public class PgServerThread implements Runnable { ...@@ -509,11 +509,11 @@ public class PgServerThread implements Runnable {
switch (pgType) { switch (pgType) {
case PgServer.PG_TYPE_INT2: case PgServer.PG_TYPE_INT2:
writeInt(2); writeInt(2);
dataOut.writeShort(rs.getShort(column)); writeShort(rs.getShort(column));
break; break;
case PgServer.PG_TYPE_INT4: case PgServer.PG_TYPE_INT4:
writeInt(4); writeInt(4);
dataOut.writeInt(rs.getInt(column)); writeInt(rs.getInt(column));
break; break;
case PgServer.PG_TYPE_INT8: case PgServer.PG_TYPE_INT8:
writeInt(8); writeInt(8);
...@@ -565,33 +565,23 @@ public class PgServerThread implements Runnable { ...@@ -565,33 +565,23 @@ public class PgServerThread implements Runnable {
// binary // binary
switch (pgType) { switch (pgType) {
case PgServer.PG_TYPE_INT2: case PgServer.PG_TYPE_INT2:
if (paramLen != 4) { checkParamLength(4, paramLen);
throw DbException.getInvalidValueException("paramLen", paramLen); prep.setShort(col, readShort());
}
prep.setShort(col, dataIn.readShort());
break; break;
case PgServer.PG_TYPE_INT4: case PgServer.PG_TYPE_INT4:
if (paramLen != 4) { checkParamLength(4, paramLen);
throw DbException.getInvalidValueException("paramLen", paramLen); prep.setInt(col, readInt());
}
prep.setInt(col, dataIn.readInt());
break; break;
case PgServer.PG_TYPE_INT8: case PgServer.PG_TYPE_INT8:
if (paramLen != 8) { checkParamLength(8, paramLen);
throw DbException.getInvalidValueException("paramLen", paramLen);
}
prep.setLong(col, dataIn.readLong()); prep.setLong(col, dataIn.readLong());
break; break;
case PgServer.PG_TYPE_FLOAT4: case PgServer.PG_TYPE_FLOAT4:
if (paramLen != 4) { checkParamLength(4, paramLen);
throw DbException.getInvalidValueException("paramLen", paramLen);
}
prep.setFloat(col, dataIn.readFloat()); prep.setFloat(col, dataIn.readFloat());
break; break;
case PgServer.PG_TYPE_FLOAT8: case PgServer.PG_TYPE_FLOAT8:
if (paramLen != 8) { checkParamLength(8, paramLen);
throw DbException.getInvalidValueException("paramLen", paramLen);
}
prep.setDouble(col, dataIn.readDouble()); prep.setDouble(col, dataIn.readDouble());
break; break;
case PgServer.PG_TYPE_BYTEA: case PgServer.PG_TYPE_BYTEA:
...@@ -608,6 +598,12 @@ public class PgServerThread implements Runnable { ...@@ -608,6 +598,12 @@ public class PgServerThread implements Runnable {
} }
} }
private static void checkParamLength(int expected, int got) {
if (expected != got) {
throw DbException.getInvalidValueException("paramLen", got);
}
}
private void sendErrorResponse(Exception re) throws IOException { private void sendErrorResponse(Exception re) throws IOException {
SQLException e = DbException.toSQLException(re); SQLException e = DbException.toSQLException(re);
server.traceError(e); server.traceError(e);
......
...@@ -41,13 +41,23 @@ public class TestPgServer extends TestBase { ...@@ -41,13 +41,23 @@ public class TestPgServer extends TestBase {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
testPGAdapter(); testPgAdapter();
testKeyAlias(); testKeyAlias();
testCancelQuery(); testCancelQuery();
testBinaryTypes(); testBinaryTypes();
} }
private void testPGAdapter() throws SQLException { private boolean getPgJdbcDriver() {
try {
Class.forName("org.postgresql.Driver");
return true;
} catch (ClassNotFoundException e) {
println("PostgreSQL JDBC driver not found - PgServer not tested");
return false;
}
}
private void testPgAdapter() throws SQLException {
deleteDb("test"); deleteDb("test");
Server server = Server.createPgServer("-baseDir", getBaseDir(), "-pgPort", "5535", "-pgDaemon"); Server server = Server.createPgServer("-baseDir", getBaseDir(), "-pgPort", "5535", "-pgDaemon");
assertEquals(5535, server.getPort()); assertEquals(5535, server.getPort());
...@@ -55,23 +65,24 @@ public class TestPgServer extends TestBase { ...@@ -55,23 +65,24 @@ public class TestPgServer extends TestBase {
server.start(); server.start();
assertStartsWith(server.getStatus(), "PG server running at pg://"); assertStartsWith(server.getStatus(), "PG server running at pg://");
try { try {
Class.forName("org.postgresql.Driver"); if (getPgJdbcDriver()) {
testPgClient(); testPgClient();
} catch (ClassNotFoundException e) { }
println("PostgreSQL JDBC driver not found - PgServer not tested");
} finally { } finally {
server.stop(); server.stop();
} }
} }
private void testCancelQuery() throws Exception { private void testCancelQuery() throws Exception {
if (!getPgJdbcDriver()) {
return;
}
Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test"); Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test");
server.start(); server.start();
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
try { try {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa");
final Statement stat = conn.createStatement(); final Statement stat = conn.createStatement();
stat.execute("create alias sleep for \"java.lang.Thread.sleep\""); stat.execute("create alias sleep for \"java.lang.Thread.sleep\"");
...@@ -101,8 +112,6 @@ public class TestPgServer extends TestBase { ...@@ -101,8 +112,6 @@ public class TestPgServer extends TestBase {
} finally { } finally {
conn.close(); conn.close();
} }
} catch (ClassNotFoundException e) {
println("PostgreSQL JDBC driver not found - PgServer not tested");
} finally { } finally {
server.stop(); server.stop();
executor.shutdown(); executor.shutdown();
...@@ -119,6 +128,7 @@ public class TestPgServer extends TestBase { ...@@ -119,6 +128,7 @@ public class TestPgServer extends TestBase {
stat.execute("create table test(id int primary key, name varchar)"); stat.execute("create table test(id int primary key, name varchar)");
stat.execute("create index idx_test_name on test(name, id)"); stat.execute("create index idx_test_name on test(name, id)");
stat.execute("grant all on test to test"); stat.execute("grant all on test to test");
stat.close();
conn.close(); conn.close();
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "test", "test"); conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "test", "test");
...@@ -131,7 +141,13 @@ public class TestPgServer extends TestBase { ...@@ -131,7 +141,13 @@ public class TestPgServer extends TestBase {
assertEquals(6, rs.getInt(1)); assertEquals(6, rs.getInt(1));
stat.execute("deallocate test"); stat.execute("deallocate test");
PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)"); PreparedStatement prep;
prep = conn.prepareStatement("select * from test where name = ?");
prep.setNull(1, Types.VARCHAR);
rs = prep.executeQuery();
assertFalse(rs.next());
prep = conn.prepareStatement("insert into test values(?, ?)");
ParameterMetaData meta = prep.getParameterMetaData(); ParameterMetaData meta = prep.getParameterMetaData();
assertEquals(2, meta.getParameterCount()); assertEquals(2, meta.getParameterCount());
prep.setInt(1, 1); prep.setInt(1, 1);
...@@ -272,11 +288,12 @@ public class TestPgServer extends TestBase { ...@@ -272,11 +288,12 @@ public class TestPgServer extends TestBase {
} }
private void testKeyAlias() throws SQLException { private void testKeyAlias() throws SQLException {
if (!getPgJdbcDriver()) {
return;
}
Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test"); Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test");
server.start(); server.start();
try { try {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -288,19 +305,19 @@ public class TestPgServer extends TestBase { ...@@ -288,19 +305,19 @@ public class TestPgServer extends TestBase {
assertEquals("MEMORY", rs.getString(1)); assertEquals("MEMORY", rs.getString(1));
conn.close(); conn.close();
} catch (ClassNotFoundException e) {
println("PostgreSQL JDBC driver not found - PgServer not tested");
} finally { } finally {
server.stop(); server.stop();
} }
} }
private void testBinaryTypes() throws SQLException { private void testBinaryTypes() throws SQLException {
if (!getPgJdbcDriver()) {
return;
}
Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test"); Server server = Server.createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "test", "mem:test");
server.start(); server.start();
try { try {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/test", "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -335,8 +352,6 @@ public class TestPgServer extends TestBase { ...@@ -335,8 +352,6 @@ public class TestPgServer extends TestBase {
rs.getBytes(10)); rs.getBytes(10));
conn.close(); conn.close();
} catch (ClassNotFoundException e) {
println("PostgreSQL JDBC driver not found - PgServer not tested");
} finally { } finally {
server.stop(); server.stop();
} }
......
...@@ -35,8 +35,11 @@ public class TestTraceSystem extends TestBase { ...@@ -35,8 +35,11 @@ public class TestTraceSystem extends TestBase {
private static void testAdapter() { private static void testAdapter() {
TraceSystem ts = new TraceSystem(null); TraceSystem ts = new TraceSystem(null);
ts.setName("test");
ts.setLevelFile(TraceSystem.ADAPTER); ts.setLevelFile(TraceSystem.ADAPTER);
ts.getTrace("test").debug("test");
ts.getTrace("test").info("test"); ts.getTrace("test").info("test");
ts.getTrace("test").error(new Exception(), "test");
ts.close(); ts.close();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论