Skip to content

Commit

Permalink
style: cleanup and Java 5' features for tests (#602)
Browse files Browse the repository at this point in the history
add acces modifiers
Java 5's for-each
simplify some expressions
  • Loading branch information
AlexElin authored and vlsi committed Jul 10, 2016
1 parent 1d8ebfc commit 28c0c22
Show file tree
Hide file tree
Showing 40 changed files with 232 additions and 263 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public V3ParameterListTests(String test) {
@Override @Override
@Before @Before
protected void setUp() throws Exception { protected void setUp() throws Exception {
SocketFactory socketFactory = SocketFactoryFactory.getSocketFactory(System SocketFactory socketFactory = SocketFactoryFactory.getSocketFactory(System.getProperties());
.getProperties());
HostSpec hostSpec = new HostSpec(TestUtil.getServer(), TestUtil.getPort()); HostSpec hostSpec = new HostSpec(TestUtil.getServer(), TestUtil.getPort());
pci = new ProtocolConnectionImpl(new PGStream(socketFactory, hostSpec), "", pci = new ProtocolConnectionImpl(new PGStream(socketFactory, hostSpec), "",
"", new Properties(), new Logger(), 5000); "", new Properties(), new Logger(), 5000);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ protected void updateProperties(Properties props) {
public void testDeepInternalsBatchedQueryDecorator() throws Exception { public void testDeepInternalsBatchedQueryDecorator() throws Exception {
PgPreparedStatement pstmt = null; PgPreparedStatement pstmt = null;
try { try {
pstmt = (PgPreparedStatement) con pstmt = (PgPreparedStatement) con.prepareStatement("INSERT INTO testbatch VALUES (?,?)");
.prepareStatement("INSERT INTO testbatch VALUES (?,?)");


pstmt.setInt(1, 1); pstmt.setInt(1, 1);
pstmt.setInt(2, 2); pstmt.setInt(2, 2);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testPreparedStatementPreparation() throws Throwable {
Connection conn = con; Connection conn = con;
conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
try { try {
Timestamp ts = new Timestamp(2016 - 1900, 1 - 1, 31, 0, 0, 0, 0); Timestamp ts = new Timestamp(2016 - 1900, 0, 31, 0, 0, 0, 0);
int[] types = new int[]{ int[] types = new int[]{
Types.NUMERIC, Types.NUMERIC,
Types.DATE, Types.DATE,
Expand All @@ -96,21 +96,20 @@ public void testPreparedStatementPreparation() throws Throwable {
Object[] row3 = new Object[]{1, ts, 0.0, true, null, ts, 0.0, 0.0}; Object[] row3 = new Object[]{1, ts, 0.0, true, null, ts, 0.0, 0.0};
Object[][] tRows = new Object[][]{row0, row1, row2, row3}; Object[][] tRows = new Object[][]{row0, row1, row2, row3};
List<Object[]> rowList = new ArrayList<Object[]>(); List<Object[]> rowList = new ArrayList<Object[]>();
for (int i = 0; i < ROW_INDEXES.length; i++) { for (int ROW_INDEX : ROW_INDEXES) {
rowList.add(tRows[ROW_INDEXES[i]]); rowList.add(tRows[ROW_INDEX]);
} }


StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append( sb.append(
"INSERT INTO BugFreezeTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8) VALUES (?,?,?,?,?,?,?,?)"); "INSERT INTO BugFreezeTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8) VALUES (?,?,?,?,?,?,?,?)");
String insert = sb.toString(); String insert = sb.toString();
PgPreparedStatement pst = (PgPreparedStatement) conn.prepareStatement(insert); PgPreparedStatement pst = (PgPreparedStatement) conn.prepareStatement(insert);
String statementName = null; String statementName;
int count = 0; int count = 0;
do { do {
Object[][] rows = rowList.toArray(new Object[0][]); Object[][] rows = rowList.toArray(new Object[0][]);
for (int j = 0; j < rows.length; j++) { for (Object[] row : rows) {
Object[] row = rows[j];
for (int k = 0; k < row.length; k++) { for (int k = 0; k < row.length; k++) {
Object o = row[k]; Object o = row[k];
int paramIndex = k + 1; int paramIndex = k + 1;
Expand Down Expand Up @@ -153,8 +152,7 @@ private String getStatementName(PgPreparedStatement pst) throws Exception {
Field statementNameField = Field statementNameField =
Class.forName("org.postgresql.core.v3.SimpleQuery").getDeclaredField("statementName"); Class.forName("org.postgresql.core.v3.SimpleQuery").getDeclaredField("statementName");
statementNameField.setAccessible(true); statementNameField.setAccessible(true);
String statementName = (String) statementNameField.get(query); return (String) statementNameField.get(query);
return statementName;
} }


/** /**
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testHStoreSelectNullValue() throws SQLException {
} }


public void testHStoreSend() throws SQLException { public void testHStoreSend() throws SQLException {
Map<String, Integer> correct = Collections.singletonMap("a", new Integer(1)); Map<String, Integer> correct = Collections.singletonMap("a", 1);
PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text"); PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text");
pstmt.setObject(1, correct); pstmt.setObject(1, correct);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
Expand All @@ -76,7 +76,7 @@ public void testHStoreSend() throws SQLException {
} }


public void testHStoreUsingPSSetObject4() throws SQLException { public void testHStoreUsingPSSetObject4() throws SQLException {
Map<String, Integer> correct = Collections.singletonMap("a", new Integer(1)); Map<String, Integer> correct = Collections.singletonMap("a", 1);
PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text"); PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text");
pstmt.setObject(1, correct, Types.OTHER, -1); pstmt.setObject(1, correct, Types.OTHER, -1);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public class MultiHostsConnectionTest extends TestCase {
private static Map<HostSpec, Object> hostStatusMap; private static Map<HostSpec, Object> hostStatusMap;


static { static {
Field field;
try { try {
field = GlobalHostStatusTracker.class.getDeclaredField("hostStatusMap"); Field field = GlobalHostStatusTracker.class.getDeclaredField("hostStatusMap");
field.setAccessible(true); field.setAccessible(true);
hostStatusMap = (Map<HostSpec, Object>) field.get(null); hostStatusMap = (Map<HostSpec, Object>) field.get(null);


Expand Down
4 changes: 2 additions & 2 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/BaseTest4.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
public class BaseTest4 { public class BaseTest4 {


public enum BinaryMode { public enum BinaryMode {
REGULAR, FORCE; REGULAR, FORCE
} }


public enum AutoCommit { public enum AutoCommit {
YES, NO; YES, NO
} }


protected Connection con; protected Connection con;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void addRow(Statement statement, String value) throws SQLException {
} }
} }


enum FailMode { private enum FailMode {
NO_FAIL_JUST_INSERTS, NO_FAIL_SELECT, NO_FAIL_JUST_INSERTS, NO_FAIL_SELECT,
FAIL_VIA_SELECT_PARSE, FAIL_VIA_SELECT_RUNTIME, FAIL_VIA_SELECT_PARSE, FAIL_VIA_SELECT_RUNTIME,
FAIL_VIA_DUP_KEY; FAIL_VIA_DUP_KEY;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void injectFailure(Statement statement, BatchType batchType) throws SQLEx
} }
} }


enum FailPosition { private enum FailPosition {
NONE, FIRST_ROW, SECOND_ROW, MIDDLE, ALMOST_LAST_ROW, LAST_ROW; NONE, FIRST_ROW, SECOND_ROW, MIDDLE, ALMOST_LAST_ROW, LAST_ROW;


public boolean supports(FailMode mode) { public boolean supports(FailMode mode) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public void testBatchWithReWrittenBatchStatementWithFixedParameter()
"INSERT INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx", "INSERT INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx",
// "INSERT /*xxx*/INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx", // "INSERT /*xxx*/INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx",
}; };
for (int i = 0; i < odd.length; i++) { for (String s : odd) {
String s = odd[i];
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
pstmt = con.prepareStatement(s); pstmt = con.prepareStatement(s);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ protected void setUp() throws Exception {


String sql; String sql;


Statement st;
/* /*
* this would have to be executed using the postgres user in order to get access to a C function * this would have to be executed using the postgres user in order to get access to a C function
* *
*/ */
Connection privilegedCon = TestUtil.openPrivilegedDB(); Connection privilegedCon = TestUtil.openPrivilegedDB();
st = privilegedCon.createStatement(); Statement st = privilegedCon.createStatement();
try { try {
sql = sql =
"CREATE OR REPLACE FUNCTION lo_manage() RETURNS pg_catalog.trigger AS '$libdir/lo' LANGUAGE C"; "CREATE OR REPLACE FUNCTION lo_manage() RETURNS pg_catalog.trigger AS '$libdir/lo' LANGUAGE C";
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import org.postgresql.test.TestUtil; import org.postgresql.test.TestUtil;


import junit.framework.Assert;
import junit.framework.TestCase; import junit.framework.TestCase;


import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
Expand Down Expand Up @@ -33,13 +32,13 @@ public void testTimeout() {
try { try {
DriverManager.getConnection(UNREACHABLE_URL, props); DriverManager.getConnection(UNREACHABLE_URL, props);
} catch (SQLException e) { } catch (SQLException e) {
Assert.assertTrue("Unexpected " + e.toString(), assertTrue("Unexpected " + e.toString(),
e.getCause() instanceof SocketTimeoutException); e.getCause() instanceof SocketTimeoutException);
final long interval = System.currentTimeMillis() - startTime; final long interval = System.currentTimeMillis() - startTime;
final long connectTimeoutMillis = CONNECT_TIMEOUT * 1000; final long connectTimeoutMillis = CONNECT_TIMEOUT * 1000;
final long maxDeviation = connectTimeoutMillis / 10; final long maxDeviation = connectTimeoutMillis / 10;
// check that it was not a default system timeout, an approximate value is used // check that it was not a default system timeout, an approximate value is used
Assert.assertTrue(Math.abs(interval - connectTimeoutMillis) < maxDeviation); assertTrue(Math.abs(interval - connectTimeoutMillis) < maxDeviation);
return; return;
} }
fail("SQLException expected"); fail("SQLException expected");
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class CopyLargeFileTest extends TestCase {




protected void setUp() throws Exception { protected void setUp() throws Exception {

super.setUp(); super.setUp();


con = TestUtil.openDB(); con = TestUtil.openDB();
Expand All @@ -49,9 +48,8 @@ protected void setUp() throws Exception {
private void feedTable() throws Exception { private void feedTable() throws Exception {
PreparedStatement stmt = con.prepareStatement( PreparedStatement stmt = con.prepareStatement(
TestUtil.insertSQL("pgjdbc_issue366_test_glossary", "text_id, name", "?, ?")); TestUtil.insertSQL("pgjdbc_issue366_test_glossary", "text_id, name", "?, ?"));
char ch = ' ';
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++) {
ch = (char) ('A' + i); // black magic char ch = (char) ('A' + i); // black magic
insertData(stmt, "VERY_LONG_STRING_TO_REPRODUCE_ISSUE_366_" + ch + ch + ch, insertData(stmt, "VERY_LONG_STRING_TO_REPRODUCE_ISSUE_366_" + ch + ch + ch,
"" + ch + ch + ch); "" + ch + ch + ch);
} }
Expand Down
8 changes: 3 additions & 5 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/CopyTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ public CopyTest(String name) {
private byte[] getData(String[] origData) { private byte[] getData(String[] origData) {
ByteArrayOutputStream buf = new ByteArrayOutputStream(); ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buf); PrintStream ps = new PrintStream(buf);
for (int i = 0; i < origData.length; i++) { for (String anOrigData : origData) {
ps.print(origData[i]); ps.print(anOrigData);
} }
return buf.toByteArray(); return buf.toByteArray();
} }


protected void setUp() throws Exception { protected void setUp() throws Exception {


con = TestUtil.openDB(); con = TestUtil.openDB();


TestUtil.createTable(con, "copytest", "stringvalue text, intvalue int, numvalue numeric(5,2)"); TestUtil.createTable(con, "copytest", "stringvalue text, intvalue int, numvalue numeric(5,2)");
Expand Down Expand Up @@ -154,7 +152,7 @@ public int read() {
} }
}, 3); }, 3);
} catch (Exception e) { } catch (Exception e) {
if (e.toString().indexOf("COPYTEST") == -1) { if (!e.toString().contains("COPYTEST")) {
fail("should have failed trying to read from our bogus stream."); fail("should have failed trying to read from our bogus stream.");
} }
} }
Expand Down
Loading

0 comments on commit 28c0c22

Please sign in to comment.