001: package pygmy.nntp.test;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestSuite;
005: import junit.framework.Test;
006: import pygmy.nntp.NewsGroup;
007: import pygmy.nntp.Article;
008:
009: import java.io.IOException;
010:
011: public class NewsGroupTest extends TestCase {
012:
013: NewsGroup group;
014: String groupName = "comp.lang.java";
015:
016: protected void setUp() throws Exception {
017: System.out.println("NewsGroupTest.setUp");
018: group = NntpTestUtil.createNewsGroup(groupName);
019: }
020:
021: protected void tearDown() throws Exception {
022: System.out.println("NewsGroupTest.tearDown");
023: NntpTestUtil.deleteTree(group.getDirectory());
024: }
025:
026: public void testConstruction() throws Exception {
027: System.out.println("NewsGroupTest.testConstruction");
028: assertEquals(
029: "Assert that the newsgroup name is what we created it with.",
030: groupName, group.getName());
031: assertTrue(
032: "Assert that an empty group's firstIndex is greater than the lastIndex",
033: group.getFirstIndex() > group.getLastIndex());
034: assertEquals("Assert that the size of the newsgroup is zero.",
035: 0, group.size());
036: }
037:
038: public void testAddArticle() throws Exception {
039: System.out.println("NewsGroupTest.testAddArticle");
040: assertEquals(0, group.size());
041:
042: Article article = addTestArticle();
043: assertEquals(1, group.size());
044: assertEquals(group.getFirstIndex(), group.getLastIndex());
045: assertEquals(group.getLastIndex(), article.getArticleNumber());
046:
047: Article article2 = NntpTestUtil.createArticle("test.eml");
048: article2.setMessageId(null);
049: String oldPath2 = article2.getHeader().get("Path");
050: group.addArticle(article2, "localhost");
051: assertEquals(2, group.size());
052: assertTrue(
053: "Assert that first and last indexes are different after adding two articles.",
054: group.getFirstIndex() != group.getLastIndex());
055: assertEquals("Assert the first article is the firstIndex",
056: group.getFirstIndex(), article.getArticleNumber());
057: assertEquals("Assert the last article is the last index", group
058: .getLastIndex(), article2.getArticleNumber());
059: assertTrue(
060: "Assert the old path is contained with the new path, and it doesn't start at index 0.",
061: article2.getHeader().get("Path").indexOf(oldPath2) > 0);
062: assertTrue("Assert the old path starts with localhost.",
063: article2.getHeader().get("Path")
064: .startsWith("localhost"));
065: assertNotNull("Assert messsage ID is not NULL.", article2
066: .getMessageId());
067: assertNotNull("Assert Date-Received is not NULL", article2
068: .getHeader().get("Date-Received"));
069: }
070:
071: private Article addTestArticle() throws IOException {
072: Article article = NntpTestUtil.createArticle("test.eml");
073: group.addArticle(article, "localhost");
074: return article;
075: }
076:
077: public void testGetMessage() throws Exception {
078: System.out.println("NewsGroupTest.testGetMessage");
079:
080: Article article = addTestArticle();
081: Article sameArticle = group.getMessage(article
082: .getArticleNumber());
083:
084: assertEquals(
085: "Assert that their message IDs are the same. Assume the rest is the same.",
086: sameArticle.getMessageId(), article.getMessageId());
087: assertEquals(
088: "Assert that their article numbers are the same. Assume the rest is the same.",
089: sameArticle.getArticleNumber(), article
090: .getArticleNumber());
091: }
092:
093: public static Test suite() {
094: return new TestSuite(NewsGroupTest.class);
095: }
096:
097: public static void main(String[] args) {
098: junit.textui.TestRunner.run(suite());
099: }
100: }
|