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

📄 threaddaomysqltest.java

📁 Chinaxp 论坛源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.redsoft.forum.dao.mysql;import junit.framework.TestCase;import org.redsoft.forum.dao.DAOFactory;import org.redsoft.forum.dao.PersistentThread;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.exception.ThreadNotFoundException;import org.redsoft.forum.fixture.MysqlFixture;import org.redsoft.forum.dao.Thread;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Collection;import java.util.Iterator;/** * 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();            org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(                    "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();            org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(                    "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");            }            org.redsoft.forum.dao.Thread reply = new org.redsoft.forum.dao.Thread(                    "my first",                    "hello world,hello world",                    "huang",                    current,                    thread.getId(),                    category,                    1,                    0,                    thread.getId(), true);            org.redsoft.forum.dao.Thread reply1 = new org.redsoft.forum.dao.Thread(                    "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 org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(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.getParent_id(), thread.getParent_id(), thread_1.getParent_id());            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();            dao.findByUID(100);            fail("Expect ThreadNotFoundException");        } catch (final DAOException DAOException) {            fail("Unexpected excetpion: " + DAOException);        } catch (final ThreadNotFoundException e) {            //Pass        }    }    public void testFindByCategoryNormal() {        try {            final ThreadDAOmySql dao = new ThreadDAOmySql();            long current = 1;            final org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(100,                    "my first",                    "hello world,hello world",                    "charles",                    current,                    -1,                    category,                    1,                    0, 0, 0, true);            dao.addThread(thread);            final org.redsoft.forum.dao.Thread thread1 = new org.redsoft.forum.dao.Thread(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 org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(100,                    "my first",                    "hello world,hello world",                    "charles",                    current,                    -1,                    category,                    1,                    0, 0, 0, true);            dao.addThread(thread);            final org.redsoft.forum.dao.Thread thread1 = new org.redsoft.forum.dao.Thread(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;        org.redsoft.forum.dao.Thread pts[] = new org.redsoft.forum.dao.Thread[THREADS];        for (int i = 0; i < THREADS; i++) {            pts[i] = new org.redsoft.forum.dao.Thread(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].setParent_id(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 {            for (int i = 0; i < THREADS; i++) {                try {                    dao.removeThread(pts[i]);                } catch (Exception e) {                }            }        }    }    public void testUpdateThreadNULLObject() {        try {            final ThreadDAOmySql dao = new ThreadDAOmySql();            final org.redsoft.forum.dao.Thread pt = null;            dao.updateThread(pt);            fail("Except NULL Object");        } catch (final DAOException DAOException) {            fail("Unexpected excetpion: " + DAOException);        } catch (final ThreadNotFoundException threadNotFoundException) {            fail("Unexpected excetpion: " + threadNotFoundException);        } catch (final IllegalArgumentException IllegalArgumentException) {            //pass        }    }    public void testUpdateThreadNotFound() throws Exception {        try {            final ThreadDAOmySql dao = new ThreadDAOmySql();            final org.redsoft.forum.dao.Thread pt = dao.findByUID(1);            pt.setId(100);            dao.updateThread(pt);            fail("Expect ThreadNotFoundException");        } catch (final DAOException DAOException) {            fail("Unexpected excetpion: " + DAOException);

⌨️ 快捷键说明

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