KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > execute > ui > DataTypeTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.execute.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JTable JavaDoc;
31 import javax.swing.table.TableCellRenderer JavaDoc;
32 import junit.framework.TestSuite;
33
34
35
36
37
38 import org.netbeans.junit.NbTestCase;
39 import org.netbeans.modules.db.sql.execute.SQLExecuteHelper;
40 import org.netbeans.modules.db.sql.execute.SQLExecutionLogger;
41 import org.netbeans.modules.db.sql.execute.SQLExecutionResult;
42 import org.netbeans.modules.db.sql.execute.SQLExecutionResults;
43 import org.netbeans.modules.db.sql.execute.ui.SQLResultPanel.SQLResultTable;
44 import org.netbeans.modules.db.sql.execute.ui.util.TestCaseContext;
45 import org.netbeans.modules.db.sql.execute.ui.util.DbUtil;
46 import org.netbeans.modules.db.sql.execute.ui.util.TestCaseDataFactory;
47
48 /**
49  *
50  * @author luke
51  */

52 public class DataTypeTest extends NbTestCase{
53     TestCaseContext context;
54     Connection JavaDoc conn;
55     JTable JavaDoc table;
56     Locale JavaDoc defaultLocale;
57     public DataTypeTest(String JavaDoc s,TestCaseContext c) {
58         super(s);
59         context=c;
60         debug("TestCase: "+context);
61     }
62
63     
64     public void testData(){
65         debug("testData()");
66         
67         
68         int count=table.getColumnCount();
69         int row=table.getRowCount();
70         if(row==0)
71             fail(context+": table exist but there is no data in table probably there is an error in insert sql script in file: "+TestCaseDataFactory.DB_SQLCREATE);
72         Map JavaDoc data=context.getData();
73         assertEquals(context+": number of entries in file with data and columns in database are different for test case: "+context,data.size(),count);
74         for(int i=0;i<count;i++){
75            String JavaDoc column=table.getColumnName(i);
76            String JavaDoc expected=(String JavaDoc)data.get(column);
77            if(expected==null)
78                   expected=(String JavaDoc)data.get(column.toLowerCase());
79            if(expected==null)
80                fail(context+": the file with data doesn't contains entry called: "+column);
81            TableCellRenderer JavaDoc renderer=table.getCellRenderer(0,i) ;
82            
83            Component JavaDoc c=table.prepareRenderer(renderer,0,i);
84            String JavaDoc actual=(String JavaDoc)((JLabel JavaDoc)c).getText();
85            assertEquals(context+": values are different for column: "+column,expected,actual);
86         }
87         
88         
89     }
90     
91     
92     private ResultSetTableModel executeSQL(String JavaDoc sql,Connection JavaDoc conn) throws Exception JavaDoc{
93         SQLExecutionResults sqlrs=SQLExecuteHelper.execute(sql,0,sql.length(),conn,null,new SQLExecutionLogger() {
94             public void cancel() {
95             }
96             public void finish(long executionTime) {
97             }
98             public void log(SQLExecutionResult result) {
99             }
100         });
101         SQLResultPanelModel panelModel=SQLResultPanelModel.create(sqlrs);
102         ResultSetTableModel model=panelModel.getResultSetModel();
103         
104         return model;
105     }
106     
107     protected void setUp() throws Exception JavaDoc {
108         debug("setUp()");
109         Properties JavaDoc prop=context.getProperties();
110         File JavaDoc[] jars=context.getJars();
111         conn = DbUtil.createConnection(prop,jars);
112         String JavaDoc sql_create=context.getSqlCreate();
113         String JavaDoc sql_select=context.getSqlSelect();
114         debug("sql_select: "+sql_select);
115         executeSQL(sql_create,conn);
116         ResultSetTableModel model=executeSQL(sql_select,conn);
117         if(model==null)
118             throw new RuntimeException JavaDoc(context+": model ResultSetTableModel is null probably there is a error in sql statement");
119         table=new SQLResultTable();
120         defaultLocale = Locale.getDefault();
121         Locale.setDefault(Locale.US);
122         table.setModel(model);
123         
124         
125         
126         
127     }
128
129     protected void tearDown() throws Exception JavaDoc {
130        debug("tearDown()");
131        Locale.setDefault(defaultLocale);
132        String JavaDoc sql_del=context.getSqlDel();
133        executeSQL(sql_del,conn);
134        conn.close();
135     }
136     
137     
138     public static TestSuite suite() throws Exception JavaDoc{
139         TestSuite suite=new TestSuite();
140         TestCaseDataFactory factory=TestCaseDataFactory.getTestCaseFactory();
141         Object JavaDoc[] context=factory.getTestCaseContext();
142         for(int i=0;i<context.length;i++){
143             Class JavaDoc[] args={String JavaDoc.class,TestCaseContext.class};
144             Object JavaDoc[] o={"testData",context[i]};
145             Constructor JavaDoc con=DataTypeTest.class.getConstructor(args);
146             DataTypeTest testcase=(DataTypeTest)con.newInstance(o);
147             suite.addTest(testcase);
148         }
149         return suite;
150     }
151     
152     
153     private void debug(String JavaDoc message){
154        
155             log(message);
156             System.out.println("> " + message);
157        
158     }
159 }
160
Popular Tags