1 21 package org.dbunit.dataset.stream; 22 23 import junit.framework.TestCase; 24 import org.dbunit.dataset.Column; 25 import org.dbunit.dataset.datatype.DataType; 26 27 32 public abstract class AbstractProducerTest extends TestCase 33 { 34 private static final String [] TABLE_NAMES = { 35 "DUPLICATE_TABLE", 36 "SECOND_TABLE", 37 "TEST_TABLE", 38 "DUPLICATE_TABLE", 39 "NOT_NULL_TABLE", 40 "EMPTY_TABLE", 41 }; 42 43 public AbstractProducerTest(String s) 44 { 45 super(s); 46 } 47 48 protected String [] getExpectedNames() throws Exception 49 { 50 return (String [])TABLE_NAMES.clone(); 51 } 52 53 protected int[] getExpectedRowCount() throws Exception 54 { 55 return new int[] {1, 2, 3, 2, 1, 0}; 56 } 57 58 protected String getNotNullTableName() throws Exception 59 { 60 return "NOT_NULL_TABLE"; 61 } 62 63 protected Column[] createExpectedColumns(Column.Nullable nullable) throws Exception 64 { 65 Column[] columns = new Column[4]; 66 for (int i = 0; i < columns.length; i++) 67 { 68 columns[i] = new Column("COLUMN" + i, DataType.UNKNOWN, nullable); 69 } 70 return columns; 71 } 72 73 protected Object [] createExpectedRow(int row) throws Exception 74 { 75 Object [] values = new Object [4]; 76 for (int i = 0; i < values.length; i++) 77 { 78 values[i] = "row " + row + " col " + i; 79 } 80 return values; 81 } 82 83 protected abstract IDataSetProducer createProducer() throws Exception ; 84 85 public void testProduce() throws Exception 86 { 87 MockDataSetConsumer consumer = new MockDataSetConsumer(); 89 consumer.addExpectedStartDataSet(); 90 String [] expectedNames = getExpectedNames(); 91 int[] rowCounts = getExpectedRowCount(); 92 for (int i = 0; i < expectedNames.length; i++) 93 { 94 String expectedName = expectedNames[i]; 95 Column.Nullable nullable = expectedName.equals(getNotNullTableName()) ? 96 Column.NO_NULLS : Column.NULLABLE; 97 Column[] expectedColumns = createExpectedColumns(nullable); 98 99 consumer.addExpectedStartTable(expectedName, expectedColumns); 100 for (int j = 0; j < rowCounts[i]; j++) 101 { 102 consumer.addExpectedRow(expectedName, createExpectedRow(j)); 103 } 104 consumer.addExpectedEndTable(expectedName); 105 } 106 consumer.addExpectedEndDataSet(); 107 108 IDataSetProducer producer = createProducer(); 110 producer.setConsumer(consumer); 111 112 producer.produce(); 114 consumer.verify(); 115 } 116 117 public void testProduceWithoutConsumer() throws Exception 118 { 119 IDataSetProducer producer = createProducer(); 120 producer.produce(); 121 } 122 123 } 124 | Popular Tags |