001: package pygmy.nntp.test;
002:
003: import junit.framework.TestCase;
004: import junit.framework.Test;
005: import junit.framework.TestSuite;
006: import pygmy.nntp.Forum;
007: import pygmy.nntp.NewsGroup;
008: import pygmy.nntp.Article;
009: import pygmy.nntp.NntpUtil;
010:
011: import java.io.File;
012: import java.util.Iterator;
013: import java.util.Date;
014: import java.util.List;
015:
016: public class ForumTest extends TestCase {
017:
018: Forum forum;
019:
020: protected void setUp() throws Exception {
021: forum = NntpTestUtil.createTestForum();
022: }
023:
024: protected void tearDown() throws Exception {
025: File[] groups = forum.getNewsRepository().listFiles();
026: for (int i = 0; i < groups.length; i++) {
027: if (groups[i].isDirectory()) {
028: NntpTestUtil.deleteTree(groups[i]);
029: }
030: }
031: }
032:
033: public void testCreateNewsgroup() throws Exception {
034: String name = "comp.lang.java";
035: NewsGroup group = forum.createNewsgroup(name);
036: assertEquals(name, group.getName());
037:
038: NewsGroup sameGroup = forum.getNewsgroup(group.getName());
039: assertEquals(group.getName(), sameGroup.getName());
040: assertEquals(group.getFirstIndex(), sameGroup.getFirstIndex());
041: assertEquals(group.getLastIndex(), sameGroup.getLastIndex());
042: assertEquals(group.size(), sameGroup.size());
043: }
044:
045: public void testAddArticle() throws Exception {
046: String name = "comp.lang.java.programmer";
047: NewsGroup group = forum.createNewsgroup(name);
048: Article article = NntpTestUtil.createArticle("test.eml");
049: forum.addArticle(article, "foo");
050: File repository = forum.getArticleRepository();
051:
052: File[] list = repository.listFiles();
053: assertEquals(NntpUtil.base64Encode(article.getMessageId()),
054: list[0].getName());
055: assertEquals(1, group.size());
056: }
057:
058: public void testGroupIterator() throws Exception {
059: String[] groups = createSomeNewsGroups();
060:
061: for (Iterator it = forum.newsgroupIterator(); it.hasNext();) {
062: Object next = it.next();
063: assertTrue("Assert we get newsgroup instances.",
064: next instanceof NewsGroup);
065: assertNewsGroupContained(groups, (NewsGroup) next);
066: }
067: }
068:
069: public void testGetMessageSince() {
070: String groups[] = createSomeNewsGroups();
071:
072: NewsGroup group = forum.getNewsgroup(groups[0]);
073: group.getDirectory().setLastModified(1L);
074:
075: List list = forum.getNewsgroups(new Date(0), null);
076: assertEquals("Assert we get all the groups", groups.length,
077: list.size());
078:
079: list = forum.getNewsgroups(new Date(2), null);
080: assertEquals("Assert we get all the groups but the first",
081: groups.length - 1, list.size());
082: for (int it = 0; it < list.size(); it++) {
083: NewsGroup newsgroup = (NewsGroup) list.get(it);
084: assertNewsGroupContained(groups, newsgroup);
085: }
086:
087: list = forum.getNewsgroups(new Date(2),
088: new String[] { "comp." });
089: assertEquals("Assert we get only comp.lang.ada", 1, list.size());
090: assertEquals("Assert we get only comp.lang.ada",
091: "comp.lang.ada", ((NewsGroup) list.get(0)).getName());
092: }
093:
094: private void assertNewsGroupContained(String[] groups,
095: NewsGroup newsgroup) {
096: boolean found = false;
097: for (int i = 0; i < groups.length; i++) {
098: if (groups[i].equals(newsgroup.getName())) {
099: found = true;
100: }
101: }
102: assertTrue("Assert newsgroup " + newsgroup.getName()
103: + " is in the list.", found);
104: }
105:
106: private String[] createSomeNewsGroups() {
107: String[] groups = { "comp.lang.java", "comp.lang.ada",
108: "alt.blah.blah", "alt.hack" };
109: for (int i = 0; i < groups.length; i++) {
110: forum.createNewsgroup(groups[i]);
111: }
112: return groups;
113: }
114:
115: public static Test suite() {
116: return new TestSuite(ForumTest.class);
117: }
118:
119: public static void main(String[] args) {
120: junit.textui.TestRunner.run(suite());
121: }
122: }
|