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.NntpRequest;
007: import pygmy.nntp.NoCurrentNewsgroupException;
008: import pygmy.nntp.NoCurrentArticleException;
009:
010: import java.util.Properties;
011: import java.io.ByteArrayInputStream;
012: import java.io.IOException;
013:
014: import pygmy.core.Http;
015:
016: public class NntpRequestTest extends TestCase {
017:
018: protected void setUp() throws Exception {
019: super .setUp();
020: }
021:
022: protected void tearDown() throws Exception {
023: super .tearDown();
024: }
025:
026: public void testNextCommand() throws IOException {
027: StringBuffer buffer = new StringBuffer();
028: buffer.append("list");
029: buffer.append(Http.CRLF);
030: buffer.append("article <foo>");
031: buffer.append(Http.CRLF);
032: buffer.append("foo bar baz");
033: buffer.append(Http.CRLF);
034: NntpRequest request = createNntpRequest(buffer);
035: request.nextCommand();
036: assertEquals("list", request.getCommand());
037: assertEquals(0, request.parameterLength());
038: assertNull(request.getParameter(0));
039:
040: request.nextCommand();
041: assertEquals("article", request.getCommand());
042: assertEquals(1, request.parameterLength());
043: assertEquals("<foo>", request.getParameter(0));
044:
045: request.nextCommand();
046: assertEquals("foo", request.getCommand());
047: assertEquals(2, request.parameterLength());
048: assertEquals("bar", request.getParameter(0));
049: assertEquals("baz", request.getParameter(1));
050: }
051:
052: public void testIsDone() throws IOException {
053: StringBuffer buffer = new StringBuffer();
054: buffer.append("quit");
055: buffer.append(Http.CRLF);
056: NntpRequest request = createNntpRequest(buffer);
057: request.nextCommand();
058: assertEquals("quit", request.getCommand());
059: assertTrue(request.isDone());
060: }
061:
062: public void testGetCurrentNewsgroup() throws IOException,
063: NoCurrentNewsgroupException, NoCurrentArticleException {
064: StringBuffer buffer = new StringBuffer();
065: buffer.append("list");
066: buffer.append(Http.CRLF);
067: NntpRequest request = createNntpRequest(buffer);
068:
069: try {
070: request.getCurrentNewsgroup();
071: fail("Assert NntpRequest throws an exception when there is no current group.");
072: } catch (NoCurrentNewsgroupException e) {
073: assertTrue(true);
074: }
075:
076: try {
077: request.getCurrentArticle();
078: fail("Assert NntpRequest throws an exception when there is no current article.");
079: } catch (NoCurrentArticleException e) {
080: assertTrue(true);
081: }
082:
083: String newsgroup = "comp.lang.java";
084: request.setCurrentNewsgroup(newsgroup);
085: assertEquals("Assert current news group is " + newsgroup,
086: newsgroup, request.getCurrentNewsgroup());
087:
088: String articlePointer = "1";
089: request.setCurrentArticle(articlePointer);
090: assertEquals("Assert current article pointer is "
091: + articlePointer, articlePointer, request
092: .getCurrentArticle());
093: }
094:
095: private NntpRequest createNntpRequest(StringBuffer buffer)
096: throws IOException {
097: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
098: .toString().getBytes());
099: NntpRequest request = new NntpRequest(null, new Properties(),
100: bais);
101: return request;
102: }
103:
104: public static Test suite() {
105: return new TestSuite(NntpRequestTest.class);
106: }
107:
108: public static void main(String[] args) {
109: junit.textui.TestRunner.run(suite());
110: }
111: }
|