⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountdaomysqltest.java

📁 一个功能较为完善的论坛
💻 JAVA
字号:
package org.redsoft.forum.dao.mysql;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import org.redsoft.forum.dao.mysql.AccountDAOmySql;
import java.sql.ResultSet;
import java.sql.Connection;
import org.redsoft.forum.web.Account;
import org.redsoft.forum.exception.AccountNotFoundException;
import org.redsoft.forum.exception.AccountAlreadyExistException;
import java.sql.SQLException;
import org.redsoft.forum.fixture.MysqlFixture;

/**
 * Test case for AccountDAOmySql
 *
 */
public class AccountDAOmySqlTest extends TestCase {

private MysqlFixture mysqlFixtureIns = new MysqlFixture();

private Connection conn = null;
private static final String USER_NAME ="ForTest";

public AccountDAOmySqlTest(String name) {
    super(name);
}

public void setUp() throws Exception {
	mysqlFixtureIns.setUp();
}

public void tearDown() throws Exception {
	mysqlFixtureIns.tearDown();
}

public void testAddAccountNormal(){

    AccountDAOmySql dao = new AccountDAOmySql();
    Account account = new Account(USER_NAME,"charles","charles_hhb@hotmail.com");

    try {
        dao.removeAccount( account.getUserName() );
    } catch( final SQLException sql ){
        fail("Unexpected exception" + sql.toString() );
    }

    try{
        dao.addAccount( account );
        Account account_new = dao.findByUserName( account.getUserName() );
        assertEquals("Expecting charles",account.getUserName(),account_new.getUserName() );
        assertEquals("Expecting charles",account.getPassword(),account_new.getPassword() );
        assertEquals("Expecting charles@hotmail.com",account.getEmail(),account_new.getEmail() );
        dao.removeAccount( account.getUserName() );
    }catch( final Exception e ){
        e.printStackTrace();
        fail("Unexpected exception::" + e.toString());
    }
}

public void testAddAccountAlreadyExist(){
    AccountDAOmySql dao = null;
    Account account = null;
    try{
	dao = new AccountDAOmySql();
        account = new Account(USER_NAME,"charles","charles_hhb@hotmail.com");
        dao.addAccount( account );
        Account account_new = dao.findByUserName( account.getUserName() );
        assertEquals("Expecting " + USER_NAME,account.getUserName(),account_new.getUserName() );
        assertEquals("Expecting charles",account.getPassword(),account_new.getPassword() );
        assertEquals("Expecting charles@hotmail.com",account.getEmail(),account_new.getEmail() );
        dao.addAccount( account );
        fail("AccountAlreadyExistException expected");
    }catch( final SQLException e ){
        e.printStackTrace();
        fail("Unexpected exception::" + e.toString());
    }catch( final AccountNotFoundException notFound ){
		notFound.printStackTrace();
		fail("Unexpected exception: " + notFound.toString() );

    }catch( final AccountAlreadyExistException ex ){
		// Pass
		try{
			dao.removeAccount( account.getUserName() );
		}catch( final SQLException sql ){
			sql.printStackTrace();
			fail("Unexpected exception ");
		}
	}
}

public void testUpdateAccountAlreadyExist(){
    AccountDAOmySql dao = null;
    Account account = null;
    try{
	dao = new AccountDAOmySql();
        account = new Account(USER_NAME,"charles","charles_hhb@hotmail.com");
        dao.addAccount( account );
        dao.updateAccount( account );
    }catch( final SQLException e ){
        e.printStackTrace();
        fail("Unexpected exception::" + e.toString());
    }catch( final AccountNotFoundException notFound ){
	notFound.printStackTrace();
	fail("Unexpected exception: " + notFound.toString() );

    }catch( final AccountAlreadyExistException ex ){
		// Pass
		try{
	        dao.removeAccount( account.getUserName() );
		}catch( final SQLException sql ){
			sql.printStackTrace();
			fail("Unexpected exception ");
		}
	}
}

public void testRemoveAccount(){

    AccountDAOmySql dao = new AccountDAOmySql();
    Account account = new Account(USER_NAME,"charles","charles_hhb@hotmail.com");
    try{
        dao.addAccount( account );
    }catch( final AccountAlreadyExistException ex ){
        //pass
    }catch( final SQLException sql ){
        fail("Unexpected exception" + sql.toString() );
    }

    try {
        dao.removeAccount( account.getUserName() );
        Account account_new = dao.findByUserName( account.getUserName() );
        fail("Expecting AccountNotFoundException");
    } catch( final SQLException sql ){
        fail("Unexpected exception" + sql.toString() );
    } catch( final AccountNotFoundException notFound ){
        // Pass
    }

}

public void testFindByUserNameNotFound(){
	try{
		AccountDAOmySql dao = new AccountDAOmySql();
        dao.findByUserName( "NotExist" );
		// fail
		fail("Expecting excpeiton");
	}catch( final AccountNotFoundException e ){
		//Pass
	}catch( final SQLException sql){
		sql.printStackTrace();
		fail("Unexpected exeption:" + sql.toString() );
	}
}

}//EOC

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -