01: package com.ecyrd.jspwiki.providers;
02:
03: import junit.framework.*;
04:
05: import java.io.FileWriter;
06: import java.io.PrintWriter;
07: import java.io.File;
08: import java.io.StringReader;
09: import java.util.*;
10:
11: import org.apache.log4j.*;
12:
13: import com.ecyrd.jspwiki.*;
14:
15: public class CachingProviderTest extends TestCase {
16: public CachingProviderTest(String s) {
17: super (s);
18: }
19:
20: public void setUp() throws Exception {
21: TestEngine.emptyWorkDir();
22:
23: Properties props2 = new Properties();
24:
25: props2.load(TestEngine.findTestProperties());
26: PropertyConfigurator.configure(props2);
27: }
28:
29: public void tearDown() {
30: TestEngine.emptyWorkDir();
31: TestEngine.deleteTestPage("Testi");
32: }
33:
34: /**
35: * Checks that at startup we call the provider once, and once only.
36: */
37: public void testInitialization() throws Exception {
38: Properties props = new Properties();
39: props.load(TestEngine.findTestProperties());
40:
41: props.setProperty("jspwiki.usePageCache", "true");
42: props.setProperty("jspwiki.pageProvider",
43: "com.ecyrd.jspwiki.providers.CounterProvider");
44: props.setProperty("jspwiki.cachingProvider.capacity", "100");
45:
46: TestEngine engine = new TestEngine(props);
47:
48: CounterProvider p = (CounterProvider) ((CachingProvider) engine
49: .getPageManager().getProvider()).getRealProvider();
50:
51: assertEquals("init", 1, p.m_initCalls);
52: assertEquals("getAllPages", 1, p.m_getAllPagesCalls);
53: assertEquals("pageExists", 0, p.m_pageExistsCalls);
54: assertEquals("getPage", 2, p.m_getPageCalls); // These two are for non-existant pages (with and without s)
55: assertEquals("getPageText", 4, p.m_getPageTextCalls);
56:
57: engine.getPage("Foo");
58:
59: assertEquals("pageExists2", 0, p.m_pageExistsCalls);
60: assertEquals("getPage2", 2, p.m_getPageCalls);
61: }
62:
63: public void testSneakyAdd() throws Exception {
64: Properties props = new Properties();
65: props.load(TestEngine.findTestProperties());
66:
67: props.setProperty("jspwiki.cachingProvider.cacheCheckInterval",
68: "2");
69:
70: TestEngine engine = new TestEngine(props);
71:
72: String dir = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
73:
74: File f = new File(dir, "Testi.txt");
75: String content = "[fuufaa]";
76:
77: PrintWriter out = new PrintWriter(new FileWriter(f));
78: FileUtil.copyContents(new StringReader(content), out);
79: out.close();
80:
81: Thread.sleep(4000L); // Make sure we wait long enough
82:
83: WikiPage p = engine.getPage("Testi");
84: assertNotNull("page did not exist?", p);
85:
86: String text = engine.getText("Testi");
87: assertEquals("text", "[fuufaa]", text);
88:
89: // TODO: ReferenceManager check as well
90: }
91:
92: public static Test suite() {
93: return new TestSuite(CachingProviderTest.class);
94: }
95:
96: }
|