001: /*
002: ** $Id: TestView.java,v 1.4 2000/10/26 08:34:16 mrw Exp $
003: **
004: **
005: ** Mike Wilson, October 2000, mrw@whisperingwind.co.uk
006: **
007: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
008: **
009: ** This program is free software; you can redistribute it and/or modify
010: ** it under the terms of the GNU General Public License as published by
011: ** the Free Software Foundation; either version 2 of the License, or
012: ** (at your option) any later version.
013: **
014: ** This program is distributed in the hope that it will be useful,
015: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
016: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: ** GNU General Public License for more details.
018: **
019: ** You should have received a copy of the GNU Library General
020: ** Public License along with this library; if not, write to the
021: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: ** Boston, MA 02111-1307 USA.
023: */
024:
025: package uk.co.whisperingwind.vienna;
026:
027: import java.awt.BorderLayout;
028: import java.sql.Connection;
029: import java.sql.DriverManager;
030: import java.sql.SQLException;
031: import javax.swing.JScrollPane;
032: import javax.swing.JTextArea;
033: import uk.co.whisperingwind.framework.ActionFactory;
034: import uk.co.whisperingwind.framework.ModelEvent;
035: import uk.co.whisperingwind.framework.PanelDialogView;
036:
037: class TestView extends PanelDialogView implements Runnable {
038: private JTextArea resultText = new JTextArea(15, 40);
039: private ActionFactory.DefaultAction dismissAction = null;
040: private Thread testThread = null;
041: private boolean continueTest = true;
042:
043: private String url = null;
044: private String driverClass = null;
045: private String userName = null;
046: private String password = null;
047:
048: public TestView(String theUrl, String theDriverClass,
049: String theUserName, String thePassword) {
050: super (null, false);
051: content.setTitle("Connection test");
052:
053: url = theUrl;
054: driverClass = theDriverClass;
055: userName = theUserName;
056: password = thePassword;
057:
058: JScrollPane scroller = new JScrollPane(resultText);
059: resultText.setLineWrap(true);
060: resultText.setWrapStyleWord(true);
061: resultText.setEditable(false);
062: panel.add(scroller, BorderLayout.CENTER);
063:
064: ActionFactory factory = new ActionFactory(
065: "/uk/co/whisperingwind/images");
066:
067: dismissAction = factory.createAction("dismiss");
068: dismissAction.toolButtonFactory(buttonPanel);
069: dismissAction.addActionListener(actionListener);
070:
071: content.pack();
072: }
073:
074: public void startTest() {
075: Thread t = new Thread(this );
076: t.setDaemon(true);
077: t.start();
078: }
079:
080: /*
081: ** Synchronizing on resultText isn't really necessary -- only this
082: ** thread updates it.
083: **
084: ** This shouldn't really be written like this -- a Swing component
085: ** should never be updated by any thread other than event
086: ** dispatch.
087: */
088:
089: public void run() {
090: content.setVisible(true);
091: boolean testPassed = true;
092:
093: if (continueTest && testPassed) {
094: synchronized (resultText) {
095: resultText.append("Test 1: Load JDBC driver\n\n");
096: }
097:
098: try {
099: Class.forName(driverClass);
100:
101: synchronized (resultText) {
102: resultText.append("TEST 1 PASSED\n");
103: }
104: } catch (java.lang.ClassNotFoundException ex) {
105: testPassed = false;
106:
107: synchronized (resultText) {
108: resultText.append("TEST FAILED\n");
109: resultText.append("Cannot load the JDBC driver ");
110: resultText.append(driverClass);
111: resultText
112: .append(". Please make sure the JDBC driver's");
113: resultText
114: .append(" .zip or .jar file is correctly");
115: resultText.append(" installed in ");
116: resultText.append(System
117: .getProperty("java.ext.dirs"));
118: }
119: }
120: }
121:
122: if (continueTest && testPassed) {
123: synchronized (resultText) {
124: resultText
125: .append("\nTest 2: Connect to the database\n\n");
126: }
127:
128: try {
129: DriverManager.getConnection(url, userName, password);
130:
131: synchronized (resultText) {
132: resultText.append("TEST 2 PASSED\n");
133: }
134: } catch (SQLException ex) {
135: testPassed = false;
136:
137: synchronized (resultText) {
138: resultText.append("TEST FAILED\n");
139: resultText
140: .append("Cannot connect to the database using ");
141: resultText
142: .append("the specified user name and password.");
143: resultText
144: .append(" Please check the URL, user name and ");
145: resultText.append("password are correct.\n\n");
146: resultText
147: .append("Messaged returned by the driver");
148: resultText.append(" is " + ex.getMessage());
149: }
150: }
151: }
152:
153: if (continueTest && testPassed) {
154: synchronized (resultText) {
155: resultText.append("\nALL TESTS PASSED\n");
156: }
157: }
158: }
159:
160: protected void modelEvent(ModelEvent event) {
161: }
162:
163: protected void cleanUp() {
164: }
165:
166: public void closeDialog() {
167: stopThread();
168: super .closeDialog();
169: }
170:
171: private synchronized void stopThread() {
172: if (testThread != null) {
173: stopTest();
174: testThread.interrupt();
175:
176: try {
177: testThread.join();
178: testThread = null;
179: } catch (InterruptedException ex) {
180: // Do nothing
181: }
182: }
183: }
184:
185: private synchronized void stopTest() {
186: continueTest = false;
187: }
188: }
|