37
37
import org .firebirdsql .jaybird .xca .FBManagedConnection ;
38
38
import org .junit .jupiter .api .Test ;
39
39
import org .junit .jupiter .api .Timeout ;
40
+ import org .junit .jupiter .api .extension .ExtendWith ;
40
41
import org .junit .jupiter .api .extension .RegisterExtension ;
41
42
import org .junit .jupiter .params .ParameterizedTest ;
42
43
import org .junit .jupiter .params .provider .EnumSource ;
43
44
import org .junit .jupiter .params .provider .ValueSource ;
45
+ import org .mockito .Mock ;
46
+ import org .mockito .junit .jupiter .MockitoExtension ;
44
47
45
48
import java .nio .charset .StandardCharsets ;
46
49
import java .sql .*;
73
76
* @author Roman Rokytskyy
74
77
* @author Mark Rotteveel
75
78
*/
79
+ @ ExtendWith (MockitoExtension .class )
76
80
class FBConnectionTest {
77
81
78
82
private static final String CREATE_TABLE = """
@@ -784,27 +788,23 @@ void legacyAuthUserCannotConnectByDefault() throws Exception {
784
788
785
789
@ Test
786
790
@ Timeout (10 )
787
- void setNetworkTimeout_isUsed () throws Exception {
791
+ void setNetworkTimeout_isUsed (@ Mock ExecutorService executor ) throws Exception {
788
792
assumeThat ("Test assumes pure Java implementation (native doesn't support setNetworkTimeout)" ,
789
793
GDS_TYPE , isPureJavaType ());
790
- ExecutorService executor = Executors .newSingleThreadExecutor ();
791
-
792
- // Workaround for bug with TPBMapper being shared (has conflict with testTpbMapping)
793
- Properties props = getDefaultPropertiesForConnection ();
794
- props .put ("TRANSACTION_READ_COMMITTED" , "read_committed,rec_version,write,wait" );
795
- try (Connection connection1 = getConnectionViaDriverManager ();
796
- Statement statement1 = connection1 .createStatement ();
797
- Connection connection2 = DriverManager .getConnection (getUrl (), props )) {
794
+ try (var connection1 = getConnectionViaDriverManager ();
795
+ var statement1 = connection1 .createStatement ();
796
+ var connection2 = getConnectionViaDriverManager (
797
+ "TRANSACTION_READ_COMMITTED" , "read_committed,rec_version,write,wait" )) {
798
798
executeCreateTable (connection1 , "create table locking (id integer, colval varchar(50))" );
799
799
statement1 .execute ("insert into locking(id, colval) values (1, 'abc')" );
800
800
801
- try (ResultSet rs1 = statement1 .executeQuery ("select id, colval from locking with lock" )) {
801
+ try (var rs1 = statement1 .executeQuery ("select id, colval from locking with lock" )) {
802
802
rs1 .next ();
803
803
804
804
connection2 .setNetworkTimeout (executor , 50 );
805
805
long start = 0 ;
806
- try (Statement statement2 = connection2 .createStatement ();
807
- ResultSet rs2 = statement2 .executeQuery ("select id, colval from locking with lock" )) {
806
+ try (var statement2 = connection2 .createStatement ();
807
+ var rs2 = statement2 .executeQuery ("select id, colval from locking with lock" )) {
808
808
start = System .currentTimeMillis ();
809
809
// Fetch will block waiting for lock held by statement1/rs1 to be released
810
810
SQLException exception = assertThrows (SQLException .class , rs2 ::next );
@@ -818,20 +818,14 @@ void setNetworkTimeout_isUsed() throws Exception {
818
818
}
819
819
820
820
assertTrue (connection2 .isClosed (), "Expected connection2 to be closed by timeout" );
821
- } finally {
822
- executor .shutdown ();
823
821
}
824
822
}
825
823
826
824
@ Test
827
- void setNetworkTimeout_invalidTimeout () throws Exception {
828
- ExecutorService executorService = Executors .newSingleThreadExecutor ();
829
- try (Connection connection = getConnectionViaDriverManager ()) {
830
- SQLException exception = assertThrows (SQLException .class ,
831
- () -> connection .setNetworkTimeout (executorService , -1 ));
825
+ void setNetworkTimeout_invalidTimeout (@ Mock ExecutorService executor ) throws Exception {
826
+ try (var connection = getConnectionViaDriverManager ()) {
827
+ var exception = assertThrows (SQLException .class , () -> connection .setNetworkTimeout (executor , -1 ));
832
828
assertThat (exception , fbMessageStartsWith (JaybirdErrorCodes .jb_invalidTimeout ));
833
- } finally {
834
- executorService .shutdown ();
835
829
}
836
830
}
837
831
@@ -844,42 +838,24 @@ void setNetworkTimeout_invalidExecutor() throws Exception {
844
838
}
845
839
846
840
@ Test
847
- void setNetworkTimeout_getAndSetSeries () throws Exception {
841
+ void setNetworkTimeout_getAndSetSeries (@ Mock ExecutorService executor ) throws Exception {
848
842
assumeThat ("Type is pure Java" , GDS_TYPE , isPureJavaType ());
849
- ExecutorService executorService = Executors .newSingleThreadExecutor ();
850
- try (Connection connection = getConnectionViaDriverManager ()) {
843
+ try (var connection = getConnectionViaDriverManager ()) {
851
844
assertEquals (0 , connection .getNetworkTimeout (), "Expected 0 as initial network timeout" );
852
845
853
- connection .setNetworkTimeout (executorService , 500 );
854
- final CyclicBarrier barrier = new CyclicBarrier (2 );
855
- Runnable waitForBarrier = () -> {
856
- try {
857
- barrier .await (500 , TimeUnit .MILLISECONDS );
858
- } catch (TimeoutException | InterruptedException | BrokenBarrierException e ) {
859
- e .printStackTrace ();
860
- }
861
- };
862
- executorService .execute (waitForBarrier );
863
- barrier .await (500 , TimeUnit .MILLISECONDS );
846
+ connection .setNetworkTimeout (executor , 500 );
864
847
assertEquals (500 , connection .getNetworkTimeout (), "Unexpected getNetworkTimeout" );
865
848
866
- barrier .reset ();
867
- connection .setNetworkTimeout (executorService , 0 );
868
- executorService .execute (waitForBarrier );
869
- barrier .await (500 , TimeUnit .MILLISECONDS );
849
+ connection .setNetworkTimeout (executor , 0 );
870
850
assertEquals (0 , connection .getNetworkTimeout (), "Unexpected getNetworkTimeout" );
871
- } finally {
872
- executorService .shutdown ();
873
851
}
874
852
}
875
853
876
854
@ Test
877
855
void testWireCompression () throws Exception {
878
856
assumeThat ("Test only works with pure java connections" , GDS_TYPE , isPureJavaType ());
879
857
assumeTrue (getDefaultSupportInfo ().supportsWireCompression (), "Test requires wire compression" );
880
- Properties props = getDefaultPropertiesForConnection ();
881
- props .setProperty ("wireCompression" , "true" );
882
- try (Connection connection = DriverManager .getConnection (getUrl (), props )) {
858
+ try (var connection = getConnectionViaDriverManager ("wireCompression" , "true" )) {
883
859
assertTrue (connection .isValid (0 ));
884
860
GDSServerVersion serverVersion =
885
861
connection .unwrap (FirebirdConnection .class ).getFbDatabase ().getServerVersion ();
0 commit comments