7 Replies Latest reply on Nov 6, 2013 3:47 AM by Masao Kunii

    @ShouldMatchDataSet orderBy does not work properly

    Masao Kunii Newbie

      Hi,

       

      I am using Arquillian Persistence Extension 1.0.0.alpha6 with Arquillian 1.0.3.Final and found @ShouldMatchDataSet's orderBy does not work properly because of the bug of DBUnit about sorting(https://sourceforge.net/p/dbunit/bugs/347/).

       

      For example, there is testcase below: 

      @Test
      @UsingDataSet("seeding.xml")
      @ShouldMatchDataSet(value = "seeding.xml", orderBy = {"id"})
      public void should_entity_found_xls() {
      }
      

       

      seeding data is:

      <dataset>
         <TEST id="113" text="text113"/>
         <TEST id="1" text="text1"/>
         <TEST id="12" text="text12"/>
      </dataset>
      

       

      When column "id" is number data type, this testcase will be faild.

      java.lang.AssertionError: Test failed in 4 cases. 
      test | In row 1: expected value of id "113" but was "12".
      test | In row 1: expected value of text "text113" but was "text12".
      test | In row 2: expected value of id "12" but was "113".
      test | In row 2: expected value of text "text12" but was "text113".
      

       

      Actual database data is sorted properly like:

         <TEST id="1" text="text1"/>
         <TEST id="12" text="text12"/>
         <TEST id="113" text="text113"/>
      

       

      But expected data is sorted like:

         <TEST id="1" text="text1"/>
         <TEST id="113" text="text113"/>
         <TEST id="12" text="text12"/>
      

        

      Then APE will compare with bad ordered rows.

      If we avoid this problem, we have to do something below:

      • Only using string column to sort
      • Not sort expected dataset at line 86 org.jboss.arquillian.persistence.dbunit.DataSetComparator#compareContent

      -          final ITable expectedTable = sort(expectedDataSet, tableName, columnsForSorting);

      • Create a wrapper of Table object to sort and use it with meta data of current dataset at org.jboss.arquillian.persistence.dbunit.DataSetComparator#sort
      import org.dbunit.dataset.AbstractTable;
      import org.dbunit.dataset.DataSetException;
      import org.dbunit.dataset.ITable;
      import org.dbunit.dataset.ITableMetaData;
      
      
      public class TableWrapper extends AbstractTable {
        ITable table;
        ITableMetaData metaData;
        public TableWrapper(ITable table, ITableMetaData metaData) {
        this.table = table;
        this.metaData = metaData;
        }
        @Override
        public ITableMetaData getTableMetaData() {
        return metaData;
        }
        @Override
        public int getRowCount() {
        return table.getRowCount();
        }
        @Override
        public Object getValue(int row, String column) throws DataSetException {
        return table.getValue(row, column);
        }
      }
      

       

      At line 86 DataSetComparator#compareContent

      -          final ITable expectedTable = sort(expectedDataSet, tableName, columnsForSorting);
      +          final ITable expectedTable = sort(expectedDataSet.getTable(tableName), filteredCurrentDataSet.getTable(tableName), columnsForSorting);
      

       

      DataSetComparator#sort

      -   private ITable sort(IDataSet dataSet, String tableName, final List<String> columnsForSorting) throws DataSetException
      +   private ITable sort(ITable table, ITableMetaData tableMetaData, final List<String> columnsForSorting) throws DataSetException
          {
      -      final SortedTable sortedTable = new SortedTable(dataSet.getTable(tableName), toArray(columnsForSorting));
      +      final SortedTable sortedTable = new SortedTable(new TableWrapper(table, tableMetaData), toArray(columnsForSorting));
             sortedTable.setUseComparable(true);
             return sortedTable;
          }
      

       

      • Wait for the DBUnit bug fixed

       

      Or is there any work around for this?

       

       

      Thanks,

      Masao