|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 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.r2dbc.testing; |
| 17 | + |
| 18 | +import io.r2dbc.spi.ConnectionFactory; |
| 19 | +import io.r2dbc.spi.ConnectionFactoryOptions; |
| 20 | +import lombok.SneakyThrows; |
| 21 | + |
| 22 | +import java.util.function.Supplier; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import javax.sql.DataSource; |
| 26 | + |
| 27 | +import org.mariadb.jdbc.MariaDbDataSource; |
| 28 | +import org.mariadb.r2dbc.MariadbConnectionFactoryProvider; |
| 29 | +import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase; |
| 30 | +import org.testcontainers.containers.MariaDBContainer; |
| 31 | +import org.testcontainers.utility.DockerImageName; |
| 32 | + |
| 33 | +/** |
| 34 | + * Utility class for testing against MariaDB. |
| 35 | + * |
| 36 | + * @author Mark Paluch |
| 37 | + * @author Jens Schauder |
| 38 | + */ |
| 39 | +public class MariaDbTestSupport { |
| 40 | + |
| 41 | + private static ExternalDatabase testContainerDatabase; |
| 42 | + |
| 43 | + public static final String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" // |
| 44 | + + " id integer PRIMARY KEY,\n" // |
| 45 | + + " name varchar(255) NOT NULL,\n" // |
| 46 | + + " manual integer NULL\n," // |
| 47 | + + " cert varbinary(255) NULL\n" // |
| 48 | + + ") ENGINE=InnoDB;"; |
| 49 | + |
| 50 | + public static final String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" // |
| 51 | + + " id integer AUTO_INCREMENT PRIMARY KEY,\n" // |
| 52 | + + " name varchar(255) NOT NULL,\n" // |
| 53 | + + " flag boolean NOT NULL,\n" // |
| 54 | + + " manual integer NULL\n" // |
| 55 | + + ") ENGINE=InnoDB;"; |
| 56 | + |
| 57 | + public static final String CREATE_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "CREATE TABLE `LegoSet` (\n" // |
| 58 | + + " `Id` integer AUTO_INCREMENT PRIMARY KEY,\n" // |
| 59 | + + " `Name` varchar(255) NOT NULL,\n" // |
| 60 | + + " `Manual` integer NULL\n" // |
| 61 | + + ") ENGINE=InnoDB;"; |
| 62 | + |
| 63 | + public static final String DROP_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "DROP TABLE `LegoSet`"; |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns a database either hosted locally at {@code localhost:3306/mysql} or running inside Docker. |
| 67 | + * |
| 68 | + * @return information about the database. Guaranteed to be not {@literal null}. |
| 69 | + */ |
| 70 | + public static ExternalDatabase database() { |
| 71 | + |
| 72 | + if (Boolean.getBoolean("spring.data.r2dbc.test.preferLocalDatabase")) { |
| 73 | + |
| 74 | + return getFirstWorkingDatabase( // |
| 75 | + MariaDbTestSupport::local, // |
| 76 | + MariaDbTestSupport::testContainer // |
| 77 | + ); |
| 78 | + } else { |
| 79 | + |
| 80 | + return getFirstWorkingDatabase( // |
| 81 | + MariaDbTestSupport::testContainer, // |
| 82 | + MariaDbTestSupport::local // |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @SafeVarargs |
| 88 | + private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase>... suppliers) { |
| 89 | + |
| 90 | + return Stream.of(suppliers).map(Supplier::get) // |
| 91 | + .filter(ExternalDatabase::checkValidity) // |
| 92 | + .findFirst() // |
| 93 | + .orElse(ExternalDatabase.unavailable()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns a locally provided database . |
| 98 | + */ |
| 99 | + private static ExternalDatabase local() { |
| 100 | + |
| 101 | + return ProvidedDatabase.builder() // |
| 102 | + .hostname("localhost") // |
| 103 | + .port(3306) // |
| 104 | + .database("mysql") // |
| 105 | + .username("root") // |
| 106 | + .password("my-secret-pw") // |
| 107 | + .jdbcUrl("jdbc:mariadb://localhost:3306/mysql") // |
| 108 | + .build(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns a database provided via Testcontainers. |
| 113 | + */ |
| 114 | + private static ExternalDatabase testContainer() { |
| 115 | + |
| 116 | + if (testContainerDatabase == null) { |
| 117 | + |
| 118 | + try { |
| 119 | + |
| 120 | + String osArch = System.getProperty("os.arch"); |
| 121 | + |
| 122 | + DockerImageName armImageName = DockerImageName.parse("arm64v8/mariadb:10.3") |
| 123 | + .asCompatibleSubstituteFor("mariadb"); |
| 124 | + |
| 125 | + DockerImageName mariadb = DockerImageName.parse("mariadb").withTag("10.3.6"); |
| 126 | + var container = new MariaDBContainer<>("aarch64".equals(osArch) ? armImageName : mariadb); |
| 127 | + |
| 128 | + container.start(); |
| 129 | + |
| 130 | + testContainerDatabase = ProvidedDatabase.builder(container) // |
| 131 | + .username("root") // |
| 132 | + .database(container.getDatabaseName()) // |
| 133 | + .build(); |
| 134 | + } catch (IllegalStateException ise) { |
| 135 | + // docker not available. |
| 136 | + testContainerDatabase = ExternalDatabase.unavailable(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return testContainerDatabase; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Creates a new R2DBC MariaDB {@link ConnectionFactory} configured from the {@link ExternalDatabase}. |
| 145 | + */ |
| 146 | + public static ConnectionFactory createConnectionFactory(ExternalDatabase database) { |
| 147 | + |
| 148 | + ConnectionFactoryOptions options = ConnectionUtils.createOptions("mariadb", database); |
| 149 | + return new MariadbConnectionFactoryProvider().create(options); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Creates a new {@link DataSource} configured from the {@link ExternalDatabase}. |
| 154 | + */ |
| 155 | + @SneakyThrows |
| 156 | + public static DataSource createDataSource(ExternalDatabase database) { |
| 157 | + |
| 158 | + MariaDbDataSource dataSource = new MariaDbDataSource(); |
| 159 | + |
| 160 | + dataSource.setUser(database.getUsername()); |
| 161 | + dataSource.setPassword(database.getPassword()); |
| 162 | + dataSource.setUrl( |
| 163 | + String.format("jdbc:mariadb://%s:%d/%s?", database.getHostname(), database.getPort(), database.getDatabase())); |
| 164 | + |
| 165 | + return dataSource; |
| 166 | + } |
| 167 | +} |
0 commit comments