Skip to content

Commit

Permalink
update: 添加JsonAdaptor的测试代码,更新pom文件中测试用的JDBC部分
Browse files Browse the repository at this point in the history
  • Loading branch information
ywjno committed Aug 29, 2024
1 parent 014b7dc commit be0c559
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 10 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc41</version>
<version>42.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion test/org/nutz/dao/test/normal/mysql/AllMysqlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({MysqlJsonTest.class})
@Suite.SuiteClasses({MysqlJsonTest.class, MysqlJsonAdaptorTest.class})
public class AllMysqlTest {}
48 changes: 48 additions & 0 deletions test/org/nutz/dao/test/normal/mysql/MysqlJsonAdaptorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.nutz.dao.test.normal.mysql;

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;

import org.junit.Test;
import org.nutz.dao.Cnd;
import org.nutz.dao.test.DaoCase;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

public class MysqlJsonAdaptorTest extends DaoCase {

@Override
protected void before() {
if (!dao.meta().isMySql()) {
return;
}
dao.create(MysqlJsonAdaptorTestBean.class, true);
}

@Test
public void adapotor() {
if (!dao.meta().isMySql()) {
return;
}

MysqlJsonAdaptorTestBean testBean = new MysqlJsonAdaptorTestBean();
StudentResult result = new StudentResult();
result.setPhysics(new BigDecimal("100"));
testBean.setNoneAdaptor(result);
testBean.setJsonAdaptor(result);
testBean.setJsonCompactAdaptor(result);
testBean.setJsonTidyAdaptor(result);

int insertId = dao.insert(testBean).getId();

org.nutz.dao.entity.Record record = dao.fetch("t_mysql_json_adaptor_test_bean", Cnd.where("id","=",insertId));
// mysql 在保存 json 格式字段的时候会自动格式化该字段的值
// mariadb 的话就没问题
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.compact()), record.getString("jsonCompactAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonTidyAdaptor"));
}
}
68 changes: 68 additions & 0 deletions test/org/nutz/dao/test/normal/mysql/MysqlJsonAdaptorTestBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.nutz.dao.test.normal.mysql;

import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.ColType;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonAdaptor;
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonCompactAdaptor;
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonTidyAdaptor;

@Table("t_mysql_json_adaptor_test_bean")
public class MysqlJsonAdaptorTestBean {

@Id
private int id;

@ColDefine(customType = "json", type = ColType.MYSQL_JSON)
private StudentResult noneAdaptor;

@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonAdaptor.class)
private StudentResult jsonAdaptor;

@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonCompactAdaptor.class)
private StudentResult jsonCompactAdaptor;

@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonTidyAdaptor.class)
private StudentResult jsonTidyAdaptor;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public StudentResult getNoneAdaptor() {
return noneAdaptor;
}

public void setNoneAdaptor(StudentResult noneAdaptor) {
this.noneAdaptor = noneAdaptor;
}

public StudentResult getJsonAdaptor() {
return jsonAdaptor;
}

public void setJsonAdaptor(StudentResult jsonAdaptor) {
this.jsonAdaptor = jsonAdaptor;
}

public StudentResult getJsonCompactAdaptor() {
return jsonCompactAdaptor;
}

public void setJsonCompactAdaptor(StudentResult jsonCompactAdaptor) {
this.jsonCompactAdaptor = jsonCompactAdaptor;
}

public StudentResult getJsonTidyAdaptor() {
return jsonTidyAdaptor;
}

public void setJsonTidyAdaptor(StudentResult jsonTidyAdaptor) {
this.jsonTidyAdaptor = jsonTidyAdaptor;
}
}
2 changes: 1 addition & 1 deletion test/org/nutz/dao/test/normal/psql/AllPsqlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({PsqlJsonTest.class, PsqlArrayTest.class})
@Suite.SuiteClasses({PsqlJsonTest.class, PsqlArrayTest.class, PsqlJsonAdaptorTest.class})
public class AllPsqlTest {}
47 changes: 47 additions & 0 deletions test/org/nutz/dao/test/normal/psql/PsqlJsonAdaptorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.nutz.dao.test.normal.psql;

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;

import org.junit.Test;
import org.nutz.dao.Cnd;
import org.nutz.dao.test.DaoCase;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

public class PsqlJsonAdaptorTest extends DaoCase {

@Override
protected void before() {
if (!dao.meta().isPostgresql()) {
return;
}
dao.create(PsqlJsonAdaptorTestBean.class, true);
}

@Test
public void adapotor() {
if (!dao.meta().isPostgresql()) {
return;
}

PsqlJsonAdaptorTestBean testBean = new PsqlJsonAdaptorTestBean();
org.nutz.dao.test.normal.psql.StudentResult result = new StudentResult();
result.setPhysics(new BigDecimal("100"));
testBean.setNoneAdaptor(result);
testBean.setJsonAdaptor(result);
testBean.setJsonCompactAdaptor(result);
testBean.setJsonTidyAdaptor(result);

int insertId = dao.insert(testBean).getId();

org.nutz.dao.entity.Record record = dao.fetch("t_psql_json_adaptor_test_bean", Cnd.where("id","=",insertId));
// 设置成 jsonb 格式的时候会自动格式化该字段的值
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.compact()), record.getString("jsonCompactAdaptor"));
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonTidyAdaptor"));
}
}
68 changes: 68 additions & 0 deletions test/org/nutz/dao/test/normal/psql/PsqlJsonAdaptorTestBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.nutz.dao.test.normal.psql;

import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.ColType;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.impl.jdbc.psql.PsqlJsonAdaptor;
import org.nutz.dao.impl.jdbc.psql.PsqlJsonCompactAdaptor;
import org.nutz.dao.impl.jdbc.psql.PsqlJsonTidyAdaptor;

@Table("t_psql_json_adaptor_test_bean")
public class PsqlJsonAdaptorTestBean {

@Id
private int id;

@ColDefine(customType = "jsonb", type = ColType.PSQL_JSON)
private StudentResult noneAdaptor;

@ColDefine(customType = "json", type = ColType.PSQL_JSON, adaptor = PsqlJsonAdaptor.class)
private StudentResult jsonAdaptor;

@ColDefine(customType = "json", type = ColType.PSQL_JSON, adaptor = PsqlJsonCompactAdaptor.class)
private StudentResult jsonCompactAdaptor;

@ColDefine(customType = "json", type = ColType.PSQL_JSON, adaptor = PsqlJsonTidyAdaptor.class)
private StudentResult jsonTidyAdaptor;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public org.nutz.dao.test.normal.psql.StudentResult getNoneAdaptor() {
return noneAdaptor;
}

public void setNoneAdaptor(StudentResult noneAdaptor) {
this.noneAdaptor = noneAdaptor;
}

public StudentResult getJsonAdaptor() {
return jsonAdaptor;
}

public void setJsonAdaptor(StudentResult jsonAdaptor) {
this.jsonAdaptor = jsonAdaptor;
}

public StudentResult getJsonCompactAdaptor() {
return jsonCompactAdaptor;
}

public void setJsonCompactAdaptor(StudentResult jsonCompactAdaptor) {
this.jsonCompactAdaptor = jsonCompactAdaptor;
}

public StudentResult getJsonTidyAdaptor() {
return jsonTidyAdaptor;
}

public void setJsonTidyAdaptor(StudentResult jsonTidyAdaptor) {
this.jsonTidyAdaptor = jsonTidyAdaptor;
}
}
8 changes: 4 additions & 4 deletions test/tmpl.nutz-test.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
driver=com.mysql.jdbc.Driver
driver=com.mysql.cj.jdbc.Driver
#driver=org.mariadb.jdbc.Driver
url=jdbc:mysql://127.0.0.1/nutztest?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
url=jdbc:mysql://127.0.0.1:3306/nutztest?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
username=root
password=root

Expand All @@ -21,7 +21,7 @@ password=root
#url=jdbc:h2:mem:

#driver=org.postgresql.Driver
#url=jdbc:postgresql://127.0.0.1:5433/nutztest
#url=jdbc:postgresql://127.0.0.1:5432/nutztest
#username=postgres
#password=root
#validationQuery=select 1
Expand All @@ -33,4 +33,4 @@ password=root
#driver=org.sqlite.JDBC
#url=jdbc:sqlite:nutz.db
#username=root
#password=root
#password=root

0 comments on commit be0c559

Please sign in to comment.