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.ui;
043:
044: import java.awt.Component;
045: import java.io.File;
046: import java.lang.reflect.Constructor;
047: import java.sql.Connection;
048: import java.util.Locale;
049: import java.util.Map;
050: import java.util.Properties;
051: import javax.swing.JLabel;
052: import javax.swing.JTable;
053: import javax.swing.table.TableCellRenderer;
054: import junit.framework.TestSuite;
055:
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.db.sql.execute.SQLExecuteHelper;
058: import org.netbeans.modules.db.sql.execute.SQLExecutionLogger;
059: import org.netbeans.modules.db.sql.execute.SQLExecutionResult;
060: import org.netbeans.modules.db.sql.execute.SQLExecutionResults;
061: import org.netbeans.modules.db.sql.execute.ui.SQLResultPanel.SQLResultTable;
062: import org.netbeans.modules.db.sql.execute.ui.util.TestCaseContext;
063: import org.netbeans.modules.db.sql.execute.ui.util.DbUtil;
064: import org.netbeans.modules.db.sql.execute.ui.util.TestCaseDataFactory;
065:
066: /**
067: *
068: * @author luke
069: */
070: public class DataTypeTest extends NbTestCase {
071: TestCaseContext context;
072: Connection conn;
073: JTable table;
074: Locale defaultLocale;
075:
076: public DataTypeTest(String s, TestCaseContext c) {
077: super (s);
078: context = c;
079: debug("TestCase: " + context);
080: }
081:
082: public void testData() {
083: debug("testData()");
084:
085: int count = table.getColumnCount();
086: int row = table.getRowCount();
087: if (row == 0)
088: fail(context
089: + ": table exist but there is no data in table probably there is an error in insert sql script in file: "
090: + TestCaseDataFactory.DB_SQLCREATE);
091: Map data = context.getData();
092: assertEquals(
093: context
094: + ": number of entries in file with data and columns in database are different for test case: "
095: + context, data.size(), count);
096: for (int i = 0; i < count; i++) {
097: String column = table.getColumnName(i);
098: String expected = (String) data.get(column);
099: if (expected == null)
100: expected = (String) data.get(column.toLowerCase());
101: if (expected == null)
102: fail(context
103: + ": the file with data doesn't contains entry called: "
104: + column);
105: TableCellRenderer renderer = table.getCellRenderer(0, i);
106:
107: Component c = table.prepareRenderer(renderer, 0, i);
108: String actual = (String) ((JLabel) c).getText();
109: assertEquals(context
110: + ": values are different for column: " + column,
111: expected, actual);
112: }
113:
114: }
115:
116: private ResultSetTableModel executeSQL(String sql, Connection conn)
117: throws Exception {
118: SQLExecutionResults sqlrs = SQLExecuteHelper.execute(sql, 0,
119: sql.length(), conn, null, new SQLExecutionLogger() {
120: public void cancel() {
121: }
122:
123: public void finish(long executionTime) {
124: }
125:
126: public void log(SQLExecutionResult result) {
127: }
128: });
129: SQLResultPanelModel panelModel = SQLResultPanelModel
130: .create(sqlrs);
131: ResultSetTableModel model = panelModel.getResultSetModel();
132:
133: return model;
134: }
135:
136: protected void setUp() throws Exception {
137: debug("setUp()");
138: Properties prop = context.getProperties();
139: File[] jars = context.getJars();
140: conn = DbUtil.createConnection(prop, jars);
141: String sql_create = context.getSqlCreate();
142: String sql_select = context.getSqlSelect();
143: debug("sql_select: " + sql_select);
144: executeSQL(sql_create, conn);
145: ResultSetTableModel model = executeSQL(sql_select, conn);
146: if (model == null)
147: throw new RuntimeException(
148: context
149: + ": model ResultSetTableModel is null probably there is a error in sql statement");
150: table = new SQLResultTable();
151: defaultLocale = Locale.getDefault();
152: Locale.setDefault(Locale.US);
153: table.setModel(model);
154:
155: }
156:
157: protected void tearDown() throws Exception {
158: debug("tearDown()");
159: Locale.setDefault(defaultLocale);
160: String sql_del = context.getSqlDel();
161: executeSQL(sql_del, conn);
162: conn.close();
163: }
164:
165: public static TestSuite suite() throws Exception {
166: TestSuite suite = new TestSuite();
167: TestCaseDataFactory factory = TestCaseDataFactory
168: .getTestCaseFactory();
169: Object[] context = factory.getTestCaseContext();
170: for (int i = 0; i < context.length; i++) {
171: Class[] args = { String.class, TestCaseContext.class };
172: Object[] o = { "testData", context[i] };
173: Constructor con = DataTypeTest.class.getConstructor(args);
174: DataTypeTest testcase = (DataTypeTest) con.newInstance(o);
175: suite.addTest(testcase);
176: }
177: return suite;
178: }
179:
180: private void debug(String message) {
181:
182: log(message);
183: System.out.println("> " + message);
184:
185: }
186: }
|