From 60e43fa6568edd9f6c9712f3a58252cc535d069b Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Wed, 6 Jun 2018 09:15:38 +0800 Subject: [PATCH] Remove GSON dependency, move test/ to src/integration-test --- build.gradle | 4 +--- .../json/PgJsonPathsIntegrationSpec.groovy | 8 ++++++-- .../json/PgJsonValuesIntegrationSpec.groovy | 4 ++++ .../json/PgJsonbEqualsIntegrationSpec.groovy | 4 ++++ .../json/PgJsonbPathsIntegrationSpec.groovy | 8 ++++++-- .../json/PgJsonbValuesIntegrationSpec.groovy | 4 ++++ .../net/kaleidos/hibernate/usertype/ArrayType.java | 12 ++---------- .../kaleidos/hibernate/usertype/JsonMapType.groovy | 13 ++++++------- 8 files changed, 33 insertions(+), 24 deletions(-) rename {test/integration => src/integration-test/groovy}/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy (87%) rename {test/integration => src/integration-test/groovy}/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy (95%) rename {test/integration => src/integration-test/groovy}/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy (88%) rename {test/integration => src/integration-test/groovy}/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy (87%) rename {test/integration => src/integration-test/groovy}/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy (95%) diff --git a/build.gradle b/build.gradle index 3422e22..46c84bc 100644 --- a/build.gradle +++ b/build.gradle @@ -75,11 +75,10 @@ dependencies { profile 'org.grails.profiles:web-plugin' provided 'org.grails:grails-plugin-services' provided 'org.grails:grails-plugin-domain-class' + provided 'org.postgresql:postgresql:42.2.2' testCompile 'org.grails:grails-plugin-testing' testCompile 'org.grails:grails-gorm-testing-support' - provided 'org.postgresql:postgresql:42.2.2' - // plugins provided 'org.grails.plugins:hibernate5' @@ -88,7 +87,6 @@ dependencies { provided("org.hibernate:hibernate-ehcache:$hibernateVersion") { exclude group: 'net.sf.ehcache', module: 'ehcache' } - compile 'com.google.code.gson:gson:2.8.4' } task wrapper(type: Wrapper) { diff --git a/test/integration/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy similarity index 87% rename from test/integration/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy rename to src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy index 4e3dfb8..f5ffa91 100644 --- a/test/integration/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy +++ b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy @@ -1,15 +1,19 @@ package net.kaleidos.hibernate.json +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration import spock.lang.Specification import spock.lang.Unroll import test.json.TestMapJson +@Integration +@Rollback class PgJsonPathsIntegrationSpec extends Specification { def pgJsonTestSearchService @Unroll - void 'Test equals finding nested values (json)'() { + void 'Test equals finding nested values (json): a'() { setup: new TestMapJson(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) new TestMapJson(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) @@ -30,7 +34,7 @@ class PgJsonPathsIntegrationSpec extends Specification { } @Unroll - void 'Test equals finding nested values (json)'() { + void 'Test equals finding nested values (json): b'() { setup: new TestMapJson(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) new TestMapJson(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) diff --git a/test/integration/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy similarity index 95% rename from test/integration/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy rename to src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy index 32bce4d..2cd9a08 100644 --- a/test/integration/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy +++ b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy @@ -1,9 +1,13 @@ package net.kaleidos.hibernate.json +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration import spock.lang.Specification import spock.lang.Unroll import test.json.TestMapJson +@Integration +@Rollback class PgJsonValuesIntegrationSpec extends Specification { def pgJsonTestSearchService diff --git a/test/integration/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy similarity index 88% rename from test/integration/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy rename to src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy index 1afd5a2..e924189 100644 --- a/test/integration/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy +++ b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy @@ -1,9 +1,13 @@ package net.kaleidos.hibernate.json +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration import spock.lang.Specification import spock.lang.Unroll import test.json.TestMapJsonb +@Integration +@Rollback class PgJsonbEqualsIntegrationSpec extends Specification { def pgJsonbTestSearchService diff --git a/test/integration/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy similarity index 87% rename from test/integration/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy rename to src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy index 6393c7a..ac21443 100644 --- a/test/integration/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy +++ b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy @@ -1,15 +1,19 @@ package net.kaleidos.hibernate.json +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration import spock.lang.Specification import spock.lang.Unroll import test.json.TestMapJsonb +@Integration +@Rollback class PgJsonbPathsIntegrationSpec extends Specification { def pgJsonbTestSearchService @Unroll - void 'Test equals finding nested values (jsonb)'() { + void 'Test equals finding nested values (jsonb): a'() { setup: new TestMapJsonb(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) @@ -30,7 +34,7 @@ class PgJsonbPathsIntegrationSpec extends Specification { } @Unroll - void 'Test equals finding nested values (jsonb)'() { + void 'Test equals finding nested values (jsonb): b'() { setup: new TestMapJsonb(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) diff --git a/test/integration/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy similarity index 95% rename from test/integration/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy rename to src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy index 3918a0f..d7d9afa 100644 --- a/test/integration/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy +++ b/src/integration-test/groovy/net/kaleidos/hibernate/json/PgJsonbValuesIntegrationSpec.groovy @@ -1,9 +1,13 @@ package net.kaleidos.hibernate.json +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration import spock.lang.Specification import spock.lang.Unroll import test.json.TestMapJsonb +@Integration +@Rollback class PgJsonbValuesIntegrationSpec extends Specification { def pgJsonbTestSearchService diff --git a/src/main/groovy/net/kaleidos/hibernate/usertype/ArrayType.java b/src/main/groovy/net/kaleidos/hibernate/usertype/ArrayType.java index 67f2a2f..0142d95 100644 --- a/src/main/groovy/net/kaleidos/hibernate/usertype/ArrayType.java +++ b/src/main/groovy/net/kaleidos/hibernate/usertype/ArrayType.java @@ -7,16 +7,8 @@ import org.hibernate.usertype.UserType; import java.io.Serializable; -import java.sql.Array; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; +import java.sql.*; +import java.util.*; public class ArrayType implements UserType, ParameterizedType { public static final int INTEGER_ARRAY = 90001; diff --git a/src/main/groovy/net/kaleidos/hibernate/usertype/JsonMapType.groovy b/src/main/groovy/net/kaleidos/hibernate/usertype/JsonMapType.groovy index 9be558d..73d3ac9 100644 --- a/src/main/groovy/net/kaleidos/hibernate/usertype/JsonMapType.groovy +++ b/src/main/groovy/net/kaleidos/hibernate/usertype/JsonMapType.groovy @@ -1,9 +1,9 @@ package net.kaleidos.hibernate.usertype -import com.google.gson.Gson -import com.google.gson.GsonBuilder +import grails.converters.JSON import groovy.transform.CompileStatic import org.apache.commons.lang.ObjectUtils +import org.grails.web.json.JSONObject import org.hibernate.HibernateException import org.hibernate.engine.spi.SharedSessionContractImplementor import org.hibernate.usertype.UserType @@ -21,7 +21,6 @@ class JsonMapType implements UserType { static int SQLTYPE = 90021 private final Type userType = Map - private final Gson gson = new GsonBuilder().serializeNulls().create() @Override int[] sqlTypes() { @@ -48,7 +47,7 @@ class JsonMapType implements UserType { PGobject o = rs.getObject(names[0]) as PGobject String jsonString = o?.value - gson.fromJson(jsonString, userType) + jsonString ? new JSONObject(jsonString) : null } @Override @@ -56,7 +55,7 @@ class JsonMapType implements UserType { if (value == null) { st.setNull(index, Types.OTHER) } else { - st.setObject(index, gson.toJson(value, userType), Types.OTHER) + st.setObject(index, (value as JSON).toString(), Types.OTHER) } } @@ -80,12 +79,12 @@ class JsonMapType implements UserType { @Override Serializable disassemble(Object value) throws HibernateException { - gson.toJson(value, userType) + (value as JSON).toString() } @Override Object assemble(Serializable cached, Object owner) throws HibernateException { - gson.fromJson((String) cached, userType) + new JSONObject(cached.toString()) } @Override