use of com.ctrip.platform.dal.dao.configure.DataBase in project dal by ctripcorp.
the class DatabaseSelectorTest method isSelectTest.
@Test
public void isSelectTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
DatabaseSelector selector = new DatabaseSelector(null, ms, ss, false, true);
String dbName = selector.select();
Assert.assertTrue(dbName.equals(S1) || dbName.equals(S2));
selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, false, true);
Assert.assertTrue(selector.select().equals(S1));
selector = new DatabaseSelector(new DalHints().inDatabase(S2), ms, ss, false, true);
Assert.assertTrue(selector.select().equals(S2));
selector = new DatabaseSelector(new DalHints().inDatabase(M2), ms, ss, false, true);
assertSelector(selector, M2);
// masterOnly
selector = new DatabaseSelector(null, ms, ss, true, true);
dbName = selector.select();
assertSelector(selector, M2, M1);
selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, true, true);
assertSelector(selector, M1);
selector = new DatabaseSelector(new DalHints().inDatabase(M2), ms, ss, true, true);
assertSelector(selector, M2);
selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, true, true);
assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
}
use of com.ctrip.platform.dal.dao.configure.DataBase in project dal by ctripcorp.
the class DatabaseSelectorTest method hasMixedMasterAndSlaveButMasterMarkdownTest.
@Test
public void hasMixedMasterAndSlaveButMasterMarkdownTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
autoMarkdown(M1);
DatabaseSelector selector = new DatabaseSelector(null, ms, ss, false, true);
String dbName = selector.select();
Assert.assertTrue(dbName.equals(S1) || dbName.equals(S2));
selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, false, true);
assertSelector(selector, ErrorCode.MarkdownConnection);
selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, false, true);
Assert.assertEquals(S1, selector.select());
selector = new DatabaseSelector(new DalHints().inDatabase(S2), ms, ss, false, true);
Assert.assertEquals(S2, selector.select());
// masterOnly
selector = new DatabaseSelector(null, ms, ss, true, true);
assertSelector(selector, ErrorCode.MarkdownConnection);
selector = new DatabaseSelector(null, ms, ss, true, false);
assertSelector(selector, ErrorCode.MarkdownConnection);
selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, true, true);
assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, true, false);
assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
}
use of com.ctrip.platform.dal.dao.configure.DataBase in project dal by ctripcorp.
the class DatabaseSelectorTest method onlyHaveMasterHaTest.
@Test
public void onlyHaveMasterHaTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = null;
DatabaseSelector selector;
Set<String> matched;
matched = newHashSet(M1, M2);
DalHints hints = new DalHints().setHA(new DalHA());
selector = new DatabaseSelector(hints, ms, ss, false, true);
assertSelector(selector, matched);
selector = new DatabaseSelector(hints, ms, ss, false, true);
assertSelector(selector, matched);
// reset
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
selector = new DatabaseSelector(hints, ms, ss, false, false);
assertSelector(selector, matched);
selector = new DatabaseSelector(hints, ms, ss, false, false);
assertSelector(selector, matched);
// Master only
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
selector = new DatabaseSelector(hints, ms, ss, true, true);
assertSelector(selector, M1, M2);
selector = new DatabaseSelector(hints, ms, ss, true, true);
assertSelector(selector, M1, M2);
// reset
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
selector = new DatabaseSelector(hints, ms, ss, true, false);
assertSelector(selector, M1, M2);
selector = new DatabaseSelector(hints, ms, ss, true, false);
assertSelector(selector, M1, M2);
}
use of com.ctrip.platform.dal.dao.configure.DataBase in project dal by ctripcorp.
the class DatabaseSelectorTest method hasHASlavesTest.
@Test
public void hasHASlavesTest() throws DalException {
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
DalHA ha = new DalHA();
ha.addDB(S1);
DatabaseSelector selector = new DatabaseSelector(new DalHints().setHA(ha), null, ss, false, true);
Assert.assertEquals(S2, selector.select());
// masterOnly
ha = new DalHA();
ha.addDB(S1);
selector = new DatabaseSelector(new DalHints().setHA(ha), null, ss, true, true);
assertSelector(selector, ErrorCode.NullLogicDbName);
ha = new DalHA();
ha.addDB(S1);
selector = new DatabaseSelector(new DalHints().setHA(ha), null, ss, true, false);
assertSelector(selector, ErrorCode.NullLogicDbName);
}
use of com.ctrip.platform.dal.dao.configure.DataBase in project dal by ctripcorp.
the class DatabaseSelectorTest method hasMixedMasterAndSlaveButOneMasterMarkdownTest.
@Test
public void hasMixedMasterAndSlaveButOneMasterMarkdownTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
autoMarkdown(M1);
DatabaseSelector selector = new DatabaseSelector(new DalHints(), ms, ss, false, true);
String dbName = selector.select();
Assert.assertTrue(dbName.equals(S1) || dbName.equals(S2));
// masterOnly
selector = new DatabaseSelector(null, ms, ss, true, true);
Assert.assertEquals(M2, selector.select());
selector = new DatabaseSelector(null, ms, ss, true, false);
Assert.assertEquals(M2, selector.select());
selector = new DatabaseSelector(null, ms, ss, false, false);
Assert.assertEquals(M2, selector.select());
}
Aggregations