Search in sources :

Example 1 with AbstractSchemaMigrator

use of org.hibernate.tool.schema.internal.AbstractSchemaMigrator in project hibernate-orm by hibernate.

the class CheckForExistingForeignKeyTest method testCheckForExistingForeignKeyOne2One.

/**
 * Check detection of existing foreign key with the same mappings for a simple mapping (table1.objectId =>
 * table2.id).
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testCheckForExistingForeignKeyOne2One() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    // Make sure the match is not successful based on key name
    foreignKey.setName("objectId2id_1");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(getForeignKeyInformation("table2", "id", "object2Id_2"));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    AbstractSchemaMigrator schemaMigrator = new SchemaMigrator();
    // Check single-column-key to single-column-key, existing (table1.objectId => table2.id)
    boolean found = (boolean) method.invoke(schemaMigrator, foreignKey, tableInformation);
    Assert.assertTrue("Key should be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Test(org.junit.Test)

Example 2 with AbstractSchemaMigrator

use of org.hibernate.tool.schema.internal.AbstractSchemaMigrator in project hibernate-orm by hibernate.

the class CheckForExistingForeignKeyTest method testCheckForNotExistingForeignKeyOne2One.

/**
 * Check detection of not existing foreign key with the same mappings for a simple mapping (table1.objectId =>
 * table2.id).
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testCheckForNotExistingForeignKeyOne2One() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    // Make sure the match is not successful based on key name
    foreignKey.setName("objectId2id_1");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(getForeignKeyInformation("table2", "blah", "blahKey_001"));
    fks.add(getForeignKeyInformation("table3", "id", "blahKey_002"));
    fks.add(getForeignKeyInformation("table3", "blah", "blahKey_003"));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    AbstractSchemaMigrator schemaMigrator = new SchemaMigrator();
    // Check single-column-key to single-column-key, existing (table1.objectId => table2.id)
    boolean found = (boolean) method.invoke(schemaMigrator, foreignKey, tableInformation);
    Assert.assertFalse("Key should not be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Test(org.junit.Test)

Aggregations

Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 Name (org.hibernate.boot.model.relational.Namespace.Name)2 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)2 IdentifierHelper (org.hibernate.engine.jdbc.env.spi.IdentifierHelper)2 Column (org.hibernate.mapping.Column)2 ForeignKey (org.hibernate.mapping.ForeignKey)2 Table (org.hibernate.mapping.Table)2 TableInformationImpl (org.hibernate.tool.schema.extract.internal.TableInformationImpl)2 ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)2 InformationExtractor (org.hibernate.tool.schema.extract.spi.InformationExtractor)2 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)2 AbstractSchemaMigrator (org.hibernate.tool.schema.internal.AbstractSchemaMigrator)2 Test (org.junit.Test)2