001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.lang.reflect.Field;
022: import java.sql.Types;
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Set;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: *
032: * Uses reflection to test JDBCTypeMapper against java.sql.Types. This will
033: * help catch (when upgrading to a new release) any new types that aren't
034: * accounted for.
035: *
036: * @author manningr
037: */
038: public class JDBCTypeMapperTest extends TestCase {
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042: }
043:
044: protected void tearDown() throws Exception {
045: super .tearDown();
046: }
047:
048: public void testGetJdbcTypeList() {
049:
050: String[] typeListArr = JDBCTypeMapper.getJdbcTypeList();
051: List<String> typeList = Arrays.asList(typeListArr);
052: Set<String> s = new HashSet<String>();
053: s.addAll(typeList);
054:
055: Field[] fields = java.sql.Types.class.getDeclaredFields();
056: for (int i = 0; i < fields.length; i++) {
057: Field field = fields[i];
058: assertTrue(s.contains(field.getName()));
059: }
060: }
061:
062: public void testGetJdbcTypeName() throws Exception {
063: Field[] fields = java.sql.Types.class.getDeclaredFields();
064: for (int i = 0; i < fields.length; i++) {
065: Field field = fields[i];
066: String fieldName = field.getName();
067: int jdbcType = field.getInt(null);
068: String typeName = JDBCTypeMapper.getJdbcTypeName(jdbcType);
069: assertEquals(fieldName, typeName);
070: }
071: }
072:
073: public void testGetJdbcType() throws Exception {
074: Field[] fields = java.sql.Types.class.getDeclaredFields();
075: for (int i = 0; i < fields.length; i++) {
076: Field field = fields[i];
077: String fieldName = field.getName();
078: int expectedType = field.getInt(null);
079: int actualType = JDBCTypeMapper.getJdbcType(fieldName);
080: assertEquals("Field name=" + fieldName, expectedType,
081: actualType);
082: }
083:
084: assertEquals("Field name=null", Types.NULL, JDBCTypeMapper
085: .getJdbcType(null));
086: assertEquals("Field name=NVARCHAR", Types.VARCHAR,
087: JDBCTypeMapper.getJdbcType("NVARCHAR"));
088:
089: }
090:
091: public void testIsNumberType() throws Exception {
092: Integer[] numberTypes = new Integer[] { Types.BIGINT,
093: Types.DECIMAL, Types.DOUBLE, Types.FLOAT,
094: Types.INTEGER, Types.NUMERIC };
095: Set<Integer> s = new HashSet<Integer>();
096: s.addAll(Arrays.asList(numberTypes));
097: testIsType(s, new NumberTypeCheck());
098: }
099:
100: public void testIsDateType() throws Exception {
101: Integer[] dateTypes = new Integer[] { Types.DATE, Types.TIME,
102: Types.TIMESTAMP };
103: Set<Integer> s = new HashSet<Integer>();
104: s.addAll(Arrays.asList(dateTypes));
105: testIsType(s, new DateTypeCheck());
106: }
107:
108: public void testIsLongType() throws Exception {
109: Integer[] longTypes = new Integer[] { Types.LONGVARBINARY,
110: Types.LONGVARCHAR, Types.BLOB, Types.CLOB, };
111: Set<Integer> s = new HashSet<Integer>();
112: s.addAll(Arrays.asList(longTypes));
113: testIsType(s, new LongTypeCheck());
114: }
115:
116: public void testIsType(Set<Integer> s, TypeCheck checker)
117: throws Exception {
118: Field[] fields = java.sql.Types.class.getDeclaredFields();
119: for (int i = 0; i < fields.length; i++) {
120: Field field = fields[i];
121: String fieldName = field.getName();
122: Integer jdbcType = field.getInt(null);
123: if (s.contains(jdbcType)) {
124: assertTrue("Field name=" + fieldName, checker
125: .isType(jdbcType));
126: } else {
127: assertFalse("Field name=" + fieldName, checker
128: .isType(jdbcType));
129: }
130: }
131: }
132:
133: private interface TypeCheck {
134: boolean isType(int type);
135: }
136:
137: private class NumberTypeCheck implements TypeCheck {
138: public boolean isType(int type) {
139: return JDBCTypeMapper.isNumberType(type);
140: }
141: }
142:
143: private class DateTypeCheck implements TypeCheck {
144: public boolean isType(int type) {
145: return JDBCTypeMapper.isDateType(type);
146: }
147: }
148:
149: private class LongTypeCheck implements TypeCheck {
150: public boolean isType(int type) {
151: return JDBCTypeMapper.isLongType(type);
152: }
153: }
154:
155: }
|