Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.service;
 
 
 
 import static com.ursaj.hfs.HfsTestUtils.assertThrows;
 import static com.ursaj.hfs.HfsTestUtils.getField;
 import static org.mockito.Mockito.*;
 import static org.testng.Assert.*;

HfsDbAdapter test cases.
 
 public class HfsDbAdapterTest {
    
Test data source initialization.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testDataSource() throws Exception {
         final InitialContext ctx = new InitialContext();
         final DataSource dataSource = mock(DataSource.class);
         final HfsDbAdapter adapter = new EmptyDbService();
         final String beanName = adapter.getDataSourceName();
 
         ctx.unbind(beanName);
         assertThrows(IllegalStateException.class"Failed to lookup data source in local context.",
             adapter"getDataSource");
 
         ctx.bind(beanNamedataSource);
         assertSame(adapter.getDataSource(), dataSource);
 
         adapter.setDataSource(null);
         assertNull(getField(adapter"dataSource"DataSource.class), "Expects data source field was reset to null.");
         assertSame(adapter.getDataSource(), dataSource);
 
         ctx.unbind(beanName);
         assertSame(adapter.getDataSource(), dataSource);
 
         adapter.setDataSource(null);
         assertNull(getField(adapter"dataSource"DataSource.class), "Expects data source field was reset to null.");
         assertThrows(IllegalStateException.class"Failed to lookup data source in local context.",
             adapter"getDataSource");
     }

    
Test action execution with provided connection.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testDoWithConnection() throws Exception {
         final DataSource dataSource = mock(DataSource.class);
         final Connection connection = mock(Connection.class);
         final HfsDbAdapter adapter = new EmptyDbService();
         final AtomicBoolean done = new AtomicBoolean();
         final HfsDbAdapter.Action action = new HfsDbAdapter.Action<Void>() {
             @Override
             public Void execute(Connection connectionthrows SQLExceptionIOException {
                 if (!done.compareAndSet(falsetrue))
                     throw new IOException("Already executed.");
 
                 return null;
             }
         };
 
         new InitialContext().rebind(adapter.getDataSourceName(), dataSource);
 
         // Check failed due to no DB connection.
         doThrow(new SQLException("No connection.")).when(dataSource).getConnection();
         assertThrows(IOException.class"Failed to execute SQL request."adapter"doWithinConnection"action);
 
         // Check succeed.
         doReturn(connection).when(dataSource).getConnection();
         assertFalse(done.get());
         adapter.doWithinConnection(action);
 
         // Check failed due to 'action' throws IO exception.
         assertTrue(done.get());
        assertThrows(IOException.class"Already executed."adapter"doWithinConnection"action);
        // Check failed due to connection has not been closed correctly.
        done.set(false);
        doThrow(new SQLException("Silent #1.")).when(connection).rollback();
        doThrow(new SQLException("Silent #2.")).when(connection).close();
        assertThrows(IOException.class"Failed to execute SQL request."adapter"doWithinConnection"action);
        assertTrue(done.get());
    }

    
DB adapter for tests.
    private static final class EmptyDbService extends HfsDbAdapter {
    }
New to GrepCode? Check out our FAQ X