|
| 1 | +/* |
| 2 | + * Copyright 2021-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.dialect; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.ComponentScan; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.annotation.FilterType; |
| 33 | +import org.springframework.context.annotation.Import; |
| 34 | +import org.springframework.core.convert.converter.Converter; |
| 35 | +import org.springframework.data.annotation.Id; |
| 36 | +import org.springframework.data.convert.CustomConversions; |
| 37 | +import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; |
| 38 | +import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes; |
| 39 | +import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; |
| 40 | +import org.springframework.data.jdbc.testing.DatabaseType; |
| 41 | +import org.springframework.data.jdbc.testing.EnabledOnDatabase; |
| 42 | +import org.springframework.data.jdbc.testing.IntegrationTest; |
| 43 | +import org.springframework.data.jdbc.testing.TestConfiguration; |
| 44 | +import org.springframework.data.mapping.model.SimpleTypeHolder; |
| 45 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 46 | +import org.springframework.data.relational.core.mapping.Table; |
| 47 | +import org.springframework.data.repository.CrudRepository; |
| 48 | + |
| 49 | +import com.huawei.gaussdb.jdbc.util.PGobject; |
| 50 | + |
| 51 | +/** |
| 52 | + * Integration tests for GaussDB Dialect. Start this test with {@code -Dspring.profiles.active=gaussdb}. |
| 53 | + * |
| 54 | + * Notes: this file is token from PostgresDialectIntegrationTests and add specific changes for GaussDB |
| 55 | + * |
| 56 | + * @author liubao |
| 57 | + */ |
| 58 | +@IntegrationTest |
| 59 | +@EnabledOnDatabase(DatabaseType.GAUSSDB) |
| 60 | +public class GaussDBDialectIntegrationTests { |
| 61 | + |
| 62 | + @Autowired CustomerRepository customerRepository; |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldSaveAndLoadJson() throws SQLException { |
| 66 | + |
| 67 | + PGobject sessionData = new PGobject(); |
| 68 | + sessionData.setType("jsonb"); |
| 69 | + sessionData.setValue("{\"hello\": \"json\"}"); |
| 70 | + |
| 71 | + Customer saved = customerRepository |
| 72 | + .save(new Customer(null, "Adam Smith", new JsonHolder("{\"hello\": \"world\"}"), sessionData)); |
| 73 | + |
| 74 | + Optional<Customer> loaded = customerRepository.findById(saved.getId()); |
| 75 | + |
| 76 | + assertThat(loaded).hasValueSatisfying(actual -> { |
| 77 | + |
| 78 | + assertThat(actual.getPersonData().getContent()).isEqualTo("{\"hello\": \"world\"}"); |
| 79 | + assertThat(actual.getSessionData().getValue()).isEqualTo("{\"hello\": \"json\"}"); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @Configuration |
| 84 | + @Import(TestConfiguration.class) |
| 85 | + @EnableJdbcRepositories(considerNestedRepositories = true, |
| 86 | + includeFilters = @ComponentScan.Filter(value = CustomerRepository.class, type = FilterType.ASSIGNABLE_TYPE)) |
| 87 | + static class Config { |
| 88 | + |
| 89 | + @Bean |
| 90 | + CustomConversions jdbcCustomConversions(Dialect dialect) { |
| 91 | + SimpleTypeHolder simpleTypeHolder = new SimpleTypeHolder(dialect.simpleTypes(), JdbcSimpleTypes.HOLDER); |
| 92 | + |
| 93 | + return new JdbcCustomConversions( |
| 94 | + CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(dialect)), userConverters()); |
| 95 | + } |
| 96 | + |
| 97 | + private List<Object> storeConverters(Dialect dialect) { |
| 98 | + |
| 99 | + List<Object> converters = new ArrayList<>(); |
| 100 | + converters.addAll(dialect.getConverters()); |
| 101 | + converters.addAll(JdbcCustomConversions.storeConverters()); |
| 102 | + return converters; |
| 103 | + } |
| 104 | + |
| 105 | + private List<Object> userConverters() { |
| 106 | + return Arrays.asList(JsonHolderToPGobjectConverter.INSTANCE, PGobjectToJsonHolderConverter.INSTANCE); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + enum JsonHolderToPGobjectConverter implements Converter<JsonHolder, PGobject> { |
| 111 | + |
| 112 | + INSTANCE; |
| 113 | + |
| 114 | + @Override |
| 115 | + public PGobject convert(JsonHolder source) { |
| 116 | + PGobject result = new PGobject(); |
| 117 | + result.setType("jsonb"); |
| 118 | + try { |
| 119 | + result.setValue(source.getContent()); |
| 120 | + } catch (SQLException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + return result; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + enum PGobjectToJsonHolderConverter implements Converter<PGobject, JsonHolder> { |
| 128 | + |
| 129 | + INSTANCE; |
| 130 | + |
| 131 | + @Override |
| 132 | + public JsonHolder convert(PGobject source) { |
| 133 | + return new JsonHolder(source.getValue()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Table("customers") |
| 138 | + public static final class Customer { |
| 139 | + |
| 140 | + @Id private final Long id; |
| 141 | + private final String name; |
| 142 | + private final JsonHolder personData; |
| 143 | + private final PGobject sessionData; |
| 144 | + |
| 145 | + public Customer(Long id, String name, JsonHolder personData, PGobject sessionData) { |
| 146 | + this.id = id; |
| 147 | + this.name = name; |
| 148 | + this.personData = personData; |
| 149 | + this.sessionData = sessionData; |
| 150 | + } |
| 151 | + |
| 152 | + public Long getId() { |
| 153 | + return this.id; |
| 154 | + } |
| 155 | + |
| 156 | + public String getName() { |
| 157 | + return this.name; |
| 158 | + } |
| 159 | + |
| 160 | + public JsonHolder getPersonData() { |
| 161 | + return this.personData; |
| 162 | + } |
| 163 | + |
| 164 | + public PGobject getSessionData() { |
| 165 | + return this.sessionData; |
| 166 | + } |
| 167 | + |
| 168 | + public boolean equals(final Object o) { |
| 169 | + if (o == this) |
| 170 | + return true; |
| 171 | + if (!(o instanceof final Customer other)) |
| 172 | + return false; |
| 173 | + final Object this$id = this.getId(); |
| 174 | + final Object other$id = other.getId(); |
| 175 | + if (!Objects.equals(this$id, other$id)) |
| 176 | + return false; |
| 177 | + final Object this$name = this.getName(); |
| 178 | + final Object other$name = other.getName(); |
| 179 | + if (!Objects.equals(this$name, other$name)) |
| 180 | + return false; |
| 181 | + final Object this$personData = this.getPersonData(); |
| 182 | + final Object other$personData = other.getPersonData(); |
| 183 | + if (!Objects.equals(this$personData, other$personData)) |
| 184 | + return false; |
| 185 | + final Object this$sessionData = this.getSessionData(); |
| 186 | + final Object other$sessionData = other.getSessionData(); |
| 187 | + return Objects.equals(this$sessionData, other$sessionData); |
| 188 | + } |
| 189 | + |
| 190 | + public int hashCode() { |
| 191 | + final int PRIME = 59; |
| 192 | + int result = 1; |
| 193 | + final Object $id = this.getId(); |
| 194 | + result = result * PRIME + ($id == null ? 43 : $id.hashCode()); |
| 195 | + final Object $name = this.getName(); |
| 196 | + result = result * PRIME + ($name == null ? 43 : $name.hashCode()); |
| 197 | + final Object $personData = this.getPersonData(); |
| 198 | + result = result * PRIME + ($personData == null ? 43 : $personData.hashCode()); |
| 199 | + final Object $sessionData = this.getSessionData(); |
| 200 | + result = result * PRIME + ($sessionData == null ? 43 : $sessionData.hashCode()); |
| 201 | + return result; |
| 202 | + } |
| 203 | + |
| 204 | + public String toString() { |
| 205 | + return "PostgresDialectIntegrationTests.Customer(id=" + this.getId() + ", name=" + this.getName() |
| 206 | + + ", personData=" + this.getPersonData() + ", sessionData=" + this.getSessionData() + ")"; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public static class JsonHolder { |
| 211 | + String content; |
| 212 | + |
| 213 | + public JsonHolder(String content) { |
| 214 | + this.content = content; |
| 215 | + } |
| 216 | + |
| 217 | + public JsonHolder() {} |
| 218 | + |
| 219 | + public String getContent() { |
| 220 | + return this.content; |
| 221 | + } |
| 222 | + |
| 223 | + public void setContent(String content) { |
| 224 | + this.content = content; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + interface CustomerRepository extends CrudRepository<Customer, Long> {} |
| 229 | + |
| 230 | +} |
0 commit comments