001: package com.mockrunner.test.jdbc;
002:
003: import java.io.File;
004: import java.sql.SQLException;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import junit.framework.TestCase;
009:
010: import com.mockrunner.jdbc.FileResultSetFactory;
011: import com.mockrunner.mock.jdbc.MockResultSet;
012:
013: public class FileResultSetFactoryTest extends TestCase {
014: public void testGoodCreate() throws Exception {
015: FileResultSetFactory factory = new FileResultSetFactory(
016: "src/com/mockrunner/test/jdbc/testresult.txt");
017: MockResultSet resultSet = factory.create("");
018: doTestResultSet(factory, resultSet);
019: factory = new FileResultSetFactory(
020: "/com/mockrunner/test/jdbc/testresult.txt");
021: resultSet = factory.create("");
022: doTestResultSet(factory, resultSet);
023: factory = new FileResultSetFactory(
024: "com/mockrunner/test/jdbc/testresult.txt");
025: resultSet = factory.create("");
026: doTestResultSet(factory, resultSet);
027: factory = new FileResultSetFactory(new File(
028: "src/com/mockrunner/test/jdbc/testresult.txt"));
029: resultSet = factory.create("");
030: doTestResultSet(factory, resultSet);
031: }
032:
033: public void testBadCreate() throws Exception {
034: FileResultSetFactory factory = new FileResultSetFactory(
035: "not found");
036: try {
037: factory.create("");
038: fail();
039: } catch (RuntimeException exc) {
040: //should throw exception
041: }
042: factory = new FileResultSetFactory(new File("not found"));
043: try {
044: factory.create("");
045: fail();
046: } catch (RuntimeException exc) {
047: //should throw exception
048: }
049: }
050:
051: private void doTestResultSet(FileResultSetFactory factory,
052: MockResultSet resultSet) throws SQLException {
053: assertEquals(5, resultSet.getRowCount());
054: assertEquals(3, resultSet.getColumnCount());
055: resultSet.next();
056: assertEquals("TestColumn1", resultSet.getString(1));
057: assertEquals("TestColumn2", resultSet.getString(2));
058: assertEquals("TestColumn3", resultSet.getString(3));
059: resultSet.next();
060: assertEquals(1, resultSet.getInt(1));
061: assertEquals(3, resultSet.getLong("Column2"));
062: assertEquals(4, resultSet.getShort(3));
063: resultSet.next();
064: assertEquals("Entry1", resultSet.getObject(1));
065: assertEquals("Entry2", resultSet.getString(2));
066: assertEquals("Entry3", resultSet.getObject("Column3"));
067: resultSet.next();
068: assertEquals(25.3, resultSet.getDouble("Column1"), 0.01);
069: assertEquals(26.7, resultSet.getDouble(2), 0.01);
070: assertEquals(12.3, resultSet.getFloat(3), 0.01);
071: resultSet.next();
072: assertEquals("Test", resultSet.getString(1));
073: assertEquals(null, resultSet.getString(2));
074: assertEquals("Test", resultSet.getString(3));
075: factory.setFirstLineContainsColumnNames(true);
076: resultSet = factory.create("");
077: assertEquals(4, resultSet.getRowCount());
078: assertEquals(3, resultSet.getColumnCount());
079: resultSet.next();
080: assertEquals(1, resultSet.getInt("TestColumn1"));
081: assertEquals(3, resultSet.getLong("TestColumn2"));
082: assertEquals(4, resultSet.getShort("TestColumn3"));
083: resultSet.next();
084: assertEquals("Entry1", resultSet.getObject(1));
085: assertEquals("Entry2", resultSet.getString(2));
086: assertEquals("Entry3", resultSet.getObject(3));
087: }
088:
089: public void testCreateWithTemplates() throws Exception {
090: FileResultSetFactory factory = new FileResultSetFactory(
091: "src/com/mockrunner/test/jdbc/testtemplateresult.txt");
092: factory.setFirstLineContainsColumnNames(true);
093: MockResultSet resultSet = factory.create("");
094: doTestResultSetTemplatesDisabled(resultSet);
095: factory.setUseTemplates(true);
096: resultSet = factory.create("");
097: doTestResultSetDefaultTemplatesEnabled(resultSet);
098: Map customMap = new HashMap();
099: customMap.put("customMarker", "template1");
100: customMap.put("anotherCustomMarker", "template2");
101: factory.setTemplateConfiguration("%", customMap);
102: resultSet = factory.create("");
103: doTestResultSetCustomTemplatesEnabled(resultSet);
104: factory.setUseTemplates(false);
105: resultSet = factory.create("");
106: doTestResultSetTemplatesDisabled(resultSet);
107: }
108:
109: private void doTestResultSetTemplatesDisabled(
110: MockResultSet resultSet) throws SQLException {
111: assertEquals(2, resultSet.getRowCount());
112: assertEquals(3, resultSet.getColumnCount());
113: resultSet.next();
114: assertEquals("$defaultDate", resultSet.getString("TestColumn1"));
115: assertEquals("$defaultString", resultSet
116: .getString("TestColumn2"));
117: assertEquals("$defaultInteger", resultSet.getString(3));
118: resultSet.next();
119: assertEquals("%customMarker", resultSet.getObject(1));
120: assertEquals(null, resultSet.getString(2));
121: assertEquals("%anotherCustomMarker", resultSet
122: .getObject("TestColumn3"));
123: }
124:
125: private void doTestResultSetDefaultTemplatesEnabled(
126: MockResultSet resultSet) throws SQLException {
127: assertEquals(2, resultSet.getRowCount());
128: assertEquals(3, resultSet.getColumnCount());
129: resultSet.next();
130: assertEquals("1970-01-01", resultSet.getString(1));
131: assertEquals("", resultSet.getString("TestColumn2"));
132: assertEquals(0, resultSet.getInt("TestColumn3"));
133: resultSet.next();
134: assertEquals("%customMarker", resultSet.getObject(1));
135: assertEquals(null, resultSet.getString(2));
136: assertEquals("%anotherCustomMarker", resultSet
137: .getObject("TestColumn3"));
138: }
139:
140: private void doTestResultSetCustomTemplatesEnabled(
141: MockResultSet resultSet) throws SQLException {
142: assertEquals(2, resultSet.getRowCount());
143: assertEquals(3, resultSet.getColumnCount());
144: resultSet.next();
145: assertEquals("$defaultDate", resultSet.getString("TestColumn1"));
146: assertEquals("$defaultString", resultSet
147: .getString("TestColumn2"));
148: assertEquals("$defaultInteger", resultSet.getString(3));
149: resultSet.next();
150: assertEquals("template1", resultSet.getObject(1));
151: assertEquals(null, resultSet.getString("TestColumn2"));
152: assertEquals("template2", resultSet.getObject("TestColumn3"));
153: }
154:
155: public void testGetFile() {
156: FileResultSetFactory factory = new FileResultSetFactory(
157: "src/com/mockrunner/test/jdbc/testresult.txt");
158: assertEquals(new File(
159: "src/com/mockrunner/test/jdbc/testresult.txt"), factory
160: .getFile());
161: factory = new FileResultSetFactory(new File(
162: "src/com/mockrunner/test/jdbc/testresult.txt"));
163: assertEquals(new File(
164: "src/com/mockrunner/test/jdbc/testresult.txt"), factory
165: .getFile());
166: factory = new FileResultSetFactory("badfile");
167: try {
168: factory.getFile();
169: fail();
170: } catch (RuntimeException exc) {
171: //should throw exception
172: }
173: factory = new FileResultSetFactory(new File("badfile"));
174: try {
175: factory.getFile();
176: fail();
177: } catch (RuntimeException exc) {
178: //should throw exception
179: }
180: }
181: }
|