KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > dbschema > util > AbstractDBSchemaTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.cmp2.dbschema.util;
8
9 import net.sourceforge.junitejb.EJBTestCase;
10
11 import javax.sql.DataSource JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.sql.SQLException JavaDoc;
16
17
18 /**
19  *
20  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
21  */

22 public class AbstractDBSchemaTestCase
23    extends EJBTestCase
24 {
25    protected String JavaDoc datasourceName = "java:/DefaultDS";
26    protected DataSource JavaDoc datasource;
27
28    public AbstractDBSchemaTestCase(String JavaDoc s)
29    {
30       super(s);
31    }
32
33    protected Connection JavaDoc getConnection()
34       throws NamingException JavaDoc, SQLException JavaDoc
35    {
36       return getDataSource().getConnection();
37    }
38
39    protected DataSource JavaDoc getDataSource() throws NamingException JavaDoc
40    {
41       if(datasource == null)
42       {
43          datasource = getDataSource(datasourceName);
44       }
45       return datasource;
46    }
47
48    protected DataSource JavaDoc getDataSource(String JavaDoc datasource) throws NamingException JavaDoc
49    {
50       InitialContext JavaDoc ic = null;
51       try
52       {
53          ic = new InitialContext JavaDoc();
54          return (DataSource JavaDoc)ic.lookup(datasource);
55       }
56       finally
57       {
58          if(ic != null)
59             try
60             {
61                ic.close();
62             }
63             catch(Exception JavaDoc ignore){}
64       }
65    }
66 }
67
Popular Tags