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.editing;
004:
005: import fitnesse.*;
006: import fitnesse.testutil.AbstractRegex;
007: import fitnesse.components.SaveRecorder;
008: import fitnesse.http.*;
009: import fitnesse.wiki.*;
010:
011: public class SaveResponderTest extends AbstractRegex {
012: private WikiPage root;
013:
014: private Response response;
015:
016: public MockRequest request;
017:
018: public Responder responder;
019:
020: private PageCrawler crawler;
021:
022: public void setUp() throws Exception {
023: root = InMemoryPage.makeRoot("RooT");
024: crawler = root.getPageCrawler();
025: request = new MockRequest();
026: responder = new SaveResponder();
027: }
028:
029: public void testResponse() throws Exception {
030: crawler.addPage(root, PathParser.parse("ChildPage"));
031: request.setResource("ChildPage");
032: request.addInput(EditResponder.SAVE_ID, "12345");
033: request.addInput(EditResponder.CONTENT_INPUT_NAME,
034: "some new content");
035: request.addInput(EditResponder.TICKET_ID, ""
036: + SaveRecorder.newTicket());
037:
038: Response response = responder.makeResponse(new FitNesseContext(
039: root), request);
040: assertEquals(303, response.getStatus());
041: assertHasRegexp("Location: ChildPage", response
042: .makeHttpHeaders());
043:
044: String newContent = root.getChildPage("ChildPage").getData()
045: .getContent();
046: assertEquals("some new content", newContent);
047:
048: checkRecentChanges(root, "ChildPage");
049: }
050:
051: private void checkRecentChanges(WikiPage source, String changedPage)
052: throws Exception {
053: // Within STIQ we don't need (or want) RecentChanges, so don't check for it's existence.
054:
055: // assertTrue("RecentChanges should exist", source.hasChildPage("RecentChanges"));
056: // String recentChanges = source.getChildPage("RecentChanges").getData().getContent();
057: // assertTrue("ChildPage should be in RecentChanges", recentChanges.indexOf(changedPage) != -1);
058: }
059:
060: public void testCanCreatePage() throws Exception {
061: request.setResource("ChildPageTwo");
062: request.addInput(EditResponder.SAVE_ID, "12345");
063: request.addInput(EditResponder.CONTENT_INPUT_NAME,
064: "some new content");
065: request.addInput(EditResponder.TICKET_ID, ""
066: + SaveRecorder.newTicket());
067:
068: responder.makeResponse(new FitNesseContext(root), request);
069:
070: assertEquals(true, root.hasChildPage("ChildPageTwo"));
071: String newContent = root.getChildPage("ChildPageTwo").getData()
072: .getContent();
073: assertEquals("some new content", newContent);
074: checkRecentChanges(root, "ChildPageTwo");
075: }
076:
077: public void testKnowsWhenToMerge() throws Exception {
078: String simplePageName = "SimplePageName";
079: createAndSaveANewPage(simplePageName);
080:
081: request.setResource(simplePageName);
082: request.addInput(EditResponder.CONTENT_INPUT_NAME,
083: "some new content");
084: request.addInput(EditResponder.SAVE_ID, ""
085: + (SaveRecorder.newIdNumber() - 10000));
086: request.addInput(EditResponder.TICKET_ID, ""
087: + SaveRecorder.newTicket());
088:
089: SimpleResponse response = (SimpleResponse) responder
090: .makeResponse(new FitNesseContext(root), request);
091:
092: assertHasRegexp("Merge", response.getContent());
093: }
094:
095: public void testKnowWhenNotToMerge() throws Exception {
096: String pageName = "NewPage";
097: createAndSaveANewPage(pageName);
098: String newContent = "some new Content work damn you!";
099: request.setResource(pageName);
100: request.addInput(EditResponder.CONTENT_INPUT_NAME, newContent);
101: request.addInput(EditResponder.SAVE_ID, ""
102: + SaveRecorder.newIdNumber());
103: request.addInput(EditResponder.TICKET_ID, ""
104: + SaveRecorder.newTicket());
105:
106: Response response = responder.makeResponse(new FitNesseContext(
107: root), request);
108: assertEquals(303, response.getStatus());
109:
110: request.addInput(EditResponder.CONTENT_INPUT_NAME, newContent
111: + " Ok I'm working now");
112: request.addInput(EditResponder.SAVE_ID, ""
113: + SaveRecorder.newIdNumber());
114: response = responder.makeResponse(new FitNesseContext(root),
115: request);
116: assertEquals(303, response.getStatus());
117: }
118:
119: public void testUsernameIsSavedInPageProperties() throws Exception {
120: addRequestParameters();
121: request.setCredentials("Aladdin", "open sesame");
122: response = responder.makeResponse(new FitNesseContext(root),
123: request);
124:
125: String user = root.getChildPage("EditPage").getData()
126: .getAttribute(WikiPage.LAST_MODIFYING_USER);
127: assertEquals("Aladdin", user);
128: }
129:
130: private void createAndSaveANewPage(String pageName)
131: throws Exception {
132: WikiPage simplePage = crawler.addPage(root, PathParser
133: .parse(pageName));
134:
135: PageData data = simplePage.getData();
136: SaveRecorder.pageSaved(data);
137: simplePage.commit(data);
138: }
139:
140: private void doSimpleEdit() throws Exception {
141: crawler.addPage(root, PathParser.parse("EditPage"));
142: addRequestParameters();
143:
144: response = responder.makeResponse(new FitNesseContext(root),
145: request);
146: }
147:
148: private void addRequestParameters() {
149: request.setResource("EditPage");
150: request.addInput(EditResponder.SAVE_ID, "12345");
151: request.addInput(EditResponder.CONTENT_INPUT_NAME,
152: "some new content");
153: request.addInput(EditResponder.TICKET_ID, ""
154: + SaveRecorder.newTicket());
155: }
156:
157: public void testHasVersionHeader() throws Exception {
158: doSimpleEdit();
159: assertTrue("header missing", response
160: .getHeader("Previous-Version") != null);
161: }
162: }
|