View Javadoc
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.stream;
22  
23  import java.io.FileReader;
24  
25  import org.dbunit.dataset.DataSetException;
26  import org.dbunit.dataset.ForwardOnlyDataSetTest;
27  import org.dbunit.dataset.IDataSet;
28  import org.dbunit.dataset.xml.FlatXmlDataSetTest;
29  import org.dbunit.dataset.xml.FlatXmlProducer;
30  import org.xml.sax.InputSource;
31  
32  /**
33   * @author Manuel Laflamme
34   * @author Last changed by: $Author$
35   * @version $Revision$ $Date$
36   * @since 1.x (Apr 18, 2003)
37   */
38  public class StreamingDataSetTest extends ForwardOnlyDataSetTest
39  {
40      public StreamingDataSetTest(String s)
41      {
42          super(s);
43      }
44  
45      protected IDataSet createDataSet() throws Exception
46      {
47          IDataSetProducer source = new FlatXmlProducer(
48                  new InputSource(new FileReader(FlatXmlDataSetTest.DATASET_FILE)));
49          return new StreamingDataSet(source);
50      }
51  
52      protected IDataSet createDuplicateDataSet() throws Exception
53      {
54          return new StreamingDataSet(
55                  new DataSetProducerAdapter(super.createDuplicateDataSet()));
56      }
57      
58      public void testReturnsOnException() throws Exception
59      {
60      	RuntimeException exceptionToThrow = new IllegalArgumentException("For this test case we throw something that we normally would never do");
61      	ExceptionThrowingProducer source = new ExceptionThrowingProducer(exceptionToThrow);
62      	StreamingDataSet streamingDataSet = new StreamingDataSet(source);
63      	try {
64      		streamingDataSet.createIterator(false);
65      	}
66      	catch(DataSetException expected) {
67      		Throwable cause = expected.getCause();
68      		assertEquals(IllegalArgumentException.class, cause.getClass());
69      		assertEquals(exceptionToThrow, cause);
70      	}
71      }
72      
73      private static class ExceptionThrowingProducer implements IDataSetProducer
74      {
75      	private RuntimeException exceptionToThrow;
76      	
77  		public ExceptionThrowingProducer(RuntimeException exceptionToThrow) {
78  			super();
79  			this.exceptionToThrow = exceptionToThrow;
80  		}
81  
82  		public void produce() throws DataSetException {
83  			throw exceptionToThrow;
84  		}
85  
86  		public void setConsumer(IDataSetConsumer consumer)
87  				throws DataSetException {
88  			// Ignore for this test
89  		}
90      	
91      }
92  }