-
Notifications
You must be signed in to change notification settings - Fork 713
/
PluginsLoaderTest.java
361 lines (294 loc) · 13.2 KB
/
PluginsLoaderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.plugins;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import fitnesse.ConfigurationParameter;
import fitnesse.authentication.Authenticator;
import fitnesse.authentication.MultiUserAuthenticator;
import fitnesse.authentication.OneUserAuthenticator;
import fitnesse.authentication.PromiscuousAuthenticator;
import fitnesse.components.ComponentFactory;
import fitnesse.components.PluginsClassLoader;
import fitnesse.responders.ResponderFactory;
import fitnesse.responders.WikiPageResponder;
import fitnesse.responders.editing.ContentFilter;
import fitnesse.responders.editing.EditResponder;
import fitnesse.testrunner.MultipleTestSystemFactory;
import fitnesse.testrunner.TestSystemFactoryRegistry;
import fitnesse.testsystems.Descriptor;
import fitnesse.testsystems.TestSystem;
import fitnesse.testsystems.TestSystemFactory;
import fitnesse.testsystems.slim.CustomComparator;
import fitnesse.testsystems.slim.CustomComparatorRegistry;
import fitnesse.testsystems.slim.HtmlTable;
import fitnesse.testsystems.slim.SlimTestContext;
import fitnesse.testsystems.slim.SlimTestContextImpl;
import fitnesse.testsystems.slim.Table;
import fitnesse.testsystems.slim.tables.SlimAssertion;
import fitnesse.testsystems.slim.tables.SlimTable;
import fitnesse.testsystems.slim.tables.SlimTableFactory;
import fitnesse.testutil.SimpleAuthenticator;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikiPageFactory;
import fitnesse.wiki.WikiPageFactoryRegistry;
import fitnesse.wiki.fs.FileSystemPageFactory;
import fitnesse.wikitext.parser.ParseSpecification;
import fitnesse.wikitext.parser.ScanString;
import fitnesse.wikitext.parser.SymbolMatch;
import fitnesse.wikitext.parser.SymbolProvider;
import fitnesse.wikitext.parser.SymbolStream;
import fitnesse.wikitext.parser.SymbolType;
import fitnesse.wikitext.parser.Today;
import fitnesse.wikitext.parser.VariableSource;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class PluginsLoaderTest {
private Properties testProperties;
private PluginsLoader loader;
private ResponderFactory responderFactory;
private SymbolProvider testProvider;
private WikiPageFactoryRegistry testWikiPageFactoryRegistry;
private SlimTableFactory testSlimTableFactory;
private CustomComparatorRegistry testCustomComparatorsRegistry;
private MultipleTestSystemFactory testTestSystemFactory;
@Before
public void setUp() throws Exception {
testProperties = new Properties();
responderFactory = new ResponderFactory(".");
testProvider = new SymbolProvider(new SymbolType[] {});
testWikiPageFactoryRegistry = new FileSystemPageFactory();
testSlimTableFactory = new SlimTableFactory();
testCustomComparatorsRegistry = new CustomComparatorRegistry();
testTestSystemFactory = new MultipleTestSystemFactory(testSlimTableFactory, testCustomComparatorsRegistry);
URL sampleUrl = new File("test/fitnesse/plugins").toURI().toURL();
PluginsClassLoader.addUrlToClasspath(sampleUrl);
loader = new PluginsLoader(new ComponentFactory(testProperties));
assertSymbolTypeMatch("!today", false);
}
@Test
public void testAddPlugins() throws Exception {
testProperties.setProperty(ConfigurationParameter.PLUGINS.getKey(), DummyPlugin.class.getName());
loader = new PluginsLoader(new ComponentFactory(testProperties));
loader.loadResponders(responderFactory);
loader.loadSymbolTypes(testProvider);
loader.loadWikiPageFactories(testWikiPageFactoryRegistry);
loader.loadTestSystems(testTestSystemFactory);
loader.loadSlimTables(testSlimTableFactory);
loader.loadCustomComparators(testCustomComparatorsRegistry);
assertEquals(WikiPageResponder.class, responderFactory.getResponderClass("custom1"));
assertEquals(EditResponder.class, responderFactory.getResponderClass("custom2"));
assertSymbolTypeMatch("!today", true);
}
@Test
public void shouldHandleInstanceMethods() throws Exception {
testProperties.setProperty(ConfigurationParameter.PLUGINS.getKey(), InstantiableDummyPlugin.class.getName());
testProperties.setProperty("responderName", "instanceTest");
loader = new PluginsLoader(new ComponentFactory(testProperties));
loader.loadResponders(responderFactory);
assertEquals(WikiPageResponder.class, responderFactory.getResponderClass("instanceTest"));
}
private void assertSymbolTypeMatch(String input, boolean expected) {
SymbolMatch match = new ParseSpecification().provider(testProvider).findMatch(new ScanString(input, 0), 0, new SymbolStream());
assertEquals(match.isMatch(), expected);
}
@Test
public void testAddResponderPlugins() throws Exception {
String respondersValue = "custom1:" + WikiPageResponder.class.getName() + ",custom2:" + EditResponder.class.getName();
testProperties.setProperty(ConfigurationParameter.RESPONDERS.getKey(), respondersValue);
loader = new PluginsLoader(new ComponentFactory(testProperties));
loader.loadResponders(responderFactory);
assertEquals(WikiPageResponder.class, responderFactory.getResponderClass("custom1"));
assertEquals(EditResponder.class, responderFactory.getResponderClass("custom2"));
}
@Test
public void testWikiWidgetPlugins() throws Exception {
String symbolValues = Today.class.getName();
testProperties.setProperty(ConfigurationParameter.SYMBOL_TYPES.getKey(), symbolValues);
loader.loadSymbolTypes(testProvider);
assertSymbolTypeMatch("!today", true);
}
@Test
public void testWikiWidgetPluginsViaFeatureFactory() throws Exception {
assertSymbolTypeMatch("!monthsFromToday2", false);
loader.loadSymbolTypes(testProvider);
assertSymbolTypeMatch("!monthsFromToday2", true);
}
@Test
public void testAuthenticatorDefaultCreation() throws Exception {
Authenticator authenticator = loader.getAuthenticator(new PromiscuousAuthenticator());
assertNotNull(authenticator);
assertEquals(PromiscuousAuthenticator.class, authenticator.getClass());
}
@Test
public void testAuthenticatorCustomCreation() throws Exception {
testProperties.setProperty(ConfigurationParameter.AUTHENTICATOR.getKey(), SimpleAuthenticator.class.getName());
Authenticator authenticator = loader.getAuthenticator(new PromiscuousAuthenticator());
assertNotNull(authenticator);
assertEquals(SimpleAuthenticator.class, authenticator.getClass());
}
@Test
public void testMakeNullAuthenticator() throws Exception {
Authenticator a = loader.makeAuthenticator(null);
assertTrue(a instanceof PromiscuousAuthenticator);
}
@Test
public void testMakeOneUserAuthenticator() throws Exception {
Authenticator a = loader.makeAuthenticator("bob:uncle");
assertTrue(a instanceof OneUserAuthenticator);
OneUserAuthenticator oua = (OneUserAuthenticator) a;
assertEquals("bob", oua.getUser());
assertEquals("uncle", oua.getPassword());
}
@Test
public void testMakeMultiUserAuthenticator() throws Exception {
final String passwordFilename = "testpasswd";
File passwd = new File(passwordFilename);
passwd.createNewFile();
Authenticator a = loader.makeAuthenticator(passwordFilename);
assertTrue(a instanceof MultiUserAuthenticator);
passwd.delete();
}
@Test
public void noContentFilter() throws Exception {
ContentFilter filter = loader.loadContentFilter();
assertNull(filter);
}
@Test
public void haveContentFilter() throws Exception {
testProperties.setProperty(ConfigurationParameter.CONTENT_FILTER.getKey(), TestContentFilter.class.getName());
ContentFilter filter = loader.loadContentFilter();
assertNotNull(filter);
assertEquals(TestContentFilter.class, filter.getClass());
}
@Test
public void testWikiPageFactoryCreation() throws Exception {
testProperties.setProperty(ConfigurationParameter.WIKI_PAGE_FACTORIES.getKey(), FooWikiPageFactory.class.getName());
FileSystemPageFactory wikiPageFactory = mock(FileSystemPageFactory.class);
loader.loadWikiPageFactories(wikiPageFactory);
verify(wikiPageFactory).registerWikiPageFactory(any(FooWikiPageFactory.class));
}
@Test
public void testSlimTablesCreation() throws PluginException {
SlimTableFactory slimTableFactory = new SlimTableFactory();
testProperties.setProperty(ConfigurationParameter.SLIM_TABLES.getKey(), "test:" + TestSlimTable.class.getName());
loader.loadSlimTables(slimTableFactory);
HtmlTable table = makeMockTable("test");
SlimTable slimTable = slimTableFactory.makeSlimTable(table, "foo", new SlimTestContextImpl());
assertSame(TestSlimTable.class, slimTable.getClass());
}
@Test
public void testSlimTablesWithColonCreation() throws PluginException {
testProperties.setProperty(ConfigurationParameter.SLIM_TABLES.getKey(), "test::" + TestSlimTable.class.getName());
SlimTableFactory slimTableFactory = new SlimTableFactory();
loader.loadSlimTables(slimTableFactory);
HtmlTable table = makeMockTable("test:");
SlimTable slimTable = slimTableFactory.makeSlimTable(table, "foo", new SlimTestContextImpl());
assertSame(TestSlimTable.class, slimTable.getClass());
}
@Test
public void testSlimTablesCreationViaFeatureFactory() throws Exception {
SlimTableFactory slimTableFactory = new SlimTableFactory();
loader.loadSlimTables(slimTableFactory);
HtmlTable table = makeMockTable(DummyPluginFeatureFactory.SLIM_TABLE);
SlimTable slimTable = slimTableFactory.makeSlimTable(table, "foo", new SlimTestContextImpl());
assertSame(TestSlimTable.class, slimTable.getClass());
}
@Test
public void testCustomComparatorsCreation() throws PluginException {
CustomComparatorRegistry customComparatorRegistry = new CustomComparatorRegistry();
testProperties.setProperty(ConfigurationParameter.CUSTOM_COMPARATORS.getKey(), "test:" + TestCustomComparator.class.getName());
loader.loadCustomComparators(customComparatorRegistry);
CustomComparator customComparator = customComparatorRegistry.getCustomComparatorForPrefix("test");
assertNotNull(customComparator);
assertTrue(customComparator instanceof TestCustomComparator);
}
@Test
public void testTestSystemCreation() throws PluginException {
testProperties.setProperty(ConfigurationParameter.TEST_SYSTEMS.getKey(), "foo:" + FooTestSystemFactory.class.getName());
TestSystemFactoryRegistry registrar = mock(TestSystemFactoryRegistry.class);
loader.loadTestSystems(registrar);
verify(registrar).registerTestSystemFactory(eq("foo"), any(TestSystemFactory.class));
}
private HtmlTable makeMockTable(String tableIdentifier) {
// Create just enough "table" to test if
TableTag tableTag = new TableTag();
TableRow tableRow = new TableRow();
TableColumn tableColumn = new TableColumn();
tableColumn.setChildren(new NodeList(new TextNode(tableIdentifier)));
tableRow.setChildren(new NodeList(tableColumn));
tableTag.setChildren(new NodeList(tableRow));
return new HtmlTable(tableTag);
}
public static class TestContentFilter implements ContentFilter {
public TestContentFilter(Properties p) {
p.propertyNames();
}
public boolean isContentAcceptable(String content, String page) {
return false;
}
}
static public class DummyPlugin {
public static void registerResponders(ResponderFactory factory) {
factory.addResponder("custom1", WikiPageResponder.class);
factory.addResponder("custom2", EditResponder.class);
}
public static void registerSymbolTypes(SymbolProvider provider) {
provider.add(new Today());
}
}
static public class InstantiableDummyPlugin {
public final ComponentFactory componentFactory;
public InstantiableDummyPlugin(ComponentFactory componentFactory) {
this.componentFactory = componentFactory;
}
public void registerResponders(ResponderFactory factory) {
factory.addResponder(componentFactory.getProperty("responderName"), WikiPageResponder.class);
}
}
public static class TestSlimTable extends SlimTable {
public TestSlimTable(Table table, String id, SlimTestContext testContext) {
super(table, id, testContext);
}
@Override
protected String getTableType() {
return null;
}
@Override
public List<SlimAssertion> getAssertions() {
return null;
}
}
public static class TestCustomComparator implements CustomComparator {
@Override
public boolean matches(String actual, String expected) {
return false;
}
}
public static class FooTestSystemFactory implements TestSystemFactory {
@Override
public TestSystem create(Descriptor descriptor) throws IOException {
return null;
}
}
public static class FooWikiPageFactory implements WikiPageFactory {
@Override
public WikiPage makePage(File path, String pageName, WikiPage parent, VariableSource variableSource) {
return null;
}
@Override
public boolean supports(File path) {
return false;
}
}
}