Search in sources :

Example 1 with Forum

use of cn.edu.zju.acm.onlinejudge.bean.Forum in project zoj by licheng.

the class ForumPersistenceImpl method getAllForums.

/**
     * <p>
     * Get all forums in persistence layer.
     * </p>
     * 
     * @return a list of Forum instances containing all forums in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<Forum> getAllForums() throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_ALL_FORUMS);
            ResultSet rs = ps.executeQuery();
            List<Forum> forums = new ArrayList<Forum>();
            while (rs.next()) {
                Forum forum = new Forum();
                forum.setId(rs.getLong(DatabaseConstants.FORUM_FORUM_ID));
                forum.setName(rs.getString(DatabaseConstants.FORUM_NAME));
                forum.setDescription(rs.getString(DatabaseConstants.FORUM_DESCRIPTION));
                forums.add(forum);
            }
            return forums;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get all forums", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 2 with Forum

use of cn.edu.zju.acm.onlinejudge.bean.Forum in project zoj by licheng.

the class AuthorizationPersistenceImplTest method testCreateForum1.

/**
	 * Tests createForum method
	 * @throws Exception to JUnit
	 */
public void testCreateForum1() throws Exception {
    Forum forum = new Forum();
    forum.setName("name");
    forum.setDescription("desc");
    persistence.createForum(forum, 1);
    Forum forum1 = persistence.getForum(forum.getId());
    checkForum(forum, forum1);
}
Also used : Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 3 with Forum

use of cn.edu.zju.acm.onlinejudge.bean.Forum in project zoj by licheng.

the class ContestPersistenceImplTest method newForum.

/**
	 * Creates a new forum.
	 * @param id the id
	 * @return a new forum instance
	 */
private Forum newForum(long id) {
    Forum forum = new Forum();
    forum.setId(id);
    forum.setName("forum" + id);
    forum.setDescription("forum" + id + " description");
    return forum;
}
Also used : Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 4 with Forum

use of cn.edu.zju.acm.onlinejudge.bean.Forum in project zoj by licheng.

the class AuthorizationPersistenceImplTest method testGetAllForums.

/**
	 * Tests getAllForums method
	 * @throws Exception to JUnit
	 */
public void testGetAllForums() throws Exception {
    List forums = persistence.getAllForums();
    assertEquals("size is wrong", 3, forums.size());
    Set nameSet = new HashSet(Arrays.asList(new String[] { "forum1", "forum2", "forum3" }));
    Set descSet = new HashSet(Arrays.asList(new String[] { "forum1 description", "forum2 description", "forum3 description" }));
    for (Iterator it = forums.iterator(); it.hasNext(); ) {
        Forum forum = (Forum) it.next();
        assertTrue("wrong name", nameSet.contains(forum.getName()));
        assertTrue("wrong description", descSet.contains(forum.getDescription()));
        nameSet.remove(forum.getName());
        descSet.remove(forum.getDescription());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 5 with Forum

use of cn.edu.zju.acm.onlinejudge.bean.Forum in project zoj by licheng.

the class AuthorizationPersistenceImplTest method testUpdateForum2.

/**
	 * Tests updateForum method
	 * @throws Exception to JUnit
	 */
public void testUpdateForum2() throws Exception {
    try {
        Forum forum = new Forum();
        forum.setName("foo");
        forum.setDescription("bar");
        persistence.updateForum(forum, 1);
        fail("PersistenceException should be thrown");
    } catch (PersistenceException pe) {
    // ok
    }
}
Also used : PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Aggregations

Forum (cn.edu.zju.acm.onlinejudge.bean.Forum)19 ArrayList (java.util.ArrayList)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2