001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.sql.execute;
043:
044: import java.lang.reflect.Field;
045: import java.lang.reflect.InvocationHandler;
046: import java.lang.reflect.Method;
047: import java.lang.reflect.Modifier;
048: import java.lang.reflect.Proxy;
049: import java.sql.DatabaseMetaData;
050: import java.sql.Types;
051: import org.netbeans.junit.NbTestCase;
052:
053: /**
054: *
055: * @author Andrei Badea
056: */
057: public class ResultSetTableModelSupportTest extends NbTestCase {
058:
059: public ResultSetTableModelSupportTest(String testName) {
060: super (testName);
061: }
062:
063: /**
064: * Tests that a ColumnTypeDef is defined for each type from java.sql.Types
065: * in the TYPE_TO_DEF map.
066: */
067: public void testAllTypes() throws IllegalAccessException {
068: Class clazz = Types.class;
069: Field[] fields = clazz.getFields();
070: for (int i = 0; i < fields.length; i++) {
071: Field field = fields[i];
072: if (field.getType() == int.class
073: && Modifier.isStatic(field.getModifiers())) {
074: int type = field.getInt(clazz);
075: if (ResultSetTableModelSupport.TYPE_TO_DEF
076: .get(new Integer(type)) == null) {
077: fail("No ColumnTypeDef for java.Types."
078: + field.getName());
079: }
080: }
081: }
082: }
083:
084: /**
085: * Tests that a ColumnTypeDef is defined even for types not in
086: * java.sql.Types.
087: */
088: public void testUnknownTypesIssue71040()
089: throws IllegalAccessException {
090: // for the test to be meaningful we have to ensure
091: // our type is not in java.sql.Types
092: int type = Integer.MAX_VALUE;
093: Class clazz = Types.class;
094: Field[] fields = clazz.getFields();
095: for (int i = 0; i < fields.length; i++) {
096: Field field = fields[i];
097: if (field.getType() == int.class
098: && Modifier.isStatic(field.getModifiers())) {
099: if (type == field.getInt(clazz)) {
100: fail("Type " + type
101: + " already defined in java.sql.Types as "
102: + field.getName());
103: }
104: }
105: }
106: DatabaseMetaData dmd = (DatabaseMetaData) Proxy
107: .newProxyInstance(this .getClass().getClassLoader(),
108: new Class[] { DatabaseMetaData.class },
109: new InvocationHandler() {
110: public Object invoke(Object proxy,
111: Method method, Object[] args)
112: throws Throwable {
113: return null;
114: }
115: });
116: assertNotNull(ResultSetTableModelSupport.getColumnTypeDef(dmd,
117: type));
118: }
119: }
|