Java tutorial
/* * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mariadb.embedded; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.fail; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.SQLException; import org.junit.After; import org.junit.Test; import org.slf4j.plus.ExceptionLogger; import org.slf4j.plus.ExceptionLoggerFactory; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.util.ReflectionUtils; import com.mariadb.embedded.exception.MariadbServerException; import ch.vorburger.mariadb4j.DBConfiguration; import lombok.NonNull; /** * {@link MariadbServer} tests */ public class MariadbServerTests { /** * {@link ExceptionLogger} */ private static final ExceptionLogger exceptionLogger = ExceptionLoggerFactory.getLogger(); /** * DB name */ private static final String DB_NAME = "foo"; /** * {@link MariadbServer} */ private MariadbServer server; /** * {@link After} */ @After public void after() { if (this.server != null) { this.server.stop(); } } /** * {@link MariadbServer#builder()} */ @Test public void builder() { // Basic setup { String baseDir = "baseDir"; String libDir = "libDir"; String dataDir = "dataDir"; String arg1 = "--max-connections=100"; String arg2 = "--max-connect-errors=10000"; MariadbServer server = MariadbServer.builder() /* @formatter:off */ .setPort(12345).setBaseDir(baseDir).setLibDir(libDir).setDataDir(dataDir) .addArgument(arg1, arg2) // @checkstyle:ok /* @formatter:on */ .build(); DBConfiguration config = this.getConfiguration(server); assertThat(config.getPort(), equalTo(12345)); assertThat(config.getBaseDir(), equalTo(baseDir)); assertThat(config.getLibDir(), equalTo(libDir)); assertThat(config.getDataDir(), equalTo(dataDir)); assertThat(config.getArgs().size(), equalTo(2)); assertThat(config.getArgs().get(0), equalTo(arg1)); assertThat(config.getArgs().get(1), equalTo(arg2)); } // Random port { DBConfiguration config = this.getConfiguration(MariadbServer.builder().build()); assertThat(config.getPort(), greaterThan(0)); } } /** * {@link MariadbServer#start()} and {@link MariadbServer#stop()} */ @Test public void startAndStop() { // Start { this.server = MariadbServer.builder().build().start(); assertThat(this.server.isRunning(), equalTo(true)); } // Already running try { this.server.start(); fail(); } catch (MariadbServerException e) { exceptionLogger.debug(e); } // Another start MariadbServer another = null; try { another = MariadbServer.builder().build().start(); } finally { if (another != null) { another.stop(); } } // Connection try (Connection connection = this.createConnection(this.server, DB_NAME)) { assertThat(connection.prepareCall("select 1").execute(), equalTo(true)); } catch (SQLException e) { exceptionLogger.debug(e); fail(); } // Stop { assertThat(this.server.stop(), equalTo(true)); assertThat(this.server.isRunning(), equalTo(false)); } } /** * Get {@link DBConfiguration} in {@link MariadbServer}. * * @param server {@link MariadbServer} * @return {@link DBConfiguration} */ private DBConfiguration getConfiguration(MariadbServer server) { Field field = ReflectionUtils.findField(MariadbServer.class, "config"); ReflectionUtils.makeAccessible(field); return (DBConfiguration) ReflectionUtils.getField(field, server); } /** * Create a new {@link Connection}. * * @param server {@link MariadbServer} * @param schema schema * @return {@link Connection} * @throws SQLException if failed to connect */ private Connection createConnection(@NonNull MariadbServer server, String schema) throws SQLException { return DataSourceBuilder.create() /* @formatter:off */ .url("jdbc:mysql://localhost:" + server.getPort() + "/" + schema + "?createDatabaseIfNotExist=true") .build() /* @formatter:on */ .getConnection(); } }