Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2008,  Unitils.org
  *
  * 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.unitils.orm.hibernate.util;
import static junit.framework.Assert.assertTrue;
Assert class that offers assert methods for testing things that are specific to Hibernate.

Author(s):
Timmy Maris
Filip Neven
Tim Ducheyne
public class HibernateAssert {


    
Checks if the mapping of the Hibernate managed objects with the database is still correct.

Parameters:
configuration The hibernate config, not null
session The hibernate session, not null
databaseDialect The database dialect, not null
    public static void assertMappingWithDatabaseConsistent(Configuration configurationSession sessionDialect databaseDialect) {
        String[] script = generateDatabaseUpdateScript(configurationsessiondatabaseDialect);
        List<Stringdifferences = new ArrayList<String>();
        for (String line : script) {
            // ignore constraints
            if (line.indexOf("add constraint") == -1) {
                differences.add(line);
            }
        }
        assertTrue("Found mismatches between Java objects and database tables. Applying following DDL statements to the " +
                "database should resolve the problem: \n" + formatErrorMessage(differences), differences.isEmpty());
    }


    
Generates a String array with DML statements based on the Hibernate mapping files.

Parameters:
configuration The hibernate config, not null
session The hibernate session, not null
databaseDialect The database dialect, not null
Returns:
String[] array of DDL statements that were needed to keep the database in sync with the mapping file
    private static String[] generateDatabaseUpdateScript(Configuration configurationSession sessionDialect databaseDialect) {
        try {
            DatabaseMetadata dbm = new DatabaseMetadata(session.connection(), databaseDialect);
            return configuration.generateSchemaUpdateScript(databaseDialectdbm);
        } catch (SQLException e) {
            throw new UnitilsException("Could not retrieve database metadata"e);
        }
    }


    
Formats the given list of messages.

Parameters:
messageParts The different parts of the message
Returns:
A formatted message, containing the different message parts.
    private static String formatErrorMessage(List<StringmessageParts) {
        StringBuffer message = new StringBuffer();
        for (String messagePart : messageParts) {
            message.append(messagePart);
            message.append(";\n");
        }
        return message.toString();
    }
New to GrepCode? Check out our FAQ X