01: package pygmy.nntp.test;
02:
03: import junit.framework.TestCase;
04: import junit.framework.Test;
05: import junit.framework.TestSuite;
06: import pygmy.core.Http;
07: import pygmy.nntp.NntpRequest;
08: import pygmy.nntp.NntpResponse;
09: import pygmy.nntp.GroupHandler;
10:
11: import java.util.Properties;
12: import java.io.ByteArrayInputStream;
13: import java.io.ByteArrayOutputStream;
14:
15: public class GroupHandlerTest extends NntpHandlerTestCase {
16:
17: public void testHandleNntp() throws Exception {
18: StringBuffer buffer = new StringBuffer();
19: buffer.append("list");
20: buffer.append(Http.CRLF);
21: buffer.append("list");
22: buffer.append(Http.CRLF);
23: buffer.append("group comp.lang.java");
24: buffer.append(Http.CRLF);
25: buffer.append("group foo.bar.baz");
26: buffer.append(Http.CRLF);
27: NntpRequest request = new NntpRequest(null, new Properties(),
28: new ByteArrayInputStream(buffer.toString().getBytes()));
29: ByteArrayOutputStream baos = new ByteArrayOutputStream();
30: NntpResponse response = new NntpResponse(baos);
31:
32: request.nextCommand();
33: GroupHandler handler = new GroupHandler(forum);
34: assertTrue("Assert that the handler handled the request.",
35: handler.handleNntp(request, response));
36: assertTrue(baos.toString().indexOf(
37: "215 list of newsgroups follows" + Http.CRLF + ".") >= 0);
38:
39: forum.createNewsgroup("comp.lang.java");
40: forum.createNewsgroup("comp.lang.ada").addArticle(
41: NntpTestUtil.createArticle("test.eml"), "localhost");
42: forum.createNewsgroup("rec.music.makers").addArticle(
43: NntpTestUtil.createArticle("test.eml"), "localhost");
44: request.nextCommand();
45: assertTrue("Assert that the handler handled the request.",
46: handler.handleNntp(request, response));
47: assertTrue(baos.toString().indexOf(
48: "215 list of newsgroups follows") >= 0);
49:
50: baos.reset();
51: request.nextCommand();
52: assertTrue("Assert that the handler handled the request.",
53: handler.handleNntp(request, response));
54: assertTrue(baos.toString().indexOf(
55: "211 0 2147483647 0 comp.lang.java") >= 0);
56:
57: baos.reset();
58: request.nextCommand();
59: assertTrue("Assert that the handler handled the request.",
60: handler.handleNntp(request, response));
61: assertTrue(baos.toString().indexOf("411 no such news group") >= 0);
62: }
63:
64: public static Test suite() {
65: return new TestSuite(GroupHandlerTest.class);
66: }
67:
68: public static void main(String[] args) {
69: junit.textui.TestRunner.run(suite());
70: }
71: }
|