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.*;
006: import fitnesse.testutil.FitNesseUtil;
007: import java.util.*;
008:
009: public class ProxyPageTest extends TestCase {
010: private WikiPage root;
011:
012: private WikiPage original;
013:
014: private ProxyPage proxy;
015:
016: public WikiPage child1;
017:
018: private PageCrawler crawler;
019:
020: private WikiPagePath page1Path;
021:
022: public void setUp() throws Exception {
023: CachingPage.cacheTime = 0;
024: root = InMemoryPage.makeRoot("RooT");
025: crawler = root.getPageCrawler();
026: page1Path = PathParser.parse("PageOne");
027: original = crawler.addPage(root, page1Path, "page one content");
028: child1 = crawler.addPage(original,
029: PathParser.parse("ChildOne"), "child one");
030: crawler.addPage(original, PathParser.parse("ChildTwo"),
031: "child two");
032: PageData data = original.getData();
033: data.setAttribute("Attr1", "true");
034: original.commit(data);
035:
036: FitNesseUtil.startFitnesse(root);
037:
038: proxy = new ProxyPage(original);
039: proxy.setTransientValues("localhost", new Date().getTime());
040: proxy.setHostPort(FitNesseUtil.port);
041: }
042:
043: public void tearDown() throws Exception {
044: FitNesseUtil.stopFitnesse();
045: }
046:
047: public void testConstructor() throws Exception {
048: assertEquals("page one content", proxy.getData().getContent());
049: assertEquals("PageOne", proxy.getName());
050: assertEquals(true, proxy.getData().hasAttribute("Attr1"));
051: }
052:
053: public void testHasChildren() throws Exception {
054: assertEquals(false, proxy.hasChildPage("BlaH"));
055: assertEquals(true, proxy.hasChildPage("ChildOne"));
056: assertEquals(true, proxy.hasChildPage("ChildTwo"));
057: }
058:
059: public void testGetChildrenOneAtATime() throws Exception {
060: WikiPage child1 = proxy.getChildPage("ChildOne");
061: assertEquals("child one", child1.getData().getContent());
062: WikiPage child2 = proxy.getChildPage("ChildTwo");
063: assertEquals("child two", child2.getData().getContent());
064: }
065:
066: public void testGetAllChildren() throws Exception {
067: List children = proxy.getChildren();
068: assertEquals(2, children.size());
069: WikiPage child = (WikiPage) children.get(0);
070: assertEquals(true, "ChildOne".equals(child.getName())
071: || "ChildTwo".equals(child.getName()));
072: child = (WikiPage) children.get(1);
073: assertEquals(true, "ChildOne".equals(child.getName())
074: || "ChildTwo".equals(child.getName()));
075: }
076:
077: public void testSetHostAndPort() throws Exception {
078: List children = proxy.getChildren();
079: proxy.setTransientValues("a.new.host", new Date().getTime());
080: proxy.setHostPort(123);
081:
082: assertEquals("a.new.host", proxy.getHost());
083: assertEquals(123, proxy.getHostPort());
084:
085: for (Iterator iterator = children.iterator(); iterator
086: .hasNext();) {
087: ProxyPage page = (ProxyPage) iterator.next();
088: assertEquals("a.new.host", page.getHost());
089: assertEquals(123, page.getHostPort());
090: }
091: }
092:
093: public void testCanFindNewChildOfAProxy() throws Exception {
094: ProxyPage child1Proxy = (ProxyPage) proxy
095: .getChildPage("ChildOne");
096: assertNull(child1Proxy.getChildPage("ChildOneChild"));
097:
098: crawler.addPage(child1, PathParser.parse("ChildOneChild"),
099: "child one child");
100: assertNotNull(child1Proxy.getChildPage("ChildOneChild"));
101: }
102:
103: public void testHasSubpageCallsLoadChildrenNoMoreThanNeeded()
104: throws Exception {
105: proxy.loadChildren();
106: ProxyPage.retrievalCount = 0;
107: proxy.hasChildPage("ChildTwo");
108: assertEquals(0, ProxyPage.retrievalCount);
109: proxy.hasChildPage("SomeMissingChild");
110: assertEquals(1, ProxyPage.retrievalCount);
111: }
112:
113: public void testChildrenAreCached() throws Exception {
114: CachingPage.cacheTime = 100;
115: proxy.getChildren();
116: int startingNumber = ProxyPage.retrievalCount;
117: assertEquals(startingNumber, ProxyPage.retrievalCount);
118: proxy.getChildren();
119: assertEquals(startingNumber, ProxyPage.retrievalCount);
120: Thread.sleep(200);
121: proxy.getChildren();
122: assertEquals(startingNumber + 1, ProxyPage.retrievalCount);
123: }
124: }
|