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.wiki;
004:
005: import junit.framework.TestCase;
006: import fitnesse.components.FitNesseTraversalListener;
007: import java.util.*;
008:
009: public class PageCrawlerTest extends TestCase implements
010: FitNesseTraversalListener {
011: private WikiPage root;
012:
013: private WikiPage page1;
014:
015: private WikiPage page2;
016:
017: private WikiPage child1;
018:
019: private WikiPage grandChild1;
020:
021: private PageCrawler crawler;
022:
023: private WikiPagePath page1Path;
024:
025: private WikiPagePath child1FullPath;
026:
027: private WikiPagePath page2Path;
028:
029: private WikiPagePath grandChild1FullPath;
030:
031: public void setUp() throws Exception {
032: root = InMemoryPage.makeRoot("RooT");
033: crawler = new PageCrawlerImpl();
034:
035: page1Path = PathParser.parse("PageOne");
036: page2Path = PathParser.parse("PageTwo");
037: child1FullPath = PathParser.parse("PageOne.ChildOne");
038: grandChild1FullPath = PathParser
039: .parse("PageOne.ChildOne.GrandChildOne");
040: page1 = crawler.addPage(root, page1Path);
041: page2 = crawler.addPage(root, page2Path);
042: child1 = crawler.addPage(page1, PathParser.parse("ChildOne"));
043: grandChild1 = crawler.addPage(child1, PathParser
044: .parse("GrandChildOne"));
045: }
046:
047: public void testPageExists() throws Exception {
048: assertTrue(crawler.pageExists(page1, PathParser
049: .parse("ChildOne")));
050: assertFalse(crawler.pageExists(page1, PathParser
051: .parse("BlahBlah")));
052: }
053:
054: public void testPageExistsUsingPath() throws Exception {
055: assertTrue(crawler.pageExists(page1, PathParser
056: .parse("ChildOne")));
057: assertTrue(crawler.pageExists(root, child1FullPath));
058: assertTrue(crawler.pageExists(root, grandChild1FullPath));
059: assertTrue(crawler.pageExists(root, PathParser
060: .parse(".PageOne")));
061: assertTrue(crawler.pageExists(root, PathParser
062: .parse(".PageOne.ChildOne.GrandChildOne")));
063:
064: assertFalse(crawler.pageExists(page1, PathParser
065: .parse("BlahBlah")));
066: assertFalse(crawler.pageExists(page1, PathParser
067: .parse("PageOne.BlahBlah")));
068: }
069:
070: public void testGetPage() throws Exception {
071: assertEquals(null, crawler.getPage(page1, page1Path));
072: assertEquals(page1, crawler.getPage(root, page1Path));
073: assertEquals(page2, crawler.getPage(root, page2Path));
074: assertEquals(page1, crawler.getPage(page1, PathParser
075: .parse(".PageOne")));
076: assertEquals(page1, crawler.getPage(grandChild1, PathParser
077: .parse(".PageOne")));
078: assertEquals(grandChild1, crawler.getPage(page1, PathParser
079: .parse("ChildOne.GrandChildOne")));
080: assertEquals(root, crawler.getPage(root, PathParser
081: .parse("root")));
082: assertEquals(root, crawler.getPage(root, PathParser.parse(".")));
083: assertEquals(root, crawler.getPage(root, PathParser.parse("")));
084: }
085:
086: public void testGetFullPath() throws Exception {
087: assertEquals(page1Path, crawler.getFullPath(page1));
088: assertEquals(page2Path, crawler.getFullPath(page2));
089: assertEquals(child1FullPath, crawler.getFullPath(child1));
090: assertEquals(grandChild1FullPath, crawler
091: .getFullPath(grandChild1));
092: assertEquals(PathParser.parse(""), crawler.getFullPath(root));
093: }
094:
095: public void testGetAbsolutePathForChild() throws Exception {
096: WikiPagePath somePagePath = PathParser.parse("SomePage");
097: WikiPagePath somePageFullPath = crawler.getFullPathOfChild(
098: root, somePagePath);
099: assertEquals("SomePage", PathParser.render(somePageFullPath));
100:
101: WikiPagePath pageOnePath = page1Path;
102: WikiPagePath pageOneFullPath = crawler.getFullPathOfChild(root,
103: pageOnePath);
104: assertEquals("PageOne", PathParser.render(pageOneFullPath));
105:
106: WikiPagePath SomePageChildFullPath = crawler
107: .getFullPathOfChild(child1, somePagePath);
108: assertEquals("PageOne.ChildOne.SomePage", PathParser
109: .render(SomePageChildFullPath));
110:
111: WikiPagePath otherPagePath = PathParser
112: .parse("SomePage.OtherPage");
113: WikiPagePath otherPageFullPath = crawler.getFullPathOfChild(
114: root, otherPagePath);
115: assertEquals("SomePage.OtherPage", PathParser
116: .render(otherPageFullPath));
117:
118: WikiPagePath somePageAbsolutePath = PathParser
119: .parse(".SomePage");
120: WikiPagePath somePageAbsoluteFullPath = crawler
121: .getFullPathOfChild(child1, somePageAbsolutePath);
122: assertEquals("SomePage", PathParser
123: .render(somePageAbsoluteFullPath));
124: }
125:
126: public void testAddPage() throws Exception {
127: WikiPage page = crawler.addPage(page1, PathParser
128: .parse("SomePage"));
129: assertEquals(PathParser.parse("PageOne.SomePage"), crawler
130: .getFullPath(page));
131: assertEquals(page1, page.getParent());
132: }
133:
134: public void testRecursiveAddbyName() throws Exception {
135: crawler.addPage(root, PathParser.parse("AaAa"), "its content");
136: assertTrue(root.hasChildPage("AaAa"));
137:
138: crawler.addPage(root, PathParser.parse("AaAa.BbBb"), "floop");
139: assertTrue(crawler.pageExists(root, PathParser
140: .parse("AaAa.BbBb")));
141: assertEquals("floop", crawler.getPage(root,
142: PathParser.parse("AaAa.BbBb")).getData().getContent());
143: }
144:
145: public void testAddChildPageWithMissingParent() throws Exception {
146: WikiPage page = crawler.addPage(root, PathParser
147: .parse("WikiMail.BadSubject0123"), "");
148: assertNotNull(page);
149: assertEquals("BadSubject0123", page.getName());
150: assertEquals(PathParser.parse("WikiMail.BadSubject0123"),
151: crawler.getFullPath(page));
152: }
153:
154: public void testGetRelativePageName() throws Exception {
155: assertEquals("PageOne", crawler.getRelativeName(root, page1));
156: assertEquals("PageOne.ChildOne", crawler.getRelativeName(root,
157: child1));
158: assertEquals("ChildOne", crawler.getRelativeName(page1, child1));
159: assertEquals("GrandChildOne", crawler.getRelativeName(child1,
160: grandChild1));
161: assertEquals("ChildOne.GrandChildOne", crawler.getRelativeName(
162: page1, grandChild1));
163: }
164:
165: public void testIsRoot() throws Exception {
166: assertTrue(crawler.isRoot(root));
167: WikiPage page = crawler.addPage(root, page1Path);
168: assertFalse(crawler.isRoot(page));
169: }
170:
171: Set traversedPages = new HashSet();
172:
173: public void testTraversal() throws Exception {
174: crawler.traverse(root, this );
175: assertEquals(5, traversedPages.size());
176: assertTrue(traversedPages.contains("PageOne"));
177: assertTrue(traversedPages.contains("ChildOne"));
178: }
179:
180: public void processPage(WikiPage page) throws Exception {
181: traversedPages.add(page.getName());
182: }
183:
184: public String getSearchPattern() throws Exception {
185: return "blah";
186: }
187:
188: public void testdoesntTraverseSymbolicPages() throws Exception {
189: PageData data = page1.getData();
190: data.getProperties().addSymbolicLink("SymLink",
191: PathParser.parse("PageTwo"));
192: page1.commit(data);
193:
194: crawler.traverse(root, this );
195: assertEquals(5, traversedPages.size());
196:
197: assertFalse(traversedPages.contains("SymLink"));
198: }
199: }
|