<commit_before>package liquibase.dbtest;

import liquibase.CatalogAndSchema;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.OracleDatabase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.datatype.DataTypeFactory;
import liquibase.diff.DiffGeneratorFactory;
import liquibase.diff.DiffResult;
import liquibase.diff.compare.CompareControl;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.DiffToChangeLog;
import liquibase.diff.output.report.DiffToReport;
import liquibase.exception.ChangeLogParseException;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.exception.ValidationFailedException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.lockservice.LockService;
import liquibase.lockservice.LockServiceFactory;
import liquibase.logging.LogFactory;
import liquibase.resource.CompositeResourceAccessor;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.servicelocator.ServiceLocator;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.SnapshotControl;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.core.DropTableStatement;
import liquibase.structure.core.*;
import liquibase.test.*;
import liquibase.util.RegexMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import static org.junit.Assert.*;

/**
 * Base class for all database integration tests.  There is an AbstractIntegrationTest subclass for each supported database.
 * The database is assumed to exist at the host returned by getDatabaseServerHostname.  Currently this is hardcoded to an integration test server
 * at liquibase world headquarters.  Feel free to change the return value, but don't check it in.  We are going to improve the config of this at some point.
 */
public abstract class AbstractIntegrationTest {
    protected String completeChangeLog;
    private String rollbackChangeLog;
    private String includedChangeLog;
    private String encodingChangeLog;
    private String externalfkInitChangeLog;
    private String externalEntityChangeLog;
    private String externalEntityChangeLog2;
    private String invalidReferenceChangeLog;
    private String objectQuotingStrategyChangeLog;

    protected String contexts = "test, context-b";
    private Database database;
    private String jdbcUrl;
    protected String username;
    protected String password;

    protected AbstractIntegrationTest(String changelogDir, Database dbms) throws Exception {
        LogFactory.getInstance().getLog().setLogLevel("info");

        this.completeChangeLog = "changelogs/" + changelogDir + "/complete/root.changelog.xml";
        this.rollbackChangeLog = "changelogs/" + changelogDir + "/rollback/rollbackable.changelog.xml";
        this.includedChangeLog = "changelogs/" + changelogDir + "/complete/included.changelog.xml";
        this.encodingChangeLog = "changelogs/common/encoding.changelog.xml";
        this.externalfkInitChangeLog= "changelogs/common/externalfk.init.changelog.xml";
        this.externalEntityChangeLog= "changelogs/common/externalEntity.changelog.xml";
        this.externalEntityChangeLog2= "com/example/nonIncluded/externalEntity.changelog.xml";
        this.invalidReferenceChangeLog= "changelogs/common/invalid.reference.changelog.xml";
        this.objectQuotingStrategyChangeLog = "changelogs/common/object.quoting.strategy.changelog.xml";

        // Set default username and password to lbuser/lbuser,
        // except for hsqldb, which needs sa/<empty password>
        this.setUsername( dbms.getShortName().equalsIgnoreCase("hsqldb") ? "sa" : "lbuser");
        this.setPassword( dbms.getShortName().equalsIgnoreCase("hsqldb") ? "" : "lbuser");

        // Get the integration test properties for both global settings and (if applicable) local overrides.
        Properties integrationTestProperties;
        integrationTestProperties = new Properties();
        integrationTestProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("liquibase/liquibase.integrationtest.properties"));
        InputStream localProperties=Thread.currentThread().getContextClassLoader().getResourceAsStream("liquibase/liquibase.integrationtest.local.properties");
        if(localProperties!=null)
            integrationTestProperties.load(localProperties);

        // Login username
        String username = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".username");
        if(username==null)
            username=integrationTestProperties.getProperty("integration.test.username");
        this.setUsername(username);

        // Login password
        String password = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".password");
        if(password==null)
            password=integrationTestProperties.getProperty("integration.test.password");
        this.setPassword(password);

        // JDBC URL (no global default so all databases!)
        String url = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".url");
        if ( (url==null) || (url.length()) == 0) {
            throw new LiquibaseException("No JDBC URL found for integration test of database type " + dbms.getShortName());
        }
        this.setJdbcUrl(url);

        ServiceLocator.getInstance().setResourceAccessor(TestContext.getInstance().getTestResourceAccessor());
    }

    private void openConnection(String url, String username, String password) throws Exception {
        DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(url, username, password);

        if (connection != null) {
            database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        }
    }

    /**
     * Clear all test schemas between each test
     * @throws Exception If an unhandld exception occurs during test setup
     */
    @Before
    public void setUp() throws Exception {

        openConnection(getJdbcUrl(), getUsername(), getPassword());

        if (database != null) {
            if (!database.getConnection().getAutoCommit()) {
                database.rollback();
            }

            SnapshotGeneratorFactory.resetAll();
            ExecutorService.getInstance().reset();

            LockServiceFactory.getInstance().resetAll();<commit_msg>Change integration tests that cannot be executed due to unavailable DB connections from "successful" to "ignored" to prevent a false sense of security.<commit_after>package liquibase.dbtest;

import liquibase.CatalogAndSchema;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.OracleDatabase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.datatype.DataTypeFactory;
import liquibase.diff.DiffGeneratorFactory;
import liquibase.diff.DiffResult;
import liquibase.diff.compare.CompareControl;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.DiffToChangeLog;
import liquibase.diff.output.report.DiffToReport;
import liquibase.exception.ChangeLogParseException;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.exception.ValidationFailedException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.lockservice.LockService;
import liquibase.lockservice.LockServiceFactory;
import liquibase.logging.LogFactory;
import liquibase.resource.CompositeResourceAccessor;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.servicelocator.ServiceLocator;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.SnapshotControl;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.core.DropTableStatement;
import liquibase.structure.core.*;
import liquibase.test.*;
import liquibase.util.RegexMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import static org.junit.Assert.*;

/**
 * Base class for all database integration tests.  There is an AbstractIntegrationTest subclass for each supported database.
 * The database is assumed to exist at the host returned by getDatabaseServerHostname.  Currently this is hardcoded to an integration test server
 * at liquibase world headquarters.  Feel free to change the return value, but don't check it in.  We are going to improve the config of this at some point.
 */
public abstract class AbstractIntegrationTest {
    protected String completeChangeLog;
    private String rollbackChangeLog;
    private String includedChangeLog;
    private String encodingChangeLog;
    private String externalfkInitChangeLog;
    private String externalEntityChangeLog;
    private String externalEntityChangeLog2;
    private String invalidReferenceChangeLog;
    private String objectQuotingStrategyChangeLog;

    protected String contexts = "test, context-b";
    private Database database;
    private String jdbcUrl;
    protected String username;
    protected String password;

    protected AbstractIntegrationTest(String changelogDir, Database dbms) throws Exception {
        LogFactory.getInstance().getLog().setLogLevel("info");

        this.completeChangeLog = "changelogs/" + changelogDir + "/complete/root.changelog.xml";
        this.rollbackChangeLog = "changelogs/" + changelogDir + "/rollback/rollbackable.changelog.xml";
        this.includedChangeLog = "changelogs/" + changelogDir + "/complete/included.changelog.xml";
        this.encodingChangeLog = "changelogs/common/encoding.changelog.xml";
        this.externalfkInitChangeLog= "changelogs/common/externalfk.init.changelog.xml";
        this.externalEntityChangeLog= "changelogs/common/externalEntity.changelog.xml";
        this.externalEntityChangeLog2= "com/example/nonIncluded/externalEntity.changelog.xml";
        this.invalidReferenceChangeLog= "changelogs/common/invalid.reference.changelog.xml";
        this.objectQuotingStrategyChangeLog = "changelogs/common/object.quoting.strategy.changelog.xml";

        // Set default username and password to lbuser/lbuser,
        // except for hsqldb, which needs sa/<empty password>
        this.setUsername( dbms.getShortName().equalsIgnoreCase("hsqldb") ? "sa" : "lbuser");
        this.setPassword( dbms.getShortName().equalsIgnoreCase("hsqldb") ? "" : "lbuser");

        // Get the integration test properties for both global settings and (if applicable) local overrides.
        Properties integrationTestProperties;
        integrationTestProperties = new Properties();
        integrationTestProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("liquibase/liquibase.integrationtest.properties"));
        InputStream localProperties=Thread.currentThread().getContextClassLoader().getResourceAsStream("liquibase/liquibase.integrationtest.local.properties");
        if(localProperties!=null)
            integrationTestProperties.load(localProperties);

        // Login username
        String username = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".username");
        if(username==null)
            username=integrationTestProperties.getProperty("integration.test.username");
        this.setUsername(username);

        // Login password
        String password = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".password");
        if(password==null)
            password=integrationTestProperties.getProperty("integration.test.password");
        this.setPassword(password);

        // JDBC URL (no global default so all databases!)
        String url = integrationTestProperties.getProperty("integration.test." + dbms.getShortName() + ".url");
        if ( (url==null) || (url.length()) == 0) {
            throw new LiquibaseException("No JDBC URL found for integration test of database type " + dbms.getShortName());
        }
        this.setJdbcUrl(url);

        ServiceLocator.getInstance().setResourceAccessor(TestContext.getInstance().getTestResourceAccessor());
    }

    private void openConnection(String url, String username, String password) throws Exception {
        DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(url, username, password);

        if (connection != null) {
            database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        }
    }

    /**
     * Clear all test schemas between each test
     * @throws Exception If an unhandld exception occurs during test setup
     */
    @Before
    public void setUp() throws Exception {
        // Try to get a connection to the DBMS (or start the embedded DBMS types)
        openConnection(getJdbcUrl(), getUsername(), getPassword());

        // Do not count the test as successful if we skip it because of a failed login. Count it as skipped instead.
        org.junit.Assume.assumeTrue(database != null);

        if (database != null) {
            if (!database.getConnection().getAutoCommit()) {
                database.rollback();
            }

            SnapshotGeneratorFactory.resetAll();
            ExecutorService.getInstance().reset();

            LockServiceFactory.getInstance().resetAll();
{'fraction_non_alphanumeric': 0.09078781070074428, 'fraction_numerical': 0.0004212891447830361, 'mean_word_length': 4.380808462410275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '1120625', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3517, 'n_words': 843}