From b966dc67f128e663e29dbb9235b29a582f9caa66 Mon Sep 17 00:00:00 2001 From: BrucePang Date: Mon, 20 May 2024 16:11:51 +0800 Subject: [PATCH] [improve]: update test cases, update README.md --- java/spring-jdbc/README-CN.md | 112 ++++++++++------- java/spring-jdbc/README.md | 116 +++++++++++------- .../OceanBaseSpringJdbcApplicationTest.java | 32 +---- 3 files changed, 143 insertions(+), 117 deletions(-) diff --git a/java/spring-jdbc/README-CN.md b/java/spring-jdbc/README-CN.md index dd7a239..16fc932 100644 --- a/java/spring-jdbc/README-CN.md +++ b/java/spring-jdbc/README-CN.md @@ -57,59 +57,81 @@ public class OceanBaseSpringJdbcApplicationTest { 3.编写测试方法,执行 SQL 语句。 ```java -// MySQL Type Create Table -@Test -public void createByMySQLTypeDate(){ - // MySQL Create Table - sql ="CREATE TABLE D_DPRECORD(DEV_ID VARCHAR(50),"+ - "CAR_SPEED INT(3),"+ - "CAP_DATE TIMESTAMP," + - "DEV_CHNID VARCHAR(50) not null," + - "TRSFMARK INT(1) DEFAULT 0," + - "CREATE_TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + - ");"; - jdbcTemplate.execute(sql); -} +package com.oceanbase.samples; +import com.alibaba.druid.pool.DruidDataSourceFactory; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; -// MySQL/Oracle Type Add Test Data -@Test -public void addTest(){ - int i = 1; - for (;i<=100;i++){ - sql = "INSERT INTO D_DPRECORD VALUES " + - "('DEV_ID"+i+"',"+i+",'2021-01-01 00:00:00','DEV_CHNID"+i+"',"+i+",'2021-01-01 00:00:00');"; - jdbcTemplate.execute(sql); +/** + * OceanBaseSpringJdbcApplication 简单单元测试 + * Unit test for simple OceanBaseSpringJdbcApplication. + */ +public class OceanBaseSpringJdbcApplicationTest +{ + private static JdbcTemplate jdbcTemplate; + private String sql; + static { + Map map = new HashMap(); + map.put("url", "jdbc:mysql://localhost:2881/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"); + map.put("driverClassName", "com.mysql.cj.jdbc.Driver"); + map.put("username", "root@test"); + map.put("password", ""); + try { + Class.forName(map.get("driverClassName")); + jdbcTemplate = new JdbcTemplate(DruidDataSourceFactory.createDataSource(map)); + //防止异常语句,没有这两句,会出错(Prevent abnormal statements, without which errors will occur) + jdbcTemplate.execute("set transaction_isolation = 'READ-COMMITTED';"); // MySQL 8.0 之后,系统变量 tx_isolation 被更改为 transaction_isolation (After MySQL 8.0, the system variable tx_isolation was changed to transaction_isolation) + // jdbcTemplate.execute("set tx_isolation = 'READ-COMMITTED';"); // MySQL 8.0 之前的版本使用 tx_isolation (tx_isolation is used in versions before MySQL 8.0) + } catch (Exception e) { + e.printStackTrace(); + } } -} -// MySQL/Oracle Type Query Test Data -@Test -public void queryTest(){ - sql = "SELECT * FROM D_DPRECORD;"; - jdbcTemplate.queryForList(sql).forEach(System.out::println); -} + @Before + public void initializeDatabase() { + Resource initSchema = new ClassPathResource("init.sql"); + try { + // Execute init.sql script to create and populate the database + ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), initSchema); + } catch (Exception e) { + throw new RuntimeException("Failed to execute init.sql script", e); + } + } -// MySQL/Oracle Type Update Test Data -@Test -public void updateTest(){ - sql = "UPDATE D_DPRECORD SET CAR_SPEED = 100 WHERE DEV_ID = 'DEV_ID1';"; - jdbcTemplate.execute(sql); -} -// MySQL/Oracle Type Delete Test Data -@Test -public void deleteTest(){ - sql = "DELETE FROM D_DPRECORD WHERE DEV_ID = 'DEV_ID1';"; - jdbcTemplate.execute(sql); -} + // insert + @Test + public void testInsert() { + String sql = "INSERT INTO staff (name) VALUES (?)"; + int rowsAffected = jdbcTemplate.update(sql, "New Staff"); + System.out.println("rowsAffected: " + rowsAffected); + } -// MySQL/Oracle Type Drop Table -@Test -public void dropTable(){ - sql = "DROP TABLE D_DPRECORD;"; - jdbcTemplate.execute(sql); + // select + @Test + public void testSelect() { + String sql = "SELECT * FROM staff WHERE name = ?"; + RowMapper rowMapper = new RowMapper() { + @Override + public String mapRow(ResultSet rs, int rowNum) throws SQLException { + return rs.getString("name"); + } + }; + List names = jdbcTemplate.query(sql, rowMapper, "New Staff"); + System.out.println("names: " + names); + } } ``` diff --git a/java/spring-jdbc/README.md b/java/spring-jdbc/README.md index 4ae4bd1..46813eb 100644 --- a/java/spring-jdbc/README.md +++ b/java/spring-jdbc/README.md @@ -62,59 +62,83 @@ public class OceanBaseSpringJdbcApplicationTest { ```java // MySQL Type Create Table -@Test -public void createByMySQLTypeDate(){ - // MySQL Create Table - sql ="CREATE TABLE D_DPRECORD(DEV_ID VARCHAR(50),"+ - "CAR_SPEED INT(3),"+ - "CAP_DATE TIMESTAMP," + - "DEV_CHNID VARCHAR(50) not null," + - "TRSFMARK INT(1) DEFAULT 0," + - "CREATE_TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + - ");"; - jdbcTemplate.execute(sql); -} - - +package com.oceanbase.samples; + +import com.alibaba.druid.pool.DruidDataSourceFactory; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.datasource.init.ScriptUtils; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * OceanBaseSpringJdbcApplication 简单单元测试 + * Unit test for simple OceanBaseSpringJdbcApplication. + */ +public class OceanBaseSpringJdbcApplicationTest +{ + private static JdbcTemplate jdbcTemplate; + private String sql; + static { + Map map = new HashMap(); + map.put("url", "jdbc:mysql://localhost:2881/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"); + map.put("driverClassName", "com.mysql.cj.jdbc.Driver"); + map.put("username", "root@test"); + map.put("password", ""); + try { + Class.forName(map.get("driverClassName")); + jdbcTemplate = new JdbcTemplate(DruidDataSourceFactory.createDataSource(map)); + //防止异常语句,没有这两句,会出错(Prevent abnormal statements, without which errors will occur) + jdbcTemplate.execute("set transaction_isolation = 'READ-COMMITTED';"); // MySQL 8.0 之后,系统变量 tx_isolation 被更改为 transaction_isolation (After MySQL 8.0, the system variable tx_isolation was changed to transaction_isolation) + // jdbcTemplate.execute("set tx_isolation = 'READ-COMMITTED';"); // MySQL 8.0 之前的版本使用 tx_isolation (tx_isolation is used in versions before MySQL 8.0) + } catch (Exception e) { + e.printStackTrace(); + } + } -// MySQL/Oracle Type Add Test Data -@Test -public void addTest(){ - int i = 1; - for (;i<=100;i++){ - sql = "INSERT INTO D_DPRECORD VALUES " + - "('DEV_ID"+i+"',"+i+",'2021-01-01 00:00:00','DEV_CHNID"+i+"',"+i+",'2021-01-01 00:00:00');"; - jdbcTemplate.execute(sql); + @Before + public void initializeDatabase() { + Resource initSchema = new ClassPathResource("init.sql"); + try { + // Execute init.sql script to create and populate the database + ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), initSchema); + } catch (Exception e) { + throw new RuntimeException("Failed to execute init.sql script", e); + } } -} -// MySQL/Oracle Type Query Test Data -@Test -public void queryTest(){ - sql = "SELECT * FROM D_DPRECORD;"; - jdbcTemplate.queryForList(sql).forEach(System.out::println); -} -// MySQL/Oracle Type Update Test Data -@Test -public void updateTest(){ - sql = "UPDATE D_DPRECORD SET CAR_SPEED = 100 WHERE DEV_ID = 'DEV_ID1';"; - jdbcTemplate.execute(sql); -} + // insert + @Test + public void testInsert() { + String sql = "INSERT INTO staff (name) VALUES (?)"; + int rowsAffected = jdbcTemplate.update(sql, "New Staff"); + System.out.println("rowsAffected: " + rowsAffected); + } -// MySQL/Oracle Type Delete Test Data -@Test -public void deleteTest(){ - sql = "DELETE FROM D_DPRECORD WHERE DEV_ID = 'DEV_ID1';"; - jdbcTemplate.execute(sql); + // select + @Test + public void testSelect() { + String sql = "SELECT * FROM staff WHERE name = ?"; + RowMapper rowMapper = new RowMapper() { + @Override + public String mapRow(ResultSet rs, int rowNum) throws SQLException { + return rs.getString("name"); + } + }; + List names = jdbcTemplate.query(sql, rowMapper, "New Staff"); + System.out.println("names: " + names); + } } -// MySQL/Oracle Type Drop Table -@Test -public void dropTable(){ - sql = "DROP TABLE D_DPRECORD;"; - jdbcTemplate.execute(sql); -} ``` Modify the connection info in code, and use `run.sh` to run the example code. diff --git a/java/spring-jdbc/src/test/java/com/oceanbase/samples/OceanBaseSpringJdbcApplicationTest.java b/java/spring-jdbc/src/test/java/com/oceanbase/samples/OceanBaseSpringJdbcApplicationTest.java index 19ac408..c33e872 100644 --- a/java/spring-jdbc/src/test/java/com/oceanbase/samples/OceanBaseSpringJdbcApplicationTest.java +++ b/java/spring-jdbc/src/test/java/com/oceanbase/samples/OceanBaseSpringJdbcApplicationTest.java @@ -1,7 +1,6 @@ package com.oceanbase.samples; import com.alibaba.druid.pool.DruidDataSourceFactory; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.core.io.ClassPathResource; @@ -53,17 +52,17 @@ public void initializeDatabase() { } - // 创建(Create)操作的单元测试 + // insert @Test - public void testCreate() { + public void testInsert() { String sql = "INSERT INTO staff (name) VALUES (?)"; int rowsAffected = jdbcTemplate.update(sql, "New Staff"); - Assert.assertEquals(1, rowsAffected); + System.out.println("rowsAffected: " + rowsAffected); } - // 读取(Read)操作的单元测试 + // select @Test - public void testRead() { + public void testSelect() { String sql = "SELECT * FROM staff WHERE name = ?"; RowMapper rowMapper = new RowMapper() { @Override @@ -72,25 +71,6 @@ public String mapRow(ResultSet rs, int rowNum) throws SQLException { } }; List names = jdbcTemplate.query(sql, rowMapper, "New Staff"); - Assert.assertEquals("New Staff", names.get(0)); + System.out.println("names: " + names); } - - // 更新(Update)操作的单元测试 - @Test - public void testUpdate() { - String sql = "UPDATE staff SET name = ? WHERE name = ?"; - int rowsAffected = jdbcTemplate.update(sql, "Updated Staff", "New Staff"); - Assert.assertEquals(1, rowsAffected); - } - - // 删除(Delete)操作的单元测试 - @Test - public void testDelete() { - String sql = "DELETE FROM staff WHERE name = ?"; - int rowsAffected = jdbcTemplate.update(sql, "Updated Staff"); - Assert.assertEquals(1, rowsAffected); - } - - - }