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:
004: package fitnesse.wiki;
005:
006: import junit.framework.TestCase;
007: import java.io.File;
008: import java.util.*;
009: import fitnesse.util.FileUtil;
010:
011: public class FileSystemPageTest extends TestCase {
012: private static final String defaultPath = "./teststorage";
013:
014: private static final File base = new File(defaultPath);
015:
016: private FileSystemPage root;
017:
018: private PageCrawler crawler;
019:
020: public void setUp() throws Exception {
021: FileUtil.deleteFileSystemDirectory(base);
022: createFileSystemDirectory(base);
023: root = (FileSystemPage) FileSystemPage.makeRoot(defaultPath,
024: "RooT");
025: crawler = root.getPageCrawler();
026: }
027:
028: public void tearDown() throws Exception {
029: FileUtil.deleteFileSystemDirectory(base);
030: FileUtil.deleteFileSystemDirectory("RooT");
031: }
032:
033: public static void createFileSystemDirectory(File current) {
034: current.mkdir();
035: }
036:
037: public void testCreateBase() throws Exception {
038: FileSystemPage levelA = (FileSystemPage) crawler.addPage(root,
039: PathParser.parse("PageA"), "");
040: assertEquals("./teststorage/RooT/PageA", levelA
041: .getFileSystemPath());
042: assertTrue(new File(defaultPath + "/RooT/PageA").exists());
043: }
044:
045: public void testTwoLevel() throws Exception {
046: WikiPage levelA = crawler.addPage(root, PathParser
047: .parse("PageA"));
048: crawler.addPage(levelA, PathParser.parse("PageB"));
049: assertTrue(new File(defaultPath + "/RooT/PageA/PageB").exists());
050: }
051:
052: public void testContent() throws Exception {
053: WikiPagePath rootPath = PathParser.parse("root");
054: assertEquals("", crawler.getPage(root, rootPath).getData()
055: .getContent());
056: crawler.addPage(root, PathParser.parse("AaAa"), "A content");
057: assertEquals("A content", root.getChildPage("AaAa").getData()
058: .getContent());
059: WikiPagePath bPath = PathParser.parse("AaAa.BbBb");
060: crawler.addPage(root, bPath, "B content");
061: assertEquals("B content", crawler.getPage(root, bPath)
062: .getData().getContent());
063: }
064:
065: public void testBigContent() throws Exception {
066: StringBuffer buffer = new StringBuffer();
067: for (int i = 0; i < 1000; i++)
068: buffer.append("abcdefghijklmnopqrstuvwxyz");
069: crawler.addPage(root, PathParser.parse("BigPage"), buffer
070: .toString());
071: String content = root.getChildPage("BigPage").getData()
072: .getContent();
073: assertTrue(buffer.toString().equals(content));
074: }
075:
076: public void testPageExists() throws Exception {
077: crawler.addPage(root, PathParser.parse("AaAa"), "A content");
078: assertTrue(root.hasChildPage("AaAa"));
079: }
080:
081: public void testGetChidren() throws Exception {
082: crawler.addPage(root, PathParser.parse("AaAa"), "A content");
083: crawler.addPage(root, PathParser.parse("BbBb"), "B content");
084: crawler.addPage(root, PathParser.parse("CcCc"), "C content");
085: new File(defaultPath + "/root/someOtherDir").mkdir();
086: List children = root.getChildren();
087: assertEquals(3, children.size());
088: for (Iterator iterator = children.iterator(); iterator
089: .hasNext();) {
090: WikiPage child = (WikiPage) iterator.next();
091: String name = child.getName();
092: boolean isOk = "AaAa".equals(name) || "BbBb".equals(name)
093: || "CcCc".equals(name);
094: assertTrue("WikiPAge is not a valid one: " + name, isOk);
095: }
096: }
097:
098: public void testRemovePage() throws Exception {
099: WikiPage levelOne = crawler.addPage(root, PathParser
100: .parse("LevelOne"));
101: crawler.addPage(levelOne, PathParser.parse("LevelTwo"));
102: levelOne.removeChildPage("LevelTwo");
103: File fileOne = new File(defaultPath + "/RooT/LevelOne");
104: File fileTwo = new File(defaultPath + "/RooT/LevelOne/LevelTwo");
105: assertTrue(fileOne.exists());
106: assertFalse(fileTwo.exists());
107: }
108:
109: public void testDelTree() throws Exception {
110: FileSystemPage fsRoot = (FileSystemPage) FileSystemPage
111: .makeRoot(".", "RooT");
112: WikiPage levelOne = crawler.addPage(fsRoot, PathParser
113: .parse("LevelOne"));
114: crawler.addPage(levelOne, PathParser.parse("LevelTwo"));
115: File childOne = new File("RooT/LevelOne");
116: File childTwo = new File("RooT/LevelOne/LevelTwo");
117: assertTrue(childOne.exists());
118: FileUtil.deleteFileSystemDirectory(childOne);
119: assertFalse(childTwo.exists());
120: assertFalse(childOne.exists());
121: }
122:
123: public void testDefaultAttributes() throws Exception {
124: WikiPage page = crawler.addPage(root, PathParser
125: .parse("PageOne"), "something");
126: assertTrue(page.getData().hasAttribute(WikiPage.ACTION_EDIT));
127: }
128:
129: public void testPersistentAttributes() throws Exception {
130: crawler.addPage(root, PathParser.parse("FrontPage"));
131: WikiPage createdPage = root.getChildPage("FrontPage");
132: PageData data = createdPage.getData();
133: data.setAttribute(WikiPage.ACTION_TEST, "true");
134: data.setAttribute(WikiPage.ACTION_SEARCH, "true");
135: createdPage.commit(data);
136: assertTrue(data.hasAttribute(WikiPage.ACTION_TEST));
137: assertTrue(data.hasAttribute(WikiPage.ACTION_SEARCH));
138: WikiPage page = root.getChildPage("FrontPage");
139: assertTrue(page.getData().hasAttribute(WikiPage.ACTION_TEST));
140: assertTrue(page.getData().hasAttribute(WikiPage.ACTION_SEARCH));
141: }
142:
143: public void testCachedInfo() throws Exception {
144: WikiPage page1 = crawler.addPage(root, PathParser
145: .parse("PageOne"), "page one");
146: WikiPage child1 = crawler.addPage(page1, PathParser
147: .parse("ChildOne"), "child one");
148: WikiPage child = page1.getChildPage("ChildOne");
149: assertSame(child1, child);
150: }
151:
152: public void testCanFindExistingPages() throws Exception {
153: crawler.addPage(root, PathParser.parse("FrontPage"),
154: "front page");
155: WikiPage newRoot = FileSystemPage.makeRoot(defaultPath, "RooT");
156: assertNotNull(newRoot.getChildPage("FrontPage"));
157: }
158:
159: public void testGetPath() throws Exception {
160: assertEquals(defaultPath + "/RooT", root.getFileSystemPath());
161: }
162:
163: public void testLastModifiedTime() throws Exception {
164: WikiPage page = crawler.addPage(root, PathParser
165: .parse("SomePage"), "some text");
166: page.commit(page.getData());
167: Date now = new Date();
168: Date lastModified = page.getData().getLastModificationTime();
169: assertTrue(now.getTime() - lastModified.getTime() <= 1000);
170: }
171:
172: public void testUnicodeCharacters() throws Exception {
173: WikiPage page = crawler.addPage(root, PathParser
174: .parse("SomePage"), "\uba80\uba81\uba82\uba83");
175: PageData data = page.getData();
176: assertEquals("\uba80\uba81\uba82\uba83", data.getContent());
177: }
178:
179: public void XtestUnicodeInVersions() throws Exception {
180: // FIXME
181: WikiPage page = crawler.addPage(root, PathParser
182: .parse("SomePage"), "\uba80\uba81\uba82\uba83");
183: PageData data = page.getData();
184: data.setContent("blah");
185: VersionInfo info = page.commit(data);
186:
187: data = page.getDataVersion(info.getName());
188: String expected = "\uba80\uba81\uba82\uba83";
189: String actual = data.getContent();
190:
191: assertEquals(expected, actual);
192: }
193:
194: public void testLoadChildrenWhenPageIsDeletedManualy()
195: throws Exception {
196: WikiPage page = crawler.addPage(root, PathParser
197: .parse("TestPage"));
198: page.getChildren();
199: FileUtil.deleteFileSystemDirectory(((FileSystemPage) page)
200: .getFileSystemPath());
201: try {
202: page.getChildren();
203: } catch (Exception e) {
204: fail("No Exception should be thrown");
205: }
206: }
207:
208: public void XtestVersionedPropertiedLoadedProperly()
209: throws Exception {
210: // FIXME
211: WikiPage page = crawler.addPage(root, PathParser
212: .parse("TestPage"));
213: PageData data = page.getData();
214: WikiPageProperties oldProps = data.getProperties();
215: WikiPageProperties props = new WikiPageProperties();
216: props.set("MyProp", "my value");
217: data.setProperties(props);
218: page.commit(data);
219:
220: data.setProperties(oldProps);
221: VersionInfo version = page.commit(data);
222:
223: PageData versionedData = page.getDataVersion(version.getName());
224: WikiPageProperties versionedProps = versionedData
225: .getProperties();
226:
227: assertTrue(versionedProps.has("MyProp"));
228: assertEquals("my value", versionedProps.get("MyProp"));
229: }
230: }
|