diff --git a/.gitignore b/.gitignore index 7ebade6..5580fea 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,3 @@ sql/oracle/init/.cache pom.xml.releaseBackup pom.xml.versionsBackup release.properties - diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4ab956d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +sudo: required +language: java +jdk: openjdk8 +services: + - docker + - postgresql + - mysql +before_install: + - ./build/setup_postgres.sh + - ./build/setup_mysql.sh + - ./build/setup_oracle.sh +cache: + directories: + - $HOME/.m2 diff --git a/build/setup_mysql.sh b/build/setup_mysql.sh new file mode 100755 index 0000000..314c2e0 --- /dev/null +++ b/build/setup_mysql.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +mysql < sql/mysql/create-test-user.sql +mysql -u test --password=test test < sql/mysql/schema-mysql-1.sql diff --git a/build/setup_oracle.sh b/build/setup_oracle.sh new file mode 100755 index 0000000..620262e --- /dev/null +++ b/build/setup_oracle.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +CONTAINER_NAME="oracle18" + +# pull from private repo +echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin +docker pull morejdbc/morejdbc-oracle-18:latest + +# run +echo "Running $CONTAINER_NAME container" +docker run -d --name "$CONTAINER_NAME" -p 1521:1521 --expose=1521 morejdbc/morejdbc-oracle-18:latest + +# wait database to be ready +DB_IS_READY="0" +while test "$DB_IS_READY" = "0"; +do + sleep 5; + + if docker logs "$CONTAINER_NAME" | grep -q 'DATABASE IS READY TO USE'; + then + DB_IS_READY="1" + fi + + if docker logs "$CONTAINER_NAME" | grep -q 'DATABASE SETUP WAS NOT SUCCESSFUL'; + then + exit 1 + fi + echo "It's Oracle. Please wait..." +done; +### + +docker exec -i "$CONTAINER_NAME" sqlplus / as sysdba << EOF +$(cat sql/oracle/create-test-user.sql) +exit; +EOF + +docker exec -i "$CONTAINER_NAME" sqlplus test/test << EOF +$(cat sql/oracle/schema-oracle-1.sql) +exit; +EOF diff --git a/build/setup_postgres.sh b/build/setup_postgres.sh new file mode 100755 index 0000000..3c3bd51 --- /dev/null +++ b/build/setup_postgres.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +psql -U postgres -f sql/postgres/create-test-user.sql +psql -U test -f sql/postgres/schema-postgres-1.sql diff --git a/pom.xml b/pom.xml index 1cfb474..8c9b6b5 100644 --- a/pom.xml +++ b/pom.xml @@ -67,15 +67,21 @@ - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 test org.mockito mockito-core - 1.10.19 + 3.4.4 test @@ -95,7 +101,7 @@ mysql mysql-connector-java - 8.0.16 + 8.0.11 test @@ -107,8 +113,8 @@ com.oracle.database.jdbc - ojdbc6 - 11.2.0.4 + ojdbc8 + 19.7.0.0 test @@ -149,8 +155,13 @@ + org.apache.maven.plugins maven-compiler-plugin - 3.6.2 + 3.8.1 + + 8 + 8 + org.apache.maven.plugins @@ -165,6 +176,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + maven-jar-plugin 3.2.0 diff --git a/readme-oracle-tests.md b/readme-oracle-tests.md index 0bf534f..849549d 100644 --- a/readme-oracle-tests.md +++ b/readme-oracle-tests.md @@ -3,20 +3,18 @@ https://download.liquibase.org/download/ Set environment variable LIQUIBASE_HOME Copy driver to $LIQUIBASE_HOME/lib -#### Run docker with Oracle XE 11 release 2 +#### Run docker with Oracle XE 18c (you need to build it first) ``` cd sql/oracle -rm -f init/.cache -docker run --rm -it -p 1521:1521 -v $PWD/init:/docker-entrypoint-initdb.d --name morejdbc-oracle wnameless/oracle-xe-11g-r2 +docker run --name morejdbc-oracle -d -p 1521:1521 oracle/database:18.4.0-xe ``` You can pass the tablespace volume with extra parameter `-v $HOME/oracle_data:/u01/app/oracle` #### Create Oracle schema as test user ``` - # Workaround only for XE and Russian locale (ORA-12705: Cannot access NLS data files or invalid environment specified) # export JAVA_OPTS="-Duser.country=en -Duser.language=en" -$LIQUIBASE_HOME/liquibase --url=jdbc:oracle:thin:@127.0.0.1:1521:XE --username=test --password=test --changeLogFile=changelog.xml --logLevel=info update +$LIQUIBASE_HOME/liquibase --url=jdbc:oracle:thin:@127.0.0.1:1521:XEPDB1 --username=test --password=test --changeLogFile=changelog.xml --logLevel=info update ``` #### Run tests @@ -31,5 +29,4 @@ docker stop morejdbc-oracle ``` For more information: -https://github.com/wnameless/docker-oracle-xe-11g -https://hub.docker.com/r/wnameless/oracle-xe-11g-r2 +https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers diff --git a/readme.md b/readme.md index 2633d7d..2efdbec 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,10 @@ +[![license](https://img.shields.io/badge/License-Apache%202.0-green)](https://www.apache.org/licenses/LICENSE-2.0) +[![build](https://travis-ci.com/dmvolodin/morejdbc.svg?branch=master)](https://travis-ci.com/github/dmvolodin/morejdbc) + + + This project contains helper classes to call stored procedures and functions. The base framework is spring (spring-jdbc). -It is fair-typesafe (no unsafe casts inside). Most compatible database is Oracle. +It is fair-typesafe (no unsafe casts inside). The most compatible database is Oracle. To add the library in Maven: ```xml @@ -18,26 +23,27 @@ For Oracle procedure/function calls you can use NamedJdbcCall. The parameters ar * support default value * support overloading -Consider you have a procedure and a function: +Consider you have a procedure and a function in a package: ```sql -CREATE PROCEDURE test_math( - val1 IN number, - val2 number, - out_sum OUT number, - out_mlt OUT number -) IS - BEGIN - out_sum := val1 + val2; - out_mlt := val1 * val2; - END; - -CREATE FUNCTION get_concat(s1 varchar2, s2 varchar2) - RETURN VARCHAR2 -IS - BEGIN - RETURN s1 || s2; - END; +create or replace package body test_more_jdbc_pkg +as + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number) + is + begin + po_sum := p_number1 + p_number2; + po_mlt := p_number1 * p_number2; + end; + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2 is + begin + return p_string1 || p_string2; + end; +end; ``` You can call it: @@ -52,11 +58,11 @@ private JdbcTemplate jdbcTemplate; ... Out sum = Out.of(INTEGER); Out mlt = Out.of(BIGINT); -jdbcTemplate.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", sum) - .out("out_mlt", mlt) +jdbcTemplate.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", sum) + .out("po_mlt", mlt) ); // sum.get() is 3 (Integer) // mlt.get() is 2L (Long) @@ -68,11 +74,11 @@ or in value-consumer style with lambdas ```java AtomicReference sum = new AtomicReference<>(); AtomicReference mlt = new AtomicReference<>(); -jdbcTemplate.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", INTEGER, sum::set) - .out("out_mlt", BIGINT, mlt::set) +jdbcTemplate.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", INTEGER, sum::set) + .out("po_mlt", BIGINT, mlt::set) ); // sum.get() is 3 (Integer) // mlt.get() is 2L (Long) @@ -81,10 +87,10 @@ jdbcTemplate.execute(call("test_math") For functions: ```java -String result = jdbcTemplate.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", "abc") // note: reordered s1, s2 +String result = jdbcTemplate.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", "def") + .in("p_string1", "abc") // note: reordered p_string1, p_string2 ); -// result is "abcdef" (s1 || s2) +// result is "abcdef" (p_string1 + p_string2) ``` diff --git a/sql/mysql/create-test-user.sql b/sql/mysql/create-test-user.sql new file mode 100644 index 0000000..094ab38 --- /dev/null +++ b/sql/mysql/create-test-user.sql @@ -0,0 +1,4 @@ +create database test; +create user 'test'@'%' identified by 'test'; +grant all privileges on *.* to 'test'@'%'; +flush privileges; diff --git a/sql/mysql/init/create-test-user.sql b/sql/mysql/init/create-test-user.sql deleted file mode 100644 index 7fde26b..0000000 --- a/sql/mysql/init/create-test-user.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE DATABASE test; - -CREATE USER 'test'@'%' - IDENTIFIED BY 'test'; - -GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'; - -FLUSH PRIVILEGES; - diff --git a/sql/mysql/schema-mysql-1.sql b/sql/mysql/schema-mysql-1.sql index 606543c..6f5b8eb 100644 --- a/sql/mysql/schema-mysql-1.sql +++ b/sql/mysql/schema-mysql-1.sql @@ -1,19 +1,14 @@ -- liquibase formatted sql -- changeset seregamorph:FEA-1-create-schema-1 splitStatements:false -CREATE PROCEDURE test_math( - IN val1 int, - IN val2 int, - OUT out_sum int, - OUT out_mlt int -) - BEGIN +delimiter // +create procedure test_math(IN val1 int, IN val2 int, OUT out_sum int, OUT out_mlt int) +begin set out_sum = val1 + val2; set out_mlt = val1 * val2; - END; +end; +// -- changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -CREATE FUNCTION get_concat(s1 varchar(50), s2 varchar(50)) - RETURNS VARCHAR(100) DETERMINISTIC - RETURN concat(s1, s2); - +create function get_concat(s1 varchar(50), s2 varchar(50)) returns varchar(100) + return concat(s1, s2); diff --git a/sql/oracle/create-test-user.sql b/sql/oracle/create-test-user.sql new file mode 100644 index 0000000..7f2ce5e --- /dev/null +++ b/sql/oracle/create-test-user.sql @@ -0,0 +1,2 @@ +create user test identified by test default tablespace users quota 100m on users; +grant connect, resource to test; diff --git a/sql/oracle/init/create-test-user.sql b/sql/oracle/init/create-test-user.sql deleted file mode 100644 index c37c241..0000000 --- a/sql/oracle/init/create-test-user.sql +++ /dev/null @@ -1,16 +0,0 @@ - -CREATE USER test -IDENTIFIED BY test - DEFAULT TABLESPACE users; - -ALTER USER test -quota 100M on users; - -GRANT -CREATE SESSION, -CREATE SEQUENCE, -CREATE TABLE, -CREATE TRIGGER, -CREATE PROCEDURE -TO test; - diff --git a/sql/oracle/schema-oracle-1.sql b/sql/oracle/schema-oracle-1.sql index f6d6933..8ce8067 100644 --- a/sql/oracle/schema-oracle-1.sql +++ b/sql/oracle/schema-oracle-1.sql @@ -1,108 +1,110 @@ --liquibase formatted sql --changeset seregamorph:FEA-1-create-schema-1 -create table TEMP_PK_TRIGGER ( - ID NUMBER not null primary key, - VALUE VARCHAR2(400 char) -); - -create sequence TEMP_PK_TRIGGER_SEQ; - ---changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -create or replace trigger TEMP_PK_TRIGGER_BI - before insert - on TEMP_PK_TRIGGER - for each row - begin - :new.id := temp_pk_trigger_seq.nextval; - end; - ---changeset seregamorph:FEA-1-create-schema-3 splitStatements:false -CREATE PROCEDURE test_math( - val1 IN number, - val2 number, - out_sum OUT number, - out_mlt OUT number -) IS - BEGIN - out_sum := val1 + val2; - out_mlt := val1 * val2; - END; - ---changeset seregamorph:FEA-1-create-schema-4 splitStatements:false -CREATE FUNCTION get_concat(s1 varchar2, s2 varchar2) - RETURN VARCHAR2 -IS - BEGIN - RETURN s1 || s2; - END; - ---changeset seregamorph:FEA-1-create-schema-5 splitStatements:false -CREATE FUNCTION simple_decode(p_str varchar2) - return varchar2 +create table table_with_identity_pk +( + id number generated always as identity (start with 1 increment by 1) primary key, + value varchar2(400 char) +) +/ + +--changeset seregamorph:FEA-1-create-schema-2 +create or replace package test_more_jdbc_pkg +as + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number); + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2; + + function get_simple_decoded_string(p_string varchar2) + return varchar2; + + function get_cursor_from_key_value_as_string(p_key_value_string varchar2) + return sys_refcursor; + + procedure get_cursor_from_key_value_as_string(p_key_value_string varchar2, po_cursor out sys_refcursor); + + function get_two_blobs_concatenated(p_blob1 blob, p_blob2 blob) + return blob; + + procedure calc_sum_of_two_numbers_with_in_out_parameter(p_number1 number, + p_number2 number, + pio_sum in out number); +end; +/ + +--changeset seregamorph:FEA-1-create-schema-3 +create or replace package body test_more_jdbc_pkg as - v_str varchar2(4000 char); - begin - v_str := replace(p_str, '%3D', '='); - v_str := replace(v_str, '%0A', chr(10)); - v_str := replace(v_str, '%0D', chr(13)); - v_str := replace(v_str, '%3B', ';'); - v_str := replace(v_str, '%7C', '|'); - v_str := replace(v_str, '%25', '%'); - - return v_str; - end simple_decode; - ---changeset seregamorph:FEA-1-create-schema-6 splitStatements:false -CREATE FUNCTION get_extras_tab(extra_string varchar2) - return sys_refcursor -is - v_cur sys_refcursor; - begin - open v_cur for - select - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value - from ( - select regexp_substr(extra_string, '[^;]+', 1, level) - as pair - from dual - connect by instr(extra_string, ';', 1, level) > 0 - ); - - return v_cur; - end; - ---changeset seregamorph:FEA-1-create-schema-7 splitStatements:false -CREATE FUNCTION blobs_concat(b1 blob, b2 blob) - return blob -is - b1_copy blob := b1; - begin - dbms_lob.append(b1_copy, b2); - return b1_copy; - end; - ---changeset seregamorph:FEA-1-create-schema-8 splitStatements:false -CREATE PROCEDURE test_in_out(x in number, y number, io_sum in out number) -is - begin - io_sum := x + y + io_sum; - end; - ---changeset seregamorph:FEA-1-create-schema-9 splitStatements:false -CREATE PROCEDURE proc_extras_tab(extra_string varchar2, out_extra_string OUT varchar2, v_cur OUT sys_refcursor) - is -begin - out_extra_string := extra_string; - open v_cur for - select - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value - from ( - select regexp_substr(extra_string, '[^;]+', 1, level) - as pair - from dual - connect by instr(extra_string, ';', 1, level) > 0 - ); + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number) + is + begin + po_sum := p_number1 + p_number2; + po_mlt := p_number1 * p_number2; + end; + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2 is + begin + return p_string1 || p_string2; + end; + + function get_simple_decoded_string(p_string varchar2) + return varchar2 is + v_result varchar2(4000 char); + begin + v_result := replace(p_string, '%3D', '='); + v_result := replace(v_result, '%0A', chr(10)); + v_result := replace(v_result, '%0D', chr(13)); + v_result := replace(v_result, '%3B', ';'); + v_result := replace(v_result, '%7C', '|'); + v_result := replace(v_result, '%25', '%'); + + return v_result; + end; + + function get_cursor_from_key_value_as_string(p_key_value_string varchar2) + return sys_refcursor is + v_result sys_refcursor; + begin + open v_result for + select get_simple_decoded_string(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, + get_simple_decoded_string(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value + from ( + select regexp_substr(p_key_value_string, '[^;]+', 1, level) as pair + from dual + connect by instr(p_key_value_string, ';', 1, level) > 0 + ); + + return v_result; + end; + + procedure get_cursor_from_key_value_as_string(p_key_value_string varchar2, po_cursor out sys_refcursor) + is + begin + po_cursor := get_cursor_from_key_value_as_string(p_key_value_string); + end; + + function get_two_blobs_concatenated(p_blob1 blob, p_blob2 blob) + return blob is + v_result blob := p_blob1; + begin + dbms_lob.append(v_result, p_blob2); + return v_result; + end; + + procedure calc_sum_of_two_numbers_with_in_out_parameter(p_number1 number, + p_number2 number, + pio_sum in out number) + is + begin + pio_sum := p_number1 + p_number2 + pio_sum; + end; end; +/ diff --git a/sql/postgres/create-test-user.sql b/sql/postgres/create-test-user.sql new file mode 100644 index 0000000..2daba82 --- /dev/null +++ b/sql/postgres/create-test-user.sql @@ -0,0 +1,3 @@ +create user test with encrypted password 'test'; +create database test; +grant all privileges on database test to test; diff --git a/sql/postgres/init/create-test-user.sql b/sql/postgres/init/create-test-user.sql deleted file mode 100644 index d884868..0000000 --- a/sql/postgres/init/create-test-user.sql +++ /dev/null @@ -1,3 +0,0 @@ -CREATE USER test WITH ENCRYPTED PASSWORD 'test'; -CREATE DATABASE test; -GRANT ALL PRIVILEGES ON DATABASE test TO test; diff --git a/sql/postgres/schema-postgres-1.sql b/sql/postgres/schema-postgres-1.sql index 4a40fb9..93a7061 100644 --- a/sql/postgres/schema-postgres-1.sql +++ b/sql/postgres/schema-postgres-1.sql @@ -1,28 +1,27 @@ --liquibase formatted sql ---changeset seregamorph:FEA-1-create-schema-1 splitStatements:false -CREATE OR REPLACE FUNCTION hi_lo( - a NUMERIC, - b NUMERIC, - c NUMERIC, - OUT hi NUMERIC, - OUT lo NUMERIC) -AS $$ -BEGIN - hi := GREATEST(a, b, c); - lo := LEAST(a, b, c); -END; $$ -LANGUAGE plpgsql; +--changeset seregamorph:fea-1-create-schema-1 splitstatements:false +create or replace function hi_lo( + a numeric, + b numeric, + c numeric, + out hi numeric, + out lo numeric) +as $$ +begin + hi := greatest(a, b, c); + lo := least(a, b, c); +end; $$ +language plpgsql; ---changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -CREATE OR REPLACE FUNCTION refcursorfunc() - RETURNS refcursor AS $$ -DECLARE +--changeset seregamorph:fea-1-create-schema-2 splitstatements:false +create or replace function refcursorfunc() + returns refcursor as $$ +declare mycurs refcursor; -BEGIN - OPEN mycurs FOR SELECT 1 - UNION SELECT 2; - RETURN mycurs; -END; $$ -LANGUAGE plpgsql; - +begin + open mycurs for select 1 + union select 2; + return mycurs; +end; $$ +language plpgsql; diff --git a/src/test/java/org/morejdbc/H2JdbcCallTest.java b/src/test/java/org/morejdbc/H2JdbcCallTest.java index b7fe191..9ec9c34 100644 --- a/src/test/java/org/morejdbc/H2JdbcCallTest.java +++ b/src/test/java/org/morejdbc/H2JdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @@ -14,26 +14,26 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.SqlTypes.INTEGER; public class H2JdbcCallTest { private Connection connection; + private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { Properties props = TestUtils.propertiesFromString(TestUtils.readString("h2_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); this.jdbc = new JdbcTemplate(dataSource); - jdbc.execute("CREATE ALIAS mult FOR \"org.morejdbc.H2Functions.mult\""); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -47,15 +47,17 @@ public void testPureJdbc() throws SQLException { call.setInt(2, 2); call.setInt(3, 3); call.execute(); - assertEquals(6, call.getInt(1)); } @Test public void testMultOut() { Out out = Out.of(INTEGER); + jdbc.execute(callSql("{? = call mult(?, ?)}") - .out(out).in(2).in(3)); + .out(out) + .in(2) + .in(3)); assertEquals(Integer.valueOf(6), out.get()); } @@ -63,8 +65,11 @@ public void testMultOut() { @Test public void testMultConsumer() { AtomicInteger out = new AtomicInteger(); + jdbc.execute(callSql("{? = call mult(?, ?)}") - .out(INTEGER, out::set).in(2).in(3)); + .out(INTEGER, out::set) + .in(2) + .in(3)); assertEquals(6, out.get()); } diff --git a/src/test/java/org/morejdbc/MockitoCallTest.java b/src/test/java/org/morejdbc/MockitoCallTest.java index f82bf01..8c6379d 100644 --- a/src/test/java/org/morejdbc/MockitoCallTest.java +++ b/src/test/java/org/morejdbc/MockitoCallTest.java @@ -1,6 +1,6 @@ package org.morejdbc; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import java.util.Arrays; @@ -8,15 +8,13 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.NamedJdbcCall.call; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; public class MockitoCallTest { @@ -26,15 +24,12 @@ public void testCallSqlMock() { JdbcTemplate jdbc = mock(JdbcTemplate.class); MockOut sum = MockOut.of(INTEGER); MockOut mlt = MockOut.of(INTEGER); - when(jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(10).in(20).out(sum).out(mlt))).then(invocation -> { + when(jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}").in(10).in(20).out(sum).out(mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200); return null; }); - Result result = serviceCallSql(jdbc, 10, 20); - assertEquals(30, result.sum); assertEquals(200, result.mlt); } @@ -44,18 +39,12 @@ public void testCallNamedMock() { JdbcTemplate jdbc = mock(JdbcTemplate.class); MockOut sum = MockOut.of(INTEGER); MockOut mlt = MockOut.of(BIGINT); - when(jdbc.execute(call("test_math") - .in("val1", 10) - .in("val2", 20) - .out("out_sum", sum) - .out("out_mlt", mlt))).then(invocation -> { + when(jdbc.execute(call("test_math").in("val1", 10).in("val2", 20).out("out_sum", sum).out("out_mlt", mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200L); return null; }); - Result result = serviceCallNamed(jdbc, 10, 20); - assertEquals(30, result.sum); assertEquals(200, result.mlt); } @@ -63,69 +52,52 @@ public void testCallNamedMock() { @Test public void testCallNamedFunctionMock() { JdbcTemplate jdbc = mock(JdbcTemplate.class); - when(jdbc.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", 4))) - .thenReturn("4def"); - + when(jdbc.execute(call("get_concat", VARCHAR).in("s2", "def").in("s1", 4))).thenReturn("4def"); String result = serviceCallNamedFunction(jdbc, 4, "def"); - assertEquals("4def", result); } @Test public void testRefCursorOutParam() { JdbcTemplate jdbc = mock(JdbcTemplate.class); - MockOut>> out = MockOut.of(cursor((rs, rowNum) -> { - return immutableEntry("key", "value"); - })); - when(jdbc.execute(call("proc_extras_tab") - .in("extra_string", "1=value1;2=value2;6=value6;") - .out("v_cur", out))).then(invocation -> { - out.setTo(invocation.getArguments()[0], Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + MockOut>> out = MockOut.of(cursor((rs, rowNum) -> immutableEntry("key", "value"))); + when(jdbc.execute(call("proc_extras_tab").in("extra_string", "1=value1;2=value2;6=value6;").out("v_cur", out))).then(invocation -> { + out.setTo(invocation.getArguments()[0], Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6"))); return null; }); - - List> extras = - serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); - assertEquals(extras, Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + List> extras = serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); } - private static List> serviceTestRefCursorOutParam( - JdbcTemplate jdbc, String extra) { - Out>> outExtras = Out.of(cursor((rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - })); - jdbc.execute(call("proc_extras_tab") - .in("extra_string", extra) - .out("v_cur", outExtras)); + private static List> serviceTestRefCursorOutParam(JdbcTemplate jdbc, String extra) { + Out>> outExtras = Out.of(cursor((rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")))); + jdbc.execute(call("proc_extras_tab").in("extra_string", extra).out("v_cur", outExtras)); return outExtras.get(); } private static Result serviceCallSql(JdbcTemplate jdbc, int val1, int val2) { Out sum = Out.of(INTEGER); Out mlt = Out.of(INTEGER); + jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(val1).in(val2).out(sum).out(mlt)); + .in(val1) + .in(val2) + .out(sum) + .out(mlt)); + return new Result(sum.get(), mlt.get()); } private static Result serviceCallNamed(JdbcTemplate jdbc, int val1, int val2) { AtomicReference sum = new AtomicReference<>(); AtomicReference mlt = new AtomicReference<>(); + jdbc.execute(call("test_math") .in("val1", val1) .in("val2", val2) .out("out_sum", INTEGER, sum::set) .out("out_mlt", BIGINT, mlt::set)); + return new Result(sum.get(), mlt.get()); } diff --git a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java index 9a86e99..d00770d 100644 --- a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java +++ b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @@ -11,7 +11,7 @@ import java.sql.SQLException; import java.util.Properties; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.SqlTypes.INTEGER; @@ -23,7 +23,7 @@ public class MysqlJdbcCallTest { private Connection connection; private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { Properties props = TestUtils.propertiesFromString(TestUtils.readString("mysql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); @@ -31,7 +31,7 @@ public void before() throws SQLException { this.jdbc = new JdbcTemplate(dataSource); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -42,9 +42,13 @@ public void after() throws SQLException { public void testMath() { Out sum = Out.of(INTEGER); Out mlt = Out.of(INTEGER); + jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(10).in(20).out(sum).out(mlt) - ); + .in(10) + .in(20) + .out(sum) + .out(mlt)); + assertEquals(30, sum.get().intValue()); assertEquals(200, mlt.get().intValue()); } diff --git a/src/test/java/org/morejdbc/OracleJdbcCallTest.java b/src/test/java/org/morejdbc/OracleJdbcCallTest.java index 96abce4..50026be 100644 --- a/src/test/java/org/morejdbc/OracleJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.SqlTypeValue; @@ -20,16 +20,10 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.BLOB; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; import static org.morejdbc.TestUtils.jdbc; @@ -38,15 +32,19 @@ */ public class OracleJdbcCallTest { + /** + * the default datatype for null variables is defined as varchar2(32) + */ + private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, StatementCreatorUtils::setParameterValue, CallableStatement::getObject); private Connection connection; private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); Locale def = Locale.getDefault(); try { - // workarond for XE with russian locale + // workaround for XE with russian locale Locale.setDefault(Locale.ENGLISH); this.connection = DriverManager.getConnection(props.getProperty("url"), props); } finally { @@ -55,7 +53,7 @@ public void before() throws SQLException { this.jdbc = jdbc(this.connection); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -66,67 +64,64 @@ public void after() throws SQLException { public void testCall1() { Out sum = Out.of(INTEGER); Out mlt = Out.of(INTEGER); - jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(1).in(2).out(sum).out(mlt) - ); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); - } - /** - * the default datatype for null variables is defined as varchar2(32) - */ - private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, - StatementCreatorUtils::setParameterValue, CallableStatement::getObject); + jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?)}") + .in(8) + .in(3) + .out(sum) + .out(mlt)); + + assertEquals(Integer.valueOf(11), sum.get()); + assertEquals(Integer.valueOf(24), mlt.get()); + } @Test public void testCallBadArgs() { Out sum = Out.of(INTEGER); Out mlt = Out.of(INTEGER); try { - jdbc.execute(callSql("{call test_math(?, ?, ?, ?, ?)}") - .in(1).in(2L).out(sum).out(mlt).in(null, UNKNOWN) - ); + jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?, ?)}") + .in(0) + .in(1L) + .out(sum) + .out(mlt) + .in(null, UNKNOWN)); + fail(); } catch (BadSqlGrammarException e) { // sql via SqlProvider - assertTrue(e.getMessage() - .contains("bad SQL grammar [{call test_math(?, ?, ?, ?, ?)}];")); + assertTrue(e.getMessage().contains("bad SQL grammar [{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?, ?)}];")); } } @Test public void testInsertReturning() { - // temp_pk_trigger (id number(9), value varchar2(20 char)); + // table_with_identity_pk (id number(9) identity primary key, value varchar2(20 char)); // checks charset String valueIn = "ั‚ะตัั‚" + System.currentTimeMillis(); Out idOut = Out.of(BIGINT); AtomicReference valueOut = new AtomicReference<>(); - jdbc.execute(callSql( - "BEGIN INSERT INTO temp_pk_trigger(value) VALUES (?) " + - "RETURNING id, value INTO ?, ?; END;") + + jdbc.execute(callSql("begin insert into table_with_identity_pk (value) values (?) " + "returning id, value into ?, ?; end;") .in(valueIn) - .out(idOut).out(VARCHAR, valueOut::set) - ); + .out(idOut) + .out(VARCHAR, valueOut::set)); + System.out.println(idOut.get()); System.out.println(valueOut.get()); assertTrue(idOut.get() > 0); - assertEquals(valueOut.get(), valueIn); + assertEquals(valueIn, valueOut.get()); } @Test public void testCallFuncResultSet() { - Out>> extras = Out.of(cursor((row, rowNum) -> { - return immutableEntry(row.getString("id"), row.getString("value")); - })); - jdbc.execute(callSql("{? = call get_extras_tab(?)}") - .out(extras).in("1=value1;2=value2;6=value6;") - ); - assertEquals(extras.get(), Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + Out>> extras = Out.of(cursor((row, rowNum) -> immutableEntry(row.getString("id"), row.getString("value")))); + + jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_cursor_from_key_value_as_string(?)}") + .out(extras) + .in("1=value1;2=value2;6=value6;")); + + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras.get()); } @Test @@ -136,13 +131,12 @@ public void testLongBlobConcat() { byte[] blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - Out result = Out.of(BLOB); - jdbc.execute(callSql("{? = call blobs_concat(?, ?)}") + jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_two_blobs_concatenated(?, ?)}") .out(result) - .in(blob1).in(blob2) - ); + .in(blob1) + .in(blob2)); byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result.get()); diff --git a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java index 451976f..9decba9 100644 --- a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlTypeValue; @@ -21,16 +21,11 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.NamedJdbcCall.call; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.BINARY; -import static org.morejdbc.SqlTypes.BLOB; -import static org.morejdbc.SqlTypes.DECIMAL; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; import static org.morejdbc.TestUtils.jdbc; @@ -39,10 +34,16 @@ */ public class OracleNamedJdbcCallTest { + /** + * the default datatype for null variables is defined as varchar2(32) + */ + private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, StatementCreatorUtils::setParameterValue, CallableStatement::getObject); + private Connection connection; + private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); Locale def = Locale.getDefault(); @@ -56,7 +57,7 @@ public void before() throws SQLException { this.jdbc = jdbc(this.connection); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -68,14 +69,14 @@ public void testNamedCall1() { Out sum = Out.of(INTEGER); Out mlt = Out.of(BIGINT); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", sum) - .out("out_mlt", mlt)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", sum) + .out("po_mlt", mlt)); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Long.valueOf(2L)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Long.valueOf(2L), mlt.get()); } @Test @@ -83,14 +84,14 @@ public void testNamedCall1Consumer() { AtomicReference sum = new AtomicReference<>(); AtomicReference mlt = new AtomicReference<>(); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", INTEGER, sum::set) - .out("out_mlt", BIGINT, mlt::set)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", INTEGER, sum::set) + .out("po_mlt", BIGINT, mlt::set)); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Long.valueOf(2L)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Long.valueOf(2L), mlt.get()); } @Test @@ -98,165 +99,151 @@ public void testNamedCall2() { Out sum = Out.of(INTEGER); Out mlt = Out.of(INTEGER); - jdbc.execute(call("test_math") - .out("out_mlt", mlt) - .out("out_sum", sum) - .in("val1", "1") - .in("val2", new BigDecimal(2))); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .out("po_mlt", mlt) + .out("po_sum", sum) + .in("p_number1", "1") + .in("p_number2", new BigDecimal(2))); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testNamedCall3() { - NamedJdbcCall call = call("test_math") - .in("val1", 1) - .in("val2", 2); - Out sum = call.out("out_sum", INTEGER); - Out mlt = call.out("out_mlt", INTEGER); - + NamedJdbcCall call = call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2); + Out sum = call.out("po_sum", INTEGER); + Out mlt = call.out("po_mlt", INTEGER); jdbc.execute(call); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testNamedCallFunc1() { - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s1", "abc") - .in("s2", (String) null)); + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string1", "abc") + .in("p_string2", (String) null)); - assertEquals(result, "abc"); + assertEquals("abc", result); } @Test public void testNamedCallFunc2() { - // reorder s1, s2 - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s2", new StringBuilder("WL-1")) - .in("s1", "abc")); + // reordered p_string1, p_string2 + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", new StringBuilder("XYZ")) + .in("p_string1", "abc")); - assertEquals(result, "abcWL-1"); + assertEquals("abcXYZ", result); } @Test public void testNamedCallFunc3() { - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", 4)); + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", "def") + .in("p_string1", 4)); - assertEquals(result, "4def"); + assertEquals("4def", result); } - /** - * the default datatype for null variables is defined as varchar2(32) - */ - private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, - StatementCreatorUtils::setParameterValue, CallableStatement::getObject); - @Test public void testNamedCallFunc4() { // pass null-string with unknown type - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s1", "abc") - .in("s2", null, UNKNOWN)); + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string1", "abc") + .in("p_string2", null, UNKNOWN)); - assertEquals(result, "abc"); + assertEquals("abc", result); } @Test public void testNamedCallFuncResultSet() { - RowMapper> mapper = (rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - }; - - List> extras = jdbc.execute(call("get_extras_tab", cursor(mapper)) - .in("extra_string", "1=value1;2=value2;6=value6;")); - - assertEquals(extras, Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); + + List> extras = jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string", cursor(mapper)) + .in("p_key_value_string", "1=value1;2=value2;6=value6;")); + + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); } @Test public void testRefCursorOutParam() { - RowMapper> mapper = (rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - }; - Out outExtraString = Out.of(VARCHAR); + RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); Out>> outExtras = Out.of(cursor(mapper)); - jdbc.execute(call("proc_extras_tab") - .in("extra_string", "1=value1;2=value2;6=value6;") - .out("out_extra_string", outExtraString) - .out("v_cur", outExtras)); + jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string") + .in("p_key_value_string", "1=value1;2=value2;6=value6;") + .out("po_cursor", outExtras)); - assertEquals(outExtras.get(), Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), outExtras.get()); } @Test public void testNamedCallInOut1() { Out sum = Out.of(DECIMAL); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", new BigDecimal(5), sum) - .in("y", 2)); - assertEquals(sum.get(), new BigDecimal(8)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", new BigDecimal(5), sum) + .in("p_number2", 2)); + + assertEquals(new BigDecimal(8), sum.get()); } @Test public void testNamedCallInOut2() { Out sum = Out.of(INTEGER); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", 5, sum) - .in("y", 2)); - assertEquals(sum.get(), Integer.valueOf(8)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", 5, sum) + .in("p_number2", 2)); + + assertEquals(Integer.valueOf(8), sum.get()); } @Test public void testNamedCallInOut2Consumer() { AtomicReference sum = new AtomicReference<>(); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", 5, sum::set) - .in("y", 2)); - assertEquals(sum.get(), Integer.valueOf(8)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", 5, sum::set) + .in("p_number2", 2)); + + assertEquals(Integer.valueOf(8), sum.get()); } @Test public void testPackageCallable4arg() { Out sum = Out.of(BIGINT); Out mlt = Out.of(INTEGER); - jdbc.execute(call("test_math") - .out("out_sum", sum) - .in("val1", 1) - .in("val2", 2) - .out("out_mlt", mlt)); - assertEquals(sum.get(), Long.valueOf(3L)); - assertEquals(mlt.get(), Integer.valueOf(2)); + + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .out("po_sum", sum) + .in("p_number1", 1) + .in("p_number2", 2) + .out("po_mlt", mlt)); + + assertEquals(Long.valueOf(3L), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testPackageCallable3arg() { Out sum = Out.of(INTEGER); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2) - .out("out_sum", sum) - .out("out_mlt", Out.of(INTEGER))); - assertEquals(sum.get(), Integer.valueOf(3)); + + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2) + .out("po_sum", sum) + .out("po_mlt", Out.of(INTEGER))); + + assertEquals(Integer.valueOf(3), sum.get()); } @Test @@ -267,9 +254,9 @@ public void testLongBinaryConcat() { ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - byte[] result = jdbc.execute(call("blobs_concat", BINARY) - .in("b1", blob1, BINARY) - .in("b2", blob2, BINARY)); + byte[] result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BINARY) + .in("p_blob1", blob1, BINARY) + .in("p_blob2", blob2, BINARY)); byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); @@ -283,9 +270,9 @@ public void testLongBlobConcat() { ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - byte[] result = jdbc.execute(call("blobs_concat", BLOB) - .in("b1", blob1) - .in("b2", blob2)); + byte[] result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BLOB) + .in("p_blob1", blob1) + .in("p_blob2", blob2)); byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); diff --git a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java index 1857796..e3fb236 100644 --- a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java +++ b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.CallableStatementCallback; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @@ -20,7 +20,7 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.PostgresSqlTypes.cursor; import static org.morejdbc.SqlTypes.NUMERIC; @@ -31,10 +31,12 @@ public class PostgresJdbcCallTest { private Connection connection; + private JdbcTemplate jdbc; + private TransactionTemplate transactionTemplate; - @Before + @BeforeEach public void before() throws SQLException { Properties props = TestUtils.propertiesFromString(TestUtils.readString("psql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); @@ -44,7 +46,7 @@ public void before() throws SQLException { this.jdbc = new JdbcTemplate(dataSource); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -53,16 +55,12 @@ public void after() throws SQLException { @Test public void testSelect() { - List list = jdbc.query( - "SELECT hi, lo FROM hi_lo(?, ?, ?)", - (row, rowNum) -> { - HiLo hl = new HiLo(); - hl.hi = row.getInt("hi"); - hl.lo = row.getInt("lo"); - return hl; - }, - 10, 20, 30 - ); + List list = jdbc.query("SELECT hi, lo FROM hi_lo(?, ?, ?)", (row, rowNum) -> { + HiLo hl = new HiLo(); + hl.hi = row.getInt("hi"); + hl.lo = row.getInt("lo"); + return hl; + }, 10, 20, 30); assertEquals(1, list.size()); HiLo hl = list.get(0); assertEquals(30, hl.hi); @@ -81,7 +79,6 @@ public void testCallExecute() { return cs; }, (CallableStatementCallback) cs -> { cs.execute(); - assertEquals(new BigDecimal(30), cs.getBigDecimal(4)); assertEquals(new BigDecimal(10), cs.getBigDecimal(5)); return null; @@ -92,14 +89,14 @@ public void testCallExecute() { public void testCall() { AtomicReference hi = new AtomicReference<>(); AtomicReference lo = new AtomicReference<>(); - jdbc.execute(callSql( - "{call hi_lo(?, ?, ?, ?, ?)}") + + jdbc.execute(callSql("{call hi_lo(?, ?, ?, ?, ?)}") .in(10) .in(20) .in(30) .out(NUMERIC, hi::set) - .out(NUMERIC, lo::set) - ); + .out(NUMERIC, lo::set)); + assertEquals(30, hi.get().intValue()); assertEquals(10, lo.get().intValue()); } @@ -109,9 +106,9 @@ public void testRefcursor() { // refcursor out works only in transaction List values = transactionTemplate.execute(transaction -> { Out> outValues = Out.of(cursor((row, rowNum) -> row.getInt(1))); - jdbc.execute(callSql("{ ? = call refcursorfunc() }") - .out(outValues) - ); + + jdbc.execute(callSql("{ ? = call refcursorfunc() }").out(outValues)); + return outValues.get(); }); assertEquals(Arrays.asList(1, 2), values); diff --git a/src/test/java/org/morejdbc/TestUtils.java b/src/test/java/org/morejdbc/TestUtils.java index 1341404..465ba98 100644 --- a/src/test/java/org/morejdbc/TestUtils.java +++ b/src/test/java/org/morejdbc/TestUtils.java @@ -4,11 +4,7 @@ import org.springframework.jdbc.datasource.SingleConnectionDataSource; import org.springframework.jdbc.datasource.SmartDataSource; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.io.UncheckedIOException; +import java.io.*; import java.net.URL; import java.sql.Connection; import java.util.AbstractMap; diff --git a/src/test/resources/mysql_test.properties b/src/test/resources/mysql_test.properties index 0648e8c..4cfb4bf 100644 --- a/src/test/resources/mysql_test.properties +++ b/src/test/resources/mysql_test.properties @@ -1,4 +1,4 @@ -url=jdbc:mysql://127.0.0.1:3306/test +url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false user=test password=test serverTimezone=UTC diff --git a/src/test/resources/oracle_test.properties b/src/test/resources/oracle_test.properties index 8c904f6..048d3c8 100644 --- a/src/test/resources/oracle_test.properties +++ b/src/test/resources/oracle_test.properties @@ -1,3 +1,3 @@ -url=jdbc:oracle:thin:@127.0.0.1:1521:XE +url=jdbc:oracle:thin:@//127.0.0.1:1521/XEPDB1 user=test password=test