Package com.p6spy.engine.common

Source Code of com.p6spy.engine.common.P6WrapperUnwrapDelegateTest

/*
* #%L
* P6Spy
* %%
* Copyright (C) 2013 P6Spy
* %%
* 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.
* #L%
*/
package com.p6spy.engine.common;

import com.p6spy.engine.proxy.GenericInvocationHandler;
import com.p6spy.engine.proxy.ProxyFactory;
import com.p6spy.engine.test.AbstractTestConnection;
import com.p6spy.engine.test.BaseTestCase;
import com.p6spy.engine.test.TestConnection;
import com.p6spy.engine.test.TestConnectionImpl;
import org.apache.commons.dbcp.DelegatingConnection;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Wrapper;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class P6WrapperUnwrapDelegateTest extends BaseTestCase {

  @Test
  public void testCastableFromProxy() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the proxy implements the interface then the proxy should be returned
    {
      Connection unwrapped = proxy.unwrap(Connection.class);
      assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
      TestConnection unwrapped = proxy.unwrap(TestConnection.class);
      assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
      Wrapper unwrapped = proxy.unwrap(Wrapper.class);
      assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
      AutoCloseable unwrapped = proxy.unwrap(AutoCloseable.class);
      assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    // TestConnectionImpl is not implemented by the proxy - proxy will not be returned
    {
      TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class);
      assertFalse(ProxyFactory.isProxy(unwrapped));
    }

  }

  @Test
  public void testCastableFromUnderlying() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the underlying object extends the class (or matches the class) then the underlying object should be returned.
    {
      AbstractTestConnection unwrapped = proxy.unwrap(AbstractTestConnection.class);
      assertFalse(ProxyFactory.isProxy(unwrapped));
    }

    {
      TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class);
      assertFalse(ProxyFactory.isProxy(unwrapped));
    }

  }

  @Test
  public void testProxyOfWrappedConnection() throws SQLException {
    // this will be the actual connection
    Connection con = new TestConnectionImpl();

    // use a wrapper from DBCP to create a proxy of a proxy
    // Note: DBCP implements with JDBC 4.0 API so the Wrapper interface
    // is implemented here.
    DelegatingConnection underlying = new DelegatingConnection(con);

    Connection proxy = ProxyFactory.createProxy(underlying, new GenericInvocationHandler<Connection>(underlying));

    // TestConnection is an interface of the actual connection but not of the proxy.  Unwrapping works
    // but a proxy is not returned
    {
      TestConnection unwrapped = proxy.unwrap(TestConnection.class);
      assertFalse(ProxyFactory.isProxy(unwrapped));
    }

    // ResultSet is not implemented at all - an exception will be thrown
    try {
      proxy.unwrap(ResultSet.class);
      fail("Expected exception not thrown");
    } catch (SQLException e) {
    }

  }

}
TOP

Related Classes of com.p6spy.engine.common.P6WrapperUnwrapDelegateTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.