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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.File;
  24. import java.lang.reflect.InvocationHandler;
  25. import java.lang.reflect.Method;
  26. import java.lang.reflect.Proxy;
  27. import java.sql.ResultSet;
  28. import java.sql.ResultSetMetaData;
  29. import java.sql.Types;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import junit.framework.TestCase;
  33. import static com.healthmarketscience.jackcess.Database.*;
  34. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  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. db.importFile("test", new File("test/data/sample-input.tab"), "\\t");
  48. }
  49. }
  50. public void testImportFromFileWithOnlyHeaders() throws Exception
  51. {
  52. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  53. Database db = create(fileFormat);
  54. db.importFile("test", new File("test/data/sample-input-only-headers.tab"),
  55. "\\t");
  56. }
  57. }
  58. public void testCopySqlHeaders() throws Exception
  59. {
  60. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  61. TestResultSet rs = new TestResultSet();
  62. rs.addColumn(Types.INTEGER, "col1");
  63. rs.addColumn(Types.VARCHAR, "col2", 60, 0, 0);
  64. rs.addColumn(Types.VARCHAR, "col3", 500, 0, 0);
  65. rs.addColumn(Types.BINARY, "col4", 128, 0, 0);
  66. rs.addColumn(Types.BINARY, "col5", 512, 0, 0);
  67. rs.addColumn(Types.NUMERIC, "col6", 0, 7, 15);
  68. rs.addColumn(Types.VARCHAR, "col7", Integer.MAX_VALUE, 0, 0);
  69. Database db = create(fileFormat);
  70. db.copyTable("Test1", (ResultSet)Proxy.newProxyInstance(
  71. Thread.currentThread().getContextClassLoader(),
  72. new Class[]{ResultSet.class},
  73. rs));
  74. Table t = db.getTable("Test1");
  75. List<Column> columns = t.getColumns();
  76. assertEquals(7, columns.size());
  77. Column c = columns.get(0);
  78. assertEquals("col1", c.getName());
  79. assertEquals(DataType.LONG, c.getType());
  80. c = columns.get(1);
  81. assertEquals("col2", c.getName());
  82. assertEquals(DataType.TEXT, c.getType());
  83. assertEquals(120, c.getLength());
  84. c = columns.get(2);
  85. assertEquals("col3", c.getName());
  86. assertEquals(DataType.MEMO, c.getType());
  87. assertEquals(0, c.getLength());
  88. c = columns.get(3);
  89. assertEquals("col4", c.getName());
  90. assertEquals(DataType.BINARY, c.getType());
  91. assertEquals(128, c.getLength());
  92. c = columns.get(4);
  93. assertEquals("col5", c.getName());
  94. assertEquals(DataType.OLE, c.getType());
  95. assertEquals(0, c.getLength());
  96. c = columns.get(5);
  97. assertEquals("col6", c.getName());
  98. assertEquals(DataType.NUMERIC, c.getType());
  99. assertEquals(17, c.getLength());
  100. assertEquals(7, c.getScale());
  101. assertEquals(15, c.getPrecision());
  102. c = columns.get(6);
  103. assertEquals("col7", c.getName());
  104. assertEquals(DataType.MEMO, c.getType());
  105. assertEquals(0, c.getLength());
  106. }
  107. }
  108. private static class TestResultSet implements InvocationHandler
  109. {
  110. private List<Integer> _types = new ArrayList<Integer>();
  111. private List<String> _names = new ArrayList<String>();
  112. private List<Integer> _displaySizes = new ArrayList<Integer>();
  113. private List<Integer> _scales = new ArrayList<Integer>();
  114. private List<Integer> _precisions = new ArrayList<Integer>();
  115. public Object invoke(Object proxy, Method method, Object[] args)
  116. {
  117. String methodName = method.getName();
  118. if(methodName.equals("getMetaData")) {
  119. return Proxy.newProxyInstance(
  120. Thread.currentThread().getContextClassLoader(),
  121. new Class[]{ResultSetMetaData.class},
  122. this);
  123. } else if(methodName.equals("next")) {
  124. return Boolean.FALSE;
  125. } else if(methodName.equals("getColumnCount")) {
  126. return _types.size();
  127. } else if(methodName.equals("getColumnName")) {
  128. return getValue(_names, args[0]);
  129. } else if(methodName.equals("getColumnDisplaySize")) {
  130. return getValue(_displaySizes, args[0]);
  131. } else if(methodName.equals("getColumnType")) {
  132. return getValue(_types, args[0]);
  133. } else if(methodName.equals("getScale")) {
  134. return getValue(_scales, args[0]);
  135. } else if(methodName.equals("getPrecision")) {
  136. return getValue(_precisions, args[0]);
  137. } else {
  138. throw new UnsupportedOperationException(methodName);
  139. }
  140. }
  141. public void addColumn(int type, String name)
  142. {
  143. addColumn(type, name, 0, 0, 0);
  144. }
  145. public void addColumn(int type, String name, int displaySize,
  146. int scale, int precision)
  147. {
  148. _types.add(type);
  149. _names.add(name);
  150. _displaySizes.add(displaySize);
  151. _scales.add(scale);
  152. _precisions.add(precision);
  153. }
  154. public <T> T getValue(List<T> values, Object index) {
  155. return values.get((Integer)index - 1);
  156. }
  157. }
  158. }