Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/test/connections/SuppliedConnectionTest.java


1   // $Id: SuppliedConnectionTest.java 9595 2006-03-10 18:14:21Z steve.ebersole@jboss.com $
2   package org.hibernate.test.connections;
3   
4   import org.hibernate.cfg.Configuration;
5   import org.hibernate.cfg.Environment;
6   import org.hibernate.ConnectionReleaseMode;
7   import org.hibernate.Session;
8   import org.hibernate.tool.hbm2ddl.SchemaExport;
9   import org.hibernate.connection.ConnectionProvider;
10  import org.hibernate.connection.ConnectionProviderFactory;
11  import org.hibernate.connection.UserSuppliedConnectionProvider;
12  
13  import java.sql.Connection;
14  import java.sql.ResultSet;
15  
16  import junit.framework.Test;
17  import junit.framework.TestSuite;
18  
19  /**
20   * Implementation of SuppliedConnectionTest.
21   *
22   * @author Steve Ebersole
23   */
24  public class SuppliedConnectionTest extends ConnectionManagementTestCase {
25  
26    private ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider();
27    private Connection connectionUnderTest;
28  
29    public SuppliedConnectionTest(String name) {
30      super( name );
31    }
32  
33    public static Test suite() {
34      return new TestSuite( SuppliedConnectionTest.class );
35    }
36  
37    protected Session getSessionUnderTest() throws Throwable {
38      connectionUnderTest = cp.getConnection();
39      return getSessions().openSession( connectionUnderTest );
40    }
41  
42    protected void reconnect(Session session) {
43      session.reconnect( connectionUnderTest );
44    }
45  
46    protected void done() throws Throwable {
47      cp.closeConnection( connectionUnderTest );
48    }
49  
50    protected void configure(Configuration cfg) {
51      super.configure( cfg );
52      cfg.setProperty( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString() );
53      cfg.setProperty( Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName() );
54      boolean supportsScroll = true;
55      try {
56        Connection conn = cp.getConnection();
57        supportsScroll = conn.getMetaData().supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
58      }
59      catch( Throwable ignore ) {
60      }
61      cfg.setProperty( Environment.USE_SCROLLABLE_RESULTSET, "" + supportsScroll );
62    }
63  
64    protected boolean dropAfterFailure() {
65      return false;
66    }
67  
68    protected boolean recreateSchema() {
69      return false;
70    }
71  
72    protected void setUp() throws Exception {
73      super.setUp();
74      Connection conn = cp.getConnection();
75      try {
76        new SchemaExport( getCfg(), conn ).create( false, true );
77      }
78      finally {
79        if ( conn != null ) {
80          try {
81            cp.closeConnection( conn );
82          }
83          catch( Throwable ignore ) {
84          }
85        }
86      }
87    }
88  
89    protected void tearDown() throws Exception {
90      Connection conn = cp.getConnection();
91      try {
92        new SchemaExport( getCfg(), conn ).drop( false, true );
93      }
94      finally {
95        if ( conn != null ) {
96          try {
97            cp.closeConnection( conn );
98          }
99          catch( Throwable ignore ) {
100         }
101       }
102     }
103     try {
104       cp.close();
105     }
106     catch( Throwable ignore ) {
107     }
108     super.tearDown();
109   }
110 }