|
| 1 | +/* |
| 2 | + * Copyright 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 | + * http://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 | + |
| 17 | +package io.zonky.test.db.init; |
| 18 | + |
| 19 | +import com.cedarsoftware.util.DeepEquals; |
| 20 | +import io.zonky.test.db.preparer.DatabasePreparer; |
| 21 | +import io.zonky.test.db.util.ReflectionUtils; |
| 22 | +import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; |
| 23 | + |
| 24 | +import org.springframework.util.ReflectionUtils.FieldFilter; |
| 25 | + |
| 26 | +import javax.sql.DataSource; |
| 27 | +import java.lang.reflect.Modifier; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | + |
| 34 | +import static org.springframework.util.ReflectionUtils.makeAccessible; |
| 35 | + |
| 36 | +public class DataSourceScriptDatabasePreparer implements DatabasePreparer { |
| 37 | + |
| 38 | + private static final Set<String> EXCLUDED_FIELDS = new HashSet<>(Arrays.asList("dataSource", "resourceLoader")); |
| 39 | + |
| 40 | + private static final FieldFilter FIELD_FILTER = |
| 41 | + field -> !Modifier.isStatic(field.getModifiers()) && !EXCLUDED_FIELDS.contains(field.getName()); |
| 42 | + |
| 43 | + private final DataSourceScriptDatabaseInitializer initializer; |
| 44 | + private final ThreadLocalDataSource threadLocalDataSource; |
| 45 | + |
| 46 | + public DataSourceScriptDatabasePreparer(DataSourceScriptDatabaseInitializer initializer) { |
| 47 | + this.initializer = initializer; |
| 48 | + this.threadLocalDataSource = new ThreadLocalDataSource(); |
| 49 | + ReflectionUtils.setField(initializer, "dataSource", threadLocalDataSource); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public long estimatedDuration() { |
| 54 | + return 10; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void prepare(DataSource dataSource) { |
| 59 | + threadLocalDataSource.set(dataSource); |
| 60 | + try { |
| 61 | + initializer.initializeDatabase(); |
| 62 | + } finally { |
| 63 | + threadLocalDataSource.clear(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean equals(Object o) { |
| 69 | + if (this == o) return true; |
| 70 | + if (o == null || getClass() != o.getClass()) return false; |
| 71 | + DataSourceScriptDatabasePreparer that = (DataSourceScriptDatabasePreparer) o; |
| 72 | + if (initializer.getClass() != that.initializer.getClass()) return false; |
| 73 | + AtomicBoolean equal = new AtomicBoolean(true); |
| 74 | + org.springframework.util.ReflectionUtils.doWithFields(initializer.getClass(), |
| 75 | + field -> { |
| 76 | + if (!equal.get()) return; |
| 77 | + makeAccessible(field); |
| 78 | + if (!DeepEquals.deepEquals(field.get(initializer), field.get(that.initializer))) { |
| 79 | + equal.set(false); |
| 80 | + } |
| 81 | + }, |
| 82 | + FIELD_FILTER); |
| 83 | + return equal.get(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int hashCode() { |
| 88 | + AtomicInteger result = new AtomicInteger(initializer.getClass().hashCode()); |
| 89 | + org.springframework.util.ReflectionUtils.doWithFields(initializer.getClass(), |
| 90 | + field -> { |
| 91 | + makeAccessible(field); |
| 92 | + result.set(31 * result.get() + DeepEquals.deepHashCode(field.get(initializer))); |
| 93 | + }, |
| 94 | + FIELD_FILTER); |
| 95 | + return result.get(); |
| 96 | + } |
| 97 | +} |
0 commit comments