001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders;
004:
005: import fitnesse.wiki.*;
006: import fitnesse.testutil.*;
007: import fitnesse.http.*;
008: import fitnesse.FitNesseContext;
009: import fitnesse.authentication.OneUserAuthenticator;
010: import fitnesse.util.XmlUtil;
011: import org.w3c.dom.Document;
012:
013: public class WikiImportingResponderTest extends AbstractRegex {
014: private WikiImportingResponder responder;
015:
016: private String baseUrl;
017:
018: private WikiPage localRoot;
019:
020: private WikiPage pageOne;
021:
022: private WikiPage childPageOne;
023:
024: private WikiPage pageTwo;
025:
026: private WikiPage remoteRoot;
027:
028: public void setUp() throws Exception {
029: createServerSideRoot();
030:
031: responder = new WikiImportingResponder();
032:
033: FitNesseUtil.startFitnesse(remoteRoot);
034: baseUrl = "http://localhost:" + FitNesseUtil.port + "/";
035: responder.remoteHostname = "localhost";
036: responder.remotePort = FitNesseUtil.port;
037: responder.path = new WikiPagePath();
038: ChunkedResponse response = new ChunkedResponse();
039: response.readyToSend(new MockResponseSender());
040: responder.setResponse(response);
041:
042: createLocalRoot();
043: }
044:
045: private void createLocalRoot() throws Exception {
046: localRoot = InMemoryPage.makeRoot("RooT2");
047: pageOne = localRoot.addChildPage("PageOne");
048: childPageOne = pageOne.addChildPage("ChildOne");
049: pageTwo = localRoot.addChildPage("PageTwo");
050: }
051:
052: private WikiPage createServerSideRoot() throws Exception {
053: remoteRoot = InMemoryPage.makeRoot("RooT");
054: PageCrawler crawler = remoteRoot.getPageCrawler();
055: crawler.addPage(remoteRoot, PathParser.parse("PageOne"),
056: "page one");
057: crawler.addPage(remoteRoot, PathParser
058: .parse("PageOne.ChildOne"), "child one");
059: crawler.addPage(remoteRoot, PathParser.parse("PageTwo"),
060: "page two");
061: return remoteRoot;
062: }
063:
064: public void tearDown() throws Exception {
065: FitNesseUtil.stopFitnesse();
066: }
067:
068: public void testPageAdded() throws Exception {
069: responder.pageAdded(pageOne);
070:
071: PageData data = pageOne.getData();
072: assertEquals("page one", data.getContent());
073: }
074:
075: public void testChildPageAdded() throws Exception {
076: responder.pageAdded(pageOne);
077: responder.pageAdded(childPageOne);
078:
079: PageData data = childPageOne.getData();
080: assertEquals("child one", data.getContent());
081: }
082:
083: public void testExiting() throws Exception {
084: responder.pageAdded(pageOne);
085: responder.pageAdded(childPageOne);
086: responder.exitPage();
087: responder.exitPage();
088: responder.pageAdded(pageTwo);
089:
090: PageData data = pageTwo.getData();
091: assertEquals("page two", data.getContent());
092: }
093:
094: public void testGetPageTree() throws Exception {
095: responder.page = childPageOne;
096: responder.xmlizerPageHandler = new MockXmlizerPageHandler();
097: Document doc = responder.getPageTree();
098: assertNotNull(doc);
099: String xml = XmlUtil.xmlAsString(doc);
100:
101: assertSubString("PageOne", xml);
102: assertSubString("PageTwo", xml);
103: }
104:
105: public void testActionsOfMakeResponse() throws Exception {
106: Response response = makeSampleResponse(baseUrl);
107: new MockResponseSender(response);
108:
109: assertEquals(2, pageTwo.getChildren().size());
110: WikiPage importedPageOne = pageTwo.getChildPage("PageOne");
111: assertNotNull(importedPageOne);
112: assertEquals("page one", importedPageOne.getData().getContent());
113:
114: WikiPage importedPageTwo = pageTwo.getChildPage("PageTwo");
115: assertNotNull(importedPageTwo);
116: assertEquals("page two", importedPageTwo.getData().getContent());
117:
118: assertEquals(1, importedPageOne.getChildren().size());
119: WikiPage importedChildOne = importedPageOne
120: .getChildPage("ChildOne");
121: assertNotNull(importedChildOne);
122: assertEquals("child one", importedChildOne.getData()
123: .getContent());
124: }
125:
126: public void testImportingFromNonRootPageUpdatesPageContent()
127: throws Exception {
128: PageData data = pageTwo.getData();
129: data.setAttribute("WikiImportSource", baseUrl + "PageOne");
130: data.setContent("nonsense");
131: pageTwo.commit(data);
132:
133: Response response = makeSampleResponse("blah");
134: new MockResponseSender(response);
135:
136: data = pageTwo.getData();
137: assertEquals("page one", data.getContent());
138:
139: assertFalse(data.hasAttribute("WikiImportRoot"));
140: }
141:
142: public void testImportPropertiesGetAdded() throws Exception {
143: Response response = makeSampleResponse(baseUrl);
144: new MockResponseSender(response);
145:
146: checkProperties(pageTwo, "WikiImportRoot", baseUrl);
147:
148: WikiPage importedPageOne = pageTwo.getChildPage("PageOne");
149: checkProperties(importedPageOne, "WikiImportSource", baseUrl
150: + "PageOne");
151:
152: WikiPage importedPageTwo = pageTwo.getChildPage("PageTwo");
153: checkProperties(importedPageTwo, "WikiImportSource", baseUrl
154: + "PageTwo");
155:
156: WikiPage importedChildOne = importedPageOne
157: .getChildPage("ChildOne");
158: checkProperties(importedChildOne, "WikiImportSource", baseUrl
159: + "PageOne.ChildOne");
160: }
161:
162: private void checkProperties(WikiPage page, String id, String value)
163: throws Exception {
164: WikiPageProperties props = page.getData().getProperties();
165: assertTrue(props.has(id));
166: assertEquals(value, props.get(id));
167: }
168:
169: public void testHtmlOfMakeResponse() throws Exception {
170: Response response = makeSampleResponse(baseUrl);
171: String content = new MockResponseSender(response).sentData();
172:
173: assertSubString("<html>", content);
174: assertSubString("Wiki Import", content);
175:
176: assertSubString("href=\"PageTwo\"", content);
177: assertSubString("href=\"PageTwo.PageOne\"", content);
178: assertSubString("href=\"PageTwo.PageOne.ChildOne\"", content);
179: assertSubString("href=\"PageTwo.PageTwo\"", content);
180: assertSubString("Import complete.", content);
181: assertSubString("3 pages were imported.", content);
182: }
183:
184: private ChunkedResponse makeSampleResponse(String remoteUrl)
185: throws Exception {
186: responder.remoteHostname = "blah";
187: MockRequest request = makeRequest(remoteUrl);
188:
189: return getResponse(request);
190: }
191:
192: private ChunkedResponse getResponse(MockRequest request)
193: throws Exception {
194: Response response = responder.makeResponse(new FitNesseContext(
195: localRoot), request);
196: assertTrue(response instanceof ChunkedResponse);
197: ChunkedResponse chunkedResponse = (ChunkedResponse) response;
198: return chunkedResponse;
199: }
200:
201: private MockRequest makeRequest(String remoteUrl) {
202: MockRequest request = new MockRequest();
203: request.setResource("PageTwo");
204: request.addInput("responder", "import");
205: request.addInput("remoteUrl", remoteUrl);
206: return request;
207: }
208:
209: public void testMakeResponseImportingNonRootPage() throws Exception {
210: responder.remoteHostname = "blah";
211: MockRequest request = makeRequest(baseUrl + "PageOne");
212:
213: Response response = responder.makeResponse(new FitNesseContext(
214: localRoot), request);
215: String content = new MockResponseSender(response).sentData();
216:
217: assertNotNull(pageTwo.getChildPage("ChildOne"));
218: assertSubString("href=\"PageTwo.ChildOne\"", content);
219: assertSubString(">ChildOne<", content);
220: }
221:
222: public void testUrlParsing() throws Exception {
223: testUrlParsing("http://mysite.com", "mysite.com", 80, "");
224: testUrlParsing("http://mysite.com/", "mysite.com", 80, "");
225: testUrlParsing("http://mysite.com:8080/", "mysite.com", 8080,
226: "");
227: testUrlParsing("http://mysite.com:8080", "mysite.com", 8080, "");
228: testUrlParsing("http://mysite.com:80/", "mysite.com", 80, "");
229: testUrlParsing("http://mysite.com/PageOne", "mysite.com", 80,
230: "PageOne");
231: testUrlParsing("http://mysite.com/PageOne.ChildOne",
232: "mysite.com", 80, "PageOne.ChildOne");
233: }
234:
235: private void testUrlParsing(String url, String host, int port,
236: String path) throws Exception {
237: responder.parseUrl(url);
238: assertEquals(host, responder.remoteHostname);
239: assertEquals(port, responder.remotePort);
240: assertEquals(path, PathParser.render(responder.remotePath));
241: }
242:
243: public void testParsingBadUrl() throws Exception {
244: try {
245: responder.parseUrl("blah");
246: fail("should have exception");
247: } catch (Exception e) {
248: assertEquals("blah is not a valid URL.", e.getMessage());
249: }
250: }
251:
252: public void testParsingUrlWithNonWikiWord() throws Exception {
253: try {
254: responder.parseUrl("http://blah.com/notawikiword");
255: fail("should throw exception");
256: } catch (Exception e) {
257: assertEquals(
258: "The URL's resource path, notawikiword, is not a valid WikiWord.",
259: e.getMessage());
260: }
261: }
262:
263: public void testRemoteUrlNotFound() throws Exception {
264: String remoteUrl = baseUrl + "PageDoesntExist";
265: Response response = makeSampleResponse(remoteUrl);
266:
267: String content = new MockResponseSender(response).sentData();
268: assertSubString("The remote resource, " + remoteUrl
269: + ", was not found.", content);
270: }
271:
272: // FIXME: Not sure why this is failing intermittently. Unused functionality.
273: /* public void testBadDomainName() throws Exception {
274: String remoteUrl = "http://bad.domainthatdoesntexist.com/FrontPage";
275: Response response = makeSampleResponse(remoteUrl);
276:
277: String content = new MockResponseSender(response).sentData();
278: assertSubString("java.lang.NullPointerException", content);
279: }*/
280:
281: public void testErrorMessageForBadUrlProvided() throws Exception {
282: String remoteUrl = baseUrl + "blah";
283: Response response = makeSampleResponse(remoteUrl);
284:
285: String content = new MockResponseSender(response).sentData();
286: assertSubString(
287: "The URL's resource path, blah, is not a valid WikiWord.",
288: content);
289: }
290:
291: public void testUnauthorizedResponse() throws Exception {
292: makeSecurePage(remoteRoot);
293: Response response = makeSampleResponse(baseUrl);
294:
295: String content = new MockResponseSender(response).sentData();
296: checkRemoteLoginForm(content);
297: }
298:
299: private void makeSecurePage(WikiPage page) throws Exception {
300: PageData data = page.getData();
301: data.setAttribute(WikiPage.SECURE_READ);
302: page.commit(data);
303: FitNesseUtil.context.authenticator = new OneUserAuthenticator(
304: "joe", "blow");
305: }
306:
307: private void checkRemoteLoginForm(String content) {
308: assertHasRegexp("The wiki at .* requires authentication.",
309: content);
310: assertSubString("<form", content);
311: assertHasRegexp("<input[^>]*name=\"remoteUsername\"", content);
312: assertHasRegexp("<input[^>]*name=\"remotePassword\"", content);
313: }
314:
315: public void testUnauthorizedResponseFromNonRoot() throws Exception {
316: WikiPage childPage = remoteRoot.getChildPage("PageOne");
317: makeSecurePage(childPage);
318:
319: Response response = makeSampleResponse(baseUrl);
320: String content = new MockResponseSender(response).sentData();
321:
322: assertSubString("The wiki at " + baseUrl
323: + "PageOne requires authentication.", content);
324: assertSubString("<form", content);
325: }
326:
327: public void testImportingFromSecurePageWithCredentials()
328: throws Exception {
329: makeSecurePage(remoteRoot);
330:
331: MockRequest request = makeRequest(baseUrl);
332: request.addInput("remoteUsername", "joe");
333: request.addInput("remotePassword", "blow");
334: Response response = getResponse(request);
335: String content = new MockResponseSender(response).sentData();
336:
337: assertNotSubString("requires authentication", content);
338: assertSubString("3 pages were imported.", content);
339:
340: assertEquals("joe", WikiImportingResponder.remoteUsername);
341: assertEquals("blow", WikiImportingResponder.remotePassword);
342: }
343: }
|