/
EscapeSyntaxCallModeCallTest.java
91 lines (80 loc) · 3.32 KB
/
EscapeSyntaxCallModeCallTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2019, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc3;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.PGProperty;
import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.EscapeSyntaxCallMode;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLState;
import org.junit.Test;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
public class EscapeSyntaxCallModeCallTest extends EscapeSyntaxCallModeBaseTest {
@Override
protected void updateProperties(Properties props) {
super.updateProperties(props);
PGProperty.ESCAPE_SYNTAX_CALL_MODE.set(props, EscapeSyntaxCallMode.CALL.value());
}
@Test
public void testInvokeFunction() throws Throwable {
// escapeSyntaxCallMode=call will cause a CALL statement to be used for the JDBC escape call
// syntax used below. "myiofunc" is a function, so the attempted invocation should fail.
assumeCallableStatementsSupported();
assumeMinimumServerVersion(ServerVersion.v11);
CallableStatement cs = con.prepareCall("{ call myiofunc(?,?) }");
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(1, 10);
try {
cs.execute();
fail("Should throw an exception");
} catch (SQLException ex) {
assertTrue(ex.getSQLState().equalsIgnoreCase(PSQLState.WRONG_OBJECT_TYPE.getState()));
}
}
@Test
public void testInvokeFunctionHavingReturnParameter() throws Throwable {
// escapeSyntaxCallMode=call will cause a CALL statement to be used for the JDBC escape call
// syntax used below. "mysumfunc" is a function, so the attempted invocation should fail.
assumeCallableStatementsSupported();
assumeMinimumServerVersion(ServerVersion.v11);
CallableStatement cs = con.prepareCall("{ ? = call mysumfunc(?,?) }");
cs.registerOutParameter(1, Types.INTEGER);
cs.setInt(2, 10);
cs.setInt(3, 20);
try {
cs.execute();
fail("Should throw an exception");
} catch (SQLException ex) {
// version 14 changes this to undefined function
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v14)) {
assertTrue(ex.getSQLState().equalsIgnoreCase(PSQLState.UNDEFINED_FUNCTION.getState()));
} else {
assertTrue(ex.getSQLState().equalsIgnoreCase(PSQLState.WRONG_OBJECT_TYPE.getState()));
}
}
}
@Test
public void testInvokeProcedure() throws Throwable {
// escapeSyntaxCallMode=call will cause a CALL statement to be used for the JDBC escape call
// syntax used below. "myioproc" is a procedure, so the invocation should succeed.
assumeCallableStatementsSupported();
assumeMinimumServerVersion(ServerVersion.v11);
CallableStatement cs = con.prepareCall("{call myioproc(?,?)}");
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(1, 10);
cs.setInt(2, 20);
cs.execute();
// Expected output: a==1 (param 1), b==10 (param 2)
int a = cs.getInt(1);
int b = cs.getInt(2);
assertTrue("Expected myioproc() to return output parameter values 1,10 but returned " + a + "," + b, (a == 1 && b == 10));
}
}