From: James Ahlborn Date: Mon, 13 Nov 2006 14:58:29 +0000 (+0000) Subject: add unit test for new copyTable behavior X-Git-Tag: rel_1_1_8~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c1f7c9fdc130b58ff94500cb5f44f56de8921e69;p=jackcess.git add unit test for new copyTable behavior git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@136 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/DataType.java b/src/java/com/healthmarketscience/jackcess/DataType.java index dcc18d7..a19f374 100644 --- a/src/java/com/healthmarketscience/jackcess/DataType.java +++ b/src/java/com/healthmarketscience/jackcess/DataType.java @@ -187,6 +187,12 @@ public enum DataType { return _variableLength; } + public boolean isTrueVariableLength() { + // some "var len" fields do not really have a variable length, + // e.g. NUMERIC + return (isVariableLength() && (getMinSize() != getMaxSize())); + } + public boolean isLongValue() { return _longValue; } diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index 30a835b..43bee10 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -639,7 +639,10 @@ public class Database int lengthInUnits = md.getColumnDisplaySize(i); column.setSQLType(md.getColumnType(i), lengthInUnits); DataType type = column.getType(); - if(type.isVariableLength()) { + // we check for isTrueVariableLength here to avoid setting the length + // for a NUMERIC column, which pretends to be var-len, even though it + // isn't + if(type.isTrueVariableLength() && !type.isLongValue()) { column.setLengthInUnits((short)lengthInUnits); } if(type.getHasScalePrecision()) { diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 51d9220..92cbb47 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -81,7 +81,7 @@ public class DatabaseTest extends TestCase { col = new Column(); col.setName("A"); col.setType(DataType.TEXT); - col.setLength((short)(352 * 2)); + col.setLength((short)(352 * DataType.TEXT.getUnitSize())); columns.add(col); try { @@ -295,7 +295,6 @@ public class DatabaseTest extends TestCase { assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME")); assertEquals("Project1", row.get("PROJ_PROP_TITLE")); byte[] foundBinaryData = (byte[])row.get("RESERVED_BINARY_DATA"); - System.out.println("FOO found len " + foundBinaryData.length); byte[] expectedBinaryData = toByteArray(new File("test/data/test2BinData.dat")); assertTrue(Arrays.equals(expectedBinaryData, foundBinaryData)); diff --git a/test/src/java/com/healthmarketscience/jackcess/ImportTest.java b/test/src/java/com/healthmarketscience/jackcess/ImportTest.java index 2caf825..c3cb5aa 100644 --- a/test/src/java/com/healthmarketscience/jackcess/ImportTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/ImportTest.java @@ -2,13 +2,19 @@ package com.healthmarketscience.jackcess; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.healthmarketscience.jackcess.Database; - import java.io.File; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Types; +import java.util.ArrayList; +import java.util.List; + import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * @author Rob Di Marco @@ -42,4 +48,114 @@ public class ImportTest extends TestCase "\\t"); } + public void testCopySqlHeaders() throws Exception + { + TestResultSet rs = new TestResultSet(); + + rs.addColumn(Types.INTEGER, "col1"); + rs.addColumn(Types.VARCHAR, "col2", 60, 0, 0); + rs.addColumn(Types.VARCHAR, "col3", 500, 0, 0); + rs.addColumn(Types.BINARY, "col4", 128, 0, 0); + rs.addColumn(Types.BINARY, "col5", 512, 0, 0); + rs.addColumn(Types.NUMERIC, "col6", 0, 7, 15); + + Database db = create(); + db.copyTable("Test1", (ResultSet)Proxy.newProxyInstance( + Thread.currentThread().getContextClassLoader(), + new Class[]{ResultSet.class}, + rs)); + + Table t = db.getTable("Test1"); + List columns = t.getColumns(); + assertEquals(6, columns.size()); + + Column c = columns.get(0); + assertEquals("col1", c.getName()); + assertEquals(DataType.LONG, c.getType()); + + c = columns.get(1); + assertEquals("col2", c.getName()); + assertEquals(DataType.TEXT, c.getType()); + assertEquals(120, c.getLength()); + + c = columns.get(2); + assertEquals("col3", c.getName()); + assertEquals(DataType.MEMO, c.getType()); + assertEquals(0, c.getLength()); + + c = columns.get(3); + assertEquals("col4", c.getName()); + assertEquals(DataType.BINARY, c.getType()); + assertEquals(128, c.getLength()); + + c = columns.get(4); + assertEquals("col5", c.getName()); + assertEquals(DataType.OLE, c.getType()); + assertEquals(0, c.getLength()); + + c = columns.get(5); + assertEquals("col6", c.getName()); + assertEquals(DataType.NUMERIC, c.getType()); + assertEquals(17, c.getLength()); + assertEquals(7, c.getScale()); + assertEquals(15, c.getPrecision()); + + } + + + private static class TestResultSet implements InvocationHandler + { + private List _types = new ArrayList(); + private List _names = new ArrayList(); + private List _displaySizes = new ArrayList(); + private List _scales = new ArrayList(); + private List _precisions = new ArrayList(); + + public Object invoke(Object proxy, Method method, Object[] args) + { + String methodName = method.getName(); + if(methodName.equals("getMetaData")) { + return Proxy.newProxyInstance( + Thread.currentThread().getContextClassLoader(), + new Class[]{ResultSetMetaData.class}, + this); + } else if(methodName.equals("next")) { + return Boolean.FALSE; + } else if(methodName.equals("getColumnCount")) { + return _types.size(); + } else if(methodName.equals("getColumnName")) { + return getValue(_names, args[0]); + } else if(methodName.equals("getColumnDisplaySize")) { + return getValue(_displaySizes, args[0]); + } else if(methodName.equals("getColumnType")) { + return getValue(_types, args[0]); + } else if(methodName.equals("getScale")) { + return getValue(_scales, args[0]); + } else if(methodName.equals("getPrecision")) { + return getValue(_precisions, args[0]); + } else { + throw new UnsupportedOperationException(methodName); + } + } + + public void addColumn(int type, String name) + { + addColumn(type, name, 0, 0, 0); + } + + public void addColumn(int type, String name, int displaySize, + int scale, int precision) + { + _types.add(type); + _names.add(name); + _displaySizes.add(displaySize); + _scales.add(scale); + _precisions.add(precision); + } + + public T getValue(List values, Object index) { + return values.get((Integer)index - 1); + } + } + }