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

📄 threaddaojdotest.java

📁 Chinaxp 论坛源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2003 by Redsoft Factory Inc., * Apt 738, 68 Corporate Drive, Toronto, Ontario, Canada * All rights reserved. * * This software is the confidential and proprietary information * of Redsoft Factory Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Redsoft Factory. */package org.redsoft.forum.dao.jdo;import junit.framework.TestCase;import org.redsoft.forum.dao.PersistentThread;import org.redsoft.forum.dao.mysql.ThreadDAOmySql;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.exception.ThreadNotFoundException;import java.sql.ResultSet;import java.sql.Connection;import java.util.Collection;import java.util.Iterator;/** * * @author Charles Huang * Date: 23-Feb-2004 * $Id: ThreadDAOJDOTest.java,v 1.5 2004/04/10 19:57:01 cinc Exp $ */public class ThreadDAOJDOTest extends TestCase {    Connection conn;    private static int category = 100;    private static final String USER = "ForTest";    protected void setUp() throws Exception {        ThreadDAOmySql dao = new ThreadDAOmySql();        conn = dao.getConnection();    }    protected void tearDown() throws Exception {        conn.close();    }    public void testAddThreadNormal() {        try {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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);            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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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);            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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            dao.findByUID(100);            fail("Expect ThreadNotFoundException");        } catch (final DAOException DAOException) {            fail("Unexpected excetpion: " + DAOException);        } catch (final ThreadNotFoundException e) {            //Pass        }    }    public void testFindByCategoryNormal() {        try {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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;        ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();        // 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]);            }            // 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();                org.redsoft.forum.dao.Thread pt = (org.redsoft.forum.dao.Thread) obj;                assertEquals("PersistentThread", pts[cnt].getId(), pt.getId() );                cnt++;            }        } finally {            for (int i = 0; i < THREADS; i++) {                try {                    dao.removeThread(pts[i]);                } catch (Exception e) {                }            }        }    }    public void testUpdateThreadNULLObject() {        try {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            final org.redsoft.forum.dao.Thread pt = null;            dao.updateThread(null);            fail("Expect Exception");        } 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 {            ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();            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);        } catch (final ThreadNotFoundException threadNotFoundException) {            //Pass        }    }    public void testUpdateThreadNormal() throws Exception {        ThreadDAOJDO dao = (ThreadDAOJDO) new LiberatorDAOFactory().getThreadDAO();        long current = System.currentTimeMillis();        org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread(                "for testUpdateThreadNormal",                "hello hello, haha",                "charles",                current,                -1,                category,                1,                0,                1234,                true);        dao.addThread(thread);        org.redsoft.forum.dao.Thread pt = dao.findByUID( thread.getId() );        // store old values        String oldTitle = pt.getTitle();        String oldContent = pt.getContent();        boolean oldNotify = pt.isNotify();        // change values        pt.setTitle(oldTitle + "AddedTitle.");        pt.setContent(oldContent + "AddedContent.");        pt.setNotify(!oldNotify);        // update        dao.updateThread(pt);        // query again        pt = dao.findByUID( thread.getId() );

⌨️ 快捷键说明

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