<commit_before>/*
 * Copyright 2012-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 org.springframework.boot.autoconfigure.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Logger;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.mock;

/**
 * Tests for {@link DataSourceAutoConfiguration}.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class DataSourceAutoConfigurationTests {

	@Rule
	public final ExpectedException thrown = ExpectedException.none();

	private ConfigurableApplicationContext context;

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	public void testDefaultDataSourceExists() throws Exception {
		load();
		assertThat(this.context.getBean(DataSource.class)).isNotNull();
	}

	@Test
	public void testDataSourceHasEmbeddedDefault() throws Exception {
		load();
		HikariDataSource dataSource = this.context.getBean(HikariDataSource.class);
		assertThat(dataSource.getJdbcUrl()).isNotNull();
		assertThat(dataSource.getDriverClassName()).isNotNull();
	}

	@Test
	public void testBadUrl() throws Exception {
		try {
			EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
			this.thrown.expect(BeanCreationException.class);
			load("spring.datasource.url:jdbc:not-going-to-work");
		}
		finally {
			EmbeddedDatabaseConnection.override = null;
		}
	}

	@Test
	public void testBadDriverClass() throws Exception {
		this.thrown.expect(BeanCreationException.class);
		this.thrown.expectMessage(contains("org.none.jdbcDriver"));
		load("spring.datasource.driverClassName:org.none.jdbcDriver");
	}

	@Test
	public void hikariValidatesConnectionByDefault() throws Exception {
		HikariDataSource dataSource = autoConfigureDataSource(HikariDataSource.class,
				"org.apache.tomcat");
		assertThat(dataSource.getConnectionTestQuery()).isNull();
		// Use Connection#isValid()
	}

	@Test
	public void tomcatIsFallback() throws Exception {
		org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource(
				org.apache.tomcat.jdbc.pool.DataSource.class, "com.zaxxer.hikari");
		assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
	}

	@Test
	public void tomcatValidatesConnectionByDefault() {<commit_msg>Fix wrong assertion<commit_after>/*
 * Copyright 2012-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 org.springframework.boot.autoconfigure.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Logger;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
 * Tests for {@link DataSourceAutoConfiguration}.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class DataSourceAutoConfigurationTests {

	@Rule
	public final ExpectedException thrown = ExpectedException.none();

	private ConfigurableApplicationContext context;

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	public void testDefaultDataSourceExists() throws Exception {
		load();
		assertThat(this.context.getBean(DataSource.class)).isNotNull();
	}

	@Test
	public void testDataSourceHasEmbeddedDefault() throws Exception {
		load();
		HikariDataSource dataSource = this.context.getBean(HikariDataSource.class);
		assertThat(dataSource.getJdbcUrl()).isNotNull();
		assertThat(dataSource.getDriverClassName()).isNotNull();
	}

	@Test
	public void testBadUrl() throws Exception {
		try {
			EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
			this.thrown.expect(BeanCreationException.class);
			load("spring.datasource.url:jdbc:not-going-to-work");
		}
		finally {
			EmbeddedDatabaseConnection.override = null;
		}
	}

	@Test
	public void testBadDriverClass() throws Exception {
		this.thrown.expect(BeanCreationException.class);
		this.thrown.expectMessage("org.none.jdbcDriver");
		load("spring.datasource.driverClassName:org.none.jdbcDriver");
	}

	@Test
	public void hikariValidatesConnectionByDefault() throws Exception {
		HikariDataSource dataSource = autoConfigureDataSource(HikariDataSource.class,
				"org.apache.tomcat");
		assertThat(dataSource.getConnectionTestQuery()).isNull();
		// Use Connection#isValid()
	}

	@Test
	public void tomcatIsFallback() throws Exception {
		org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource(
				org.apache.tomcat.jdbc.pool.DataSource.class, "com.zaxxer.hikari");
		assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
	}

	@Test
	public void tomcatValidatesConnectionByDefault() {
{'fraction_non_alphanumeric': 0.10183353119641209, 'fraction_numerical': 0.0034296266983247594, 'mean_word_length': 5.5305770887166235, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2150475', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2120, 'n_words': 485}