/*
 * 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.ExtensionConfig;
import org.jdbi.v3.core.extension.ExtensionFactory;
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 ExtensionConfig mockConfig;

    @Mock
    private UselessDao mockDao;

    private Jdbi dbi;

    private UselessDao onDemand;

    private UselessDao anotherOnDemand;

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

        void foo();
    }

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

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

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

    @Test
    public void testEqualsDoesntAttach() throws Exception
    {
        assertThat(onDemand).isEqualTo(onDemand);
        assertThat(onDemand).isNotEqualTo(anotherOnDemand);
        verify(mockExtensionFactory, never()).attach(any(), 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(), any());
    }

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

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

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

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

@@ -1006,59 +1006,8 @@
 ce;%0A
-import org.jdbi.v3.core.extension.ExtensionConfig;%0A
 impo
@@ -1405,59 +1405,8 @@
 y;%0A%0A
-    @Mock%0A    private ExtensionConfig mockConfig;%0A%0A
     
@@ -1829,82 +1829,8 @@
 ue);
-%0A        when(mockExtensionFactory.createConfig()).thenReturn(mockConfig);
 %0A%0A  
@@ -2567,39 +2567,32 @@
 )).attach(any(),
- any(),
  any());%0A    %7D%0A%0A
@@ -2872,39 +2872,32 @@
 )).attach(any(),
- any(),
  any());%0A    %7D%0A%0A
@@ -3080,31 +3080,24 @@
 ttach(any(),
- any(),
  any());%0A   
@@ -3167,70 +3167,8 @@
 ) %7B%0A
-        when(mockConfig.createCopy()).thenReturn(mockConfig);%0A
     
@@ -3214,15 +3214,8 @@
 y(),
- any(),
  any
@@ -3474,24 +3474,8 @@
 ss),
- eq(mockConfig),
  any

Fix compile errors from merge