From a9799cf976d6e9575cf795e552a5b98f893557c4 Mon Sep 17 00:00:00 2001 From: justbk <249396768@qq.com> Date: Tue, 5 Nov 2024 14:18:51 +0800 Subject: [PATCH] add common testcase --- .../test_common/src/test/java/BlobTest.java | 91 ++++++++++ .../test_common/src/test/java/ClobTest.java | 170 ++++++++++++++++++ .../src/test/java/ExcptionTest.java | 38 ++++ .../src/test/java/JdbcBatchTest.java | 164 +++++++++++++++++ .../test_common/src/test/java/Simple.java | 61 +++++++ .../common/testcase/BaseJdbcTestCase.java | 145 +++++++++++++++ .../common/testcase/BaseJdbcTestCase2.java | 62 +++++++ .../common/testcase/BaseMyBatisTestCase.java | 67 +++++++ .../test/java/common/utils/SecurityUtils.java | 74 ++++++++ 9 files changed, 872 insertions(+) create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/BlobTest.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/ClobTest.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/ExcptionTest.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/JdbcBatchTest.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/Simple.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase2.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseMyBatisTestCase.java create mode 100644 07_jdbc/jdbc_test/test_common/src/test/java/common/utils/SecurityUtils.java diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/BlobTest.java b/07_jdbc/jdbc_test/test_common/src/test/java/BlobTest.java new file mode 100644 index 0000000..f08e589 --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/BlobTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +import org.opengauss.mapper.BlobMapper; +import org.opengauss.vo.BlobVo; +import common.testcase.BaseMyBatisTestCase; +import common.utils.SecurityUtils; +import org.apache.ibatis.io.Resources; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * description:this for BlobTest Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/11] + * @since 2021/3/11 + */ +public class BlobTest extends BaseMyBatisTestCase { + @Before + public void setUp() throws IOException { + initSession(); + mapper.dropTable(); + mapper.createTable(); + initAutoId(mapper); + } + + @After + public void tearDown() throws SQLException { + mapper.dropTable(); + closeSession(); + } + + @Test + public void queryTest() throws SQLException { + List result = mapper.selectBlobAll(); + assertEquals(0, result.size()); + } + + @Test + public void insertAndQueryTest() throws SQLException { + String testFile = "database.properties"; + File srcFile = null; + try { + srcFile = Resources.getResourceAsFile(testFile); + } catch (IOException e) { + fail("can\'t find file!"); + } + BlobVo blobVo = new BlobVo(); + blobVo.id = autoId.getAndIncrement(); + blobVo.descdata = new String("this is simple test:" + blobVo.id); + blobVo.data = new BlobVo.MyBlob(srcFile.getAbsolutePath()); + int insert = mapper.insertBlobOne(blobVo); + assertEquals(1, insert); + + BlobVo queryBlob = mapper.selectBlobById(blobVo.id); + assertNotNull(queryBlob); + assertEquals(blobVo.id, queryBlob.id); + assertEquals(blobVo.descdata, queryBlob.descdata); + assertTrue(queryBlob.data instanceof BlobVo.MyBlob); + BlobVo.MyBlob queryMyBlob = (BlobVo.MyBlob) queryBlob.data; + String newFile = queryMyBlob.fileName; + File srcNewFile = new File(newFile); + assertEquals(srcNewFile.length(), srcFile.length()); + try { + new SecurityUtils().compareFile(srcFile.getAbsolutePath(), newFile); + } catch (Exception e) { + fail("compare file failed!"); + } + } + + @Override + public BlobMapper getMapper() { + return getMapper(BlobMapper.class); + } + + @Override + public int getMaxId(BlobMapper mapper) { + return mapper.selectMaxId(); + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/ClobTest.java b/07_jdbc/jdbc_test/test_common/src/test/java/ClobTest.java new file mode 100644 index 0000000..76a3439 --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/ClobTest.java @@ -0,0 +1,170 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +import org.opengauss.driver.DriverInfoManager; +import org.opengauss.driver.IDriverInfo; +import common.testcase.BaseJdbcTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.sql.rowset.serial.SerialClob; +import java.io.IOException; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * description:this for BlobTest Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/11] + * @since 2021/3/11 + */ +public class ClobTest extends BaseJdbcTestCase { + private static final String tableName = "t_clob_test"; + IDriverInfo info = DriverInfoManager.getInfo(); + Connection conn = null; + @Before + public void setUp() throws SQLException { + conn = DriverInfoManager.getConnection(info); + dropTable(); + createTable(); + initAutoId(); + } + + @After + public void teardown() throws SQLException { + dropTable(); + conn.close(); + conn = null; + } + + @Test + public void test() throws SQLException, IOException { + String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)"; + Clob data = new SerialClob("abcd".toCharArray()); + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, 0); + ps.setClob(2, data); + ps.execute(); + } + + String query = "select id, data from " + getTableName() + " where id = 0"; + try (Statement st = conn.createStatement()) { + try (ResultSet rs = st.executeQuery(query)) { + while (rs.next()) { + System.out.println("get data"); + int id = rs.getInt(1); + Clob data1 = rs.getClob(2); + System.out.println(data1); + +// PGClob pgClob = (PGClob) data1; +// pgClob.setClob(data1); +// Writer writer = pgClob.setCharacterStream(1); +// writer.write("ddd"); + + } + } + } + } + + @Test + public void testInsertNull() throws SQLException, IOException { + String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)"; + Clob data = null; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, 0); + ps.setClob(2, data); + ps.execute(); + } + + String query = "select id, data from " + getTableName() + " where id = 0"; + try (Statement st = conn.createStatement()) { + try (ResultSet rs = st.executeQuery(query)) { + while (rs.next()) { + System.out.println("get data"); + int id = rs.getInt(1); + Clob data1 = rs.getClob(2); + System.out.println(data1); + } + } + } + } + + @Test + public void testSetObjectNull() throws SQLException, IOException { + String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)"; + Clob data = null; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, 0); + ps.setObject(2, null, Types.CLOB); + ps.execute(); + } + + String query = "select id, data from " + getTableName() + " where id = 0"; + try (Statement st = conn.createStatement()) { + try (ResultSet rs = st.executeQuery(query)) { + while (rs.next()) { + System.out.println("get data"); + int id = rs.getInt(1); + Clob data1 = rs.getClob(2); + System.out.println(data1); + } + } + } + } + + @Test + public void testInsertNullBatch() throws SQLException, IOException { + String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)"; + Object[] inputs = {null, ""}; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, 0); + ps.setObject(2, null, Types.CLOB); + ps.addBatch(); + ps.setInt(1, 1); + ps.setObject(2, ""); + ps.addBatch(); + ps.executeBatch(); + } + + String query = "select id, data from " + getTableName() + " where id = 0"; + try (Statement st = conn.createStatement()) { + try (ResultSet rs = st.executeQuery(query)) { + while (rs.next()) { + System.out.println("get data"); + int id = rs.getInt(1); + Clob data1 = rs.getClob(2); + System.out.println(data1); + } + } + } + } + + @Override + public Connection getConn() { + return conn; + } + + @Override + public String getTableName() { + return tableName; + } + + @Override + public String getTableColumns4Create() { + return "(id int primary key, data clob)"; + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/ExcptionTest.java b/07_jdbc/jdbc_test/test_common/src/test/java/ExcptionTest.java new file mode 100644 index 0000000..b4830de --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/ExcptionTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) @ justbk. 2021-2031. All rights reserved. + */ + +import org.junit.Test; + +/** + * Title: the ExcptionTest class. + *

+ * Description: + * + * @author justbk + * @version [issueManager 0.0.1, 2022/9/16] + * @since 2022/9/16 + */ +public class ExcptionTest { + public static class MyException extends Exception { + @Override + public String toString() { + return "myExp"; + } + } + private void throwExp() throws Exception { + throw new MyException(); + } + @Test + public void test1() throws Exception { + try { + throwExp(); + } catch (MyException exp) { + System.out.println("aaa" + exp); + exp.printStackTrace(); +// } catch (Exception exp) { +// System.out.println("bbb" + exp); +// exp.printStackTrace(); + } + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/JdbcBatchTest.java b/07_jdbc/jdbc_test/test_common/src/test/java/JdbcBatchTest.java new file mode 100644 index 0000000..c93d2e5 --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/JdbcBatchTest.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +import org.opengauss.driver.DriverInfoManager; +import org.opengauss.driver.IDriverInfo; +import org.opengauss.vo.BatchInsertVo; +import common.testcase.BaseJdbcTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +/** + * description:this for JdbcBatchTest Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/15] + * @since 2021/3/15 + */ +public class JdbcBatchTest extends BaseJdbcTestCase { + private static final String tableName = "t_batch_insert"; + IDriverInfo info = DriverInfoManager.getInfo(); + Connection conn = null; + @Before + public void setUp() throws SQLException { + conn = DriverInfoManager.getConnection(info); + dropTable(); + createTable(); + initAutoId(); + } + + @After + public void teardown() throws SQLException { + dropTable(); + conn.close(); + conn = null; + } + + @Test + public void testInsert() throws SQLException { + int insertNum = 10; + for (int i = 0; i < insertNum; i++) { + BatchInsertVo vo = new BatchInsertVo(); + vo.id = getNextId(); + vo.data = "this is test " + vo.id; + executeSql(insertFormat(vo.toInsertData())); + } + assertEquals(insertNum, count()); + } + + @Test + public void testBatchModeOff() throws SQLException { + String sql = insertFormat(BatchInsertVo.toPrepareStmt()); + try (PreparedStatement ps = conn.prepareStatement(sql)) { + for (int i = 0; i < 4; i++) { + ps.setInt(1, i); + ps.setString(2, "aaa"); + ps.addBatch(); + } + int[] rets = ps.executeBatch(); + assertEquals(4, rets.length); + assertArrayEquals(new int[]{1, 1, 1, 1}, rets); + } + } + + + @Test + public void testOneBatchInsert() throws SQLException { + int insertNum = 1000; + testBatchInsert(insertNum); + assertEquals(insertNum, count()); + } + + @Test + public void testManyBatchInsert() throws SQLException { + int n = 10; + int insertNum = 1000; + AtomicInteger taskNumber = new AtomicInteger(n); + for (int i = 0; i < n ; i++) { + new Thread(new Runnable() { + @Override + public void run() { + try { + testBatchInsert(insertNum); + } catch (SQLException throwables) { + System.out.println("insert with error!"); + } finally { + taskNumber.getAndDecrement(); + } + } + }).start(); + } + // in ms + long perWait = 200; + // wait 3 min + long waitTime = (3 * 60 * 1000) / perWait; + while (taskNumber.get() != 0 && waitTime > 0) { + try { + Thread.sleep(perWait); + waitTime -= 1; + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + assertEquals(n * insertNum, count()); + } + + public void testBatchInsert(int insertNum) throws SQLException { + try (Connection conn = DriverInfoManager.getConnection(info)) { + try (PreparedStatement ps = conn.prepareStatement(insertFormat(BatchInsertVo.toPrepareStmt()))) { + for (int i = 0; i < insertNum; i++) { + int id = getNextId(); + ps.setInt(1, id); + ps.setString(2, "this is test = " + id); + ps.addBatch(); + } + ps.executeBatch(); + ps.clearBatch(); + } + } + } + + private String insertFormat(String insertData) { + return String.format(Locale.ENGLISH, "insert into %s values %s", + getTableName(), + insertData); + } + + @Override + public Connection getConn() { + return conn; + } + + @Override + public String getTableName() { + return tableName; + } + + @Override + public String getTableColumns4Create() { + return BatchInsertVo.createColumns(); + } + + public static class BatchInsertVoParse implements ParseResultSet { + @Override + public BatchInsertVo parse(ResultSet rs) throws SQLException { + BatchInsertVo vo = new BatchInsertVo(); + vo.id = rs.getInt(1); + vo.data = rs.getString(2); + return vo; + } + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/Simple.java b/07_jdbc/jdbc_test/test_common/src/test/java/Simple.java new file mode 100644 index 0000000..5d48ddb --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/Simple.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) @ justbk. 2021-2031. All rights reserved. + */ + +import org.junit.Test; +import org.opengauss.util.PGobject; + +import java.awt.SystemTray; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Title: the Simple class. + *

+ * Description: + * + * @author justbk + * @version [issueManager 0.0.1, 2023/2/12] + * @since 2023/2/12 + */ +public class Simple { + private static final char[] ZEROS = {'0', '0', '0', '0', '0', '0', '0', '0', '0'}; + + @Test + public void test12() { + Long aaa = null; + System.out.println("" + aaa); + } + @Test + public void test11() throws SQLException { + List> results = new LinkedList<>(); + PGobject pg1 = new PGobject(); + pg1.setValue("1"); + results.add(new HashMap<>()); + results.add(new HashMap<>()); + results.get(0).put("aaa", pg1); + results.get(0).put("aaa", new Long(1)); + results.get(0).put("aaa", new Long(-1)); + results.get(1).put("ccc", "ddd"); + results.get(1).put("ccc", new Long(-2)); + + results = results.stream().map(curMap -> { + Map newHashMap = new HashMap<>(); + curMap.forEach((k, v) -> { + newHashMap.put(k, v.toString() + "eee"); + }); + return newHashMap; + }).collect(Collectors.toList()); + + results.stream().forEach(map -> { + map.forEach((k,v) -> { + System.out.println("aa" + k + " " + v); + }); + }); + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase.java b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase.java new file mode 100644 index 0000000..7d121f6 --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase.java @@ -0,0 +1,145 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +package common.testcase; + +import org.opengauss.driver.DriverInfoManager; +import org.opengauss.driver.conn.ConnectionProvider; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * description:this for BaseJdbcTestCase Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/16] + * @since 2021/3/16 + */ +public abstract class BaseJdbcTestCase implements ConnectionProvider { + static { + DriverInfoManager.registerDriver(); + } + protected AtomicInteger autoId = null; + protected int[] autoIdLock = new int[0]; + public abstract String getTableName(); + public abstract String getTableColumns4Create(); + + @Override + public Connection getNewConnection() { + return null; + } + + protected void createTable() throws SQLException { + String sql = String.format(Locale.ENGLISH, "create table if not exists %s %s", + getTableName(), + getTableColumns4Create()); + executeSql(sql); + } + + protected void dropTable() throws SQLException { + String sql = String.format(Locale.ENGLISH, "drop table if exists %s", + getTableName()); + executeSql(sql); + } + + public void initAutoId() throws SQLException { + synchronized (autoIdLock) { + int maxId = executeGetMaxId(); + autoId = new AtomicInteger(maxId); + } + } + + public int getNextId() { + return autoId.getAndIncrement(); + } + + protected int executeGetMaxId() throws SQLException { + String sql = String.format(Locale.ENGLISH, + "select case when max(id) is null then 0 else max(id)+1 end as maxId from %s", + getTableName()); + List maxIds = executeAndGetList(sql, new SimpleParseRs()); + return new IntConvert().convert(maxIds.get(0)); + } + + public int count() throws SQLException { + String sql = String.format(Locale.ENGLISH, + "select count(*) from %s", + getTableName()); + return new IntConvert().convert(executeAndGetOne(sql, new SimpleParseRs())); + } + public T executeAndGetOne(String sql, ParseResultSet parse) throws SQLException { + return executeAndGetOne(getConn(), sql, parse); + } + + public T executeAndGetOne(Connection conn, String sql, ParseResultSet parse) throws SQLException { + return executeAndGetList(conn, sql, parse).get(0); + } + public List executeAndGetList(String sql, ParseResultSet parse) throws SQLException { + return executeAndGetList(getConn(), sql, parse); + } + + public List executeAndGetList(Connection conn, String sql, ParseResultSet parse) throws SQLException { + try (PreparedStatement ps = conn.prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + return parse.parseList(rs); + } + } + } + + + protected void executeSql(Connection conn, String sql) throws SQLException { + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.execute(); + } + } + + protected void executeSql(String sql) throws SQLException { + executeSql(getConn(), sql); + } + + public static interface ParseResultSet { + T parse(ResultSet rs) throws SQLException; + default List parseList(ResultSet rs) throws SQLException { + List results = new ArrayList<>(1); + while (rs.next()) { + results.add(parse(rs)); + } + return results; + } + } + + public static class SimpleParseRs implements ParseResultSet { + private Object parseResult = null; + private boolean isParse = false; + @Override + public Object parse(ResultSet rs) throws SQLException { + if (isParse) { + return parseResult; + } + isParse = true; + parseResult = rs.getObject(1); + return parseResult; + } + } + + public static class IntConvert { + public Integer convert(Object obj) throws SQLException { + if (obj instanceof Integer) { + return (Integer) obj; + } + if (obj instanceof Long) { + return ((Long) obj).intValue(); + } + throw new SQLException("type not match!"); + } + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase2.java b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase2.java new file mode 100644 index 0000000..2bb51cd --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseJdbcTestCase2.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) Huawei Technologies Co.,Ltd. 2023. All rights reserved. + */ +package common.testcase; + +import org.opengauss.driver.DriverInfoManager; +import org.opengauss.driver.IDriverInfo; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * Title: the BaseJdbcTestCase2 class. + *

+ * Description: + * + * @author justbk + * @version [Tools 0.0.1, 2023/7/21] + * @since 2023/7/21 + */ +public class BaseJdbcTestCase2 extends BaseJdbcTestCase { + protected IDriverInfo driverInfo; + protected Connection conn; + public BaseJdbcTestCase2() { + super(); + driverInfo = getDriverInfo(); + } + @Override + public Connection getConn() { + if (conn == null) { + conn = getNewConnection(); + } + return conn; + } + + public String getInfoKey() { + return DriverInfoManager.instance.getActivity(); + } + + protected IDriverInfo getDriverInfo() { + return DriverInfoManager.getInfo(getInfoKey()); + } + @Override + public Connection getNewConnection() { + try { + return DriverInfoManager.getConnection(driverInfo); + } catch (SQLException sqlException) { + sqlException.printStackTrace(); + } + return null; + } + + @Override + public String getTableName() { + return this.getClass().getName().replace(".", "_") + "_jdbc"; + } + + @Override + public String getTableColumns4Create() { + return null; + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseMyBatisTestCase.java b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseMyBatisTestCase.java new file mode 100644 index 0000000..7b1a7d7 --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/common/testcase/BaseMyBatisTestCase.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +package common.testcase; + +import org.opengauss.driver.DriverInfoManager; +import org.opengauss.driver.IDriverInfo; +import org.opengauss.service.FactoryInstance; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * description:this for BaseMyBatisTestCase Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/16] + * @since 2021/3/16 + */ +public abstract class BaseMyBatisTestCase { + protected int[] autoIdCreateLock = new int[0]; + protected AtomicInteger autoId = null; + protected SqlSessionFactory factory = null; + protected SqlSession sqlSession; + protected T mapper; + + public T getMapper() { + return mapper; + } + public abstract int getMaxId(T mapper); + + protected IDriverInfo getDriverInfo() { + return DriverInfoManager.getInfo(); + } + + public T getMapper(Class clz) { + return sqlSession.getMapper(clz); + } + protected void initSession() { + try { + factory = FactoryInstance.INSTANCE.getSqlSessionFactory(getDriverInfo()); + sqlSession = factory.openSession(true); + mapper = getMapper(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + protected void closeSession() { + sqlSession.close(); + } + + public void initAutoId(T mapper) { + synchronized (autoIdCreateLock) { + int maxId = getMaxId(mapper); + autoId = new AtomicInteger(maxId); + } + } + + public int getNextId() { + return autoId.getAndIncrement(); + } +} diff --git a/07_jdbc/jdbc_test/test_common/src/test/java/common/utils/SecurityUtils.java b/07_jdbc/jdbc_test/test_common/src/test/java/common/utils/SecurityUtils.java new file mode 100644 index 0000000..b8b0f9f --- /dev/null +++ b/07_jdbc/jdbc_test/test_common/src/test/java/common/utils/SecurityUtils.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved. + */ + +package common.utils; + +import java.io.*; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Locale; + +/** + * description:this for SecurityUtils Class + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. + * + * @author Administrator + * @version [openGauss_debug 0.0.1 2021/3/16] + * @since 2021/3/16 + */ +public class SecurityUtils { + public void compareFile(String file1, String file2) throws Exception { + File f1 = new File(file1); + if (!f1.exists()) { + throw new Exception("file1:" + file1 + " not exist!"); + } + File f2 = new File(file2); + if (!f2.exists()) { + throw new Exception("file2:" + file2 + " not exist!"); + } + if (f1.length() != f2.length()) { + throw new Exception(String.format(Locale.ENGLISH, + "file size not equal,file1:%d, file2:%d", + f1.length(), + f2.length())); + } + if (f1.length() > 30 * 1024 * 1024) { + throw new Exception("file size limit <= 30M"); + } + String file1Sha256 = sha256File(f1); + String file2Sha256 = sha256File(f2); + if (!file1Sha256.equals(file2Sha256)) { + throw new Exception(String.format(Locale.ENGLISH, + "file not equal:\r\nfile1 sha256=%s\r\nfile2 sha256=%s\r\n", + file1Sha256, + file2Sha256)); + } + } + + protected String sha256File(File file) throws IOException, NoSuchAlgorithmException { + byte[] fileBytes = new byte[(int)file.length()]; + try (InputStream in = new FileInputStream(file)) { + in.read(fileBytes); + } + return byte2HexString(sha256(fileBytes)); + } + + public byte[] sha256(byte[] bytes) throws NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(bytes); + return md.digest(); + } + + public String byte2HexString(byte[] input) throws UnsupportedEncodingException { + StringBuffer sb = new StringBuffer(512); + for (int i = 0, n = input.length; i < n; i++) { + String hex = Integer.toHexString(input[i] & 0xff); + if (hex.length() == 1) { + sb.append('0'); + } + sb.append(hex); + } + return sb.toString(); + } +} -- Gitee