Skip to content

Commit

Permalink
apache#1238 Fill BackendConnectionTest.assertGetConnectionCacheIsEmpty.
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrylzhao authored and maxiaoguang64 committed Nov 25, 2018
1 parent f67d676 commit 1e0be82
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -42,21 +45,26 @@ public class BackendConnectionTest {
@Mock
private LogicSchema logicSchema;

@Mock
private JDBCBackendDataSource backendDataSource;

private BackendConnection backendConnection = new BackendConnection();

@Before
@SuppressWarnings("unchecked")
@SneakyThrows
public void setup() {
List<Connection> newConnection = mock(List.class);
JDBCBackendDataSource backendDataSource = mock(JDBCBackendDataSource.class);
when(backendDataSource.getConnections((ConnectionMode) any(), anyString(), anyInt())).thenReturn(newConnection);
when(logicSchema.getBackendDataSource()).thenReturn(backendDataSource);
backendConnection.setLogicSchema(logicSchema);
}

@Test
@SneakyThrows
public void assertGetConnectionCacheIsEmpty() {

when(backendDataSource.getConnections((ConnectionMode) any(), anyString(), eq(2))).thenReturn(mockNewConnections(2));
List<Connection> actualConnections = backendConnection.getConnections(ConnectionMode.MEMORY_STRICTLY, "ds1", 2);
assertThat(actualConnections.size(), is(2));
assertThat(backendConnection.getConnectionSize(), is(2));
}

@Test
Expand All @@ -78,4 +86,13 @@ public void assertGetConnectionSizeIsOne() {
public void assertMultiThreadGetConnection() {

}

private List<Connection> mockNewConnections(final int connectionSize) {
List<Connection> result = new ArrayList<>();
for (int i = 0; i < connectionSize; i++) {
Connection connection = mock(Connection.class);
result.add(connection);
}
return result;
}
}

0 comments on commit 1e0be82

Please sign in to comment.