move datasource routing example to spring-jpa module (#3076)
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
package org.baeldung.dsrouting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class)
|
||||
public class DataSourceRoutingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
DataSource routingDatasource;
|
||||
|
||||
@Autowired
|
||||
ClientService clientService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
|
||||
final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";
|
||||
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(routingDatasource);
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
|
||||
jdbcTemplate.execute(SQL_CLIENT_A);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
|
||||
jdbcTemplate.execute(SQL_CLIENT_B);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
||||
|
||||
// test ACME WIDGETS
|
||||
String clientName = clientService.getClientName(ClientDatabase.CLIENT_A);
|
||||
assertEquals(clientName, "CLIENT A");
|
||||
|
||||
// test WIDGETS_ARE_US
|
||||
clientName = clientService.getClientName(ClientDatabase.CLIENT_B);
|
||||
assertEquals(clientName, "CLIENT B");
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.baeldung.dsrouting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceRoutingTestConfiguration {
|
||||
|
||||
@Bean
|
||||
public ClientService clientService() {
|
||||
return new ClientService(new ClientDao(clientDatasource()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource clientDatasource() {
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
DataSource clientADatasource = clientADatasource();
|
||||
DataSource clientBDatasource = clientBDatasource();
|
||||
targetDataSources.put(ClientDatabase.CLIENT_A, clientADatasource);
|
||||
targetDataSources.put(ClientDatabase.CLIENT_B, clientBDatasource);
|
||||
|
||||
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
||||
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
||||
clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
|
||||
return clientRoutingDatasource;
|
||||
}
|
||||
|
||||
private DataSource clientADatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("CLIENT_A")
|
||||
.addScript("classpath:dsrouting-db.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
private DataSource clientBDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("CLIENT_B")
|
||||
.addScript("classpath:dsrouting-db.sql")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user