YamlDataSet.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, 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.dataset.yaml;

  22. import org.dbunit.dataset.CachedDataSet;
  23. import org.dbunit.dataset.DataSetException;
  24. import org.dbunit.dataset.IDataSet;

  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.OutputStream;
  29. import java.io.OutputStreamWriter;
  30. import java.io.Writer;

  31. /**
  32.  * Reads and writes flat YAML-based dataset documents. Contrary to the flat XML layout,
  33.  * columns are calculated by parsing the entire data set, not just the first row.
  34.  * <br/><br/>
  35.  * The format looks like this:
  36.  * <br/>
  37.  * <pre>
  38.  * &lt;table_name&gt;:
  39.  *   - &lt;column&gt;: &lt;value&gt;
  40.  *     &lt;column&gt;: &lt;value&gt;
  41.  *             ...
  42.  *   - &lt;column&gt;: &lt;value&gt;
  43.  *     &lt;column&gt;: &lt;value&gt;
  44.  *             ...
  45.  *        ...
  46.  *    ...
  47.  * </pre>
  48.  * <br/>
  49.  * I.e.:
  50.  * <br/>
  51.  * <pre>
  52.  * TEST_TABLE:
  53.  *   - COL0: "row 0 col 0"
  54.  *     COL1: "row 0 col 1"
  55.  *     COL2: "row 0 col 2"
  56.  *   - COL1: "row 1 col 1"
  57.  * SECOND_TABLE:
  58.  *   - COL0: "row 0 col 0"
  59.  *     COL1: "row 0 col 1"
  60.  * EMPTY_TABLE: []
  61.  * </pre>
  62.  *
  63.  * @author Björn Beskow
  64.  * @version $Revision$ $Date$
  65.  */
  66. public class YamlDataSet extends CachedDataSet
  67. {

  68.     /**
  69.      * Creates a YAML dataset based on a yaml file
  70.      */
  71.     public YamlDataSet(File file) throws IOException, DataSetException
  72.     {
  73.         super(new YamlProducer(file), true);
  74.     }

  75.     /**
  76.      * Creates a YAML dataset based on an inputstream
  77.      *
  78.      * @param inputStream An inputstream pointing to a YAML dataset
  79.      */
  80.     public YamlDataSet(InputStream inputStream) throws DataSetException
  81.     {
  82.         super(new YamlProducer(inputStream), true);
  83.     }

  84.     /**
  85.      * Write the specified dataset to the specified output stream as YAML.
  86.      */
  87.     public static void write(IDataSet dataSet, OutputStream out)
  88.     throws DataSetException
  89.     {
  90.         write(dataSet, new OutputStreamWriter(out));
  91.     }

  92.     /**
  93.      * Write the specified dataset to the specified writer as YAML.
  94.      */
  95.     public static void write(IDataSet dataSet, Writer out)
  96.     throws DataSetException
  97.     {
  98.         YamlWriter yamlWriter = new YamlWriter(out);
  99.         yamlWriter.write(dataSet);
  100.     }

  101. }