001: package pygmy.nntp.test;
002:
003: import junit.framework.Test;
004: import junit.framework.TestSuite;
005: import pygmy.core.Http;
006: import pygmy.nntp.*;
007:
008: import java.util.Properties;
009: import java.io.ByteArrayInputStream;
010: import java.io.ByteArrayOutputStream;
011:
012: public class ArticleHandlerTest extends NntpHandlerTestCase {
013:
014: public void testHandlePost() throws Exception {
015: StringBuffer buffer = new StringBuffer();
016: buffer.append("post");
017: buffer.append(Http.CRLF);
018: buffer.append(new String(getArticleBytes("testpost.eml")));
019: buffer.append(".");
020: buffer.append(Http.CRLF);
021: buffer.append("article 1");
022: buffer.append(Http.CRLF);
023: NntpRequest request = new NntpRequest(null, new Properties(),
024: new ByteArrayInputStream(buffer.toString().getBytes()));
025: ByteArrayOutputStream baos = new ByteArrayOutputStream();
026: NntpResponse response = new NntpResponse(baos);
027:
028: NewsGroup programmer = forum
029: .createNewsgroup("comp.lang.java.programmer");
030: forum.createNewsgroup("comp.lang.ada").addArticle(
031: NntpTestUtil.createArticle("test.eml"), "localhost");
032: forum.createNewsgroup("rec.music.makers").addArticle(
033: NntpTestUtil.createArticle("test.eml"), "localhost");
034: forum.getNewsgroup("rec.music.makers").addArticle(
035: NntpTestUtil.createArticle("test.eml"), "localhost");
036:
037: assertEquals(0, programmer.size());
038:
039: request.nextCommand();
040: ArticleHandler handler = new ArticleHandler(forum);
041: assertTrue("Assert that the handler handled the request.",
042: handler.handleNntp(request, response));
043: assertTrue(baos
044: .toString()
045: .indexOf(
046: "340 send article to be posted. End with <CR-LF>.<CR-LF>") >= 0);
047: assertTrue(baos.toString()
048: .indexOf("235 article transferred ok") >= 0);
049: programmer = forum.getNewsgroup("comp.lang.java.programmer");
050: assertEquals(1, programmer.size());
051:
052: request.setCurrentNewsgroup("comp.lang.java.programmer");
053: baos.reset();
054: request.nextCommand();
055: assertTrue("Assert that the handler handled the request.",
056: handler.handleNntp(request, response));
057: assertTrue(baos.toString().indexOf("220") >= 0);
058:
059: }
060:
061: public void testHandleNntp() throws Exception {
062: StringBuffer buffer = new StringBuffer();
063: buffer.append("article");
064: buffer.append(Http.CRLF);
065: buffer.append("article 1");
066: buffer.append(Http.CRLF);
067: buffer.append("next");
068: buffer.append(Http.CRLF);
069: buffer.append("last");
070: buffer.append(Http.CRLF);
071: buffer.append("article 1");
072: buffer.append(Http.CRLF);
073: buffer.append("next");
074: buffer.append(Http.CRLF);
075: buffer.append("last");
076: buffer.append(Http.CRLF);
077: buffer.append("article <blkdu9$pd8$1@hood.uits.indiana.edu>");
078: buffer.append(Http.CRLF);
079: buffer.append("head <blkdu9$pd8$1@hood.uits.indiana.edu>");
080: buffer.append(Http.CRLF);
081: buffer.append("body <blkdu9$pd8$1@hood.uits.indiana.edu>");
082: buffer.append(Http.CRLF);
083: buffer.append("head 1");
084: buffer.append(Http.CRLF);
085: buffer.append("body 1");
086: buffer.append(Http.CRLF);
087:
088: NntpRequest request = new NntpRequest(null, new Properties(),
089: new ByteArrayInputStream(buffer.toString().getBytes()));
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091: NntpResponse response = new NntpResponse(baos);
092:
093: forum.createNewsgroup("comp.lang.java.programmer");
094: forum.createNewsgroup("comp.lang.ada").addArticle(
095: NntpTestUtil.createArticle("test.eml"), "localhost");
096: forum.createNewsgroup("rec.music.makers").addArticle(
097: NntpTestUtil.createArticle("test.eml"), "localhost");
098: forum.getNewsgroup("rec.music.makers").addArticle(
099: NntpTestUtil.createArticle("test.eml"), "localhost");
100: forum.addArticle(NntpTestUtil.createArticle("test.eml"),
101: "localhost");
102:
103: baos.reset();
104: request.nextCommand();
105: ArticleHandler handler = new ArticleHandler(forum);
106: assertTrue("Assert that the handler handled the request.",
107: handler.handleNntp(request, response));
108: // System.out.println(baos.toString());
109: assertTrue(baos.toString().indexOf(
110: "420 no current article has been selected") >= 0);
111:
112: request.nextCommand();
113: assertTrue("Assert that the handler handled the request.",
114: handler.handleNntp(request, response));
115: // System.out.println(baos.toString());
116: assertTrue(baos.toString().indexOf(
117: "412 no newsgroup has been selected") >= 0);
118:
119: request.setCurrentNewsgroup("comp.lang.ada");
120: request.setCurrentArticle("1");
121: baos.reset();
122: request.nextCommand();
123: assertTrue("Assert that the handler handled the request.",
124: handler.handleNntp(request, response));
125: // System.out.println(baos.toString());
126: assertTrue(baos.toString().indexOf(
127: "421 no next article in this group") >= 0);
128:
129: baos.reset();
130: request.nextCommand();
131: assertTrue("Assert that the handler handled the request.",
132: handler.handleNntp(request, response));
133: // System.out.println(baos.toString());
134: assertTrue(baos.toString().indexOf(
135: "422 no previous article in this group") >= 0);
136:
137: request.setCurrentNewsgroup("rec.music.makers");
138: baos.reset();
139: request.nextCommand();
140: assertTrue("Assert that the handler handled the request.",
141: handler.handleNntp(request, response));
142: // System.out.println(baos.toString());
143: assertTrue(baos
144: .toString()
145: .indexOf(
146: "220 1 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
147:
148: baos.reset();
149: request.nextCommand();
150: assertTrue("Assert that the handler handled the request.",
151: handler.handleNntp(request, response));
152: // System.out.println(baos.toString());
153: assertTrue(baos
154: .toString()
155: .indexOf(
156: "223 2 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - request text separately") >= 0);
157:
158: baos.reset();
159: request.nextCommand();
160: assertTrue("Assert that the handler handled the request.",
161: handler.handleNntp(request, response));
162: // System.out.println(baos.toString());
163: assertTrue(baos
164: .toString()
165: .indexOf(
166: "223 1 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - request text separately") >= 0);
167:
168: baos.reset();
169: request.nextCommand();
170: assertTrue("Assert that the handler handled the request.",
171: handler.handleNntp(request, response));
172: // System.out.println(baos.toString());
173: assertTrue(baos
174: .toString()
175: .indexOf(
176: "220 0 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
177:
178: baos.reset();
179: request.nextCommand();
180: assertTrue("Assert that the handler handled the request.",
181: handler.handleNntp(request, response));
182: // System.out.println(baos.toString());
183: assertTrue(baos
184: .toString()
185: .indexOf(
186: "220 0 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
187: assertTrue(baos
188: .toString()
189: .indexOf(
190: "\"John C. Bollinger\" <jobollin@indiana.edu> wrote in message") == -1);
191:
192: baos.reset();
193: request.nextCommand();
194: assertTrue("Assert that the handler handled the request.",
195: handler.handleNntp(request, response));
196: // System.out.println(baos.toString());
197: assertTrue(baos
198: .toString()
199: .indexOf(
200: "220 0 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
201: assertTrue(baos.toString().indexOf(
202: "Message-ID: <blkdu9$pd8$1@hood.uits.indiana.edu>") == -1);
203:
204: baos.reset();
205: request.nextCommand();
206: assertTrue("Assert that the handler handled the request.",
207: handler.handleNntp(request, response));
208: // System.out.println(baos.toString());
209: assertTrue(baos
210: .toString()
211: .indexOf(
212: "220 1 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
213: assertTrue(baos
214: .toString()
215: .indexOf(
216: "\"John C. Bollinger\" <jobollin@indiana.edu> wrote in message") == -1);
217:
218: baos.reset();
219: request.nextCommand();
220: assertTrue("Assert that the handler handled the request.",
221: handler.handleNntp(request, response));
222: // System.out.println(baos.toString());
223: assertTrue(baos
224: .toString()
225: .indexOf(
226: "220 1 <blkdu9$pd8$1@hood.uits.indiana.edu> article retrieved - head and body follow") >= 0);
227: assertTrue(baos.toString().indexOf(
228: "Message-ID: <blkdu9$pd8$1@hood.uits.indiana.edu>") == -1);
229: }
230:
231: public static Test suite() {
232: return new TestSuite(ArticleHandlerTest.class);
233: }
234:
235: public static void main(String[] args) {
236: junit.textui.TestRunner.run(suite());
237: }
238: }
|