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

📄 threaddaomysqltest.java

📁 一个功能较为完善的论坛
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.redsoft.forum.dao.mysql;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import org.redsoft.forum.dao.mysql.ThreadDAOmySql;
import java.sql.ResultSet;
import java.sql.Connection;
import org.redsoft.forum.dao.PersistentThread;
import org.redsoft.forum.web.Thread;
import org.redsoft.forum.exception.ThreadNotFoundException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import org.redsoft.forum.dao.DAOFactory;

import org.redsoft.forum.exception.CategoryNotFoundException;
import org.redsoft.forum.fixture.MysqlFixture;

/**
 * Test case for ThreadDAOmySql
 *
 */
public class ThreadDAOmySqlTest extends TestCase {
private MysqlFixture mysqlFixtureIns = new MysqlFixture();
private static int category = 100;
private static final String USER="ForTest";


private Connection conn = null;

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

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

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

	public void testGetConnection() {
		try {
			ThreadDAOmySql dao = new ThreadDAOmySql();
			Connection conn = dao.getConnection();
			assertNotNull(conn);
		} catch (SQLException sqle) {
			fail("can't get the connection.");
		}

	}

public void testAddThreadNormal(){
    try{
        ThreadDAOmySql dao = new ThreadDAOmySql();
        long current = System.currentTimeMillis();
        PersistentThread thread = new PersistentThread(
                                              "my first",
                                              "hello I can't have it. \"world\",hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0,
                                              1234,
                                              true );
        thread = dao.addThread( thread );
        conn = dao.getConnection();
        final ResultSet resultSet
            = conn.createStatement().executeQuery("select * from threads where id=" + thread.getID());
        if( resultSet.next() ){
            assertEquals("Expecting 100", thread.getID(),resultSet.getLong( PersistentThread.PROPERTY_ID ));
            assertEquals("Expecting my first","my first",resultSet.getString( PersistentThread.PROPERTY_TITLE ));
            assertEquals("Expecting charles","charles",resultSet.getString( PersistentThread.PROPERTY_AUTHOR ));
            assertEquals("expecting time",current,resultSet.getLong( PersistentThread.PROPERTY_TIMESTAMP ));
            assertEquals("expecting -1",-1,resultSet.getLong( PersistentThread.PROPERTY_PARENT_ID ));
            assertEquals("expecting " + category,category,resultSet.getInt( PersistentThread.PROPERTY_CATEGORY ));
            assertEquals("expecting 1234" , 1234, resultSet.getLong( PersistentThread.PROPERTY_REPLIED_THREAD ));
        }
        else{
            fail("Can't find the inserted thread");
        }
        dao.removeThread( thread );
    }catch( final Exception e ){
        e.printStackTrace();
        fail("Unexpected exception::" + e.toString());
    }
}


public void testAddThreadAsReply(){
    try{
        ThreadDAOmySql dao = new ThreadDAOmySql();
        long current = System.currentTimeMillis();
        PersistentThread thread = new PersistentThread(
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              0,
                                              0,
                                              0,
                                              true);
        thread = dao.addThread( thread );
        conn = dao.getConnection();
        final ResultSet resultSet
            = conn.createStatement().executeQuery("select * from threads where id=" + thread.getID());
        if( resultSet.next() ){
            assertEquals("Expecting 100", thread.getID(),resultSet.getLong( PersistentThread.PROPERTY_ID ));
            assertEquals("Expecting my first","my first",resultSet.getString( PersistentThread.PROPERTY_TITLE ));
            assertEquals("Expecting charles","charles",resultSet.getString( PersistentThread.PROPERTY_AUTHOR ));
            assertEquals("expecting time",current,resultSet.getLong( PersistentThread.PROPERTY_TIMESTAMP ));
            assertEquals("expecting -1",-1,resultSet.getLong( PersistentThread.PROPERTY_PARENT_ID ));
            assertEquals("expecting " + category,category,resultSet.getInt( PersistentThread.PROPERTY_CATEGORY ));
        }
        else{
            fail("Can't find the inserted thread");
        }

        PersistentThread reply = new PersistentThread(
		                                              "my first",
		                                              "hello world,hello world",
		                                              "huang",
		                                              current,
		                                              thread.getID(),
		                                              category,
		                                              1,
                                              		  0,
                                              		  thread.getID(), true );
        PersistentThread reply1 = new PersistentThread(
				                                              "my first",
				                                              "hello world,hello world",
				                                              "huang",
				                                              current,
				                                              thread.getID(),
				                                              category,
				                                              1,
		                                              		  0,
		                                              		  thread.getID(),true );

        dao.addThread( reply );
        dao.addThread( reply1 );
        thread = dao.findByUID( thread.getID() );
        assertEquals("Expecint 2" ,2,thread.getReply() );

        dao.removeThread( thread );
        dao.removeThread( reply );
        dao.removeThread( reply1 );
    }catch( final Exception e ){
        e.printStackTrace();
        fail("Unexpected exception::" + e.toString());
    }
}

public void testFindByUIDNormal(){
    try{
        final ThreadDAOmySql dao = new ThreadDAOmySql();
        long current = System.currentTimeMillis();
        final PersistentThread thread = new PersistentThread(100,
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0,0,0,true );
        dao.addThread( thread );
        final PersistentThread thread_1 = dao.findByUID( thread.getID() );
        assertEquals("Expect " + thread.getID(), thread.getID(),thread_1.getID() );
        assertEquals("Expect " + thread.getAuthor(),thread.getAuthor(),thread_1.getAuthor() );
        assertEquals("Expect " + thread.getTimeStamp(),thread.getTimeStamp(),thread_1.getTimeStamp() );
        assertEquals("Expect " + thread.getContent(),thread.getCategory(),thread_1.getCategory() );
        assertEquals("Expect " + thread.getParentID(),thread.getParentID(),thread_1.getParentID() );
        assertEquals("Expect " + thread.getTitle(),thread.getTitle(),thread_1.getTitle() );
        assertEquals("Expect " + thread.getCategory(),thread.getCategory(),thread_1.getCategory() );
        dao.removeThread( thread);
    }catch( final Exception e ){
        e.printStackTrace();
        fail("Unexpected exception:" + e.toString() );
    }
}

public void testFindByUIDNotFound(){
    try{
        final ThreadDAOmySql dao = new ThreadDAOmySql();
        final PersistentThread thread_1 = dao.findByUID( 100 );
        fail("Expect ThreadNotFoundException");
    }catch( final SQLException sqlException ){
        fail("Unexpected excetpion: " + sqlException );
    }catch( final ThreadNotFoundException e ){
        //Pass
    }
}

public void testFindByCategoryNormal(){
    try{
        final ThreadDAOmySql dao = new ThreadDAOmySql();
        long current=1;
        final PersistentThread thread = new PersistentThread(100,
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0,0,0,true );
        dao.addThread( thread );
        final PersistentThread thread1 = new PersistentThread(101,
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0 , 0, 0, true);
        dao.addThread( thread1 );
        final Collection threads = dao.findByCategory( thread.getCategory());
        assertEquals("Expect 2",2,threads.size() );
        dao.removeThread( thread );
        dao.removeThread( thread1);
    }catch( final Exception e ){
        e.printStackTrace();
        fail("Unexpected exception:" + e.toString() );
    }
}

public void testFindByCategoryNotFound(){
    try{
        final ThreadDAOmySql dao = new ThreadDAOmySql();
        final Collection threads = dao.findByCategory(100);
        assertEquals("Expect 0",0 ,threads.size() );
    }catch( final Exception e ){
        fail("Unexpected exception" + e.toString() );
    }
}


public void testFindCountByCategoryNormal(){
    try{
        final ThreadDAOmySql dao = new ThreadDAOmySql();
        long current=1;
        final PersistentThread thread = new PersistentThread(100,
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0 , 0, 0, true);
        dao.addThread( thread );
        final PersistentThread thread1 = new PersistentThread(101,
                                              "my first",
                                              "hello world,hello world",
                                              "charles",
                                              current,
                                              -1,
                                              category,
                                              1,
                                              0, 0, 0,true );
        dao.addThread( thread1 );
        assertEquals("Expecting 2",2,dao.findCountByCategory( category ) );
        dao.removeThread( thread );
        dao.removeThread( thread1 );
    }catch( final Exception e ){
        fail("Unexpected exception" + e.toString() );
    }
}

	public void testFindByParentID() throws Exception {
		final int THREADS = 5;
        ThreadDAOmySql dao = new ThreadDAOmySql();

		// if parent id is not valid, throw exception
		try {
			dao.findByParnetID(1000);
			fail("ParentID not exists!");
		} catch(ThreadNotFoundException e) {
		}

		// generate 5 threads for one topic
		final long current = System.currentTimeMillis();
		final int parentId = 100;
		PersistentThread pts[] = new PersistentThread[THREADS];
		for(int i=0; i<THREADS; i++) {
			pts[i] = new PersistentThread(parentId+i,  // id, pk
										   "testFindByParentID", // subject
										   "content"+i,  // content
										   "userid"+i, // userid
										   current+i, // created time (long)
										   (i==0)?-1:parentId , // parent id
										   1, // category
										   current, // updated
											0, 0, 0, true); // reply

		}

		try {
			long realParentId = 0;
			dao.addThread(pts[0]);
			realParentId = pts[0].getID();

			// insert test thread first
			for(int i=1; i<THREADS; i++) {
				pts[i].setParentID(realParentId);
				dao.addThread(pts[i]);

			}

			try {
				dao.findByParnetID(pts[1].getID());
				fail("child message found");
			} catch (ThreadNotFoundException tnfe) {
			}

			// find threads with parent id = 0
			Collection threads = dao.findByParnetID(realParentId);

			assertEquals("threads size", THREADS, threads.size());

			// test the data
			Iterator itr = threads.iterator();
			int cnt = 0;
			while(itr.hasNext()) {
				Object obj = itr.next();
				PersistentThread pt = (PersistentThread)obj;
				assertEquals("PersistentThread", pts[cnt], pt);
				cnt ++;
			}
		} finally {

⌨️ 快捷键说明

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