KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > junitTests > compatibility > CompatibilitySuite


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.compatibility.CompatibilitySuite
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21 /**
22  * <p>
23  * This is the JUnit suite verifying compatibility of Derby clients and
24  * servers across Derby version levels and supported VMs. When you want
25  * to add a new class of tests to this suite, just add the classname to
26  * the accumulator in suite().
27  * </p>
28  *
29  * @author Rick
30  */

31
32 package org.apache.derbyTesting.functionTests.tests.junitTests.compatibility;
33
34 import java.io.*;
35 import java.sql.*;
36 import java.util.*;
37
38 import junit.framework.*;
39
40 import org.apache.derbyTesting.functionTests.util.DerbyJUnitTest;
41
42 public class CompatibilitySuite extends DerbyJUnitTest
43 {
44     /////////////////////////////////////////////////////////////
45
//
46
// CONSTANTS
47
//
48
/////////////////////////////////////////////////////////////
49

50     // Supported versions of the db2jcc client.
51
public static final Version IBM_2_4 = new Version( 2, 4 );
52
53     // Supported versions of Derby.
54
public static final Version DRB_10_0 = new Version( 10, 0 );
55     public static final Version DRB_10_1 = new Version( 10, 1 );
56     public static final Version DRB_10_2 = new Version( 10, 2 );
57
58     // Supported VM versions.
59
public static final Version VM_1_3 = new Version( 1, 3 );
60     public static final Version VM_1_4 = new Version( 1, 4 );
61     public static final Version VM_1_5 = new Version( 1, 5 );
62
63     public static final String JavaDoc SERVER_VERSION_FUNCTION = "getVMVersion";
64     
65     private static final String JavaDoc VERSION_PROPERTY = "java.version";
66
67     private static final int EXPECTED_CLIENT_COUNT = 1;
68
69     /////////////////////////////////////////////////////////////
70
//
71
// STATE
72
//
73
/////////////////////////////////////////////////////////////
74

75     private static Driver _driver; // the corresponding jdbc driver
76
private static Version _clientVMLevel; // level of client-side vm
77
private static Version _serverVMLevel; // level of server vm
78
private static Version _driverLevel; // client rev level
79
private static Version _serverLevel; // server rev level
80

81     /////////////////////////////////////////////////////////////
82
//
83
// JUnit BEHAVIOR
84
//
85
/////////////////////////////////////////////////////////////
86

87     /**
88      * <p>
89      * JUnit boilerplate which adds as test cases all public methods
90      * whose names start with the string "test" in the named classes.
91      * When you want to add a new class of tests, just wire it into
92      * this suite.
93      * </p>
94      */

95     public static Test suite()
96     {
97         TestSuite testSuite = new TestSuite();
98
99         testSuite.addTestSuite( JDBCDriverTest.class );
100
101         return testSuite;
102     }
103
104
105     /////////////////////////////////////////////////////////////
106
//
107
// ENTRY POINT
108
//
109
/////////////////////////////////////////////////////////////
110

111     /**
112      * <p>
113      * Run JDBC compatibility tests using either the specified client or
114      * the client that is visible
115      * on the classpath. If there is more than one client on the classpath,
116      * exits with an error.
117      * </p>
118      *
119      * <ul>
120      * <li>arg[ 0 ] = required name of database to connect to</li>
121      * <li>arg[ 1 ] = optional driver to use. if not specified, we'll look for a
122      * client on the classpath</li>
123      * </ul>
124      */

125     public static void main( String JavaDoc args[] )
126         throws Exception JavaDoc
127     {
128         int exitStatus = FAILURE_EXIT;
129         
130         if (
131                parseDebug() &&
132                parseArgs( args ) &&
133                parseVMLevel() &&
134                findClient() &&
135                findServer()
136            )
137         {
138             TestResult result = junit.textui.TestRunner.run( suite() );
139             
140             exitStatus = result.errorCount() + result.failureCount();
141         }
142
143         Runtime.getRuntime().exit( exitStatus );
144     }
145
146     /////////////////////////////////////////////////////////////
147
//
148
// PUBLIC BEHAVIOR
149
//
150
/////////////////////////////////////////////////////////////
151

152     /**
153      * <p>
154      * Get the version of the server.
155      * </p>
156      */

157     public Version getServerVersion() { return _serverLevel; }
158
159     /**
160      * <p>
161      * Get the version of the client.
162      * </p>
163      */

164     public Version getDriverVersion() { return _driverLevel; }
165
166     /**
167      * <p>
168      * Get the vm level of the server.
169      * </p>
170      */

171     public static Version getServerVMVersion() { return _serverVMLevel; }
172
173     /**
174      * <p>
175      * Get the vm level of the client.
176      * </p>
177      */

178     public Version getClientVMVersion() { return _clientVMLevel; }
179
180     /////////////////////////////////////////////////////////////
181
//
182
// DATABASE-SIDE FUNCTIONS
183
//
184
/////////////////////////////////////////////////////////////
185

186     /**
187      * <p>
188      * Get the vm level of the server.
189      * </p>
190      */

191     public static String JavaDoc getVMVersion()
192     {
193         return System.getProperty( VERSION_PROPERTY );
194     }
195
196     /////////////////////////////////////////////////////////////
197
//
198
// MINIONS
199
//
200
/////////////////////////////////////////////////////////////
201

202     ///////////////////
203
//
204
// GENERAL MINIONS
205
//
206
///////////////////
207

208     //////////////////////////
209
//
210
// INITIALIZATION MINIONS
211
//
212
//////////////////////////
213

214     //
215
// Initialize client settings based on the client found.
216
// Return true if one and only one client found, false otherwise.
217
// We allow for the special case when we're running the embedded client
218
// off the current compiled class tree rather than off product jars.
219
//
220
private static boolean findClient()
221         throws Exception JavaDoc
222     {
223         //
224
// The client may have been specified on the command line.
225
// In that case, we don't bother looking for a client on
226
// the classpath.
227
//
228
if ( getClientSettings() != null ) { faultInDriver( getClientSettings() ); }
229         else
230         {
231             String JavaDoc currentClientName = null;
232             int legalCount = LEGAL_CLIENTS.length;
233             int foundCount = 0;
234
235             for ( int i = 0; i < legalCount; i++ )
236             {
237                 String JavaDoc[] candidate = LEGAL_CLIENTS[ i ];
238
239                 if ( faultInDriver( candidate ) )
240                 {
241                     setClient( candidate );
242                     foundCount++;
243                 }
244             }
245
246             if ( foundCount != EXPECTED_CLIENT_COUNT )
247             {
248                 throw new Exception JavaDoc( "Wrong number of drivers: " + foundCount );
249             }
250         }
251
252         // Now make sure that the JDBC driver is what we expect
253

254         try {
255             _driver = DriverManager.getDriver( getClientSettings()[ DATABASE_URL ] );
256             _driverLevel = new Version( _driver.getMajorVersion(), _driver.getMinorVersion() );
257         }
258         catch (SQLException e)
259         {
260             printStackTrace( e );
261             
262             throw new Exception JavaDoc
263                 ( "Driver doesn't understand expected URL: " + getClientSettings()[ DATABASE_URL ] );
264         }
265
266         println
267             (
268                 "Driver " + _driver.getClass().getName() +
269                 " Version = " + _driverLevel
270             );
271         
272         return true;
273     }
274
275     //
276
// Initialize server settings. Assumes that you have called
277
// findClient().
278
//
279
private static boolean findServer()
280         throws Exception JavaDoc
281     {
282         try {
283             Connection conn = getConnection();
284             DatabaseMetaData dmd = conn.getMetaData();
285             String JavaDoc dbProductVersion = dmd.getDatabaseProductVersion();
286
287             _serverLevel = new Version( dbProductVersion );
288             
289             parseServerVMVersion( conn );
290         }
291         catch (Exception JavaDoc e)
292         {
293             printStackTrace( e );
294             
295             throw new Exception JavaDoc( "Error lookup up server info: " + e.getMessage() );
296         }
297         
298         println( "Server Version = " + _serverLevel );
299
300         return true;
301     }
302
303     private static boolean parseVMLevel()
304         throws Exception JavaDoc
305     {
306         String JavaDoc vmVersion = getVMVersion();
307
308         try {
309             _clientVMLevel = new Version( vmVersion );
310         }
311         catch (NumberFormatException JavaDoc e)
312         {
313             throw new Exception JavaDoc( "Badly formatted vm version: " + vmVersion );
314         }
315
316         println( "VM Version = " + _clientVMLevel );
317
318         return true;
319     }
320
321     private static boolean parseArgs( String JavaDoc args[] )
322         throws Exception JavaDoc
323     {
324         if ( ( args == null ) || (args.length == 0 ) )
325         { throw new Exception JavaDoc( "Missing database name." ); }
326         
327         setDatabaseName( args[ 0 ] );
328
329         if ( (args.length > 1) && !"".equals( args[ 1 ] ) )
330         {
331             String JavaDoc desiredClientName = args[ 1 ];
332             int count = LEGAL_CLIENTS.length;
333
334             for ( int i = 0; i < count; i++ )
335             {
336                 String JavaDoc[] candidate = LEGAL_CLIENTS[ i ];
337
338                 if ( desiredClientName.equals( candidate[ DRIVER_NAME ] ) )
339                 {
340                     setClient( candidate );
341                     break;
342                 }
343             }
344
345             if ( getClientSettings() == null )
346             {
347                 throw new Exception JavaDoc
348                     ( "Could not find client " + desiredClientName + " on the classpath." );
349             }
350         }
351             
352         return true;
353     }
354
355     /**
356      * <p>
357      * Get the vm level of the server.
358      * </p>
359      */

360     private static void parseServerVMVersion( Connection conn )
361         throws SQLException
362     {
363         dropFunction( conn, SERVER_VERSION_FUNCTION );
364
365         PreparedStatement ps = prepare
366             (
367                 conn,
368                 "create function " + SERVER_VERSION_FUNCTION + "() returns varchar(50)\n" +
369                 "parameter style java no sql language java\n" +
370                 "external name 'org.apache.derbyTesting.functionTests.tests.junitTests.compatibility.CompatibilitySuite.getVMVersion'"
371             );
372         ps.execute();
373         close( ps );
374
375         ps = prepare
376             (
377                 conn,
378                 "values " + SERVER_VERSION_FUNCTION + "()"
379             );
380
381         ResultSet rs = ps.executeQuery();
382         rs.next();
383         String JavaDoc rawVersion = rs.getString( 1 );
384         close( rs );
385         close( ps );
386
387         _serverVMLevel = new Version( rawVersion );
388             
389         println( "Server VM Version = " + _serverVMLevel );
390     }
391
392     ///////////////
393
//
394
// SQL MINIONS
395
//
396
///////////////
397

398     /////////////////////////////////////////////////////////////
399
//
400
// INNER CLASSES
401
//
402
/////////////////////////////////////////////////////////////
403

404     /**
405      * <p>
406      * This helper class exposes an entry point for creating an empty database.
407      * </p>
408      */

409     public static final class Creator
410     {
411         private static CompatibilitySuite _driver = new CompatibilitySuite();
412         
413         /**
414          * <p>
415          * Wait for server to come up, then create the database.
416          * </p>
417          *
418          * <ul>
419          * <li>args[ 0 ] = name of database to create.</li>
420          * </ul>
421          */

422         public static void main( String JavaDoc[] args )
423             throws Exception JavaDoc
424         {
425             String JavaDoc databaseName = args[ 0 ];
426
427             CompatibilitySuite.findClient();
428             
429             _driver.createDB( databaseName );
430         }
431         
432     }
433
434     /**
435      * <p>
436      * A class for storing a major and minor version number. This class
437      * assumes that more capable versions compare greater than less capable versions.
438      * </p>
439      */

440     public static final class Version implements Comparable JavaDoc
441     {
442         private int _major;
443         private int _minor;
444
445         public Version( int major, int minor )
446         {
447             constructorMinion( major, minor );
448         }
449
450         public Version( String JavaDoc desc )
451             throws NumberFormatException JavaDoc
452         {
453             StringTokenizer tokens = new StringTokenizer( desc, "." );
454
455             constructorMinion
456                 (
457                     java.lang.Integer.parseInt( tokens.nextToken() ),
458                     java.lang.Integer.parseInt( tokens.nextToken() )
459                 );
460         }
461
462         private void constructorMinion( int major, int minor )
463         {
464             _major = major;
465             _minor = minor;
466         }
467
468         /**
469          * <p>
470          * Returns true if this Version is at least as advanced
471          * as that Version.
472          * </p>
473          */

474         public boolean atLeast( Version that )
475         {
476             return this.compareTo( that ) > -1;
477         }
478
479
480         ////////////////////////////////////////////////////////
481
//
482
// Comparable BEHAVIOR
483
//
484
////////////////////////////////////////////////////////
485

486         public int compareTo( Object JavaDoc other )
487         {
488             if ( other == null ) { return -1; }
489             if ( !( other instanceof Version ) ) { return -1; }
490
491             Version that = (Version) other;
492
493             if ( this._major < that._major ) { return -1; }
494             if ( this._major > that._major ) { return 1; }
495
496             return this._minor - that._minor;
497         }
498
499         ////////////////////////////////////////////////////////
500
//
501
// Object OVERLOADS
502
//
503
////////////////////////////////////////////////////////
504

505         public String JavaDoc toString()
506         {
507             return Integer.toString( _major ) + '.' + Integer.toString( _minor );
508         }
509
510         public boolean equals( Object JavaDoc other )
511         {
512             return (compareTo( other ) == 0);
513         }
514
515         public int hashCode()
516         {
517             return _major ^ _minor;
518         }
519         
520     }
521
522     
523
524 }
525
Popular Tags