/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.jdbc.xa.test;

import java.sql.Connection;
import java.sql.Statement;

import javax.transaction.TransactionManager;

import junit.framework.TestCase;
import org.jfox.jdbc.xa.TxDataSource;
import org.jfox.tm.TxManager;
import org.huihoo.jfox.util.InitialContextHelper;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class MysqlTxDataSourceByJNDITest extends TestCase {

  private TxDataSource txds = null;

  protected void setUp() throws Exception {
//    txds = XADataSourceManager.createDataSource("MysqlTestXADataSource","jdbc:mysql://10.5.2.33/test","yang","yy");
    txds = (TxDataSource)javax.rmi.PortableRemoteObject.narrow(InitialContextHelper.getInitialConext().lookup("datasource/TestDataSource"),TxDataSource.class);
  }

  public void testNotTransaction(){
    System.out.println("########### testNotTransaction ##########");
    try {
      Connection conn = txds.getConnection();
      conn.setAutoCommit(false);
      Statement stm = conn.createStatement();
      stm.executeUpdate("insert into user set name=\"yangyong_No_Transaction :)\"");
      conn.commit();
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  public void testCommit() {
    System.out.println("########### testCommit ##########");
    try {
      TransactionManager tm = TxManager.getInstance();
      tm.begin();
      Connection conn = txds.getConnection();
      Statement stm = conn.createStatement();
      stm.executeUpdate("insert into user set name=\"yangyong :)\"");
      tm.commit();
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  public void testRollback() {
    System.out.println("########### testRollback ##########");
    try {
      TransactionManager tm = TxManager.getInstance();
      tm.begin();
      txds.getConnection();
      Connection conn = txds.getConnection();
      Statement stm = conn.createStatement();
      stm.executeUpdate("insert into user set name=\"yy :)\"");
      tm.rollback();
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  public void testSimple(){
    try {
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  public static void main(String[] args) {

  }
}