001    package com.sptci.rwt;
002    
003    import java.sql.Connection;
004    import java.util.Map;
005    
006    import static junit.framework.Assert.*;
007    import junit.framework.Test;
008    import junit.framework.TestCase;
009    import junit.framework.TestSuite;
010    
011    /**
012     * Unit test for the {@link Connections} object.  Test initialising,
013     * adding and deleting saved connections.
014     *
015     * <p>Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
016     * @author Rakesh Vidyadharan 2007-09-25
017     * @version $Id: ConnectionsTest.java 4123 2008-05-25 21:49:01Z rakesh $
018     */
019    public class ConnectionsTest extends TestCase
020    {
021      static Connections connections;
022      static final String userName = "user" + System.currentTimeMillis();
023      static final String connectionName = "connection" + System.currentTimeMillis();
024    
025      public static Test suite()
026      {
027        return new TestSuite( ConnectionsTest.class );
028      }
029    
030      /**
031       * Test instantiating a new instance through the {@link
032       * Connections#getInstance} method when no saved state is available.
033       */
034      public void testNewInstantiation() throws Exception
035      {
036        connections = Connections.getInstance( userName );
037        assertTrue( "Checking no saved data",
038            connections.getDatabaseTypes().size() == 0 );
039      }
040    
041      /**
042       * Test adding a new {@link ConnectionParameters} to the saved state using
043       * the {@link Connections#add} method.
044       */
045      public void testAdd()
046      {
047        connections.add( connectionName, CreateTestObjects.parameters );
048        assertTrue( "Checking saved connection added",
049            connections.getDatabaseTypes().size() > 0 );
050      }
051    
052      /**
053       * Test fetching a {@link java.sql.Connection} using the {@link
054       * Connections#getConnection} method.
055       *
056       * @see #connect
057       */
058      public void testConnection()
059      {
060        connect( CreateTestObjects.parameters.databaseType,
061            connectionName, connections );
062      }
063    
064      /**
065       * Test fetching a {@link java.sql.Connection} using the {@link
066       * ConnectionFactory#open( ConnectionParameters )} method after fetching
067       * the parameters from {@link Connections#getConnectionParameters}.
068       *
069       * @see #connectFromParameters
070       */
071      public void testConnectionFromParameters()
072      {
073        connectFromParameters( connections );
074      }
075    
076      /**
077       * Test adding a duplicate connection with same saved name.  Exception
078       * test case.
079       */
080      public void testDuplicateName()
081      {
082        connections.add( connectionName, CreateTestObjects.parameters );
083        assertTrue( "Checking saved connection added",
084            connections.getDatabaseTypes().size() > 0 );
085      }
086    
087      /**
088       * Test initialisation of {@link Connections} from persistent state.
089       *
090       * @see #connect
091       * @see #connectFromParameters
092       */
093      public void testInitialisation()
094      {
095        Connections c = Connections.getInstance( userName );
096        assertTrue( "Ensuring saved data loaded", c.getDatabaseTypes().size() > 0 );
097        for ( DatabaseType type : c.getDatabaseTypes() )
098        {
099          for ( Map.Entry<String,ConnectionData> entry :
100              type.getConnectionData().entrySet() )
101          {
102            connect( type.getName(), entry.getKey(), c );
103          }
104        }
105    
106        connectFromParameters( c );
107      }
108    
109      /**
110       * Test removing a saved connection from the persistent state using the
111       * {@link Connections#delete( String, String )} method.
112       */
113      public void testDeleteConnection()
114      {
115        connections.delete(
116            CreateTestObjects.parameters.databaseType, connectionName );
117        for ( DatabaseType type : connections.getDatabaseTypes() )
118        {
119          assertTrue( "Checking saved connection removed",
120              type.getConnectionData().size() == 0 );
121        }
122      }
123    
124      /**
125       * Test removing a database from the persistent state using the
126       * {@link Connections#delete( String )} method.
127       */
128      public void testDelete()
129      {
130        testAdd();
131        connections.delete( CreateTestObjects.parameters.databaseType );
132        assertTrue( "Checking saved database removed",
133            connections.getDatabaseTypes().size() == 0 );
134      }
135    
136      /**
137       * Open a connection and close it to test.
138       *
139       * @param databaseType The database engine to connect to.
140       * @param name The name of the saved connection.
141       * @param connections The connections object to use.
142       */
143      protected void connect( final String databaseType, final String name,
144          final Connections connections )
145      {
146        Connection connection =
147          connections.getConnection( databaseType, name );
148        assertNotNull( "Checking valid connection", connection );
149        ConnectionFactory.close( connection );
150      }
151    
152      /**
153       * Open a connection using {@link ConnectionParameters} and close it.
154       *
155       * @param connections The connections object to use.
156       */
157      protected void connectFromParameters( final Connections connections )
158      {
159        ConnectionParameters parms = connections.getConnectionParameters(
160            CreateTestObjects.parameters.databaseType, connectionName );
161        assertNotNull( "Checking valid ConnectionParameters", parms );
162    
163        Connection connection = ConnectionFactory.open( parms );
164        assertNotNull( "Checking valid connection", connection );
165        ConnectionFactory.close( connection );
166      }
167    }