01: /*
02: * TextRowDataConverterTest.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.exporter;
13:
14: import java.sql.Types;
15: import java.util.ArrayList;
16: import java.util.Calendar;
17: import java.util.List;
18: import workbench.storage.ResultInfo;
19: import workbench.storage.RowData;
20: import workbench.util.StrBuffer;
21:
22: /**
23: *
24: * @author support@sql-workbench.net
25: */
26: public class TextRowDataConverterTest extends junit.framework.TestCase {
27:
28: public TextRowDataConverterTest(String testName) {
29: super (testName);
30: }
31:
32: public void testConvert() {
33: try {
34: String[] cols = new String[] { "char_col", "int_col",
35: "date_col", "ts_col" };
36: int[] types = new int[] { Types.VARCHAR, Types.INTEGER,
37: Types.DATE, Types.TIMESTAMP };
38: int[] sizes = new int[] { 10, 10, 10, 10 };
39:
40: ResultInfo info = new ResultInfo(cols, types, sizes);
41: TextRowDataConverter converter = new TextRowDataConverter();
42: converter.setDefaultTimestampFormat("yyyy-MM-dd HH:mm:ss");
43: converter.setDefaultDateFormat("yyyy-MM-dd");
44: converter.setWriteHeader(true);
45: converter.setResultInfo(info);
46: converter.setDelimiter(";");
47: StrBuffer header = converter.getStart();
48: assertNotNull(header);
49: assertEquals("Wrong header",
50: "char_col;int_col;date_col;ts_col", header
51: .toString().trim());
52:
53: RowData data = new RowData(info);
54: data.setValue(0, "data1");
55: data.setValue(1, new Integer(42));
56: Calendar c = Calendar.getInstance();
57: c.set(2006, 9, 26, 17, 0);
58: c.set(Calendar.SECOND, 0);
59: c.set(Calendar.MILLISECOND, 0);
60: java.util.Date d = c.getTime();
61: data.setValue(2, c.getTime());
62: java.sql.Timestamp ts = new java.sql.Timestamp(d.getTime());
63: data.setValue(3, ts);
64:
65: StrBuffer line = converter.convertRowData(data, 0);
66: assertEquals("Wrong columns exporter",
67: "data1;42;2006-10-26;2006-10-26 17:00:00", line
68: .toString().trim());
69:
70: List columns = new ArrayList();
71: columns.add(info.getColumn(0));
72: columns.add(info.getColumn(1));
73: converter.setColumnsToExport(columns);
74: line = converter.convertRowData(data, 0);
75: assertNotNull("Data not converted", line);
76: assertEquals("Wrong columns exporter", "data1;42", line
77: .toString().trim());
78: } catch (Exception e) {
79: e.printStackTrace();
80: fail(e.getMessage());
81: }
82: }
83: }
|