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 java.io.*;
006: import java.util.*;
007: import fitnesse.testutil.AbstractRegex;
008: import fitnesse.util.XmlUtil;
009: import org.w3c.dom.Document;
010:
011: public class PageXmlizerTest extends AbstractRegex {
012: private PageXmlizer xmlizer;
013:
014: private WikiPage root;
015:
016: private PageCrawler crawler;
017:
018: public void setUp() throws Exception {
019: xmlizer = new PageXmlizer();
020: root = InMemoryPage.makeRoot("RooT");
021: crawler = root.getPageCrawler();
022: }
023:
024: public void tearDown() throws Exception {
025: }
026:
027: public void testXmlizeOneWikiPage() throws Exception {
028: Document doc = xmlizer.xmlize(root);
029: String value = XmlUtil.xmlAsString(doc);
030:
031: assertSubString("<page>", value);
032: assertSubString("</page>", value);
033: assertSubString("<name>RooT</name>", value);
034: }
035:
036: public void testDeXmlizeOneWikiPage() throws Exception {
037: Document doc = xmlizer.xmlize(root);
038: xmlizer.deXmlize(doc, root, new MockXmlizerPageHandler());
039:
040: List children = root.getChildren();
041: assertEquals(1, children.size());
042: WikiPage page = (WikiPage) children.get(0);
043: assertEquals("RooT", page.getName());
044: }
045:
046: public void testXmlizeTwoPages() throws Exception {
047: root.addChildPage("PageOne");
048: Document doc = xmlizer.xmlize(root);
049: String value = XmlUtil.xmlAsString(doc);
050:
051: assertSubString("<name>RooT</name>", value);
052: assertSubString("<name>PageOne</name>", value);
053: }
054:
055: public void testDeXmlizingTwoPages() throws Exception {
056: root.addChildPage("PageOne");
057: xmlizer.deXmlize(xmlizer.xmlize(root), root,
058: new MockXmlizerPageHandler());
059:
060: assertEquals(2, root.getChildren().size());
061: WikiPage marshaledRoot = root.getChildPage("RooT");
062: assertNotNull(marshaledRoot);
063:
064: assertEquals(1, marshaledRoot.getChildren().size());
065: assertNotNull(marshaledRoot.getChildPage("PageOne"));
066: }
067:
068: public void testXmlizingAnEntireTree() throws Exception {
069: makeFamilyOfPages();
070: Document doc = xmlizer.xmlize(root);
071:
072: String value = XmlUtil.xmlAsString(doc);
073:
074: assertSubString("PageA", value);
075: assertSubString("ChildOneA", value);
076: assertSubString("GrandChildA", value);
077: assertSubString("GreatGrandChildA", value);
078: assertSubString("ChildTwoA", value);
079: assertSubString("GrandChildTwoA", value);
080: assertSubString("PageB", value);
081: assertSubString("ChildOneB", value);
082: assertSubString("GrandChildB", value);
083: assertSubString("PageC", value);
084: }
085:
086: public void testDeXmlizingEntireTree() throws Exception {
087: makeFamilyOfPages();
088: xmlizer.deXmlize(xmlizer.xmlize(root), root,
089: new MockXmlizerPageHandler());
090:
091: assertEquals(4, root.getChildren().size());
092: WikiPage marshaledRoot = root.getChildPage("RooT");
093: assertEquals(3, marshaledRoot.getChildren().size());
094: WikiPage pageA = marshaledRoot.getChildPage("PageA");
095: assertEquals(2, pageA.getChildren().size());
096: WikiPage childOneA = pageA.getChildPage("ChildOneA");
097: WikiPage grandChildA = childOneA.getChildPage("GrandChildA");
098: WikiPage greatGrandChildA = grandChildA
099: .getChildPage("GreatGrandChildA");
100: assertNotNull(greatGrandChildA);
101:
102: assertNotNull(crawler.getPage(root, PathParser
103: .parse("RooT.PageB.ChildOneB.GrandChildB")));
104: assertNotNull(crawler.getPage(root, PathParser
105: .parse("RooT.PageC")));
106: }
107:
108: public void testDeXmlizeEntireTreeTwice() throws Exception {
109: makeFamilyOfPages();
110: Document doc = xmlizer.xmlize(root);
111: xmlizer.deXmlize(doc, root, new MockXmlizerPageHandler());
112: xmlizer.deXmlize(doc, root, new MockXmlizerPageHandler());
113:
114: assertEquals(4, root.getChildren().size());
115: WikiPage marshaledRoot = root.getChildPage("RooT");
116: assertEquals(3, marshaledRoot.getChildren().size());
117: WikiPage pageA = marshaledRoot.getChildPage("PageA");
118: assertEquals(2, pageA.getChildren().size());
119: }
120:
121: public void testDeXmlizeSkippingRootLevel() throws Exception {
122: makeFamilyOfPages();
123: WikiPage pageC = root.getChildPage("PageC");
124: xmlizer.deXmlizeSkippingRootLevel(xmlizer.xmlize(root), pageC,
125: new MockXmlizerPageHandler());
126:
127: assertEquals(3, pageC.getChildren().size());
128: WikiPage pageA = pageC.getChildPage("PageA");
129: assertEquals(2, pageA.getChildren().size());
130: WikiPage childOneA = pageA.getChildPage("ChildOneA");
131: WikiPage grandChildA = childOneA.getChildPage("GrandChildA");
132: WikiPage greatGrandChildA = grandChildA
133: .getChildPage("GreatGrandChildA");
134: assertNotNull(greatGrandChildA);
135:
136: assertNotNull(crawler.getPage(pageC, PathParser
137: .parse("PageB.ChildOneB.GrandChildB")));
138: assertNotNull(crawler.getPage(pageC, PathParser.parse("PageC")));
139: }
140:
141: public void testUsageOfHandler() throws Exception {
142: makeFamilyOfPages();
143: MockXmlizerPageHandler handler = new MockXmlizerPageHandler();
144: xmlizer.deXmlize(xmlizer.xmlize(root), root, handler);
145:
146: assertEquals(11, handler.adds.size());
147: assertTrue(handler.adds.contains("RooT"));
148: assertTrue(handler.adds.contains("PageA"));
149: assertTrue(handler.adds.contains("ChildOneA"));
150: assertTrue(handler.adds.contains("GrandChildA"));
151: assertTrue(handler.adds.contains("GreatGrandChildA"));
152: assertTrue(handler.adds.contains("ChildTwoA"));
153: assertTrue(handler.adds.contains("GrandChildTwoA"));
154: assertTrue(handler.adds.contains("PageB"));
155: assertTrue(handler.adds.contains("ChildOneB"));
156: assertTrue(handler.adds.contains("GrandChildB"));
157: assertTrue(handler.adds.contains("PageC"));
158:
159: assertEquals(11, handler.exits);
160: }
161:
162: private void makeFamilyOfPages() throws Exception {
163: addPage("PageA", "page a");
164: addPage("PageA.ChildOneA", "child one a");
165: addPage("PageA.ChildOneA.GrandChildA", "grand child a");
166: addPage("PageA.ChildOneA.GrandChildA.GreatGrandChildA",
167: "great grand child a");
168: addPage("PageA.ChildTwoA", "child two a");
169: addPage("PageA.ChildTwoA.GrandChildTwoA", "grand child two a");
170:
171: addPage("PageB", "page b");
172: addPage("PageB.ChildOneB", "child one b");
173: addPage("PageB.ChildOneB.GrandChildB", "grand child b");
174:
175: addPage("PageC", "page c");
176: }
177:
178: private void addPage(String path, String content) throws Exception {
179: crawler.addPage(root, PathParser.parse(path), content);
180: }
181:
182: public void testXmlizingData() throws Exception {
183: PageData data = new PageData(root);
184: data.setContent("this is some content.");
185: WikiPageProperties properties = data.getProperties();
186:
187: Document doc = xmlizer.xmlize(data);
188: String marshaledValue = XmlUtil.xmlAsString(doc);
189:
190: assertSubString("<data>", marshaledValue);
191: assertSubString("CDATA", marshaledValue);
192: assertSubString("this is some content", marshaledValue);
193:
194: ByteArrayOutputStream output = new ByteArrayOutputStream();
195: properties.save(output);
196: String[] propertyLines = output.toString().split("\n");
197: for (int i = 0; i < propertyLines.length; i++) {
198: String propertyLine = propertyLines[i].trim();
199: assertSubString(propertyLine, marshaledValue);
200: }
201: }
202:
203: public void testDeXmlizingPageData() throws Exception {
204: PageData data = new PageData(root);
205: data.setContent("this is some content.");
206: WikiPageProperties properties = data.getProperties();
207:
208: PageData receivedData = xmlizer.deXmlizeData(xmlizer
209: .xmlize(data));
210: assertNotSame(data, receivedData);
211:
212: assertEquals("this is some content.", receivedData.getContent());
213: WikiPageProperties receivedProperties = receivedData
214: .getProperties();
215: assertNotSame(properties, receivedProperties);
216: assertEquals(properties.toString(), receivedProperties
217: .toString());
218: }
219: }
|