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

📄 threaddaomysqltest.java

📁 一个功能较为完善的论坛
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			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 PersistentThread pt = null;
		dao.updateThread(pt);
		fail ("Except NULL Object");
    }catch( final SQLException sqlException ){
        fail("Unexpected excetpion: " + sqlException );
    }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 PersistentThread pt = dao.findByUID( 1 );
		pt.setID( 100 );
		dao.updateThread(pt);
        fail("Expect ThreadNotFoundException");
    }catch( final SQLException sqlException ){
        fail("Unexpected excetpion: " + sqlException );
    }catch( final ThreadNotFoundException threadNotFoundException ){
        //Pass
    }
		
}

public void testUpdateThreadNormal() throws Exception {
	ThreadDAOmySql dao = new ThreadDAOmySql();
	PersistentThread pt = dao.findByUID(1);
	// store old values
	String oldTitle = pt.getTitle();
	String oldContent = pt.getContent();
	boolean oldNotify = pt.getNotify();
	// 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.getNotify());
	
	// 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() {
    PersistentThread newThread = new PersistentThread(9990999l,
                                                            "my test title",
                                                            "Hello, world!",
                                                            USER,
                                                            System.currentTimeMillis(),
                                                            -1,
                                                            0,
                                                            category,
                                                            0,0,0, true);
    PersistentThread anotherThread = new PersistentThread(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);
            assertEquals("Expecting 2", 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();
        PersistentThread thread = new PersistentThread(
                                              "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());
	    }
	}

	/**
	 * Test removeThread( PersistenThread thread );
	 */
	public void testRemoveChildrenThread(){
		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,
			                                              0,
			                                              0, 0,true );
			thread = dao.addThread( thread );
			PersistentThread thread_1 = new PersistentThread( "my first",
			                               "hello I can't have it. \"world\",hello world",
			                               "charles",
			                               current,
			                               thread.getID(),
			                               category,
			                               0,
			                               0, 0,true );

			PersistentThread thread_2 = new PersistentThread( "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());
	    }
	}


	/**
	 * Test removeThread( long id );
	 */
	public void testRemoveChildrenThreadByID(){
			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,
				                                              0,
				                                              0, 0,true );
				thread = dao.addThread( thread );
				PersistentThread thread_1 = new PersistentThread( "my first",
				                               "hello I can't have it. \"world\",hello world",
				                               "charles",
				                               current,
				                               thread.getID(),
				                               category,
				                               0,
				                               0, 0,true );

				PersistentThread thread_2 = new PersistentThread( "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(){
    try{
		ThreadDAOmySql dao = new ThreadDAOmySql();
        // add thread_1 before testTime
		PersistentThread thread_1 = new PersistentThread("first",
				                                         "hello I can't have it. \"world\",hello world",
		                                                 "cinc",
				                                         System.currentTimeMillis(),
				                                         -1,
				                                         category,
				                                         0,
				                                         0, 0,true );
    	thread_1 = dao.addThread( thread_1 );
        // testTime
        long testTime = System.currentTimeMillis();
        //System.out.println ("Test time: " + testTime);
        //try{ java.lang.Thread.sleep(500); }catch (Exception e){}
        // add thread_2 : reply of thread_1
		PersistentThread thread_2 = new PersistentThread("re:first",
				                                         "oh u can have it. \"world\",hello world",
		                                                 "cinc",
				                                         System.currentTimeMillis(),
				                                         thread_1.getID(),
				                                         category,
				                                         0,
				                                         0, 0,true );
    	thread_2 = dao.addThread( thread_2 );
        // add thread_3
		PersistentThread thread_3 = new PersistentThread("second",
				                                         "oh my god",
		                                                 "cinc",
				                                         System.currentTimeMillis(),
				                                         -1,
				                                         category,
				                                         0,
				                                         0, 0,true );
    	thread_3 = dao.addThread( thread_3 );
        // add thread_4 : reply of thread_3
		PersistentThread thread_4 = new PersistentThread("re:second",
				                                         "oh your god",
		                                                 "cinc",
				                                         System.currentTimeMillis(),
				                                         thread_3.getID(),
				                                         category,
				                                         0,
				                                         0, 0,true );
    	thread_4 = dao.addThread( thread_4 );

        Collection list = dao.findThreadsPostedLaterThan( testTime );
        assertEquals("Expecting 3", 3, list.size());
        // remove threads
        dao.removeThread( thread_4 );
        dao.removeThread( thread_3 );
        dao.removeThread( thread_2 );
        dao.removeThread( thread_1 );
	}catch( final SQLException sql){
		sql.printStackTrace();
		fail("Unexpected exeption:" + sql.toString() );
	}
}

}//EOC

⌨️ 快捷键说明

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