From 26cf9a2edc47512c46f93defc7c488fb62c1eabd Mon Sep 17 00:00:00 2001 From: holyliao Date: Sun, 17 Jul 2022 10:44:22 +0800 Subject: [PATCH 1/4] =?UTF-8?q?add:=E5=A2=9E=E5=8A=A0h2=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +++ .../lingxi_java/auth/MyRepositoryTests.java | 47 +++++++++++++++++++ src/test/resources/application-h2.yml | 35 ++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java create mode 100644 src/test/resources/application-h2.yml diff --git a/pom.xml b/pom.xml index 4ce6adb..6fc7c5f 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,12 @@ 2.0.0-M3 + + com.h2database + h2 + test + + diff --git a/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java b/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java new file mode 100644 index 0000000..597d709 --- /dev/null +++ b/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java @@ -0,0 +1,47 @@ +package com.lingxi.lingxi_java.auth; + +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import com.lingxi.lingxi_java.auth.domain.User; +import com.lingxi.lingxi_java.auth.domain.UserRepository; +import com.lingxi.lingxi_java.utils.EncryptUtil; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(SpringExtension.class) +@DataJpaTest +@ActiveProfiles("h2") +class MyRepositoryTests { + + @Autowired + private TestEntityManager entityManager; + + @Autowired + private UserRepository repository; + + @Test + void testExample() { + assertNotNull(entityManager); + User user = new User(); + String username = "holyliao"; + user.setUsername(username); + user.setPassword(EncryptUtil.md5(username)); + this.entityManager.persist(user); + Optional target = this.repository.findOneByUsername(username); + + assertTrue(target.isPresent()); + assertEquals(target.get().getUsername(), username); + assertEquals(target.get().getPassword(), EncryptUtil.md5(username)); + } + +} \ No newline at end of file diff --git a/src/test/resources/application-h2.yml b/src/test/resources/application-h2.yml new file mode 100644 index 0000000..72db83f --- /dev/null +++ b/src/test/resources/application-h2.yml @@ -0,0 +1,35 @@ +logging: + level: + org: + hibernate: + type: + descriptor: + sql: trace + +spring: + datasource: + url: jdbc:h2:mem:lingxi;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=TRUE + username: sa + password: + driverClassName: org.h2.Driver + jpa: + database-platform: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: create + properties: + hibernate: + use_sql_comments: true + format_sql: true + auto_quote_keyword: true + generate-ddl: true + show-sql: true + defer-datasource-initialization: true + sql: + init: + mode: NEVER + mvc: + pathmatch: + matching-strategy: ant_path_matcher + +custom: + jwt-secret: ${JWT_SECRET:lingxi_jwt} \ No newline at end of file From f89d408f55f197902b87c89d75253915ff4928e4 Mon Sep 17 00:00:00 2001 From: holyliao Date: Sun, 17 Jul 2022 10:57:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=88=9D=E5=A7=8B=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/application-h2.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/resources/application-h2.yml b/src/test/resources/application-h2.yml index 72db83f..cf5ad03 100644 --- a/src/test/resources/application-h2.yml +++ b/src/test/resources/application-h2.yml @@ -26,7 +26,9 @@ spring: defer-datasource-initialization: true sql: init: - mode: NEVER + mode: EMBEDDED + platform: h2 + data-locations: mvc: pathmatch: matching-strategy: ant_path_matcher From 2765481f144d628df16d3b16dbf8215d27ec0a50 Mon Sep 17 00:00:00 2001 From: holyliao Date: Sun, 17 Jul 2022 22:24:17 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update:entity=E7=9A=84id=E8=87=AA=E5=A2=9E?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lingxi/lingxi_java/common/BaseEntity.java | 16 +++++++++++----- src/test/resources/application-h2.yml | 9 +++++---- src/test/resources/data-h2.sql | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/data-h2.sql diff --git a/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java b/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java index 6dc9bbb..dbb6ebb 100644 --- a/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java +++ b/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java @@ -1,7 +1,8 @@ package com.lingxi.lingxi_java.common; -import jakarta.persistence.*; -import lombok.Data; +import java.io.Serializable; +import java.util.Date; + import org.hibernate.annotations.DynamicUpdate; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; @@ -9,8 +10,13 @@ import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import java.io.Serializable; -import java.util.Date; +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import lombok.Data; @Data @MappedSuperclass @@ -18,7 +24,7 @@ @DynamicUpdate public abstract class BaseEntity implements Serializable { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @CreatedBy @Column(nullable = false, columnDefinition = "bigint default 0") diff --git a/src/test/resources/application-h2.yml b/src/test/resources/application-h2.yml index cf5ad03..abc9ad0 100644 --- a/src/test/resources/application-h2.yml +++ b/src/test/resources/application-h2.yml @@ -5,17 +5,18 @@ logging: type: descriptor: sql: trace + event: trace spring: datasource: - url: jdbc:h2:mem:lingxi;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=TRUE + url: jdbc:h2:mem:lingxi;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=TRUE;MODE=MYSQL;;DB_CLOSE_ON_EXIT=FALSE username: sa password: driverClassName: org.h2.Driver jpa: database-platform: org.hibernate.dialect.H2Dialect - hibernate: - ddl-auto: create +# hibernate: +# ddl-auto: create properties: hibernate: use_sql_comments: true @@ -28,7 +29,7 @@ spring: init: mode: EMBEDDED platform: h2 - data-locations: + data-locations: data-h2.sql mvc: pathmatch: matching-strategy: ant_path_matcher diff --git a/src/test/resources/data-h2.sql b/src/test/resources/data-h2.sql new file mode 100644 index 0000000..7b308f0 --- /dev/null +++ b/src/test/resources/data-h2.sql @@ -0,0 +1,2 @@ +INSERT INTO "user" (id, created_at, created_by, updated_at, updated_by, avatar, nick_name, password, username) +VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, '/avatar.png', '灵希', '8b12aedc03901c59ba19a5a117e56936', 'lingxi'); \ No newline at end of file From eb406fdd9c453661fc5e5d8709362fc7add1e2be Mon Sep 17 00:00:00 2001 From: holyliao Date: Mon, 18 Jul 2022 00:00:33 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update:=E6=8A=BD=E5=8F=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lingxi/lingxi_java/common/BaseEntity.java | 3 +-- .../lingxi_java/auth/MyRepositoryTests.java | 15 ++++--------- src/test/resources/application-h2.yml | 22 +++++-------------- src/test/resources/application-mysql.yml | 18 +++++++++++++++ src/test/resources/application.yml | 18 +++++++-------- src/test/resources/data-h2.sql | 2 +- 6 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 src/test/resources/application-mysql.yml diff --git a/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java b/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java index dbb6ebb..b8d2a4a 100644 --- a/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java +++ b/src/main/java/com/lingxi/lingxi_java/common/BaseEntity.java @@ -13,7 +13,6 @@ import jakarta.persistence.Column; import jakarta.persistence.EntityListeners; import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; import lombok.Data; @@ -24,7 +23,7 @@ @DynamicUpdate public abstract class BaseEntity implements Serializable { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue private Long id; @CreatedBy @Column(nullable = false, columnDefinition = "bigint default 0") diff --git a/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java b/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java index 597d709..f08a200 100644 --- a/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java +++ b/src/test/java/com/lingxi/lingxi_java/auth/MyRepositoryTests.java @@ -5,9 +5,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; -import org.springframework.test.context.ActiveProfiles; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import com.lingxi.lingxi_java.auth.domain.User; @@ -15,28 +13,23 @@ import com.lingxi.lingxi_java.utils.EncryptUtil; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +@SpringBootTest @ExtendWith(SpringExtension.class) -@DataJpaTest -@ActiveProfiles("h2") class MyRepositoryTests { - @Autowired - private TestEntityManager entityManager; - @Autowired private UserRepository repository; @Test void testExample() { - assertNotNull(entityManager); User user = new User(); String username = "holyliao"; + user.setUsername(username); user.setPassword(EncryptUtil.md5(username)); - this.entityManager.persist(user); + repository.save(user); Optional target = this.repository.findOneByUsername(username); assertTrue(target.isPresent()); diff --git a/src/test/resources/application-h2.yml b/src/test/resources/application-h2.yml index abc9ad0..006d493 100644 --- a/src/test/resources/application-h2.yml +++ b/src/test/resources/application-h2.yml @@ -1,12 +1,3 @@ -logging: - level: - org: - hibernate: - type: - descriptor: - sql: trace - event: trace - spring: datasource: url: jdbc:h2:mem:lingxi;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=TRUE;MODE=MYSQL;;DB_CLOSE_ON_EXIT=FALSE @@ -25,14 +16,13 @@ spring: generate-ddl: true show-sql: true defer-datasource-initialization: true + hibernate: + use-new-id-generator-mappings: sql: init: mode: EMBEDDED platform: h2 - data-locations: data-h2.sql - mvc: - pathmatch: - matching-strategy: ant_path_matcher - -custom: - jwt-secret: ${JWT_SECRET:lingxi_jwt} \ No newline at end of file + data-locations: classpath:data-h2.sql + config: + activate: + on-profile: h2 \ No newline at end of file diff --git a/src/test/resources/application-mysql.yml b/src/test/resources/application-mysql.yml new file mode 100644 index 0000000..53621f0 --- /dev/null +++ b/src/test/resources/application-mysql.yml @@ -0,0 +1,18 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + username: root + password: 123456 + url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/lingxi + type: com.mysql.cj.jdbc.MysqlDataSource + jpa: + generate-ddl: true + show-sql: true + database-platform: org.hibernate.dialect.MySQL8Dialect + sql: + init: + platform: mysql + data-locations: + config: + activate: + on-profile: mysql \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index ccc78d0..499617b 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -5,18 +5,18 @@ logging: type: descriptor: sql: trace + spring: - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: 123456 - url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/lingxi - jpa: - generate-ddl: true - show-sql: true mvc: pathmatch: matching-strategy: ant_path_matcher + profiles: + active: h2 custom: - jwt-secret: ${JWT_SECRET:lingxi_jwt} \ No newline at end of file + jwt-secret: ${JWT_SECRET:lingxi_jwt} + +qiniu: + ak: ${QINIU_AK:lingxi} + sk: ${QINIU_SK:lingxi} + bucket: ${QINIU_SK:upload} \ No newline at end of file diff --git a/src/test/resources/data-h2.sql b/src/test/resources/data-h2.sql index 7b308f0..3465628 100644 --- a/src/test/resources/data-h2.sql +++ b/src/test/resources/data-h2.sql @@ -1,2 +1,2 @@ INSERT INTO "user" (id, created_at, created_by, updated_at, updated_by, avatar, nick_name, password, username) -VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, '/avatar.png', '灵希', '8b12aedc03901c59ba19a5a117e56936', 'lingxi'); \ No newline at end of file +VALUES (10000, DEFAULT, DEFAULT, DEFAULT, DEFAULT, '/avatar.png', '灵希', '8b12aedc03901c59ba19a5a117e56936', 'lingxi'); \ No newline at end of file