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.responders;
004:
005: import fitnesse.wiki.*;
006: import fitnesse.http.*;
007: import fitnesse.*;
008: import fitnesse.testutil.AbstractRegex;
009: import fitnesse.util.FileUtil;
010:
011: import java.io.*;
012:
013: public class SerializedPageResponderTest extends AbstractRegex {
014: private final String RootPath = "TestRooT";
015:
016: private PageCrawler crawler;
017:
018: private WikiPage root;
019:
020: private MockRequest request;
021:
022: public void setUp() throws Exception {
023: root = InMemoryPage.makeRoot("RooT");
024: crawler = root.getPageCrawler();
025: request = new MockRequest();
026: }
027:
028: public void tearDown() throws Exception {
029: FileUtil.deleteFileSystemDirectory(RootPath);
030: }
031:
032: public void testWithInMemory() throws Exception {
033: Object obj = doSetUpWith(root, "bones");
034: doTestWith(obj);
035:
036: }
037:
038: public void testWithFileSystem() throws Exception {
039: root = FileSystemPage.makeRoot(".", RootPath);
040: Object obj = doSetUpWith(root, "bones");
041: FileUtil.deleteFileSystemDirectory(RootPath);
042: doTestWith(obj);
043: }
044:
045: private void doTestWith(Object obj) throws Exception {
046: assertNotNull(obj);
047: assertEquals(true, obj instanceof ProxyPage);
048: WikiPage page = (WikiPage) obj;
049: assertEquals("PageOne", page.getName());
050: }
051:
052: private Object doSetUpWith(WikiPage root, String proxyType)
053: throws Exception {
054: WikiPage page1 = crawler.addPage(root, PathParser
055: .parse("PageOne"), "this is page one");
056: PageData data = page1.getData();
057: data.setAttribute("Attr1", "true");
058: page1.commit(data);
059: crawler.addPage(page1, PathParser.parse("ChildOne"),
060: "this is child one");
061:
062: request.addInput("type", proxyType);
063: request.setResource("PageOne");
064:
065: return getObject(root, request);
066: }
067:
068: private Object getObject(WikiPage root, MockRequest request)
069: throws Exception {
070: Responder responder = new SerializedPageResponder();
071: SimpleResponse response = (SimpleResponse) responder
072: .makeResponse(new FitNesseContext(root), request);
073:
074: ObjectInputStream ois = new ObjectInputStream(
075: new ByteArrayInputStream(response.getContentBytes()));
076: Object obj = ois.readObject();
077: return obj;
078: }
079:
080: public void testGetContentAndAttributes() throws Exception {
081: Object obj = doSetUpWith(root, "meat");
082: assertNotNull(obj);
083: assertTrue(obj instanceof PageData);
084: PageData data = (PageData) obj;
085:
086: assertEquals("this is page one", data.getContent());
087:
088: WikiPageProperties props = data.getProperties();
089: assertEquals("true", props.get("Attr1"));
090: }
091:
092: public void testGetVersionOfPageData() throws Exception {
093: WikiPage page = crawler.addPage(root, PathParser
094: .parse("PageOne"), "some content");
095: VersionInfo commitRecord = page.commit(page.getData());
096:
097: request.addInput("type", "meat");
098: request.addInput("version", commitRecord.getName());
099: request.setResource("PageOne");
100:
101: Object obj = getObject(root, request);
102: assertEquals(PageData.class, obj.getClass());
103: PageData data = (PageData) obj;
104: assertEquals("some content", data.getContent());
105: }
106:
107: public void testGetPageHieratchyAsXml() throws Exception {
108: crawler.addPage(root, PathParser.parse("PageOne"));
109: crawler.addPage(root, PathParser.parse("PageOne.ChildOne"));
110: crawler.addPage(root, PathParser.parse("PageTwo"));
111:
112: request.setResource("root");
113: request.addInput("type", "pages");
114: Responder responder = new SerializedPageResponder();
115: SimpleResponse response = (SimpleResponse) responder
116: .makeResponse(new FitNesseContext(root), request);
117: String xml = response.getContent();
118:
119: assertEquals("text/xml", response.getContentType());
120: assertSubString("<name>PageOne</name>", xml);
121: assertSubString("<name>PageTwo</name>", xml);
122: assertSubString("<name>ChildOne</name>", xml);
123: }
124:
125: public void testGetDataAsHtml() throws Exception {
126: crawler.addPage(root, PathParser.parse("TestPageOne"),
127: "test page");
128:
129: request.setResource("TestPageOne");
130: request.addInput("type", "data");
131: Responder responder = new SerializedPageResponder();
132: SimpleResponse response = (SimpleResponse) responder
133: .makeResponse(new FitNesseContext(root), request);
134: String xml = response.getContent();
135:
136: assertEquals("text/xml", response.getContentType());
137: assertSubString("test page", xml);
138: assertSubString("<Test/>", xml);
139: }
140: }
|