/*
 * 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 org.jdbi.v3.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.Connection;
import java.sql.SQLException;

import org.h2.jdbcx.JdbcDataSource;
import org.jdbi.v3.core.extension.ExtensionFactory;
import org.jdbi.v3.core.extension.HandleSupplier;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

public class TestOnDemandMethodBehavior {
    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private ExtensionFactory mockExtensionFactory;

    @Mock
    private UselessDao mockDao;

    private Jdbi db;

    private UselessDao onDemand;

    private UselessDao anotherOnDemand;

    public interface UselessDao {
        default void run(Runnable runnable) {
            runnable.run();
        }

        default void blowUp() throws SQLException { throw new SQLException("boom"); }

        void foo();
    }

    public class UselessDaoExtension implements ExtensionFactory {
        @Override
        public boolean accepts(Class<?> extensionType) { return UselessDao.class.equals(extensionType); }

        @Override
        public <E> E attach(Class<E> extensionType, HandleSupplier handle) {
            return extensionType.cast((UselessDao) () -> { });
        }
    }

    @Before
    public void setUp() throws Exception {
        when(mockExtensionFactory.accepts(UselessDao.class)).thenReturn(true);

        final JdbcDataSource ds = new JdbcDataSource() {
            private static final long serialVersionUID = 1L;

            @Override
            public Connection getConnection() throws SQLException {
                throw new UnsupportedOperationException();
            }
        };
        db = Jdbi.create(ds);
        db.registerExtension(mockExtensionFactory);
        onDemand = db.onDemand(UselessDao.class);
        anotherOnDemand = db.onDemand(UselessDao.class);
    }

    @Test
    public void testEqualsDoesntAttach() throws Exception {
        assertThat(onDemand).isEqualTo(onDemand);
        assertThat(onDemand).isNotEqualTo(anotherOnDemand);
        verify(mockExtensionFactory, never()).attach(any(), any());
    }

    @Test
    public void testHashCodeDoesntAttach() throws Exception {
        assertThat(onDemand.hashCode()).isEqualTo(onDemand.hashCode());
        assertThat(onDemand.hashCode()).isNotEqualTo(anotherOnDemand.hashCode());
        verify(mockExtensionFactory, never()).attach(any(), any());
    }

    @Test
    public void testToStringDoesntAttach() throws Exception {
        assertThat(onDemand.toString()).isNotNull();
        verify(mockExtensionFactory, never()).attach(any(), any());
    }

    @Test
    public void testReentrantCallReusesExtension() {
        when(mockExtensionFactory.attach(any(), any()))
                .thenReturn(mockDao)
                .thenThrow(IllegalStateException.class);

        doCallRealMethod().when(mockDao).run(any());
        onDemand.run(onDemand::foo);

        verify(mockExtensionFactory).attach(eq(UselessDao.class), any());
        verify(mockDao).run(any());
        verify(mockDao).foo();
    }

    @Test(expected = SQLException.class)
    public void testExceptionThrown() throws Exception {
        db.registerExtension(new UselessDaoExtension());
        UselessDao uselessDao = db.onDemand(UselessDao.class);
        uselessDao.blowUp();
    }
}

@@ -636,16 +636,82 @@
 rtThat;%0A
+import static org.assertj.core.api.Assertions.assertThatThrownBy;%0A
 import s
@@ -750,16 +750,16 @@
 rs.any;%0A
-
 import s
@@ -1827,16 +1827,28 @@
 eption %7B
+%0A           
  throw n
@@ -1871,16 +1871,24 @@
 %22boom%22);
+%0A       
  %7D%0A%0A    
@@ -2051,16 +2051,28 @@
 nType) %7B
+%0A           
  return 
@@ -2110,16 +2110,24 @@
 onType);
+%0A       
  %7D%0A%0A    
@@ -2275,17 +2275,16 @@
  () -%3E %7B
- 
 %7D);%0A    
@@ -4142,39 +4142,8 @@
 Test
-(expected = SQLException.class)
 %0A   
@@ -4324,16 +4324,35 @@
         
+assertThatThrownBy(
 uselessD
@@ -4357,16 +4357,49 @@
 sDao
-.
+::
 blowUp
-(
+).isInstanceOf(SQLException.class
 );%0A 

Fix code style