TableFormatter.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2008, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.util;

  22. import org.dbunit.dataset.Column;
  23. import org.dbunit.dataset.DataSetException;
  24. import org.dbunit.dataset.ITable;
  25. import org.dbunit.dataset.ITableMetaData;

  26. /**
  27.  * Simple formatter to print out {@link ITable} objects in a beautiful way, for
  28.  * example on a console.
  29.  *
  30.  * @author gommma (gommma AT users.sourceforge.net)
  31.  * @author Last changed by: $Author$
  32.  * @version $Revision$ $Date$
  33.  * @since 2.4.1
  34.  */
  35. public class TableFormatter
  36. {

  37.     public TableFormatter()
  38.     {

  39.     }

  40.     /**
  41.      * Pads the given String with the given <code>padChar</code> up to the
  42.      * given
  43.      * <code>length</code>.
  44.      *
  45.      * @param s
  46.      * @param length
  47.      * @param padChar
  48.      * @return The padded string
  49.      */
  50.     public static final String padLeft(String s, int length, char padChar)
  51.     {
  52.         String result = s;

  53.         char[] padCharArray = getPadCharArray(s, length, padChar);
  54.         if (padCharArray != null)
  55.             result = pad(s, padCharArray, true);

  56.         return result;
  57.     }

  58.     /**
  59.      * Pads the given String with the given <code>padChar</code> up to the given
  60.      * <code>length</code>.
  61.      *
  62.      * @param s
  63.      * @param length
  64.      * @param padChar
  65.      * @return The padded string
  66.      */
  67.     public static final String padRight(String s, int length, char padChar)
  68.     {
  69.         String result = s;

  70.         char[] padCharArray = getPadCharArray(s, length, padChar);
  71.         if (padCharArray != null)
  72.             result = pad(s, padCharArray, false);

  73.         return result;
  74.     }

  75.     private static final char[] getPadCharArray(String s, int length,
  76.             char padChar)
  77.     {
  78.         if (length > 0 && length > s.length())
  79.         {
  80.             int padCount = length - s.length();
  81.             char[] padArray = new char[padCount];
  82.             for (int i = 0; i < padArray.length; i++)
  83.             {
  84.                 padArray[i] = padChar;
  85.             }
  86.             return padArray;
  87.         } else
  88.         {
  89.             return null;
  90.         }
  91.     }

  92.     private static final String pad(String s, char[] padArray, boolean padLeft)
  93.     {
  94.         final StringBuilder sb = new StringBuilder(s);
  95.         if (padLeft)
  96.         {
  97.             sb.insert(0, padArray);
  98.         } else
  99.         {
  100.             sb.append(padArray);
  101.         }
  102.         return sb.toString();
  103.     }

  104.     /**
  105.      * Formats a table with all data in a beautiful way. Can be useful to print
  106.      * out the table data on a console.
  107.      *
  108.      * @param table
  109.      *         The table to be formatted in a beautiful way
  110.      * @return The table data as a formatted String
  111.      * @throws DataSetException
  112.      */
  113.     public String format(ITable table) throws DataSetException
  114.     {
  115.         final StringBuilder sb = new StringBuilder();
  116.         ITableMetaData tableMetaData = table.getTableMetaData();
  117.         // Title line
  118.         sb.append("******");
  119.         sb.append(" table: ").append(tableMetaData.getTableName()).append(" ");
  120.         sb.append("**");
  121.         sb.append(" row count: ").append(table.getRowCount()).append(" ");
  122.         sb.append("******");
  123.         sb.append("\n");

  124.         // Column headers
  125.         int width = 20;
  126.         Column[] cols = tableMetaData.getColumns();
  127.         for (int i = 0; i < cols.length; i++)
  128.         {
  129.             sb.append(padRight(cols[i].getColumnName(), width, ' '));
  130.             sb.append("|");
  131.         }
  132.         sb.append("\n");

  133.         // Separator
  134.         for (int i = 0; i < cols.length; i++)
  135.         {
  136.             sb.append(padRight("", width, '='));
  137.             sb.append("|");
  138.         }
  139.         sb.append("\n");

  140.         // Values
  141.         for (int i = 0; i < table.getRowCount(); i++)
  142.         {
  143.             for (int j = 0; j < cols.length; j++)
  144.             {
  145.                 Object value = table.getValue(i, cols[j].getColumnName());
  146.                 String stringValue = String.valueOf(value);
  147.                 sb.append(padRight(stringValue, 20, ' '));
  148.                 sb.append("|");
  149.             }
  150.             // New row
  151.             sb.append("\n");
  152.         }

  153.         return sb.toString();
  154.     }

  155. }