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