001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.hibernate.util;
017:
018: import static junit.framework.Assert.assertTrue;
019: import org.hibernate.Session;
020: import org.hibernate.cfg.Configuration;
021: import org.hibernate.dialect.Dialect;
022: import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
023: import org.unitils.core.UnitilsException;
024:
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: /**
030: * Assert class that offers assert methods for testing things that are specific to Hibernate.
031: *
032: * @author Timmy Maris
033: * @author Filip Neven
034: * @author Tim Ducheyne
035: */
036: public class HibernateAssert {
037:
038: /**
039: * Checks if the mapping of the Hibernate managed objects with the database is still correct.
040: *
041: * @param configuration The hibernate config, not null
042: * @param session The hibernate session, not null
043: * @param databaseDialect The database dialect, not null
044: */
045: public static void assertMappingWithDatabaseConsistent(
046: Configuration configuration, Session session,
047: Dialect databaseDialect) {
048: String[] script = generateDatabaseUpdateScript(configuration,
049: session, databaseDialect);
050:
051: List<String> differences = new ArrayList<String>();
052: for (String line : script) {
053: // ignore constraints
054: if (line.indexOf("add constraint") == -1) {
055: differences.add(line);
056: }
057: }
058: assertTrue(
059: "Found mismatches between Java objects and database tables. Applying following DDL statements to the "
060: + "database should resolve the problem: \n"
061: + formatErrorMessage(differences), differences
062: .isEmpty());
063: }
064:
065: /**
066: * Generates a <code>String</code> array with DML statements based on the Hibernate mapping files.
067: *
068: * @param configuration The hibernate config, not null
069: * @param session The hibernate session, not null
070: * @param databaseDialect The database dialect, not null
071: * @return String[] array of DDL statements that were needed to keep the database in sync with the mapping file
072: */
073: private static String[] generateDatabaseUpdateScript(
074: Configuration configuration, Session session,
075: Dialect databaseDialect) {
076: try {
077: DatabaseMetadata dbm = new DatabaseMetadata(session
078: .connection(), databaseDialect);
079: return configuration.generateSchemaUpdateScript(
080: databaseDialect, dbm);
081: } catch (SQLException e) {
082: throw new UnitilsException(
083: "Could not retrieve database metadata", e);
084: }
085: }
086:
087: /**
088: * Formats the given list of messages.
089: *
090: * @param messageParts The different parts of the message
091: * @return A formatted message, containing the different message parts.
092: */
093: private static String formatErrorMessage(List<String> messageParts) {
094: StringBuffer message = new StringBuffer();
095: for (String messagePart : messageParts) {
096: message.append(messagePart);
097: message.append(";\n");
098: }
099: return message.toString();
100: }
101:
102: }
|