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.refactoring;
004:
005: import fitnesse.*;
006: import fitnesse.responders.AbstractResponderRegex;
007: import fitnesse.wiki.*;
008: import fitnesse.http.*;
009:
010: public class RenamePageResponderTest extends AbstractResponderRegex {
011: private WikiPagePath pageOnePath;
012:
013: private WikiPagePath pageTwoPath;
014:
015: private String pageOneName;
016:
017: private String pageTwoName;
018:
019: protected Responder responderInstance() {
020: return new RenamePageResponder();
021: }
022:
023: public void setUp() throws Exception {
024: super .setUp();
025: pageOneName = "PageOne";
026: pageTwoName = "PageTwo";
027: pageOnePath = PathParser.parse(pageOneName);
028: pageTwoPath = PathParser.parse(pageTwoName);
029: }
030:
031: public void testInvalidName() throws Exception {
032: String invalidName = "FirstName.SecondName";
033: String pageName = "MyPage";
034: crawler.addPage(root, PathParser.parse(pageName), "content");
035: Response response = doRename(pageName, invalidName);
036:
037: assertHasRegexp("Cannot rename", getResponseContent(response));
038: }
039:
040: public void testDontRenameFrontPage() throws Exception {
041: String frontPageName = "FrontPage";
042: crawler.addPage(root, PathParser.parse(frontPageName),
043: "Content");
044: Response response = doRename(frontPageName, "ReNamed");
045: assertNotNull(response);
046: assertSubString("Cannot rename", getResponseContent(response));
047: }
048:
049: public void testPageRedirection() throws Exception {
050: WikiPage pageOne = crawler.addPage(root, PathParser
051: .parse("OneOne"), "Content");
052: crawler.addPage(pageOne, PathParser.parse("TwoOne"));
053: Response response = doRename("OneOne.TwoOne", "ReName");
054: assertNotNull(response);
055: assertEquals(303, response.getStatus());
056: assertEquals("OneOne.ReName", response.getHeader("Location"));
057: }
058:
059: public void testPageWasRenamed() throws Exception {
060: String originalName = "OneOne";
061: WikiPagePath originalPath = PathParser.parse(originalName);
062: String renamedName = "WonWon";
063: WikiPagePath renamedPath = PathParser.parse(renamedName);
064:
065: crawler.addPage(root, originalPath, "Content");
066: assertTrue(crawler.pageExists(root, originalPath));
067: assertFalse(crawler.pageExists(root, renamedPath));
068:
069: doRename(originalName, renamedName);
070:
071: assertTrue(crawler.pageExists(root, renamedPath));
072: assertFalse(crawler.pageExists(root, originalPath));
073: }
074:
075: public void testReferencesChanged() throws Exception {
076: crawler.addPage(root, pageOnePath,
077: "Line one\nPageTwo\nLine three");
078: crawler.addPage(root, pageTwoPath, "Page two content");
079:
080: doRename(pageTwoName, "PageThree");
081: assertNotNull(root.getChildPage("PageThree"));
082: WikiPage pageOne = root.getChildPage(pageOneName);
083: assertEquals("Line one\nPageThree\nLine three", pageOne
084: .getData().getContent());
085: }
086:
087: public void testDontRenameToExistingPage() throws Exception {
088: crawler.addPage(root, pageOnePath, "Page one content");
089: crawler.addPage(root, pageTwoPath, "Page two content");
090:
091: Response response = doRename(pageOneName, pageTwoName);
092: assertTrue(crawler.pageExists(root, pageOnePath));
093: assertTrue(crawler.pageExists(root, pageTwoPath));
094: assertEquals("Page two content", root.getChildPage(pageTwoName)
095: .getData().getContent());
096: assertSubString("Cannot rename", getResponseContent(response));
097: }
098:
099: public void testChildPagesStayIntactWhenParentIsRenamed()
100: throws Exception {
101: crawler.addPage(root, pageOnePath, "page one");
102: crawler.addPage(root, PathParser.parse("PageOne.ChildPage"),
103: "child page");
104: crawler.addPage(root, PathParser
105: .parse("PageOne.ChildPage.GrandChild"), "grand child");
106:
107: doRename(pageOneName, pageTwoName);
108:
109: WikiPagePath path = PathParser.parse("PageTwo.ChildPage");
110: assertTrue(crawler.pageExists(root, path));
111: WikiPage page = crawler.getPage(root, path);
112: assertNotNull(page);
113: assertEquals("child page", page.getData().getContent());
114:
115: WikiPagePath grandChildPath = PathParser
116: .parse("PageTwo.ChildPage.GrandChild");
117: assertTrue(crawler.pageExists(root, grandChildPath));
118: page = crawler.getPage(root, grandChildPath);
119: assertNotNull(page);
120: assertEquals("grand child", page.getData().getContent());
121: }
122:
123: private String getResponseContent(Response response)
124: throws Exception {
125: return new MockResponseSender(response).sentData();
126: }
127:
128: private Response doRename(String fromName, String toName)
129: throws Exception {
130: request.setResource(fromName);
131: request.addInput("newName", toName);
132: return responder.makeResponse(new FitNesseContext(root),
133: request);
134: }
135: }
|