01: /*
02: * DataStoreImporterTest.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.importer;
13:
14: import java.io.File;
15: import java.io.FileWriter;
16: import java.sql.Types;
17: import junit.framework.TestCase;
18: import workbench.TestUtil;
19: import workbench.gui.dialogs.dataimport.ImportOptions;
20: import workbench.gui.dialogs.dataimport.TextImportOptions;
21: import workbench.storage.DataStore;
22:
23: /**
24: *
25: * @author support@sql-workbench.net
26: */
27: public class DataStoreImporterTest extends TestCase {
28: private TestUtil util;
29:
30: public DataStoreImporterTest(String testName) throws Exception {
31: super (testName);
32: util = new TestUtil(testName);
33: util.prepareEnvironment();
34: }
35:
36: private DataStore prepareDataStore() {
37: String[] cols = new String[] { "ID", "FIRSTNAME", "LASTNAME" };
38: int[] types = new int[] { Types.INTEGER, Types.VARCHAR,
39: Types.VARCHAR };
40: int[] sizes = new int[] { 5, 25, 25 };
41:
42: DataStore ds = new DataStore(cols, types, sizes);
43: return ds;
44: }
45:
46: public void testImportFile() {
47: try {
48: String content = "id\tfirstname\tlastname\n1\tHarry\tHandsome\n2\tMary\tMoviestart\n3\tArthur\tDent";
49: File f = new File(util.getBaseDir(), "ds_import.txt");
50:
51: FileWriter w = new FileWriter(f);
52: w.write(content);
53: w.close();
54:
55: DataStore ds = prepareDataStore();
56: DataStoreImporter importer = new DataStoreImporter(ds,
57: null, null);
58:
59: TextImportOptions to = new DefaultTextImportOptions("\t",
60: "\"");
61: ImportOptions o = new DefaultImportOptions();
62:
63: importer.setImportOptions(f, ProducerFactory.IMPORT_TEXT,
64: o, to, null);
65: importer.startImport();
66: assertEquals("Wrong number of rows imported", 3, ds
67: .getRowCount());
68:
69: String name = ds.getValueAsString(0, 1);
70: assertEquals("Wrong firstname", "Harry", name);
71:
72: } catch (Exception e) {
73: e.printStackTrace();
74: fail(e.getMessage());
75: }
76:
77: }
78:
79: public void testImportString() {
80: try {
81: String content = "id\tfirstname\tlastname\n1\tHarry\tHandsome\n2\tMary\tMoviestart\n3\tArthur\tDent";
82: DataStore ds = prepareDataStore();
83: DataStoreImporter importer = new DataStoreImporter(ds,
84: null, null);
85: importer.importString(content);
86: importer.startImport();
87: assertEquals("Wrong number of rows imported", 3, ds
88: .getRowCount());
89:
90: String name = ds.getValueAsString(0, 1);
91: assertEquals("Wrong firstname", "Harry", name);
92:
93: } catch (Exception e) {
94: e.printStackTrace();
95: fail(e.getMessage());
96: }
97: }
98:
99: }
|