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;
004:
005: import fitnesse.wiki.MockWikiPage;
006: import fitnesse.wiki.WikiPage;
007: import fitnesse.wikitext.widgets.*;
008: import junit.framework.TestCase;
009:
010: public class WidgetBuilderTest extends TestCase {
011: private WikiPage mockSource;
012:
013: public void setUp() throws Exception {
014: mockSource = new MockWikiPage();
015: }
016:
017: public void tearDown() throws Exception {
018: }
019:
020: public void testEmptyPage() throws Exception {
021: ParentWidget widget = new WidgetRoot(null, mockSource);
022: assertNotNull(widget);
023: assertEquals(0, widget.numberOfChildren());
024: }
025:
026: public void testSimpleText() throws Exception {
027: ParentWidget page = new WidgetRoot("Hello, World!", mockSource);
028: assertNotNull(page);
029: assertEquals(1, page.numberOfChildren());
030: WikiWidget widget = page.nextChild();
031: testWidgetClassAndText(widget, TextWidget.class,
032: "Hello, World!");
033: }
034:
035: public void testSimpleWikiWord() throws Exception {
036: ParentWidget page = new WidgetRoot("WikiWord", mockSource);
037: WikiWidget widget = page.nextChild();
038: testWidgetClassAndText(widget, WikiWordWidget.class, "WikiWord");
039: }
040:
041: public void testTextThenWikiWord() throws Exception {
042: ParentWidget page = new WidgetRoot("text WikiWord more Text",
043: mockSource);
044: assertEquals(3, page.numberOfChildren());
045: WikiWidget widget1 = page.nextChild();
046: WikiWidget widget2 = page.nextChild();
047: WikiWidget widget3 = page.nextChild();
048: testWidgetClassAndText(widget1, TextWidget.class, "text ");
049: testWidgetClassAndText(widget2, WikiWordWidget.class,
050: "WikiWord");
051: testWidgetClassAndText(widget3, TextWidget.class, " more Text");
052: }
053:
054: public void testWikiWord_Text_WikiWord() throws Exception {
055: ParentWidget page = new WidgetRoot("WikiWord more WordWiki",
056: mockSource);
057: assertEquals(3, page.numberOfChildren());
058: WikiWidget widget1 = page.nextChild();
059: WikiWidget widget2 = page.nextChild();
060: WikiWidget widget3 = page.nextChild();
061: testWidgetClassAndText(widget1, WikiWordWidget.class,
062: "WikiWord");
063: testWidgetClassAndText(widget2, TextWidget.class, " more ");
064: testWidgetClassAndText(widget3, WikiWordWidget.class,
065: "WordWiki");
066: }
067:
068: public void testItalic_text_WikiWord() throws Exception {
069: ParentWidget page = new WidgetRoot("''italic'' text WikiWord",
070: mockSource);
071: assertEquals(3, page.numberOfChildren());
072: WikiWidget widget1 = page.nextChild();
073: WikiWidget widget2 = page.nextChild();
074: WikiWidget widget3 = page.nextChild();
075: assertEquals(ItalicWidget.class, widget1.getClass());
076: testWidgetClassAndText(widget2, TextWidget.class, " text ");
077: testWidgetClassAndText(widget3, WikiWordWidget.class,
078: "WikiWord");
079: }
080:
081: public void testWikiWordInsideItalic() throws Exception {
082: testWikiWordInParentWidget("''WikiWord''", ItalicWidget.class,
083: "WikiWord", 1);
084: }
085:
086: public void testWikiWordInsideBold() throws Exception {
087: testWikiWordInParentWidget("'''WikiWord'''", BoldWidget.class,
088: "WikiWord", 1);
089: }
090:
091: public void testMultiLineWidget() throws Exception {
092: ParentWidget page = new WidgetRoot("{{{\npreformatted\n}}}",
093: mockSource);
094: assertEquals(1, page.numberOfChildren());
095: assertEquals(PreformattedWidget.class, page.nextChild()
096: .getClass());
097: }
098:
099: public void testEmailWidget() throws Exception {
100: ParentWidget page = new WidgetRoot("someone@somewhere.com",
101: mockSource);
102: assertEquals(1, page.numberOfChildren());
103: assertEquals(EmailWidget.class, page.nextChild().getClass());
104: }
105:
106: public void testHrule() throws Exception {
107: ParentWidget page = new WidgetRoot("-----", mockSource);
108: WikiWidget widget = page.nextChild();
109: assertEquals(HruleWidget.class, widget.getClass());
110: }
111:
112: public void testAnchorsMarker() throws Exception {
113: ParentWidget page = new WidgetRoot(".#name ", mockSource);
114: WikiWidget widget = page.nextChild();
115: assertEquals(AnchorMarkerWidget.class, widget.getClass());
116: }
117:
118: public void testAnchorsDeclaration() throws Exception {
119: ParentWidget page = new WidgetRoot("!anchor name ", mockSource);
120: WikiWidget widget = page.nextChild();
121: assertEquals(AnchorDeclarationWidget.class, widget.getClass());
122:
123: }
124:
125: public void testWikiWordInsideHeader() throws Exception {
126: testWikiWordInParentWidget("!1 WikiWord\n", HeaderWidget.class,
127: "WikiWord", 1);
128: }
129:
130: public void testWikiWordInsideCenter() throws Exception {
131: testWikiWordInParentWidget("!c WikiWord\n", CenterWidget.class,
132: "WikiWord", 1);
133: }
134:
135: public void testTable() throws Exception {
136: ParentWidget page = new WidgetRoot("|a|b|\n|c|d|\n", mockSource);
137: assertEquals(1, page.numberOfChildren());
138: WikiWidget widget = page.nextChild();
139: assertEquals(TableWidget.class, widget.getClass());
140: }
141:
142: public void testLineBreak() throws Exception {
143: ParentWidget page = new WidgetRoot("\n\r\n\r", mockSource);
144: assertEquals(3, page.numberOfChildren());
145: while (page.hasNextChild())
146: assertEquals(LineBreakWidget.class, page.nextChild()
147: .getClass());
148: }
149:
150: public void testList() throws Exception {
151: ParentWidget page = new WidgetRoot(" *Item1\n", mockSource);
152: assertEquals(1, page.numberOfChildren());
153: assertEquals(ListWidget.class, page.nextChild().getClass());
154: }
155:
156: public void testClasspath() throws Exception {
157: ParentWidget page = new WidgetRoot("!path something",
158: mockSource);
159: assertEquals(1, page.numberOfChildren());
160: assertEquals(ClasspathWidget.class, page.nextChild().getClass());
161: }
162:
163: public void testStrike() throws Exception {
164: testWikiWordInParentWidget("--WikiWord--", StrikeWidget.class,
165: "WikiWord", 1);
166: }
167:
168: public void testNoteWidget() throws Exception {
169: ParentWidget page = new WidgetRoot("!note something",
170: mockSource);
171: assertEquals(1, page.numberOfChildren());
172: assertEquals(NoteWidget.class, page.nextChild().getClass());
173: }
174:
175: public void testCollapsableWidget() throws Exception {
176: ParentWidget page = new WidgetRoot("!* title\ncontent\n*!\n",
177: mockSource);
178: assertEquals(1, page.numberOfChildren());
179: assertEquals(CollapsableWidget.class, page.nextChild()
180: .getClass());
181: }
182:
183: public void testNullPointerError() throws Exception {
184: String wikiText = "''\nsome text that should be in italics\n''";
185: WidgetRoot root = new WidgetRoot(wikiText, new MockWikiPage());
186:
187: try {
188: root.render();
189: } catch (Exception e) {
190: fail("should be no exception\n" + e);
191: }
192: }
193:
194: public void testVirtualWikiWidget() throws Exception {
195: ParentWidget page = new WidgetRoot(
196: "!virtualwiki http://localhost/FrontPage", mockSource);
197: assertEquals(1, page.numberOfChildren());
198: assertEquals(VirtualWikiWidget.class, page.nextChild()
199: .getClass());
200: }
201:
202: private void testWikiWordInParentWidget(String input,
203: Class expectedClass, String wikiWordText, int subChildren)
204: throws Exception {
205: ParentWidget page = new WidgetRoot(input, mockSource);
206: assertEquals(1, page.numberOfChildren());
207: WikiWidget widget = page.nextChild();
208: assertEquals(expectedClass, widget.getClass());
209:
210: ParentWidget iWidget = (ParentWidget) widget;
211: assertEquals(subChildren, iWidget.numberOfChildren());
212: WikiWidget childWidget = iWidget.nextChild();
213: testWidgetClassAndText(childWidget, WikiWordWidget.class,
214: wikiWordText);
215: }
216:
217: private void testWidgetClassAndText(WikiWidget widget,
218: Class expectedClass, String expectedText) {
219: assertEquals(expectedClass, widget.getClass());
220: if (widget instanceof TextWidget)
221: assertEquals(expectedText, ((TextWidget) widget).getText());
222: }
223: }
|