@BeforeClass public static void setUpBeforeClass() throws Exception { tgds = new TGroupDataSource(); tgds.setDbGroupKey("dbKey0"); List<DataSourceWrapper> dataSourceWrappers = new ArrayList<DataSourceWrapper>(); DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "rw", db1, DBType.MYSQL); DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r", db2, DBType.MYSQL); dataSourceWrappers.add(dsw1); dataSourceWrappers.add(dsw2); tgds.init(dataSourceWrappers); }
@Test public void testExecuteUpdate() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("update xxxx set name = 'newname'"); int affect = stat.executeUpdate(); Assert.assertEquals(affect, 1); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testExecuteQuery() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("select * from xxx where id=?"); stat.setByte(1, (byte) 5); MockDataSource.addPreData("id:1,name:2"); ResultSet result = stat.executeQuery(); Assert.assertEquals(result.next(), true); Assert.assertEquals(result.getLong(1), 1L); Assert.assertEquals(result.getLong(2), 2L); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
/** 同一个PreparedStatement先查询后更新 */ @Test public void testExecute2() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("select * from xxx where id=?"); stat.setByte(1, (byte) 5); boolean res = stat.execute(); Assert.assertEquals(res, true); res = stat.execute("update t set name = 'newName'"); Assert.assertEquals(res, false); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testAddBatchSql() throws SQLException { TGroupConnection conn = null; Statement stat = null; try { conn = tgds.getConnection(); stat = conn.createStatement(); stat.addBatch("update t set name = 'newName' "); stat.addBatch("update t set type = 2 "); int[] affectedRow = stat.executeBatch(); System.out.println(Arrays.toString(affectedRow)); MockDataSource.showTrace(); Assert.assertTrue(MockDataSource.hasMethod("db", "db1", "executeBatch")); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testAddBatch() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("update test set type=? where id = ?"); stat.setInt(1, 1); stat.setString(2, "2askjfoue33"); stat.addBatch(); stat.setInt(1, 2); stat.setString(2, "retrtorut48"); stat.addBatch(); int[] affectedRow = stat.executeBatch(); System.out.println(Arrays.toString(affectedRow)); MockDataSource.showTrace(); Assert.assertTrue(MockDataSource.hasMethod("db", "db1", "executeBatch")); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
protected static void setMatrixMockInfo(String groupsPath, String key, boolean isOracle) throws Exception { // -----------------获取groups信息 String groupsStr = LoadPropsUtil.loadProps2OneLine(groupsPath, key); if (groupsStr == null || StringUtils.isBlank(groupsStr)) { throw new Exception("指定path = " + groupsPath + ",key = " + key + "的groups信息为null或者为空字符。"); } // -----------------oracle or mysql String groupPath = BaseAtomGroupTestCase.GROUP_PATH; String atomPath = BaseAtomGroupTestCase.ATOM_PATH; if (isOracle) { groupPath = BaseAtomGroupTestCase.GROUP_ORA_PATH; atomPath = BaseAtomGroupTestCase.ATOM_ORA_PATH; } // -----------------获取group信息 BaseAtomGroupTestCase.dataMap = new HashMap<String, String>(); String[] groupArr = groupsStr.split(","); for (String group : groupArr) { group = group.trim(); String groupStr = ""; groupStr = LoadPropsUtil.loadProps2OneLine( groupPath + group + BaseAtomGroupTestCase.PROPERTIES_FILE, group); if (groupsStr != null && StringUtils.isNotBlank(groupsStr)) { // 获取atom信息 String[] atomArr = groupStr.split(","); for (String atom : atomArr) { atom = atom.trim(); atom = atom.substring(0, atom.indexOf(":")); BaseAtomGroupTestCase.initAtomConfig( atomPath + atom, BaseAtomGroupTestCase.APPNAME, atom); } // 获取groupkey BaseAtomGroupTestCase.dataMap.put(TGroupDataSource.getFullDbGroupKey(group), groupStr); } } // -----------------dbgroups BaseAtomGroupTestCase.dataMap.put( new MessageFormat("com.taobao.tddl.v1_{0}_dbgroups") .format(new Object[] {BaseAtomGroupTestCase.APPNAME}), groupsStr); // 建立MockServer MockServer.setConfigInfos(BaseAtomGroupTestCase.dataMap); }
@Test public void java_sql_Statement_api_support() throws Exception { TGroupConnection conn = null; Statement stmt = null; try { conn = tgds.getConnection(); stmt = conn.createStatement(); String insertSQL = "insert into crud(f1,f2) values(10,'str')"; String updateSQL = "update crud set f2='str2'"; String selectSQL = "select * from crud"; String showSQL = "show create table crud"; assertFalse(stmt.execute(insertSQL)); assertTrue(stmt.execute(selectSQL)); assertTrue(stmt.execute(showSQL)); assertFalse(stmt.execute(insertSQL, Statement.RETURN_GENERATED_KEYS)); assertFalse(stmt.execute(insertSQL, new int[] {1})); assertFalse(stmt.execute(insertSQL, new String[] {"col"})); stmt.addBatch(insertSQL); stmt.addBatch(updateSQL); int[] updateCounts = stmt.executeBatch(); assertEquals(updateCounts.length, 2); MockDataSource.addPreData("id:1,name:2"); assertTrue(stmt.executeQuery(selectSQL).next()); assertEquals(stmt.executeUpdate(insertSQL), 1); assertEquals(stmt.executeUpdate(insertSQL, Statement.RETURN_GENERATED_KEYS), 1); assertEquals(stmt.executeUpdate(insertSQL, new int[] {1}), 1); assertEquals(stmt.executeUpdate(insertSQL, new String[] {"col"}), 1); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { } } } } }