001: package fitnesse.responders;
002:
003: import fitnesse.wiki.*;
004: import fitnesse.testutil.*;
005: import fitnesse.util.XmlUtil;
006: import java.util.*;
007: import org.w3c.dom.Document;
008:
009: public class WikiImporterTest extends RegexTest implements
010: WikiImporterClient {
011: public WikiPage pageOne;
012: public WikiPage childPageOne;
013: public WikiPage pageTwo;
014: public WikiPage remoteRoot;
015: private WikiImporter importer;
016: private LinkedList<WikiPage> imports;
017: private LinkedList<Exception> errors;
018: public WikiPage localRoot;
019:
020: public void setUp() throws Exception {
021: createRemoteRoot();
022: createLocalRoot();
023:
024: FitNesseUtil.startFitnesse(remoteRoot);
025:
026: importer = new WikiImporter();
027: importer.setWikiImporterClient(this );
028: importer.parseUrl("http://localhost:" + FitNesseUtil.port);
029:
030: imports = new LinkedList<WikiPage>();
031: errors = new LinkedList<Exception>();
032: }
033:
034: public void createLocalRoot() throws Exception {
035: localRoot = InMemoryPage.makeRoot("RooT2");
036: pageOne = localRoot.addChildPage("PageOne");
037: childPageOne = pageOne.addChildPage("ChildOne");
038: pageTwo = localRoot.addChildPage("PageTwo");
039: }
040:
041: public WikiPage createRemoteRoot() throws Exception {
042: remoteRoot = InMemoryPage.makeRoot("RooT");
043: PageCrawler crawler = remoteRoot.getPageCrawler();
044: crawler.addPage(remoteRoot, PathParser.parse("PageOne"),
045: "page one");
046: crawler.addPage(remoteRoot, PathParser
047: .parse("PageOne.ChildOne"), "child one");
048: crawler.addPage(remoteRoot, PathParser.parse("PageTwo"),
049: "page two");
050: return remoteRoot;
051: }
052:
053: public void tearDown() throws Exception {
054: FitNesseUtil.stopFitnesse();
055: }
056:
057: public void testEnterChildPage() throws Exception {
058: importer.enterChildPage(pageOne, new Date());
059:
060: PageData data = pageOne.getData();
061: assertEquals("page one", data.getContent());
062: }
063:
064: public void testChildPageAdded() throws Exception {
065: importer.enterChildPage(pageOne, new Date());
066: importer.enterChildPage(childPageOne, new Date());
067:
068: PageData data = childPageOne.getData();
069: assertEquals("child one", data.getContent());
070: }
071:
072: public void testEnterChildPageWhenRemotePageNotModified()
073: throws Exception {
074: importer.enterChildPage(pageOne, new Date());
075: importer.exitPage();
076:
077: PageData data = pageOne.getData();
078: data.setContent("new content");
079: pageOne.commit(data);
080:
081: importer.enterChildPage(pageOne, new Date(0));
082:
083: assertEquals("new content", pageOne.getData().getContent());
084: }
085:
086: public void testExiting() throws Exception {
087: importer.enterChildPage(pageOne, new Date());
088: importer.enterChildPage(childPageOne, new Date());
089: importer.exitPage();
090: importer.exitPage();
091: importer.enterChildPage(pageTwo, new Date());
092:
093: PageData data = pageTwo.getData();
094: assertEquals("page two", data.getContent());
095: }
096:
097: public void testGetPageTree() throws Exception {
098: Document doc = importer.getPageTree();
099: assertNotNull(doc);
100: String xml = XmlUtil.xmlAsString(doc);
101:
102: assertSubString("PageOne", xml);
103: assertSubString("PageTwo", xml);
104: }
105:
106: public void testUrlParsing() throws Exception {
107: testUrlParsing("http://mysite.com", "mysite.com", 80, "");
108: testUrlParsing("http://mysite.com/", "mysite.com", 80, "");
109: testUrlParsing("http://mysite.com:8080/", "mysite.com", 8080,
110: "");
111: testUrlParsing("http://mysite.com:8080", "mysite.com", 8080, "");
112: testUrlParsing("http://mysite.com:80/", "mysite.com", 80, "");
113: testUrlParsing("http://mysite.com/PageOne", "mysite.com", 80,
114: "PageOne");
115: testUrlParsing("http://mysite.com/PageOne.ChildOne",
116: "mysite.com", 80, "PageOne.ChildOne");
117: }
118:
119: private void testUrlParsing(String url, String host, int port,
120: String path) throws Exception {
121: importer.parseUrl(url);
122: assertEquals(host, importer.getRemoteHostname());
123: assertEquals(port, importer.getRemotePort());
124: assertEquals(path, PathParser.render(importer.getRemotePath()));
125: }
126:
127: public void testParsingBadUrl() throws Exception {
128: try {
129: importer.parseUrl("blah");
130: fail("should have exception");
131: } catch (Exception e) {
132: assertEquals("blah is not a valid URL.", e.getMessage());
133: }
134: }
135:
136: public void testParsingUrlWithNonWikiWord() throws Exception {
137: try {
138: importer.parseUrl("http://blah.com/notawikiword");
139: fail("should throw exception");
140: } catch (Exception e) {
141: assertEquals(
142: "The URL's resource path, notawikiword, is not a valid WikiWord.",
143: e.getMessage());
144: }
145: }
146:
147: public void testImportingWiki() throws Exception {
148: localRoot = InMemoryPage.makeRoot("LocalRoot");
149: importer.importWiki(localRoot);
150:
151: assertEquals(2, localRoot.getChildren().size());
152: assertEquals(3, imports.size());
153: assertEquals(0, errors.size());
154: }
155:
156: public void testFindsOrphansOnLocalWiki() throws Exception {
157: performImportWithExtraLocalPages();
158:
159: List<WikiPagePath> orphans = importer.getOrphans();
160: assertEquals(3, orphans.size());
161: assertTrue(orphans.contains(new WikiPagePath()
162: .addName("PageThree")));
163: assertTrue(orphans.contains(new WikiPagePath().addName(
164: "PageOne").addName("ChildTwo")));
165: assertTrue(orphans
166: .contains(new WikiPagePath().addName("PageOne")
167: .addName("ChildOne").addName("GrandChildOne")));
168: assertFalse(orphans.contains(new WikiPagePath()
169: .addName("PageThatDoesntImport")));
170: assertFalse(orphans.contains(new WikiPagePath()
171: .addName("OtherImportRoot")));
172: }
173:
174: private void performImportWithExtraLocalPages() throws Exception {
175: addLocalPageWithImportProperty(localRoot, "PageThree", false);
176: addLocalPageWithImportProperty(pageOne, "ChildTwo", false);
177: addLocalPageWithImportProperty(childPageOne, "GrandChildOne",
178: false);
179: localRoot.addChildPage("PageThatDoesntImport");
180: addLocalPageWithImportProperty(localRoot, "OtherImportRoot",
181: true);
182:
183: importer.importWiki(localRoot);
184: }
185:
186: public void testOrphansAreRemoved() throws Exception {
187: performImportWithExtraLocalPages();
188:
189: assertFalse(localRoot.hasChildPage("PageThree"));
190: assertFalse(pageOne.hasChildPage("ChildTwo"));
191: assertFalse(childPageOne.hasChildPage("GrandChildOne"));
192:
193: assertTrue(localRoot.hasChildPage("PageThatDoesntImport"));
194: assertTrue(localRoot.hasChildPage("OtherImportRoot"));
195: }
196:
197: public void testWholeTreeOrphaned() throws Exception {
198: importer.importWiki(localRoot);
199:
200: remoteRoot.removeChildPage("PageOne");
201:
202: importer.importWiki(localRoot);
203:
204: assertFalse(localRoot.hasChildPage("PageOne"));
205: }
206:
207: public void testContextIsNotOrphanWhenUpdatingNonRoot()
208: throws Exception {
209: addLocalPageWithImportProperty(localRoot, "PageOne", false);
210: importer.parseUrl("http://localhost:" + FitNesseUtil.port
211: + "/PageOne");
212:
213: importer.importWiki(localRoot.getChildPage("PageOne"));
214:
215: assertEquals(0, importer.getOrphans().size());
216: }
217:
218: public void testAutoUpdatePropertySetOnRoot() throws Exception {
219: addLocalPageWithImportProperty(localRoot, "PageOne", false);
220: importer.parseUrl("http://localhost:" + FitNesseUtil.port
221: + "/PageOne");
222: importer.setAutoUpdateSetting(true);
223: WikiPage importedPage = localRoot.getChildPage("PageOne");
224: importer.importWiki(importedPage);
225:
226: WikiImportProperty importProp = WikiImportProperty
227: .createFrom(importedPage.getData().getProperties());
228: assertTrue(importProp.isAutoUpdate());
229:
230: importer.setAutoUpdateSetting(false);
231: importer.importWiki(importedPage);
232:
233: importProp = WikiImportProperty.createFrom(importedPage
234: .getData().getProperties());
235: assertFalse(importProp.isAutoUpdate());
236: }
237:
238: public void testAutoUpdate_NewPage() throws Exception {
239: importer.setAutoUpdateSetting(true);
240: importer.enterChildPage(pageOne, new Date());
241:
242: WikiImportProperty importProps = WikiImportProperty
243: .createFrom(pageOne.getData().getProperties());
244: assertTrue(importProps.isAutoUpdate());
245: }
246:
247: public void testAutoUpdateWhenRemotePageNotModified()
248: throws Exception {
249: importer.enterChildPage(pageOne, new Date());
250: importer.exitPage();
251:
252: PageData data = pageOne.getData();
253: data.setContent("new content");
254: pageOne.commit(data);
255:
256: importer.setAutoUpdateSetting(true);
257: importer.enterChildPage(pageOne, new Date(0));
258:
259: WikiImportProperty importProps = WikiImportProperty
260: .createFrom(pageOne.getData().getProperties());
261: assertTrue(importProps.isAutoUpdate());
262: }
263:
264: private WikiPage addLocalPageWithImportProperty(
265: WikiPage parentPage, String pageName, boolean isRoot)
266: throws Exception {
267: WikiPage page = parentPage.addChildPage(pageName);
268: PageData data = page.getData();
269:
270: WikiPagePath pagePath = localRoot.getPageCrawler().getFullPath(
271: page);
272: WikiImportProperty importProps = new WikiImportProperty(
273: "http://localhost:" + FitNesseUtil.port + "/"
274: + PathParser.render(pagePath));
275: if (isRoot)
276: importProps.setRoot(true);
277: importProps.addTo(data.getProperties());
278: page.commit(data);
279:
280: return page;
281: }
282:
283: public void pageImported(WikiPage page) {
284: imports.add(page);
285: }
286:
287: public void pageImportError(WikiPage page, Exception e) {
288: errors.add(e);
289: }
290: }
|