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:
007: public class WikiPagePathTest extends TestCase {
008: public WikiPagePath path;
009:
010: protected void setUp() throws Exception {
011: path = new WikiPagePath();
012: }
013:
014: public void testEmptyPath() throws Exception {
015: assertTrue(path.isEmpty());
016: assertNull(path.getFirst());
017: assertTrue(path.getRest().isEmpty());
018: }
019:
020: public void testAddOneName() throws Exception {
021: path.addName("bob");
022: assertEquals("bob", path.getFirst());
023: assertFalse(path.isEmpty());
024: assertTrue(path.getRest().isEmpty());
025: }
026:
027: public void testAddTwoNames() throws Exception {
028: path.addName("bob");
029: path.addName("martin");
030: assertFalse(path.isEmpty());
031: assertEquals("bob", path.getFirst());
032: WikiPagePath rest = path.getRest();
033: assertNotNull(rest);
034: assertEquals("martin", rest.getFirst());
035: assertTrue(rest.getRest().isEmpty());
036: }
037:
038: public void testRenderEmptyPath() throws Exception {
039: String renderedPath = PathParser.render(path);
040: assertEquals("", renderedPath);
041: }
042:
043: public void testRenderSimplePath() throws Exception {
044: path.addName("Bob");
045: String renderedPath = PathParser.render(path);
046: assertEquals("Bob", renderedPath);
047: }
048:
049: public void testRenderComplexPaths() throws Exception {
050: path.addName("Bob");
051: path.addName("Martin");
052: String renderedPath = PathParser.render(path);
053: assertEquals("Bob.Martin", renderedPath);
054: }
055:
056: public void testPop() throws Exception {
057: path.addName("Micah");
058: path.pop();
059: assertEquals("", PathParser.render(path));
060:
061: path.addName("Micah");
062: path.addName("Martin");
063: path.pop();
064: assertEquals("Micah", PathParser.render(path));
065:
066: path.pop();
067: assertEquals("", PathParser.render(path));
068: }
069:
070: public void testConstructorWithPage() throws Exception {
071: WikiPage root = InMemoryPage.makeRoot("RooT");
072: PageCrawler crawler = root.getPageCrawler();
073: WikiPagePath abcdPath = PathParser.parse("AaA.BbB.CcC.DdD");
074: WikiPagePath grandchildPath = PathParser
075: .parse("PageOne.ChildOne.GrandChildOne");
076:
077: WikiPage page1 = crawler.addPage(root, abcdPath);
078: WikiPage page2 = crawler.addPage(root, grandchildPath);
079:
080: WikiPagePath page1Path = new WikiPagePath(page1);
081: assertEquals(abcdPath, page1Path);
082: WikiPagePath page2Path = new WikiPagePath(page2);
083: assertEquals(grandchildPath, page2Path);
084: }
085:
086: public void testAppend() throws Exception {
087: WikiPagePath path = new WikiPagePath();
088: WikiPagePath a = PathParser.parse("PageA");
089: WikiPagePath pageAPath = path.append(a);
090: assertEquals("PageA", PathParser.render(pageAPath));
091:
092: WikiPagePath pageBPath = PathParser.parse("PageB");
093: WikiPagePath pageABPath = pageAPath.append(pageBPath);
094: assertEquals("PageA.PageB", PathParser.render(pageABPath));
095: }
096:
097: public void testIsAbsolute() throws Exception {
098: assertTrue(PathParser.parse(".AbsolutePage").isAbsolute());
099: assertFalse(PathParser.parse("RelativePage").isAbsolute());
100: }
101:
102: public void testGetRelativePath() throws Exception {
103: WikiPagePath somePageAbsolutePath = PathParser
104: .parse(".SomePage");
105: assertEquals("SomePage", PathParser.render(somePageAbsolutePath
106: .relativePath()));
107:
108: WikiPagePath somePagePath = PathParser.parse("SomePage");
109: assertEquals("SomePage", PathParser.render(somePagePath
110: .relativePath()));
111: }
112:
113: public void testEquals() throws Exception {
114: assertEquals(PathParser.parse("PageOne"), PathParser
115: .parse("PageOne"));
116: assertFalse(PathParser.parse("PageOne").equals(
117: PathParser.parse("PageTwo")));
118: assertFalse(PathParser.parse("PageOne").equals("a string"));
119: assertEquals(PathParser.parse("PageOne.PageTwo"), PathParser
120: .parse("PageOne.PageTwo"));
121: assertFalse(PathParser.parse("PageOne.PageTwo").equals(
122: PathParser.parse("PageOne.PageThree")));
123: assertFalse(PathParser.parse("PageOne").equals(
124: PathParser.parse(".PageOne")));
125: }
126:
127: public void testCompareTo() throws Exception {
128: WikiPagePath a = PathParser.parse("PageA");
129: WikiPagePath ab = PathParser.parse("PageA.PageB");
130: WikiPagePath b = PathParser.parse("PageB");
131: WikiPagePath aa = PathParser.parse("PageA.PageA");
132: WikiPagePath bb = PathParser.parse("PageB.PageB");
133: WikiPagePath ba = PathParser.parse("PageB.PageA");
134:
135: assertTrue(a.compareTo(a) == 0); // a == a
136: assertTrue(a.compareTo(b) != 0); // a != b
137: assertTrue(ab.compareTo(ab) == 0); // ab == ab
138: assertTrue(a.compareTo(b) == -1); // a < b
139: assertTrue(aa.compareTo(ab) == -1); // aa < ab
140: assertTrue(ba.compareTo(bb) == -1); // ba < bb
141: assertTrue(b.compareTo(a) == 1); // b > a
142: assertTrue(ab.compareTo(aa) == 1); // ab > aa
143: assertTrue(bb.compareTo(ba) == 1); // bb > ba
144: }
145:
146: public void testMakeAbsolute() throws Exception {
147: WikiPagePath p = PathParser.parse("PathOne");
148: p.makeAbsolute();
149: assertTrue(p.isAbsolute());
150:
151: WikiPagePath empty = new WikiPagePath();
152: empty.makeAbsolute();
153: assertFalse(empty.isEmpty());
154: assertTrue(empty.isAbsolute());
155: }
156:
157: public void testParentPath() throws Exception {
158: WikiPagePath path2 = new WikiPagePath();
159: assertEquals(path2, path.parentPath());
160:
161: path.addName("AbC");
162: assertEquals(path2, path.parentPath());
163:
164: path.addName("XyZ");
165: path2.addName("AbC");
166: assertEquals(path2, path.parentPath());
167: }
168:
169: public void testEquality() throws Exception {
170: WikiPagePath path1 = new WikiPagePath();
171: WikiPagePath path2 = new WikiPagePath();
172:
173: assertEquals(path1, path2);
174: assertEquals(path2, path1);
175:
176: path1.addName("AbC");
177: assertFalse(path1.equals(path2));
178: assertFalse(path2.equals(path1));
179:
180: path2.addName("AbC");
181: assertEquals(path1, path2);
182:
183: path1.addName("XyZ");
184: path2.addName("XyZ");
185: assertEquals(path1, path2);
186:
187: path1.pop();
188: assertFalse(path1.equals(path2));
189: path2.pop();
190: assertEquals(path1, path2);
191: }
192:
193: public void testStartsWith() throws Exception {
194: WikiPagePath path2 = new WikiPagePath();
195: assertTrue(path2.startsWith(path));
196:
197: path.addName("AbC");
198: assertTrue(path.startsWith(path2));
199:
200: path2.addName("AbC");
201: assertTrue(path.startsWith(path2));
202:
203: path.addName("DeF");
204: assertTrue(path.startsWith(path2));
205:
206: path2.addName("XyZ");
207: assertFalse(path.startsWith(path2));
208:
209: path2.pop();
210: path2.addName("DeF");
211: assertTrue(path.startsWith(path2));
212:
213: path2.addName("XyZ");
214: assertFalse(path.startsWith(path2));
215: }
216:
217: public void testWithNameAdded() throws Exception {
218: WikiPagePath path2 = new WikiPagePath();
219: path2.addName("AbC");
220: WikiPagePath path3 = path.withNameAdded("AbC");
221: assertEquals(path2, path3);
222: assertNotSame(path3, path2);
223: assertNotSame(path3, path);
224: }
225: }
|