You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImportTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.util;
  14. import java.io.File;
  15. import java.lang.reflect.InvocationHandler;
  16. import java.lang.reflect.Method;
  17. import java.lang.reflect.Proxy;
  18. import java.sql.ResultSet;
  19. import java.sql.ResultSetMetaData;
  20. import java.sql.Types;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Map;
  25. import com.healthmarketscience.jackcess.Column;
  26. import com.healthmarketscience.jackcess.ColumnBuilder;
  27. import com.healthmarketscience.jackcess.DataType;
  28. import com.healthmarketscience.jackcess.Database;
  29. import static com.healthmarketscience.jackcess.Database.*;
  30. import com.healthmarketscience.jackcess.Table;
  31. import com.healthmarketscience.jackcess.TableBuilder;
  32. import com.healthmarketscience.jackcess.impl.JetFormatTest;
  33. import junit.framework.TestCase;
  34. import static com.healthmarketscience.jackcess.TestUtil.*;
  35. /**
  36. * @author Rob Di Marco
  37. */
  38. public class ImportTest extends TestCase
  39. {
  40. public ImportTest(String name) {
  41. super(name);
  42. }
  43. public void testImportFromFile() throws Exception
  44. {
  45. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  46. Database db = create(fileFormat);
  47. String tableName = new ImportUtil.Builder(db, "test")
  48. .setDelimiter("\\t")
  49. .importFile(new File("src/test/data/sample-input.tab"));
  50. Table t = db.getTable(tableName);
  51. List<String> colNames = new ArrayList<String>();
  52. for(Column c : t.getColumns()) {
  53. colNames.add(c.getName());
  54. }
  55. assertEquals(Arrays.asList("Test1", "Test2", "Test3"), colNames);
  56. List<? extends Map<String, Object>> expectedRows =
  57. createExpectedTable(
  58. createExpectedRow(
  59. "Test1", "Foo",
  60. "Test2", "Bar",
  61. "Test3", "Ralph"),
  62. createExpectedRow(
  63. "Test1", "S",
  64. "Test2", "Mouse",
  65. "Test3", "Rocks"),
  66. createExpectedRow(
  67. "Test1", "",
  68. "Test2", "Partial line",
  69. "Test3", null),
  70. createExpectedRow(
  71. "Test1", " Quoted Value",
  72. "Test2", " bazz ",
  73. "Test3", " Really \"Crazy" + ImportUtil.LINE_SEPARATOR
  74. + "value\""),
  75. createExpectedRow(
  76. "Test1", "buzz",
  77. "Test2", "embedded\tseparator",
  78. "Test3", "long")
  79. );
  80. assertTable(expectedRows, t);
  81. t = new TableBuilder("test2")
  82. .addColumn(new ColumnBuilder("T1", DataType.TEXT))
  83. .addColumn(new ColumnBuilder("T2", DataType.TEXT))
  84. .addColumn(new ColumnBuilder("T3", DataType.TEXT))
  85. .toTable(db);
  86. new ImportUtil.Builder(db, "test2")
  87. .setDelimiter("\\t")
  88. .setUseExistingTable(true)
  89. .setHeader(false)
  90. .importFile(new File("src/test/data/sample-input.tab"));
  91. expectedRows =
  92. createExpectedTable(
  93. createExpectedRow(
  94. "T1", "Test1",
  95. "T2", "Test2",
  96. "T3", "Test3"),
  97. createExpectedRow(
  98. "T1", "Foo",
  99. "T2", "Bar",
  100. "T3", "Ralph"),
  101. createExpectedRow(
  102. "T1", "S",
  103. "T2", "Mouse",
  104. "T3", "Rocks"),
  105. createExpectedRow(
  106. "T1", "",
  107. "T2", "Partial line",
  108. "T3", null),
  109. createExpectedRow(
  110. "T1", " Quoted Value",
  111. "T2", " bazz ",
  112. "T3", " Really \"Crazy" + ImportUtil.LINE_SEPARATOR
  113. + "value\""),
  114. createExpectedRow(
  115. "T1", "buzz",
  116. "T2", "embedded\tseparator",
  117. "T3", "long")
  118. );
  119. assertTable(expectedRows, t);
  120. ImportFilter oddFilter = new SimpleImportFilter() {
  121. private int _num;
  122. @Override
  123. public Object[] filterRow(Object[] row) {
  124. if((_num++ % 2) == 1) {
  125. return null;
  126. }
  127. return row;
  128. }
  129. };
  130. tableName = new ImportUtil.Builder(db, "test3")
  131. .setDelimiter("\\t")
  132. .setFilter(oddFilter)
  133. .importFile(new File("src/test/data/sample-input.tab"));
  134. t = db.getTable(tableName);
  135. colNames = new ArrayList<String>();
  136. for(Column c : t.getColumns()) {
  137. colNames.add(c.getName());
  138. }
  139. assertEquals(Arrays.asList("Test1", "Test2", "Test3"), colNames);
  140. expectedRows =
  141. createExpectedTable(
  142. createExpectedRow(
  143. "Test1", "Foo",
  144. "Test2", "Bar",
  145. "Test3", "Ralph"),
  146. createExpectedRow(
  147. "Test1", "",
  148. "Test2", "Partial line",
  149. "Test3", null),
  150. createExpectedRow(
  151. "Test1", "buzz",
  152. "Test2", "embedded\tseparator",
  153. "Test3", "long")
  154. );
  155. assertTable(expectedRows, t);
  156. db.close();
  157. }
  158. }
  159. public void testImportFromFileWithOnlyHeaders() throws Exception
  160. {
  161. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  162. Database db = create(fileFormat);
  163. String tableName = new ImportUtil.Builder(db, "test")
  164. .setDelimiter("\\t")
  165. .importFile(new File("src/test/data/sample-input-only-headers.tab"));
  166. Table t = db.getTable(tableName);
  167. List<String> colNames = new ArrayList<String>();
  168. for(Column c : t.getColumns()) {
  169. colNames.add(c.getName());
  170. }
  171. assertEquals(Arrays.asList(
  172. "RESULT_PHYS_ID", "FIRST", "MIDDLE", "LAST", "OUTLIER",
  173. "RANK", "CLAIM_COUNT", "PROCEDURE_COUNT",
  174. "WEIGHTED_CLAIM_COUNT", "WEIGHTED_PROCEDURE_COUNT"),
  175. colNames);
  176. db.close();
  177. }
  178. }
  179. public void testCopySqlHeaders() throws Exception
  180. {
  181. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  182. TestResultSet rs = new TestResultSet();
  183. rs.addColumn(Types.INTEGER, "col1");
  184. rs.addColumn(Types.VARCHAR, "col2", 60, 0, 0);
  185. rs.addColumn(Types.VARCHAR, "col3", 500, 0, 0);
  186. rs.addColumn(Types.BINARY, "col4", 128, 0, 0);
  187. rs.addColumn(Types.BINARY, "col5", 512, 0, 0);
  188. rs.addColumn(Types.NUMERIC, "col6", 0, 7, 15);
  189. rs.addColumn(Types.VARCHAR, "col7", Integer.MAX_VALUE, 0, 0);
  190. Database db = create(fileFormat);
  191. ImportUtil.importResultSet((ResultSet)Proxy.newProxyInstance(
  192. Thread.currentThread().getContextClassLoader(),
  193. new Class<?>[]{ResultSet.class},
  194. rs), db, "Test1");
  195. Table t = db.getTable("Test1");
  196. List<? extends Column> columns = t.getColumns();
  197. assertEquals(7, columns.size());
  198. Column c = columns.get(0);
  199. assertEquals("col1", c.getName());
  200. assertEquals(DataType.LONG, c.getType());
  201. c = columns.get(1);
  202. assertEquals("col2", c.getName());
  203. assertEquals(DataType.TEXT, c.getType());
  204. assertEquals(120, c.getLength());
  205. c = columns.get(2);
  206. assertEquals("col3", c.getName());
  207. assertEquals(DataType.MEMO, c.getType());
  208. assertEquals(0, c.getLength());
  209. c = columns.get(3);
  210. assertEquals("col4", c.getName());
  211. assertEquals(DataType.BINARY, c.getType());
  212. assertEquals(128, c.getLength());
  213. c = columns.get(4);
  214. assertEquals("col5", c.getName());
  215. assertEquals(DataType.OLE, c.getType());
  216. assertEquals(0, c.getLength());
  217. c = columns.get(5);
  218. assertEquals("col6", c.getName());
  219. assertEquals(DataType.NUMERIC, c.getType());
  220. assertEquals(17, c.getLength());
  221. assertEquals(7, c.getScale());
  222. assertEquals(15, c.getPrecision());
  223. c = columns.get(6);
  224. assertEquals("col7", c.getName());
  225. assertEquals(DataType.MEMO, c.getType());
  226. assertEquals(0, c.getLength());
  227. }
  228. }
  229. private static class TestResultSet implements InvocationHandler
  230. {
  231. private List<Integer> _types = new ArrayList<Integer>();
  232. private List<String> _names = new ArrayList<String>();
  233. private List<Integer> _displaySizes = new ArrayList<Integer>();
  234. private List<Integer> _scales = new ArrayList<Integer>();
  235. private List<Integer> _precisions = new ArrayList<Integer>();
  236. public Object invoke(Object proxy, Method method, Object[] args)
  237. {
  238. String methodName = method.getName();
  239. if(methodName.equals("getMetaData")) {
  240. return Proxy.newProxyInstance(
  241. Thread.currentThread().getContextClassLoader(),
  242. new Class<?>[]{ResultSetMetaData.class},
  243. this);
  244. } else if(methodName.equals("next")) {
  245. return Boolean.FALSE;
  246. } else if(methodName.equals("getColumnCount")) {
  247. return _types.size();
  248. } else if(methodName.equals("getColumnName") ||
  249. methodName.equals("getColumnLabel")) {
  250. return getValue(_names, args[0]);
  251. } else if(methodName.equals("getColumnDisplaySize")) {
  252. return getValue(_displaySizes, args[0]);
  253. } else if(methodName.equals("getColumnType")) {
  254. return getValue(_types, args[0]);
  255. } else if(methodName.equals("getScale")) {
  256. return getValue(_scales, args[0]);
  257. } else if(methodName.equals("getPrecision")) {
  258. return getValue(_precisions, args[0]);
  259. } else {
  260. throw new UnsupportedOperationException(methodName);
  261. }
  262. }
  263. public void addColumn(int type, String name)
  264. {
  265. addColumn(type, name, 0, 0, 0);
  266. }
  267. public void addColumn(int type, String name, int displaySize,
  268. int scale, int precision)
  269. {
  270. _types.add(type);
  271. _names.add(name);
  272. _displaySizes.add(displaySize);
  273. _scales.add(scale);
  274. _precisions.add(precision);
  275. }
  276. private static <T> T getValue(List<T> values, Object index) {
  277. return values.get((Integer)index - 1);
  278. }
  279. }
  280. }