1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
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
89 }
90
91 }
92 }