/*
 * Copyright 2004 - 2011 Brian McCallister
 *
 * 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.skife.jdbi.v2.unstable.eod;

import junit.framework.TestCase;
import org.h2.jdbcx.JdbcDataSource;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Something;
import org.skife.jdbi.v2.Transaction;
import org.skife.jdbi.v2.TransactionStatus;

public class TestMixinInterfaces extends TestCase
{
    private DBI dbi;
    private Handle handle;


    public void setUp() throws Exception
    {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test");
        dbi = new DBI(ds);
        handle = dbi.open();

        handle.execute("create table something (id int primary key, name varchar(100))");

    }

    public void tearDown() throws Exception
    {
        handle.execute("drop table something");
        handle.close();
    }


    public void testGetHandle() throws Exception
    {
        WithGetHandle g = EOD.attach(handle, WithGetHandle.class);
        Handle h = g.getHandle();

        assertSame(handle, h);
    }

    public void testBeginAndCommitTransaction() throws Exception
    {
        TransactionStuff txl = EOD.attach(handle, TransactionStuff.class);

        txl.insert(8, "Mike");

        txl.begin();
        txl.updateName(8, "Miker");
        assertEquals("Miker", txl.byId(8).getName());
        txl.rollback();

        assertEquals("Mike", txl.byId(8).getName());

    }

    public void testInTransaction() throws Exception
    {
        TransactionStuff txl = EOD.attach(handle, TransactionStuff.class);
        txl.insert(7, "Keith");

        Something s = txl.inTransaction(new Transaction<Something, TransactionStuff>() {
            public Something inTransaction(TransactionStuff conn, TransactionStatus status) throws Exception
            {
                return conn.byId(7);
            }
        });

        assertEquals("Keith", s.getName());
    }

    public static interface WithGetHandle extends CloseMe, GetHandle
    {

    }

    public static interface TransactionStuff extends CloseMe, Transactional<TransactionStuff> {

        @SqlQuery("select id, name from something where id = :id")
        @Mapper(SomethingMapper.class)
        public Something byId(@Bind("id") long id);

        @SqlUpdate("update something set name = :name where id = :id")
        public void updateName(@Bind("id") long id, @Bind("name") String name);

        @SqlUpdate("insert into something (id, name) values (:id, :name)")
        public void insert(@Bind("id") long id, @Bind("name") String name);
    }
}


@@ -888,16 +888,60 @@
 nStatus;
+%0Aimport org.skife.jdbi.v2.util.StringMapper;
 %0A%0Apublic
@@ -1168,16 +1168,26 @@
 mem:test
+;MVCC=TRUE
 %22);%0A    
@@ -2524,32 +2524,1053 @@
 Name());%0A    %7D%0A%0A
+    public void testTransactionIsolationActuallyHappens() throws Exception%0A    %7B%0A        TransactionStuff txl = EOD.attach(handle, TransactionStuff.class);%0A        TransactionStuff tx2 = EOD.open(dbi, TransactionStuff.class);%0A%0A%0A        txl.insert(8, %22Mike%22);%0A%0A        txl.begin();%0A%0A        txl.updateName(8, %22Miker%22);%0A        assertEquals(%22Miker%22, txl.byId(8).getName());%0A        assertEquals(%22Mike%22, tx2.byId(8).getName());%0A%0A        txl.commit();%0A%0A        assertEquals(%22Miker%22, tx2.byId(8).getName());%0A%0A        tx2.close();%0A    %7D%0A%0A    public void testJustJdbiTransactions() throws Exception%0A    %7B%0A        Handle h1 = dbi.open();%0A        Handle h2 = dbi.open();%0A%0A        h1.execute(%22insert into something (id, name) values (8, 'Mike')%22);%0A%0A        h1.begin();%0A        h1.execute(%22update something set name = 'Miker' where id = 8%22);%0A%0A        assertEquals(%22Mike%22, h2.createQuery(%22select name from something where id = 8%22).map(StringMapper.FIRST).first());%0A        h1.commit();%0A        h1.close();%0A        h2.close();%0A    %7D%0A%0A
     public stati

test actual transaction isolation