Example usage for org.springframework.jdbc.datasource DelegatingDataSource DelegatingDataSource

List of usage examples for org.springframework.jdbc.datasource DelegatingDataSource DelegatingDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource DelegatingDataSource DelegatingDataSource.

Prototype

public DelegatingDataSource() 

Source Link

Document

Create a new DelegatingDataSource.

Usage

From source file:com.jolbox.bonecp.spring.TestDynamicDataSourceProxy.java

/**
 * Test method for {@link com.jolbox.bonecp.spring.DynamicDataSourceProxy#switchDataSource(com.jolbox.bonecp.BoneCPConfig)}.
 * @throws SQLException //from   www  .  j  ava  2s .  com
 * @throws ClassNotFoundException 
 */
@Test
public void testSwitchDataSource() throws SQLException, ClassNotFoundException {

    BoneCPDataSource mockDataSource = createNiceMock(BoneCPDataSource.class);
    DynamicDataSourceProxy ddsp = new DynamicDataSourceProxy();
    // Test #1: Check for correct instance.
    ddsp.setTargetDataSource(new DelegatingDataSource());
    try {
        ddsp.switchDataSource(null);
        fail("Should throw an exception");
    } catch (SQLException e) {
        // do nothing
    }

    // Test #2: Given a good config, should initialize pool and switch datasource to it
    BoneCPConfig config = new BoneCPConfig();
    config.setJdbcUrl("jdbc:mock");
    config.setUsername("sa");
    config.setPassword("");
    config.setMinConnectionsPerPartition(2);
    config.setMaxConnectionsPerPartition(2);
    config.setPartitionCount(1);

    ddsp = new DynamicDataSourceProxy(mockDataSource);

    // Old datasource should be closed.
    mockDataSource.close();
    expectLastCall().once();
    replay(mockDataSource);
    ddsp.switchDataSource(config);
    // and a new datasource should be in place
    assertNotSame(mockDataSource, ddsp.getTargetDataSource());
    verify(mockDataSource);
}