Skip to content

Commit

Permalink
HHH-13788 Test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Dec 19, 2019
1 parent 00ee6c1 commit b6f32a2
Showing 1 changed file with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,25 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.SchemaValidator;
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy;
import org.hibernate.tool.schema.TargetType;

import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.SkipLog;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -50,14 +40,16 @@
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-13788")
@RunWith(Parameterized.class)
public class SchemaUpdateTestWithUseJdbcMetadataDeaultsTest {
public class SchemaUpdateWithUseJdbcMetadataDefaultsSettingToFalseTest {

@Parameterized.Parameters
public static Collection<String> parameters() {
return Arrays.asList(
new String[] { JdbcMetadaAccessStrategy.GROUPED.toString(), JdbcMetadaAccessStrategy.INDIVIDUALLY.toString()}
);
public static String[] parameters() {
return new String[] {
JdbcMetadaAccessStrategy.GROUPED.toString(),
JdbcMetadaAccessStrategy.INDIVIDUALLY.toString()
};
}

@Parameterized.Parameter
Expand All @@ -73,8 +65,10 @@ public void setUp() throws IOException {
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder()
.applySetting( "hibernate.temp.use_jdbc_metadata_defaults", "false" )
.applySetting( AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true" )
.applySetting( AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, jdbcMetadataExtractorStrategy )
.applySetting(
AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY,
jdbcMetadataExtractorStrategy
)
.build();

final MetadataSources metadataSources = new MetadataSources( ssr );
Expand All @@ -85,34 +79,60 @@ public void setUp() throws IOException {
}

@After
public void tearsDown() {
public void tearDown() {
new SchemaExport().setHaltOnError( true )
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
.drop( EnumSet.of( TargetType.DATABASE ), metadata );
StandardServiceRegistryBuilder.destroy( ssr );
output.delete();
}

@Test
public void testSchemaUpdateWorksWhenUseJdbcMetadataDefaultsIsFalse() throws Exception {
public void testSchemaUpdateDoesNotTryToRecreateExistingTables()
throws Exception {
createSchema();

new SchemaUpdate().setHaltOnError( true )
.execute( EnumSet.of( TargetType.DATABASE ), metadata );
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );

new SchemaValidator().validate( metadata );
checkNoUpdateStatementHasBeenGenerated();
}

private void checkNoUpdateStatementHasBeenGenerated() throws IOException {
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertThat(
"The update output file should be empty because the db schema had already been generated and the domain model was not modified",
fileContent,
is( "" )
);
}

private void createSchema() throws Exception {
new SchemaUpdate().setHaltOnError( true )
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
new SchemaValidator().validate( metadata );
checkSchemaHasBeenGenerated();
output.delete();
}

final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertThat( "The update output file should be empty", fileContent, is( "" ) );
private void checkSchemaHasBeenGenerated() throws Exception {
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
Pattern fileContentPattern = Pattern.compile( "create( (column|row))? table test_entity" );
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
assertThat(
"The schema has not been correctly generated, Script file : " + fileContent.toLowerCase(),
fileContentMatcher.find(),
is( true )
);
}

@Entity
@Table(name = "TEST_ENTITY")
public static class TestEntity{
public static class TestEntity {
@Id
private Long id;

Expand Down

0 comments on commit b6f32a2

Please sign in to comment.