JDBCObjectStore implements ObjectStore { @Override public ObjectStoreConnection getConnection(boolean autoCommit, boolean readWrite) { final String caller = "getConnection"; try { PoolableDataSource src = readWrite ? rwSrc : roSrc; JDBCConnection jdbcConn = new JDBCConnection(src.getConnection(), autoCommit); jdbcConn.setOperationTimeout(opTimeout); return jdbcConn; } catch (Exception ex) { if (!readWrite) { return getConnection(autoCommit, true); } throw ZMSUtils.error(ResourceException.SERVICE_UNAVAILABLE, ex.getMessage(), caller); } } JDBCObjectStore(PoolableDataSource rwSrc, PoolableDataSource roSrc); @Override ObjectStoreConnection getConnection(boolean autoCommit, boolean readWrite); @Override void setOperationTimeout(int opTimeout); @Override void clearConnections();  }
@Test public void testGetConnection() throws SQLException { PoolableDataSource mockDataRwSrc = Mockito.mock(PoolableDataSource.class); Connection rwMockConn = Mockito.mock(Connection.class); Mockito.doReturn(rwMockConn).when(mockDataRwSrc).getConnection(); PoolableDataSource mockDataRoSrc = Mockito.mock(PoolableDataSource.class); Connection roMockConn = Mockito.mock(Connection.class); Mockito.doReturn(roMockConn).when(mockDataRoSrc).getConnection(); JDBCObjectStore store = new JDBCObjectStore(mockDataRwSrc, mockDataRoSrc); JDBCConnection jdbcConn = (JDBCConnection) store.getConnection(true, true); assertEquals(jdbcConn.con, rwMockConn); jdbcConn = (JDBCConnection) store.getConnection(true, false); assertEquals(jdbcConn.con, roMockConn); }
@Test public void testGetConnectionReadOnly() throws SQLException { PoolableDataSource mockDataSrc = Mockito.mock(PoolableDataSource.class); Connection mockConn = Mockito.mock(Connection.class); Mockito.doReturn(mockConn).when(mockDataSrc).getConnection(); JDBCObjectStore store = new JDBCObjectStore(null, mockDataSrc); assertNotNull(store.getConnection(true, false)); }
@Test public void testGetConnectionException() throws SQLException { PoolableDataSource mockDataRwSrc = Mockito.mock(PoolableDataSource.class); Mockito.doThrow(new SQLException()).when(mockDataRwSrc).getConnection(); PoolableDataSource mockDataRoSrc = Mockito.mock(PoolableDataSource.class); Mockito.doThrow(new SQLException()).when(mockDataRoSrc).getConnection(); try { JDBCObjectStore store = new JDBCObjectStore(mockDataRwSrc, mockDataRoSrc); store.getConnection(true, true); fail(); } catch (RuntimeException ex) { assertTrue(true); } try { JDBCObjectStore store = new JDBCObjectStore(mockDataRwSrc, mockDataRoSrc); store.getConnection(true, true); fail(); } catch (RuntimeException ex) { assertTrue(true); } }
@Test public void testGetReadWriteConnectionException() throws SQLException { PoolableDataSource mockDataRwSrc = Mockito.mock(PoolableDataSource.class); Connection mockConn = Mockito.mock(Connection.class); Mockito.doReturn(mockConn).when(mockDataRwSrc).getConnection(); PoolableDataSource mockDataRoSrc = Mockito.mock(PoolableDataSource.class); Mockito.doThrow(new SQLException()).when(mockDataRoSrc).getConnection(); JDBCObjectStore store = new JDBCObjectStore(mockDataRwSrc, mockDataRoSrc); JDBCConnection jdbcConn = (JDBCConnection) store.getConnection(true, true); assertEquals(jdbcConn.con, mockConn); jdbcConn = (JDBCConnection) store.getConnection(true, false); assertEquals(jdbcConn.con, mockConn); }