|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.dbcp2; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.stubbing.OngoingStubbing; |
| 22 | + |
| 23 | +import java.sql.Connection; |
| 24 | +import java.sql.Driver; |
| 25 | +import java.sql.SQLException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Properties; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.ArgumentMatchers.isNull; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | +import static org.mockito.Mockito.reset; |
| 37 | + |
| 38 | +public class TestRequestBoundaries { |
| 39 | + Driver driver = mock(TesterDriver.class); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testBeginRequestOneConnection() throws SQLException { |
| 43 | + // Verify JDK version |
| 44 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) >= 53); |
| 45 | + |
| 46 | + // Setup |
| 47 | + BasicDataSource dataSource = getDataSource(driver); |
| 48 | + TesterConnection connection = setupPhysicalConnections(1).get(0); |
| 49 | + |
| 50 | + // Get connection |
| 51 | + dataSource.getConnection(); |
| 52 | + |
| 53 | + // Verify number of calls |
| 54 | + assertCallCount(connection, 1, 0); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testEndRequestOneConnection() throws SQLException { |
| 59 | + // Verify JDK version |
| 60 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) >= 53); |
| 61 | + |
| 62 | + // Setup |
| 63 | + BasicDataSource dataSource = getDataSource(driver); |
| 64 | + TesterConnection connection = setupPhysicalConnections(1).get(0); |
| 65 | + |
| 66 | + // Get then close connection |
| 67 | + dataSource.getConnection().close(); |
| 68 | + |
| 69 | + // Verify number of calls |
| 70 | + assertCallCount(connection, 1, 1); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testBeginRequestTwoVirtualConnections() throws SQLException { |
| 75 | + // Verify JDK version |
| 76 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) >= 53); |
| 77 | + |
| 78 | + // Setup |
| 79 | + BasicDataSource dataSource = getDataSource(driver); |
| 80 | + TesterConnection connection = setupPhysicalConnections(1).get(0); |
| 81 | + |
| 82 | + // Get connection close it then get another connection |
| 83 | + dataSource.getConnection().close(); |
| 84 | + dataSource.getConnection(); |
| 85 | + |
| 86 | + // Verify number calls |
| 87 | + assertCallCount(connection, 2, 1); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testEndRequestTwoVirtualConnections() throws SQLException { |
| 92 | + // Verify JDK version |
| 93 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) >= 53); |
| 94 | + |
| 95 | + // Setup |
| 96 | + BasicDataSource dataSource = getDataSource(driver); |
| 97 | + TesterConnection connection = setupPhysicalConnections(1).get(0); |
| 98 | + |
| 99 | + // Get a connection and close then get another connection and close it |
| 100 | + dataSource.getConnection().close(); |
| 101 | + dataSource.getConnection().close(); |
| 102 | + |
| 103 | + // Verify number of calls |
| 104 | + assertCallCount(connection, 2, 2); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testRequestBoundariesTwoPhysicalConnections() throws SQLException { |
| 109 | + // Verify JDK version |
| 110 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) >= 53); |
| 111 | + |
| 112 | + // Setup |
| 113 | + BasicDataSource dataSource = getDataSource(driver); |
| 114 | + List<TesterConnection> connections = setupPhysicalConnections(2); |
| 115 | + |
| 116 | + // Get a connection, then get another connection, then close the first connection |
| 117 | + Connection fetchedConnection = dataSource.getConnection(); |
| 118 | + dataSource.getConnection(); |
| 119 | + fetchedConnection.close(); |
| 120 | + |
| 121 | + // Verify number of calls |
| 122 | + assertCallCount(connections.get(0), 1, 1); |
| 123 | + assertCallCount(connections.get(1), 1, 0); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testConnectionWithoutRequestBoundaries() throws SQLException { |
| 128 | + // Verify JDK version |
| 129 | + assumeTrue(Double.valueOf(System.getProperty("java.class.version")) < 53); |
| 130 | + |
| 131 | + // Setup |
| 132 | + BasicDataSource dataSource = getDataSource(driver); |
| 133 | + TesterConnection connection = setupPhysicalConnections(1).get(0); |
| 134 | + |
| 135 | + // Get connection |
| 136 | + dataSource.getConnection().close(); |
| 137 | + |
| 138 | + assertCallCount(connection, 0, 0); |
| 139 | + } |
| 140 | + |
| 141 | + public BasicDataSource getDataSource(Driver driver) throws SQLException { |
| 142 | + reset(driver); |
| 143 | + |
| 144 | + BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(new Properties()); |
| 145 | + dataSource.setDriver(driver); |
| 146 | + |
| 147 | + // Before testing the call count of beginRequest and endRequest method we'll make sure that the |
| 148 | + // connectionFactory has been validated which involves creating a physical connection and destroying it. If we |
| 149 | + // don't, it's going to be done automatically when the first connection is requested. This is going to mess the |
| 150 | + // call count. |
| 151 | + validateConnectionFactory(dataSource, driver); |
| 152 | + |
| 153 | + return dataSource; |
| 154 | + } |
| 155 | + |
| 156 | + public void validateConnectionFactory(BasicDataSource dataSource, Driver driver) throws SQLException { |
| 157 | + when(driver.connect(isNull(), any(Properties.class))).thenReturn(getTesterConnection()); |
| 158 | + dataSource.getLogWriter(); |
| 159 | + } |
| 160 | + |
| 161 | + public TesterConnection getTesterConnection() throws SQLException { |
| 162 | + TesterConnection connection = new TesterConnection(null, null); |
| 163 | + |
| 164 | + return connection; |
| 165 | + } |
| 166 | + |
| 167 | + public List<TesterConnection> setupPhysicalConnections(int numOfConnections) throws SQLException { |
| 168 | + List<TesterConnection> listOfConnections = new ArrayList<>(); |
| 169 | + |
| 170 | + for (int i = 0; i < numOfConnections; i++) { |
| 171 | + listOfConnections.add(getTesterConnection()); |
| 172 | + } |
| 173 | + |
| 174 | + OngoingStubbing<Connection> ongoingStubbing = when(driver.connect(isNull(), any(Properties.class))); |
| 175 | + |
| 176 | + for (Connection connection : listOfConnections) { |
| 177 | + ongoingStubbing = ongoingStubbing.thenReturn(connection); |
| 178 | + } |
| 179 | + return listOfConnections; |
| 180 | + } |
| 181 | + |
| 182 | + public void assertCallCount(TesterConnection connection, int expectedBeginRequestCalls, int expectedEndRequestCalls) |
| 183 | + throws SQLException { |
| 184 | + assertEquals(expectedBeginRequestCalls, connection.beginRequestCount.get()); |
| 185 | + assertEquals(expectedEndRequestCalls, connection.endRequestCount.get()); |
| 186 | + } |
| 187 | +} |
0 commit comments