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;
004:
005: import java.util.Properties;
006: import java.io.*;
007: import fitnesse.wiki.*;
008: import fitnesse.html.HtmlPageFactory;
009: import fitnesse.responders.*;
010: import fitnesse.responders.editing.EditResponder;
011: import fitnesse.testutil.*;
012: import fitnesse.authentication.*;
013: import fitnesse.wikitext.widgets.*;
014: import fitnesse.wikitext.WidgetBuilder;
015:
016: public class ComponentFactoryTest extends AbstractRegex {
017: private Properties testProperties;
018:
019: private ComponentFactory factory;
020:
021: public void setUp() throws Exception {
022: testProperties = new Properties();
023: factory = new ComponentFactory(".");
024: }
025:
026: private void saveTestProperties() throws IOException {
027: String propertiesFile = ComponentFactory.PROPERTIES_FILE;
028: FileOutputStream fileOutputStream = new FileOutputStream(
029: propertiesFile);
030: testProperties.store(fileOutputStream,
031: "Test ComponentFactory Properties File");
032: fileOutputStream.close();
033: }
034:
035: public void tearDown() throws Exception {
036: final File file = new File(ComponentFactory.PROPERTIES_FILE);
037: FileOutputStream out = new FileOutputStream(file);
038: out.write("".getBytes());
039: out.close();
040: file.delete();
041: }
042:
043: public void testRootPageCreation() throws Exception {
044: testProperties.setProperty(ComponentFactory.WIKI_PAGE_CLASS,
045: InMemoryPage.class.getName());
046: saveTestProperties();
047:
048: factory.loadProperties();
049: WikiPage page = factory.getRootPage(null);
050: assertNotNull(page);
051: assertEquals(InMemoryPage.class, page.getClass());
052: }
053:
054: public void testDefaultRootPage() throws Exception {
055: factory.loadProperties();
056: WikiPage page = factory.getRootPage(FileSystemPage.makeRoot(
057: "testPath", "TestRoot"));
058: assertNotNull(page);
059: assertEquals(FileSystemPage.class, page.getClass());
060: assertEquals("TestRoot", page.getName());
061: }
062:
063: public void testDefaultHtmlPageFactory() throws Exception {
064: factory.loadProperties();
065: HtmlPageFactory pageFactory = factory
066: .getHtmlPageFactory(new HtmlPageFactory());
067: assertNotNull(pageFactory);
068: assertEquals(HtmlPageFactory.class, pageFactory.getClass());
069: }
070:
071: public void testHtmlPageFactoryCreation() throws Exception {
072: testProperties.setProperty(ComponentFactory.HTML_PAGE_FACTORY,
073: TestPageFactory.class.getName());
074: saveTestProperties();
075:
076: factory.loadProperties();
077: HtmlPageFactory pageFactory = factory.getHtmlPageFactory(null);
078: assertNotNull(pageFactory);
079: assertEquals(TestPageFactory.class, pageFactory.getClass());
080: }
081:
082: public void testAddResponderPlugins() throws Exception {
083: String respondersValue = "custom1:"
084: + WikiPageResponder.class.getName() + ",custom2:"
085: + EditResponder.class.getName();
086: testProperties.setProperty(ComponentFactory.RESPONDERS,
087: respondersValue);
088: saveTestProperties();
089:
090: factory.loadProperties();
091: ResponderFactory responderFactory = new ResponderFactory(".");
092: String output = factory.loadResponderPlugins(responderFactory);
093:
094: assertSubString("custom1:" + WikiPageResponder.class.getName(),
095: output);
096: assertSubString("custom2:" + EditResponder.class.getName(),
097: output);
098:
099: assertEquals(WikiPageResponder.class, responderFactory
100: .getResponderClass("custom1"));
101: assertEquals(EditResponder.class, responderFactory
102: .getResponderClass("custom2"));
103: }
104:
105: public void testWikiWidgetPlugins() throws Exception {
106: WidgetBuilder.htmlWidgetBuilder = new WidgetBuilder(
107: new Class[] { WikiWordWidget.class });
108: String widgetsValue = BoldWidget.class.getName() + ", "
109: + ItalicWidget.class.getName();
110: testProperties.setProperty(ComponentFactory.WIKI_WIDGETS,
111: widgetsValue);
112: saveTestProperties();
113:
114: factory.loadProperties();
115: String output = factory.loadWikiWidgetPlugins();
116:
117: assertSubString(BoldWidget.class.getName(), output);
118: assertSubString(ItalicWidget.class.getName(), output);
119:
120: String builderPattern = WidgetBuilder.htmlWidgetBuilder
121: .getWidgetPattern().pattern();
122: assertSubString(BoldWidget.REGEXP, builderPattern);
123: assertSubString(ItalicWidget.REGEXP, builderPattern);
124: }
125:
126: public void testAuthenticatorDefaultCreation() throws Exception {
127: factory.loadProperties();
128: Authenticator authenticator = factory
129: .getAuthenticator(new PromiscuousAuthenticator());
130: assertNotNull(authenticator);
131: assertEquals(PromiscuousAuthenticator.class, authenticator
132: .getClass());
133: }
134:
135: public void testAuthenticatorCustomCreation() throws Exception {
136: testProperties.setProperty(ComponentFactory.AUTHENTICATOR,
137: SimpleAuthenticator.class.getName());
138: saveTestProperties();
139:
140: factory.loadProperties();
141: Authenticator authenticator = factory
142: .getAuthenticator(new PromiscuousAuthenticator());
143: assertNotNull(authenticator);
144: assertEquals(SimpleAuthenticator.class, authenticator
145: .getClass());
146: }
147:
148: public static class TestPageFactory extends HtmlPageFactory {
149: public TestPageFactory(Properties p) {
150: p.propertyNames();
151: }
152: }
153: }
|