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:
009: public class WikiPagePropertiesTest extends AbstractRegex {
010: private InputStream sampleInputStream;
011:
012: private WikiPageProperties properties;
013:
014: static String endl = System.getProperty("line.separator");
015:
016: static String sampleXml = "<?xml version=\"1.0\"?>" + endl
017: + "<properties>" + endl + "\t<Edit/>" + endl + "\t<Test/>"
018: + endl + "\t<VirtualWiki>http://someurl</VirtualWiki>"
019: + endl + "</properties>" + endl;
020:
021: public void setUp() throws Exception {
022: sampleInputStream = new ByteArrayInputStream(sampleXml
023: .getBytes());
024: properties = new WikiPageProperties(sampleInputStream);
025: }
026:
027: public void tearDown() throws Exception {
028: }
029:
030: public void testLoading() throws Exception {
031: assertTrue(properties.has(WikiPage.ACTION_EDIT));
032: assertTrue(properties.has(WikiPage.ACTION_TEST));
033: assertFalse(properties.has(WikiPage.ACTION_SUITE));
034: assertEquals("http://someurl", properties.get("VirtualWiki"));
035: }
036:
037: public void testSave() throws Exception {
038: ByteArrayOutputStream os = new ByteArrayOutputStream(1000);
039: properties.save(os);
040:
041: String xml = os.toString();
042: assertEquals(sampleXml, xml);
043: }
044:
045: public void testSetProperty() throws Exception {
046: properties = new WikiPageProperties();
047: properties.set("newProperty1");
048: assertEquals(true, properties.has("newProperty1"));
049: assertEquals("true", properties.get("newProperty1"));
050:
051: properties.set("newProperty2", "some value");
052: assertEquals("some value", properties.get("newProperty2"));
053: }
054:
055: public void testRemoveProperty() throws Exception {
056: properties = new WikiPageProperties();
057: properties.set("newProperty", "blah");
058: properties.remove("newProperty");
059: assertEquals(false, properties.has("newProperty"));
060: }
061:
062: public void testKeySet() throws Exception {
063: properties = new WikiPageProperties();
064: properties.set("one");
065: properties.set("two");
066: properties.set("three");
067: Set keys = properties.keySet();
068:
069: assertTrue(keys.contains("one"));
070: assertTrue(keys.contains("two"));
071: assertTrue(keys.contains("three"));
072: assertFalse(keys.contains("four"));
073: }
074:
075: public void testIsSerializable() throws Exception {
076: try {
077: new ObjectOutputStream(new ByteArrayOutputStream())
078: .writeObject(properties);
079: } catch (NotSerializableException e) {
080: fail("its not serializabl: " + e);
081: }
082: }
083:
084: public void testHas() throws Exception {
085: properties.set("true", "true");
086: properties.set("false", "false");
087:
088: assertTrue(properties.has("true"));
089: assertTrue(properties.has("false"));
090: }
091:
092: public void testConstructorTakingAMap() throws Exception {
093: HashMap map = new HashMap();
094: map.put("key1", "value1");
095: map.put("key2", "value2");
096: map.put("key3", "false");
097: properties = new WikiPageProperties(map);
098: assertEquals("value1", properties.get("key1"));
099: assertEquals(true, properties.has("key2"));
100: assertFalse(properties.has("key3"));
101: }
102:
103: public void testSymbolicLinks() throws Exception {
104: properties.addSymbolicLink("LinkName", PathParser
105: .parse("PatH.ToThe.PagE"));
106: assertTrue(properties.hasSymbolicLink("LinkName"));
107: assertFalse(properties.hasSymbolicLink("SomeOtherLink"));
108:
109: assertEquals(PathParser.parse("PatH.ToThe.PagE"), properties
110: .getSymbolicLink("LinkName"));
111: assertEquals(null, properties.getSymbolicLink("SomeOtherLink"));
112:
113: Set linkNames = properties.getSymbolicLinkNames();
114: assertEquals(1, linkNames.size());
115: assertEquals("LinkName", linkNames.iterator().next());
116:
117: properties.removeSymbolicLink("LinkName");
118: assertFalse(properties.hasSymbolicLink("LinkName"));
119: }
120:
121: public void testSymbolicLinksSave() throws Exception {
122: ByteArrayOutputStream output = saveSomeSymbolicLinks();
123: String xml = output.toString();
124:
125: assertHasRegexp(
126: "<symbolicLink>\\s*<name>LinkOne</name>\\s*<path>PatH.OnE</path>\\s*</symbolicLink>",
127: xml);
128: assertHasRegexp(
129: "<symbolicLink>\\s*<name>LinkTwo</name>\\s*<path>PatH.TwO</path>\\s*</symbolicLink>",
130: xml);
131: }
132:
133: private ByteArrayOutputStream saveSomeSymbolicLinks()
134: throws Exception {
135: properties.addSymbolicLink("LinkOne", PathParser
136: .parse("PatH.OnE"));
137: properties.addSymbolicLink("LinkTwo", PathParser
138: .parse("PatH.TwO"));
139: ByteArrayOutputStream output = new ByteArrayOutputStream();
140: properties.save(output);
141: return output;
142: }
143:
144: public void testSymbolicLinksLoading() throws Exception {
145: ByteArrayOutputStream output = saveSomeSymbolicLinks();
146: ByteArrayInputStream input = new ByteArrayInputStream(output
147: .toByteArray());
148: WikiPageProperties newProperties = new WikiPageProperties(input);
149:
150: assertTrue(newProperties.hasSymbolicLink("LinkOne"));
151: assertEquals(PathParser.parse("PatH.OnE"), newProperties
152: .getSymbolicLink("LinkOne"));
153: assertTrue(newProperties.hasSymbolicLink("LinkTwo"));
154: assertEquals(PathParser.parse("PatH.TwO"), newProperties
155: .getSymbolicLink("LinkTwo"));
156: }
157: }
|