Skip to content

Commit

Permalink
[improve]: update test cases, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-pang committed May 20, 2024
1 parent 2ada54b commit b966dc6
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 117 deletions.
112 changes: 67 additions & 45 deletions java/spring-jdbc/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> map = new HashMap<String, String>();
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<String> rowMapper = new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString("name");
}
};
List<String> names = jdbcTemplate.query(sql, rowMapper, "New Staff");
System.out.println("names: " + names);
}
}
```

Expand Down
116 changes: 70 additions & 46 deletions java/spring-jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> map = new HashMap<String, String>();
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<String> rowMapper = new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString("name");
}
};
List<String> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> rowMapper = new RowMapper<String>() {
@Override
Expand All @@ -72,25 +71,6 @@ public String mapRow(ResultSet rs, int rowNum) throws SQLException {
}
};
List<String> 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);
}



}

0 comments on commit b966dc6

Please sign in to comment.