View Javadoc

1   /* $Id: DatabaseLocatedTest.java,v 1.2 2004/06/14 23:08:52 jdt Exp $
2    * Created on 28-Dec-2003 by John Taylor jdt@roe.ac.uk .
3    * 
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file. 
9    */
10  package org.astrogrid.mySpace.installationtest;
11  
12  import java.sql.Connection;
13  import java.sql.DriverManager;
14  import java.sql.ResultSet;
15  import java.sql.SQLException;
16  import java.sql.Statement;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import org.astrogrid.mySpace.mySpaceManager.MMC;
22  
23  import junit.framework.TestCase;
24  
25  /***
26   * Following installation, some jiggery pokery of database files is 
27   * required.  This tests that it has been done correctly.
28   * @author john taylor
29   */
30  public class DatabaseLocatedTest extends TestCase {
31    /*** Logger */
32    private static Log log = LogFactory.getLog(DatabaseLocatedTest.class);
33    /***
34     * Constructor for DatabaseLocatedTest.
35     * @param arg0 test name
36     */
37    public DatabaseLocatedTest(final String arg0) {
38      super(arg0);
39    }
40    /***
41     * fire up the text ui
42     * @param args ignored
43     */
44    public static void main(final String[] args) {
45      junit.textui.TestRunner.run(DatabaseLocatedTest.class);
46    }
47  
48    /***
49     * Name of the jdbc driver class.  Obviously will need changing if we change database
50     */
51    private String jdbcDriverClass = "org.hsqldb.jdbcDriver";
52    /***
53     * Admin user for hsql database
54     */
55    private String hsqldbUserName = "sa";
56    /***
57     * Admin user password for hsqldatabase
58     */
59    private String hsqldbPassWord = "";
60  
61    /***
62     * keep a reference to the connection to close it in tearDown
63     */
64    private Connection connection;
65    /***
66     * First, can we get a connection to the database?
67     * @throws ClassNotFoundException driver not found
68     * @throws SQLException database connection problem
69     *
70     */
71    public final void testDatabaseExists()
72      throws ClassNotFoundException, SQLException {
73      Connection conn = getConnection();
74      assertNotNull(conn);
75    }
76    /***
77     * Get a connection to the hsqldb database
78     * @return Connection to the database 
79     * @throws ClassNotFoundException driver not found
80     * @throws SQLException database connection problem
81     */
82    private final Connection getConnection()
83      throws ClassNotFoundException, SQLException {
84      String registryName = MMC.getProperty(MMC.REGISTRYCONF, MMC.CATLOG);
85      assert registryName != null;
86      String jdbcURL = "jdbc:hsqldb:" + registryName + ".db"; //hsqldb specific
87      log.debug("jdbcURL: " + jdbcURL);
88  
89      //  Establish a connection to the database.
90      Class.forName(jdbcDriverClass);
91      connection =
92        DriverManager.getConnection(jdbcURL, hsqldbUserName, hsqldbPassWord);
93      return connection;
94    }
95  
96    /***
97     *  Check correct tables exist in database
98     * @throws ClassNotFoundException problem getting connection
99     * @throws SQLException problem getting connection, or problem on executing query
100    */
101   public final void testTablesExist()
102     throws ClassNotFoundException, SQLException {
103     Connection conn = getConnection();
104     Statement stmt = conn.createStatement();
105     ResultSet rs = stmt.executeQuery("select * from REG");
106     //will throw SQLException if not found
107     ResultSet rs2 = stmt.executeQuery("select * from SERVERS");
108     assertTrue("SERVERS must contain at least one row", rs2.next());
109   }
110   /*** 
111    * Close any open connection
112    * @see junit.framework.TestCase#tearDown()
113    */
114   public final void tearDown() {
115     log.debug("TearDown");
116     if (connection!=null) {
117       try {
118         log.debug("Closing db connection");
119         connection.close();
120       } catch( SQLException sqle ) {
121          log.debug("Tried to close database connection but got "+sqle);
122         //well, we tried
123       }
124     }
125   }
126 }