📄 threaddaomysqltest.java
字号:
} catch (final ThreadNotFoundException threadNotFoundException) { //Pass } } public void testUpdateThreadNormal() throws Exception { ThreadDAOmySql dao = new ThreadDAOmySql(); org.redsoft.forum.dao.Thread pt = dao.findByUID(1); // 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(1); assertEquals("Update Thread Title", oldTitle + "AddedTitle.", pt.getTitle()); assertEquals("Update Thread Content", oldContent + "AddedContent.", pt.getContent()); assertEquals("Update thread Notify", !oldNotify, pt.isNotify() ); // back to original value // change values pt.setTitle(oldTitle); pt.setContent(oldContent); pt.setNotify(oldNotify); // update dao.updateThread(pt); } public void testUpdateClick() throws Exception { ThreadDAOmySql dao = new ThreadDAOmySql(); PersistentThread pt = dao.findByUID(1); int oldClick = pt.getClick(); dao.updateClick(pt.getId()); // query again pt = dao.findByUID(1); assertEquals("New click count", pt.getClick(), oldClick + 1); } public void testFindByUserId() { org.redsoft.forum.dao.Thread newThread = new org.redsoft.forum.dao.Thread(9990999l, "my test title", "Hello, world!", USER, System.currentTimeMillis(), -1, 0, category, 0, 0, 0, true); org.redsoft.forum.dao.Thread anotherThread = new org.redsoft.forum.dao.Thread(9999099l, "my another", "hi", USER, System.currentTimeMillis(), -1, 0, category, 0, 0, 0, true); final int numberOfThreads = 2; final String testUserId = USER; try { // initially, I add 2 threads created by myself:john liu to database DAOFactory.getInstance().getThreadDAO().addThread(newThread); DAOFactory.getInstance().getThreadDAO().addThread(anotherThread); // test the method here Collection listOfTestThreads = DAOFactory.getInstance().getThreadDAO().findByUserId(testUserId, 1, 3); assertEquals("Expecting 3", numberOfThreads, listOfTestThreads.size()); for (Iterator itOfThread = listOfTestThreads.iterator(); itOfThread.hasNext();) { // get the PersistentThread Objects returned by findByUserID() Thread testThread = (Thread) itOfThread.next(); assertEquals("Expecting JUnit Tester For findByUserId", testUserId, testThread.getAuthor()); } DAOFactory.getInstance().getThreadDAO().removeThread(newThread); DAOFactory.getInstance().getThreadDAO().removeThread(anotherThread); } catch (final ClassCastException cce) { cce.printStackTrace(); fail("The list returned from FindByUserId(String) is not a list of PersistentThread object"); } catch (final Exception e) { e.printStackTrace(); fail("Unexpected exception::" + e.getMessage()); } } public void testRemoveThreadByID() { 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, 0, true); thread = dao.addThread(thread); dao.removeThread(thread.getId()); dao.findByUID(thread.getId()); fail("Expecting ThreadNotFoundException"); } catch (final ThreadNotFoundException ex) { //pass } catch (final Exception e) { e.printStackTrace(); fail("Unexpected exception::" + e.toString()); } } public void testRemoveThreadNotExistID() { try { ThreadDAOmySql dao = new ThreadDAOmySql(); dao.removeThread(1000); } catch (final Exception e) { e.printStackTrace(); fail("Unexpected exception::" + e.toString()); } } public void testRemoveChildrenThread() { 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, 0, 0, 0, true); thread = dao.addThread(thread); org.redsoft.forum.dao.Thread thread_1 = new org.redsoft.forum.dao.Thread("my first", "hello I can't have it. \"world\",hello world", "charles", current, thread.getId(), category, 0, 0, 0, true); org.redsoft.forum.dao.Thread thread_2 = new org.redsoft.forum.dao.Thread("my second", "hello I can't have it. \"world\",hello world", "charles", current, thread.getId(), category, 0, 0, 0, true); thread_1 = dao.addThread(thread_1); thread_2 = dao.addThread(thread_2); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 2", 2, thread.getReply()); dao.removeThread(thread_1.getId()); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 1", 1, thread.getReply()); dao.removeThread(thread_2.getId()); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 0", 0, thread.getReply()); dao.removeThread(thread.getId()); } catch (final Exception e) { e.printStackTrace(); fail("Unexpected exception::" + e.toString()); } } public void testRemoveChildrenThreadByID() { 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, 0, 0, 0, true); thread = dao.addThread(thread); org.redsoft.forum.dao.Thread thread_1 = new org.redsoft.forum.dao.Thread("my first", "hello I can't have it. \"world\",hello world", "charles", current, thread.getId(), category, 0, 0, 0, true); org.redsoft.forum.dao.Thread thread_2 = new org.redsoft.forum.dao.Thread("my second", "hello I can't have it. \"world\",hello world", "charles", current, thread.getId(), category, 0, 0, 0, true); thread_1 = dao.addThread(thread_1); thread_2 = dao.addThread(thread_2); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 2", 2, thread.getReply()); dao.removeThread(thread_1); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 1", 1, thread.getReply()); dao.removeThread(thread_2); thread = dao.findByUID(thread.getId()); assertEquals("Expecting 0", 0, thread.getReply()); dao.removeThread(thread); } catch (final Exception e) { e.printStackTrace(); fail("Unexpected exception::" + e.toString()); } } public void testFindThreadsPostedLaterThan() { ThreadDAOmySql dao = new ThreadDAOmySql(); org.redsoft.forum.dao.Thread thread_1 = new org.redsoft.forum.dao.Thread("first", "hello I can't have it. \"world\",hello world", "cinc", System.currentTimeMillis(), -1, category, 0, 0, 0, true); org.redsoft.forum.dao.Thread thread_2 = new org.redsoft.forum.dao.Thread("re:first", "oh u can have it. \"world\",hello world", "cinc", System.currentTimeMillis(), thread_1.getId(), category, 0, 0, 0, true); org.redsoft.forum.dao.Thread thread_3 = new org.redsoft.forum.dao.Thread("second", "oh my god", "cinc", System.currentTimeMillis(), -1, category, 0, 0, 0, true); org.redsoft.forum.dao.Thread thread_4 = new org.redsoft.forum.dao.Thread("re:second", "oh your god", "cinc", System.currentTimeMillis(), thread_3.getId(), category, 0, 0, 0, true); try { // add thread_1 before testTime thread_1.setTimeStamp(System.currentTimeMillis()); thread_1 = dao.addThread(thread_1); // testTime // wait for a while try { java.lang.Thread.sleep(500); } catch (Exception e) { } long testTime = System.currentTimeMillis(); //System.out.println ("\nTest time: " + testTime); // wait for a while try { java.lang.Thread.sleep(500); } catch (Exception e) { } // add thread_2 : reply of thread_1 thread_2.setTimeStamp(System.currentTimeMillis()); thread_2 = dao.addThread(thread_2); // add thread_3 thread_3.setTimeStamp(System.currentTimeMillis()); thread_3 = dao.addThread(thread_3); // add thread_4 : reply of thread_3 thread_4.setTimeStamp(System.currentTimeMillis()); thread_4 = dao.addThread(thread_4); Collection list = dao.findThreadsPostedLaterThan(testTime); assertEquals("Expecting 3", 3, list.size()); // remove threads } catch (final DAOException daoException) { daoException.printStackTrace(); fail("Unexpected exeption:" + daoException.toString()); } finally { try { dao.removeThread(thread_4); dao.removeThread(thread_3); dao.removeThread(thread_2); dao.removeThread(thread_1); } catch (DAOException e) { } } } public void testUpdateColumnThread() { ThreadDAOmySql dao = new ThreadDAOmySql(); org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread( "my first", "hello world,hello world", "charles", System.currentTimeMillis(), -1, category, 0, 0, 0, true); try { dao.addThread(thread); thread.setColumnThread(true); dao.addColumnThread(thread.getId()); PersistentThread thread_AddedToColumn = dao.findByUID(thread.getId()); assertEquals("should be true", true, thread_AddedToColumn.isColumnThread()); thread_AddedToColumn.setColumnThread(false); dao.removeColumnThread(thread_AddedToColumn.getId()); PersistentThread thread_RemovedFromColumn = dao.findByUID(thread.getId()); assertEquals("should be false", false, thread_RemovedFromColumn.isColumnThread()); } catch (final DAOException daoException) { daoException.printStackTrace(); fail("Unexpected exeption:" + daoException.toString()); } catch (ThreadNotFoundException e) { fail("Unexpected exeption:" + e.toString()); } finally { try { dao.removeThread(thread); } catch (DAOException daoe) { } } } public void testFindColumnThreadsByAccountId() { ThreadDAOmySql dao = new ThreadDAOmySql(); try { Collection results = dao.findColumnThreadsByAccountId("testuser"); assertNotNull("results", results); } catch (DAOException daoe) { fail("DB error" + daoe.getMessage()); } } public void testFindCountByUserId() { ThreadDAOmySql dao = new ThreadDAOmySql(); try { assertTrue("list size should not be zero", dao.findCountByUserId("testuser") != 0); } catch (DAOException daoe) { fail("DB error" + daoe.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -