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.FitNesseContext;
006: import fitnesse.authentication.OneUserAuthenticator;
007: import fitnesse.http.*;
008: import fitnesse.testutil.*;
009: import fitnesse.wiki.*;
010:
011: public class WikiImportingResponderTest extends RegexTest {
012: private WikiImportingResponder responder;
013: private String baseUrl;
014: private WikiImporterTest testData;
015:
016: public void setUp() throws Exception {
017: testData = new WikiImporterTest();
018: testData.createRemoteRoot();
019: testData.createLocalRoot();
020:
021: FitNesseUtil.startFitnesse(testData.remoteRoot);
022: baseUrl = "http://localhost:" + FitNesseUtil.port + "/";
023:
024: createResponder();
025: }
026:
027: private void createResponder() throws Exception {
028: responder = new WikiImportingResponder();
029: responder.path = new WikiPagePath();
030: ChunkedResponse response = new ChunkedResponse();
031: response.readyToSend(new MockResponseSender());
032: responder.setResponse(response);
033: responder.getImporter().setDeleteOrphanOption(false);
034: }
035:
036: public void tearDown() throws Exception {
037: FitNesseUtil.stopFitnesse();
038: }
039:
040: public void testActionsOfMakeResponse() throws Exception {
041: Response response = makeSampleResponse(baseUrl);
042: new MockResponseSender(response);
043:
044: assertEquals(2, testData.pageTwo.getChildren().size());
045: WikiPage importedPageOne = testData.pageTwo
046: .getChildPage("PageOne");
047: assertNotNull(importedPageOne);
048: assertEquals("page one", importedPageOne.getData().getContent());
049:
050: WikiPage importedPageTwo = testData.pageTwo
051: .getChildPage("PageTwo");
052: assertNotNull(importedPageTwo);
053: assertEquals("page two", importedPageTwo.getData().getContent());
054:
055: assertEquals(1, importedPageOne.getChildren().size());
056: WikiPage importedChildOne = importedPageOne
057: .getChildPage("ChildOne");
058: assertNotNull(importedChildOne);
059: assertEquals("child one", importedChildOne.getData()
060: .getContent());
061: }
062:
063: public void testImportingFromNonRootPageUpdatesPageContent()
064: throws Exception {
065: PageData data = testData.pageTwo.getData();
066: WikiImportProperty importProperty = new WikiImportProperty(
067: baseUrl + "PageOne");
068: importProperty.addTo(data.getProperties());
069: data.setContent("nonsense");
070: testData.pageTwo.commit(data);
071:
072: Response response = makeSampleResponse("blah");
073: new MockResponseSender(response);
074:
075: data = testData.pageTwo.getData();
076: assertEquals("page one", data.getContent());
077:
078: assertFalse(WikiImportProperty.createFrom(data.getProperties())
079: .isRoot());
080: }
081:
082: public void testImportPropertiesGetAdded() throws Exception {
083: Response response = makeSampleResponse(baseUrl);
084: new MockResponseSender(response);
085:
086: checkProperties(testData.pageTwo, baseUrl, true, null);
087:
088: WikiPage importedPageOne = testData.pageTwo
089: .getChildPage("PageOne");
090: checkProperties(importedPageOne, baseUrl + "PageOne", false,
091: testData.remoteRoot.getChildPage("PageOne"));
092:
093: WikiPage importedPageTwo = testData.pageTwo
094: .getChildPage("PageTwo");
095: checkProperties(importedPageTwo, baseUrl + "PageTwo", false,
096: testData.remoteRoot.getChildPage("PageTwo"));
097:
098: WikiPage importedChildOne = importedPageOne
099: .getChildPage("ChildOne");
100: checkProperties(importedChildOne, baseUrl + "PageOne.ChildOne",
101: false, testData.remoteRoot.getChildPage("PageOne")
102: .getChildPage("ChildOne"));
103: }
104:
105: private void checkProperties(WikiPage page, String source,
106: boolean isRoot, WikiPage remotePage) throws Exception {
107: WikiPageProperties props = page.getData().getProperties();
108: if (!isRoot)
109: assertFalse("should not have Edit property", props
110: .has("Edit"));
111:
112: WikiImportProperty importProperty = WikiImportProperty
113: .createFrom(props);
114: assertNotNull(importProperty);
115: assertEquals(source, importProperty.getSourceUrl());
116: assertEquals(isRoot, importProperty.isRoot());
117:
118: if (remotePage != null) {
119: long remoteLastModificationTime = remotePage.getData()
120: .getProperties().getLastModificationTime()
121: .getTime();
122: long importPropertyLastModificationTime = importProperty
123: .getLastRemoteModificationTime().getTime();
124: assertEquals(remoteLastModificationTime,
125: importPropertyLastModificationTime);
126: }
127: }
128:
129: public void testHtmlOfMakeResponse() throws Exception {
130: Response response = makeSampleResponse(baseUrl);
131: String content = new MockResponseSender(response).sentData();
132:
133: assertSubString("<html>", content);
134: assertSubString("Wiki Import", content);
135:
136: assertSubString("href=\"PageTwo\"", content);
137: assertSubString("href=\"PageTwo.PageOne\"", content);
138: assertSubString("href=\"PageTwo.PageOne.ChildOne\"", content);
139: assertSubString("href=\"PageTwo.PageTwo\"", content);
140: assertSubString("Import complete.", content);
141: assertSubString("3 pages were imported.", content);
142: }
143:
144: public void testHtmlOfMakeResponseWithNoModifications()
145: throws Exception {
146: Response response = makeSampleResponse(baseUrl);
147: String content = new MockResponseSender(response).sentData();
148:
149: // import a second time... nothing was modified
150: createResponder();
151: response = makeSampleResponse(baseUrl);
152: content = new MockResponseSender(response).sentData();
153:
154: assertSubString("<html>", content);
155: assertSubString("Wiki Import", content);
156:
157: assertSubString("href=\"PageTwo\"", content);
158: assertNotSubString("href=\"PageTwo.PageOne\"", content);
159: assertNotSubString("href=\"PageTwo.PageOne.ChildOne\"", content);
160: assertNotSubString("href=\"PageTwo.PageTwo\"", content);
161: assertSubString("Import complete.", content);
162: assertSubString("0 pages were imported.", content);
163: assertSubString("3 pages were unmodified.", content);
164: }
165:
166: private ChunkedResponse makeSampleResponse(String remoteUrl)
167: throws Exception {
168: MockRequest request = makeRequest(remoteUrl);
169:
170: return getResponse(request);
171: }
172:
173: private ChunkedResponse getResponse(MockRequest request)
174: throws Exception {
175: Response response = responder.makeResponse(new FitNesseContext(
176: testData.localRoot), request);
177: assertTrue(response instanceof ChunkedResponse);
178: return (ChunkedResponse) response;
179: }
180:
181: private MockRequest makeRequest(String remoteUrl) {
182: MockRequest request = new MockRequest();
183: request.setResource("PageTwo");
184: request.addInput("responder", "import");
185: request.addInput("remoteUrl", remoteUrl);
186: return request;
187: }
188:
189: public void testMakeResponseImportingNonRootPage() throws Exception {
190: MockRequest request = makeRequest(baseUrl + "PageOne");
191:
192: Response response = responder.makeResponse(new FitNesseContext(
193: testData.localRoot), request);
194: String content = new MockResponseSender(response).sentData();
195:
196: assertNotNull(testData.pageTwo.getChildPage("ChildOne"));
197: assertSubString("href=\"PageTwo.ChildOne\"", content);
198: assertSubString(">ChildOne<", content);
199: }
200:
201: public void testRemoteUrlNotFound() throws Exception {
202: String remoteUrl = baseUrl + "PageDoesntExist";
203: Response response = makeSampleResponse(remoteUrl);
204:
205: String content = new MockResponseSender(response).sentData();
206: assertSubString("The remote resource, " + remoteUrl
207: + ", was not found.", content);
208: }
209:
210: public void testBadDomainName() throws Exception {
211: String remoteUrl = "http://bad.domainthatdoesntexist.com/FrontPage";
212: Response response = makeSampleResponse(remoteUrl);
213:
214: String content = new MockResponseSender(response).sentData();
215: assertSubString(
216: "java.net.UnknownHostException: bad.domainthatdoesntexist.com",
217: content);
218: }
219:
220: public void testErrorMessageForBadUrlProvided() throws Exception {
221: String remoteUrl = baseUrl + "blah";
222: Response response = makeSampleResponse(remoteUrl);
223:
224: String content = new MockResponseSender(response).sentData();
225: assertSubString(
226: "The URL's resource path, blah, is not a valid WikiWord.",
227: content);
228: }
229:
230: public void testUnauthorizedResponse() throws Exception {
231: makeSecurePage(testData.remoteRoot);
232:
233: Response response = makeSampleResponse(baseUrl);
234: String content = new MockResponseSender(response).sentData();
235:
236: checkRemoteLoginForm(content);
237: }
238:
239: private void makeSecurePage(WikiPage page) throws Exception {
240: PageData data = page.getData();
241: data.setAttribute(WikiPage.SECURE_READ);
242: page.commit(data);
243: FitNesseUtil.context.authenticator = new OneUserAuthenticator(
244: "joe", "blow");
245: }
246:
247: private void checkRemoteLoginForm(String content) {
248: assertHasRegexp("The wiki at .* requires authentication.",
249: content);
250: assertSubString("<form", content);
251: assertHasRegexp("<input[^>]*name=\"remoteUsername\"", content);
252: assertHasRegexp("<input[^>]*name=\"remotePassword\"", content);
253: }
254:
255: public void testUnauthorizedResponseFromNonRoot() throws Exception {
256: WikiPage childPage = testData.remoteRoot
257: .getChildPage("PageOne");
258: makeSecurePage(childPage);
259:
260: Response response = makeSampleResponse(baseUrl);
261: String content = new MockResponseSender(response).sentData();
262:
263: assertSubString("The wiki at " + baseUrl
264: + "PageOne requires authentication.", content);
265: assertSubString("<form", content);
266: }
267:
268: public void testImportingFromSecurePageWithCredentials()
269: throws Exception {
270: makeSecurePage(testData.remoteRoot);
271:
272: MockRequest request = makeRequest(baseUrl);
273: request.addInput("remoteUsername", "joe");
274: request.addInput("remotePassword", "blow");
275: Response response = getResponse(request);
276: String content = new MockResponseSender(response).sentData();
277:
278: assertNotSubString("requires authentication", content);
279: assertSubString("3 pages were imported.", content);
280:
281: assertEquals("joe", WikiImporter.remoteUsername);
282: assertEquals("blow", WikiImporter.remotePassword);
283: }
284:
285: public void testListOfOrphanedPages() throws Exception {
286: WikiImporter importer = new WikiImporter();
287:
288: String tail = responder.makeTailHtml(importer).html();
289:
290: assertNotSubString("orphan", tail);
291: assertNotSubString("PageOne", tail);
292: assertNotSubString("PageOne.ChildPagae", tail);
293:
294: importer.getOrphans().add(new WikiPagePath(testData.pageOne));
295: importer.getOrphans().add(
296: new WikiPagePath(testData.childPageOne));
297:
298: tail = responder.makeTailHtml(importer).html();
299:
300: assertSubString(
301: "2 orphaned pages were found and have been removed.",
302: tail);
303: assertSubString("PageOne", tail);
304: assertSubString("PageOne.ChildOne", tail);
305: }
306:
307: public void testAutoUpdatingTurnedOn() throws Exception {
308: MockRequest request = makeRequest(baseUrl);
309: responder.setRequest(request);
310: responder.data = new PageData(new WikiPageDummy());
311:
312: responder.initializeImporter();
313: assertFalse(responder.getImporter().getAutoUpdateSetting());
314:
315: request.addInput("autoUpdate", "1");
316: responder.initializeImporter();
317: assertTrue(responder.getImporter().getAutoUpdateSetting());
318: }
319:
320: public void testAutoUpdateSettingDisplayedInTail() throws Exception {
321: WikiImporter importer = new MockWikiImporter();
322: importer.setAutoUpdateSetting(true);
323:
324: String tail = responder.makeTailHtml(importer).html();
325: assertSubString("Automatic Update turned ON", tail);
326:
327: importer.setAutoUpdateSetting(false);
328:
329: tail = responder.makeTailHtml(importer).html();
330: assertSubString("Automatic Update turned OFF", tail);
331: }
332: }
|