Search in sources :

Example 1 with PreparedStatementSpyConnectionProvider

use of org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider in project hibernate-orm by hibernate.

the class MySQLDropConstraintThrowsExceptionTest method testEnumTypeInterpretation.

@Test
public void testEnumTypeInterpretation() {
    final PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(false, false);
    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "update").applySetting(AvailableSettings.CONNECTION_PROVIDER, connectionProvider).build();
    SessionFactory sessionFactory = null;
    try {
        final Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).buildMetadata();
        sessionFactory = metadata.buildSessionFactory();
        List<String> alterStatements = connectionProvider.getExecuteStatements().stream().filter(sql -> sql.toLowerCase().contains("alter ")).map(String::trim).collect(Collectors.toList());
        if (metadata.getDatabase().getDialect() instanceof MariaDBDialect) {
            assertTrue(alterStatements.get(0).matches("alter table if exists CUSTOMER\\s+drop index .*?"));
            assertTrue(alterStatements.get(1).matches("alter table if exists CUSTOMER\\s+add constraint .*? unique \\(CUSTOMER_ID\\)"));
        } else {
            assertTrue(alterStatements.get(0).matches("alter table CUSTOMER\\s+drop index .*?"));
            assertTrue(alterStatements.get(1).matches("alter table CUSTOMER\\s+add constraint .*? unique \\(CUSTOMER_ID\\)"));
        }
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) MariaDBDialect(org.hibernate.dialect.MariaDBDialect) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) PreparedStatementSpyConnectionProvider(org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) BaseUnitTest(org.hibernate.testing.orm.junit.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SessionFactory (org.hibernate.SessionFactory)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 MariaDBDialect (org.hibernate.dialect.MariaDBDialect)1 PreparedStatementSpyConnectionProvider (org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider)1 BaseUnitTest (org.hibernate.testing.orm.junit.BaseUnitTest)1 Test (org.junit.jupiter.api.Test)1