01: package pygmy.nntp.test;
02:
03: import junit.framework.Test;
04: import junit.framework.TestSuite;
05: import pygmy.nntp.NewsHandler;
06: import pygmy.nntp.NntpRequest;
07: import pygmy.nntp.NntpResponse;
08: import pygmy.core.Http;
09:
10: import java.util.Properties;
11: import java.io.ByteArrayInputStream;
12: import java.io.ByteArrayOutputStream;
13:
14: public class NewsHandlerTest extends NntpHandlerTestCase {
15:
16: public void testHandler() throws Exception {
17: StringBuffer buffer = new StringBuffer();
18: buffer.append("ihave <blkdu9$pd8$1@hood.uits.indiana.edu>");
19: buffer.append(Http.CRLF);
20: buffer.append(new String(getArticleBytes("test.eml")));
21: buffer.append(".");
22: buffer.append(Http.CRLF);
23: buffer.append("ihave <blkdu9$pd8$1@hood.uits.indiana.edu>");
24: buffer.append(Http.CRLF);
25:
26: NntpRequest request = new NntpRequest(null, new Properties(),
27: new ByteArrayInputStream(buffer.toString().getBytes()));
28: ByteArrayOutputStream baos = new ByteArrayOutputStream();
29: NntpResponse response = new NntpResponse(baos);
30: forum.createNewsgroup("comp.lang.java.programmer");
31:
32: request.nextCommand();
33: NewsHandler handler = new NewsHandler(forum);
34: assertTrue("Assert that the handler handled the request.",
35: handler.handleNntp(request, response));
36: // System.out.println( baos.toString() );
37: assertTrue(
38: "Assert the response asked to send the article",
39: baos
40: .toString()
41: .startsWith(
42: "335 send article to be transferred. End with <CR-LF>.<CR-LF>"));
43: assertEquals("Assert the forum size has increased by one.", 1,
44: forum.getNewsgroup("comp.lang.java.programmer").size());
45: assertTrue("Assert the response received the article.", baos
46: .toString().indexOf("235 article transferred ok") >= 0);
47:
48: request.nextCommand();
49: baos.reset();
50: assertTrue("Assert that the handler handled the request.",
51: handler.handleNntp(request, response));
52: assertTrue("Assert the response denied the article", baos
53: .toString().startsWith(
54: "435 article not wanted - do not send i"));
55:
56: forum.addArticle(NntpTestUtil.createArticle("test.eml"),
57: "localhost");
58: }
59:
60: public static Test suite() {
61: return new TestSuite(NewsHandlerTest.class);
62: }
63:
64: public static void main(String[] args) {
65: junit.textui.TestRunner.run(suite());
66: }
67: }
|