Reset sequence in DBUnit?

I want to reset the database and sequence after each test in Java + DBUnit /.

I saw this question, but don't have a code solution that I'm struggling to get with. How to use Oracle sequence numbers in DBUnit?

+4
source share
3 answers

I found the answer, it was in the official documentation. It was as simple as in the dataset that you use to prepare the database, add the reset_sequences attribute with a list of the ones you want to use reset.

 <?xml version='1.0' encoding='UTF-8'?>
    <dataset reset_sequences="emp_seq, dept_seq">
        <emp empno="1" ename="Scott" deptno="10" job="project manager" />
        ....
    </dataset>

, reset , reset . reset , . DatabaseOperation .

public static final DatabaseOperation SEQUENCE_RESETTER_POSTGRES = new DatabaseOperation() {
    @Override
    public void execute(IDatabaseConnection connection, IDataSet dataSet)
            throws DatabaseUnitException, SQLException {
        String[] tables = dataSet.getTableNames();
        Statement statement = connection.getConnection().createStatement();
        for (String table : tables) {
            int startWith = dataSet.getTable(table).getRowCount() + 1;
            statement.execute("alter sequence " + table + "_PK_SEQ RESTART WITH "+ startWith);

        }
    }
};

public static final DatabaseOperation SEQUENCE_RESETTER_ORACLE = new DatabaseOperation() {
    @Override
    public void execute(IDatabaseConnection connection, IDataSet dataSet)
            throws DatabaseUnitException, SQLException {
        String[] tables = dataSet.getTableNames();
        Statement statement = connection.getConnection().createStatement();
        for (String table : tables) {
            int startWith = dataSet.getTable(table).getRowCount() + 1;
            statement.execute("drop sequence " + table + "_PK_SEQ if exists");
            statement.execute("create sequence " + table + "_PK_SEQ START WITH " + startWith);
        }
    }
};
+11

, @Chexpir, / ( PostgreSQL). , reset 1 ( , )

public class ResetSequenceOperationDecorator extends DatabaseOperation {

    private DatabaseOperation decoree;

    public ResetSequenceOperationDecorator(DatabaseOperation decoree) {
         this.decoree = decoree;
     }

     @Override
     public void execute(IDatabaseConnection connection, IDataSet dataSet) throws DatabaseUnitException, SQLException {
         String[] tables = dataSet.getTableNames();
         Statement statement = connection.getConnection().createStatement();
         for (String table : tables) {
             try {
                 statement.execute("ALTER SEQUENCE " + table + "_id_seq RESTART WITH 1");
             }
             // Don't care because the sequence does not appear to exist (but catch it silently)
             catch(SQLException ex) {
             }
         }
         decoree.execute(connection, dataSet);
     }
}

DatabaseTestCase:

public abstract class AbstractDBTestCase extends DataSourceBasedDBTestCase {

    @Override
    protected DatabaseOperation getTearDownOperation() throws Exception {
        return new ResetSequenceOperationDecorator(DatabaseOperation.DELETE_ALL);
    }
}
+4
0

All Articles