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.wikitext.widgets;
004:
005: import fitnesse.testutil.FitNesseUtil;
006: import fitnesse.wiki.*;
007: import fitnesse.html.HtmlElement;
008:
009: public class ContentsWidgetTest extends AbstractWidget {
010: private WikiPage root;
011:
012: private WikiPage parent;
013:
014: private PageCrawler crawler;
015:
016: private String endl = HtmlElement.endl;
017:
018: public void setUp() throws Exception {
019: root = InMemoryPage.makeRoot("RooT");
020: crawler = root.getPageCrawler();
021: parent = crawler.addPage(root, PathParser.parse("ParenT"),
022: "parent");
023: crawler.addPage(root, PathParser.parse("ParentTwo"),
024: "parent two");
025: crawler
026: .addPage(parent, PathParser.parse("ChildOne"),
027: "content");
028: crawler
029: .addPage(parent, PathParser.parse("ChildTwo"),
030: "content");
031: }
032:
033: public void tearDown() throws Exception {
034: }
035:
036: public void testMatch() throws Exception {
037: assertMatchEquals("!contents\n", "!contents");
038: assertMatchEquals("!contents -R\n", "!contents -R");
039: assertMatchEquals("!contents\r", "!contents");
040: assertMatchEquals("!contents -R\r", "!contents -R");
041: assertMatchEquals(" !contents\n", null);
042: assertMatchEquals(" !contents -R\n", null);
043: assertMatchEquals("!contents zap\n", null);
044: assertMatchEquals("!contents \n", "!contents ");
045: }
046:
047: public void testNoGrandchildren() throws Exception {
048: assertEquals(getHtmlWithNoHierarchy(), renderNormalTOCWidget());
049: assertEquals(getHtmlWithNoHierarchy(),
050: renderHierarchicalTOCWidget());
051: }
052:
053: public void testWithGrandchildren() throws Exception {
054: addGrandChild();
055: assertEquals(getHtmlWithNoHierarchy(), renderNormalTOCWidget());
056: assertEquals(getHtmlWithGrandChild(),
057: renderHierarchicalTOCWidget());
058: }
059:
060: public void testWithGreatGrandchildren() throws Exception {
061: addGrandChild();
062: addGreatGrandChild();
063: assertEquals(getHtmlWithNoHierarchy(), renderNormalTOCWidget());
064: assertEquals((Object) getHtmlWithGreatGrandChild(),
065: (Object) renderHierarchicalTOCWidget());
066: }
067:
068: private void addGrandChild() throws Exception {
069: crawler.addPage(parent.getChildPage("ChildOne"), PathParser
070: .parse("GrandChild"), "content");
071: }
072:
073: private void addGreatGrandChild() throws Exception {
074: crawler.addPage(parent.getChildPage("ChildOne").getChildPage(
075: "GrandChild"), PathParser.parse("GreatGrandChild"),
076: "content");
077: }
078:
079: public void testTocOnRoot() throws Exception {
080: ContentsWidget widget = new ContentsWidget(
081: new WidgetRoot(root), "!contents\n");
082: String html = widget.render();
083: assertHasRegexp("ParenT", html);
084: assertHasRegexp("ParentTwo", html);
085: }
086:
087: public void testDisplaysVirtualChildren() throws Exception {
088: WikiPage page = crawler.addPage(root, PathParser
089: .parse("VirtualParent"));
090: PageData data = page.getData();
091: data.setAttribute(WikiPageProperties.VIRTUAL_WIKI_ATTRIBUTE,
092: "http://localhost:" + FitNesseUtil.port + "/ParenT");
093: page.commit(data);
094: try {
095: FitNesseUtil.startFitnesse(root);
096: ContentsWidget widget = new ContentsWidget(new WidgetRoot(
097: page), "!contents\n");
098: String html = widget.render();
099: assertEquals(virtualChildrenHtml(), html);
100: } finally {
101: FitNesseUtil.stopFitnesse();
102: }
103: }
104:
105: public void testIsNotHierarchical() throws Exception {
106: assertFalse(new ContentsWidget(new WidgetRoot(parent),
107: "!contents\n").isRecursive());
108: }
109:
110: public void testIsHierarchical() throws Exception {
111: assertTrue(new ContentsWidget(new WidgetRoot(parent),
112: "!contents -R\n").isRecursive());
113: }
114:
115: private String renderNormalTOCWidget() throws Exception {
116: return new ContentsWidget(new WidgetRoot(parent), "!contents\n")
117: .render();
118: }
119:
120: private String renderHierarchicalTOCWidget() throws Exception {
121: return new ContentsWidget(new WidgetRoot(parent),
122: "!contents -R\n").render();
123: }
124:
125: private String getHtmlWithNoHierarchy() {
126: return "<div class=\"toc1\"><ul><li>" //
127: + "<a href=\"ParenT.ChildOne\">ChildOne</a>"
128: + "</li>"
129: + endl //
130: + "<li><a href=\"ParenT.ChildTwo\">ChildTwo</a>"
131: + "</li>" + endl //
132: + "</ul>" + endl //
133: + "</div>" + endl;
134: }
135:
136: private String getHtmlWithGrandChild() {
137: return "<div class=\"toc1\"><ul><li>" //
138: + "<a href=\"ParenT.ChildOne\">ChildOne</a>"
139: + "<div class=\"toc2\"><ul><li>" //
140: + "<a href=\"ParenT.ChildOne.GrandChild\">GrandChild</a>"
141: + "</li>"
142: + endl //
143: + "</ul>"
144: + endl //
145: + "</div>"
146: + endl //
147: + "</li>"
148: + endl //
149: + "<li><a href=\"ParenT.ChildTwo\">ChildTwo</a>"
150: + "</li>" + endl //
151: + "</ul>" + endl //
152: + "</div>" + endl;
153: }
154:
155: private String getHtmlWithGreatGrandChild() {
156: return "<div class=\"toc1\">" //
157: + "<ul>" //
158: + "<li>" //
159: + "<a href=\"ParenT.ChildOne\">ChildOne</a>"
160: + "<div class=\"toc2\">" //
161: + "<ul>" //
162: + "<li><a href=\"ParenT.ChildOne.GrandChild\">GrandChild</a>"
163: + "<div class=\"toc3\">" //
164: + "<ul>" //
165: + "<li><a href=\"ParenT.ChildOne.GrandChild.GreatGrandChild\">GreatGrandChild</a>"
166: + "</li>"
167: + endl //
168: + "</ul>"
169: + endl //
170: + "</div>"
171: + endl //
172: + "</li>"
173: + endl //
174: + "</ul>"
175: + endl //
176: + "</div>"
177: + endl //
178: + "</li>"
179: + endl //
180: + "<li><a href=\"ParenT.ChildTwo\">ChildTwo</a>"
181: + "</li>" + endl //
182: + "</ul>" + endl //
183: + "</div>" + endl;
184: }
185:
186: private String virtualChildrenHtml() {
187: return "<div class=\"toc1\">" //
188: + "<ul>" //
189: + "<li>" //
190: + "<a href=\"VirtualParent.ChildOne\">" //
191: + "<i>ChildOne</i>" + endl //
192: + "</a>" + "</li>" + endl //
193: + "<li>" //
194: + "<a href=\"VirtualParent.ChildTwo\">" //
195: + "<i>ChildTwo</i>" + endl //
196: + "</a>" + "</li>" + endl //
197: + "</ul>" + endl //
198: + "</div>" + endl;
199: }
200:
201: protected String getRegexp() {
202: return ContentsWidget.REGEXP;
203: }
204: }
|