コード例 #1
0
  private void compareForeignKey(
      String tableName, ForeignKeyDescription xmlFk, ForeignKeyDescription dbFk) {
    assertTrue(
        "Table: "
            + tableName
            + "... Wrong ConstraintName! Expected ConstraintName: "
            + xmlFk.getConstraintName()
            + " databaseConstraintName: "
            + dbFk.getConstraintName(),
        xmlFk.getConstraintName().equalsIgnoreCase(dbFk.getConstraintName()));

    assertTrue(
        "Table: "
            + tableName
            + ", ConstraintName: "
            + xmlFk.getConstraintName()
            + "... Wrong ReferencedTable! Expected RefTable: "
            + xmlFk.getRefTableName()
            + " databaseRefTable: "
            + dbFk.getRefTableName(),
        xmlFk.getRefTableName().equalsIgnoreCase(dbFk.getRefTableName()));

    for (int j = 0; j < xmlFk.getColumnSize(); j++) {
      String xmlColumn = xmlFk.getColumn(j);
      String xmlRefColumn = xmlFk.getRefColumn(j);
      assertTrue(
          "Table: "
              + tableName
              + ", ConstraintName: "
              + xmlFk.getConstraintName()
              + "... Column not found! Expected Column: "
              + xmlColumn,
          dbFk.getColumn(xmlColumn) != -1);
      if (xmlRefColumn != null) {
        assertTrue(
            "Table: "
                + tableName
                + ", ConstraintName: "
                + xmlFk.getConstraintName()
                + "... ReferencedColumn not found! Expected Column: "
                + xmlRefColumn,
            dbFk.getRefColumn(xmlRefColumn) != -1);
      }
    }

    assertTrue(
        "Table: "
            + tableName
            + ", ConstraintName: "
            + dbFk.getConstraintName()
            + "... is defined on "
            + dbFk.getColumnSize()
            + " columns, but expected "
            + xmlFk.getColumnSize(),
        xmlFk.getColumnSize() == dbFk.getColumnSize());

    assertTrue(
        "Table: "
            + tableName
            + ", ConstraintName: "
            + dbFk.getConstraintName()
            + "... is referencing "
            + dbFk.getRefColumns().size()
            + " columns, but expected "
            + xmlFk.getRefColumns().size(),
        xmlFk.getRefColumns().size() == dbFk.getRefColumns().size());

    if (!StringUtils.equalsIgnoreCase(dbFk.getOnDeleteRule(), xmlFk.getOnDeleteRule())) {
      log(
          "Table: "
              + tableName
              + ", ConstraintName: "
              + dbFk.getConstraintName()
              + "... Different onDelete rules (found "
              + dbFk.getOnDeleteRule()
              + ", expected "
              + xmlFk.getOnDeleteRule()
              + ")");
    }
  }
コード例 #2
0
  protected void compareForeignKeyDescription(
      TableDescription xmlTableDescription, TableDescription databaseTableDescription) {
    // todo onDeleteRule not yet hard checked!
    String tableName = xmlTableDescription.getTableName();
    List<ForeignKeyDescription> unCheckedDatabaseFKs = new ArrayList<ForeignKeyDescription>();
    if (databaseTableDescription.getForeignKeys() != null) {
      unCheckedDatabaseFKs.addAll(databaseTableDescription.getForeignKeys());
    }
    List<ForeignKeyDescription> unnamedFKs =
        new ArrayList<ForeignKeyDescription>(xmlTableDescription.getForeignKeySize());
    for (int i = 0; i < xmlTableDescription.getForeignKeySize(); i++) {
      ForeignKeyDescription xmlForeignKeyDescription = xmlTableDescription.getForeignKey(i);
      if (xmlForeignKeyDescription.getConstraintName() == null
          || xmlForeignKeyDescription.getConstraintName().length() == 0) {
        unnamedFKs.add(xmlForeignKeyDescription); // check later...
        continue;
      }
      ForeignKeyDescription databaseForeignKeyDescription =
          databaseTableDescription.getForeignKey(xmlForeignKeyDescription.getConstraintName());
      unCheckedDatabaseFKs.remove(databaseForeignKeyDescription);
      if (databaseForeignKeyDescription != null) {
        compareForeignKey(tableName, xmlForeignKeyDescription, databaseForeignKeyDescription);
      } else
        assertTrue(
            "Table: "
                + tableName
                + "... ConstraintName not found! Expected ConstraintName: "
                + xmlForeignKeyDescription.getConstraintName(),
            false);
    }

    for (ForeignKeyDescription unnamedFK : unnamedFKs) {
      ForeignKeyDescription dbFk = databaseTableDescription.findForeignKeyLike(unnamedFK);
      if (dbFk != null) {
        unCheckedDatabaseFKs.remove(dbFk);
        if (!StringUtils.equalsIgnoreCase(dbFk.getOnDeleteRule(), unnamedFK.getOnDeleteRule())) {
          log(
              "Table: "
                  + tableName
                  + "... Different onDelete rules (found "
                  + dbFk.getOnDeleteRule()
                  + ", expected "
                  + unnamedFK.getOnDeleteRule()
                  + ") between foreign key on "
                  + dbFk.getColumns());
        }
      } else {
        assertTrue(
            "Table: "
                + tableName
                + "... Missing unnamed foreign key on "
                + unnamedFK.getColumns()
                + " referencing "
                + unnamedFK.getRefTableName()
                + "."
                + unnamedFK.getRefColumns(),
            false);
      }
    }

    for (ForeignKeyDescription uncheckedFK : unCheckedDatabaseFKs) {
      assertTrue(
          "Table: "
              + tableName
              + " contains unexpected foreign key named '"
              + uncheckedFK
              + " on columns "
              + uncheckedFK.getColumns()
              + "' referencing table '"
              + uncheckedFK.getRefTableName()
              + "'."
              + uncheckedFK.getRefColumns(),
          false);
    }
  }