// Copyright (c) 2008 Keith D Gregory
package com.kdgregory.example.proxy;

import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.TestCase;


/**
 *  This is not your ordinary unit test: it includes the code under test.
 *  Moreover, only one of the tests passes!
 */
public class CloseQuietlyTest extends TestCase
{
//----------------------------------------------------------------------------
//  The test methods
//----------------------------------------------------------------------------

    public void testCloseQuietly() throws Exception
    {
        ResultSetClosingProxy proxy = new ResultSetClosingProxy();
        closeQuietly(proxy.toStub());
        proxy.assertCloseCalled();
    }


    public void testCloseQuietlyWithException() throws Exception
    {
        ResultSetClosingProxy proxy = new ResultSetClosingProxy()
                                      .setThrowOnClose(true);
        closeQuietly(proxy.toStub());
        proxy.assertCloseCalled();
    }


    public void testCloseQuietlyWhenNull() throws Exception
    {
        closeQuietly(null);
    }


//----------------------------------------------------------------------------
//  The method being tested -- put here for convenience
//----------------------------------------------------------------------------

    public static void closeQuietly(ResultSet rslt)
    {
        try
        {
            rslt.close();
        }
        catch (SQLException e)
        {
            // we don't really want to do this!
            throw new RuntimeException(e);
        }
    }
}
