001: package com.ecyrd.jspwiki.plugin;
002:
003: import java.util.Properties;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008:
009: import com.ecyrd.jspwiki.TestEngine;
010: import com.ecyrd.jspwiki.WikiContext;
011: import com.ecyrd.jspwiki.WikiPage;
012:
013: public class ReferringPagesPluginTest extends TestCase {
014: Properties props = new Properties();
015: TestEngine engine;
016: WikiContext context;
017: PluginManager manager;
018:
019: public ReferringPagesPluginTest(String s) {
020: super (s);
021: }
022:
023: public void setUp() throws Exception {
024: props.load(TestEngine.findTestProperties());
025:
026: props.setProperty("jspwiki.breakTitleWithSpaces", "false");
027: engine = new TestEngine(props);
028:
029: engine.saveText("TestPage", "Reference to [Foobar].");
030: engine.saveText("Foobar", "Reference to [TestPage].");
031: engine.saveText("Foobar2", "Reference to [TestPage].");
032: engine.saveText("Foobar3", "Reference to [TestPage].");
033: engine.saveText("Foobar4", "Reference to [TestPage].");
034: engine.saveText("Foobar5", "Reference to [TestPage].");
035: engine.saveText("Foobar6", "Reference to [TestPage].");
036: engine.saveText("Foobar7", "Reference to [TestPage].");
037:
038: context = new WikiContext(engine, new WikiPage(engine,
039: "TestPage"));
040: manager = new PluginManager(engine, props);
041: }
042:
043: public void tearDown() {
044: TestEngine.deleteTestPage("TestPage");
045: TestEngine.deleteTestPage("Foobar");
046: TestEngine.deleteTestPage("Foobar2");
047: TestEngine.deleteTestPage("Foobar3");
048: TestEngine.deleteTestPage("Foobar4");
049: TestEngine.deleteTestPage("Foobar5");
050: TestEngine.deleteTestPage("Foobar6");
051: TestEngine.deleteTestPage("Foobar7");
052: }
053:
054: private String mkLink(String page) {
055: return mkFullLink(page, page);
056: }
057:
058: private String mkFullLink(String page, String link) {
059: return "<a class=\"wikipage\" href=\"/Wiki.jsp?page=" + link
060: + "\">" + page + "</a>";
061: }
062:
063: public void testSingleReferral() throws Exception {
064: WikiContext context2 = new WikiContext(engine, new WikiPage(
065: engine, "Foobar"));
066:
067: String res = manager
068: .execute(context2,
069: "{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=5}");
070:
071: assertEquals(mkLink("TestPage") + "<br />", res);
072: }
073:
074: public void testMaxReferences() throws Exception {
075: String res = manager
076: .execute(context,
077: "{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=5}");
078:
079: int count = 0;
080: int index = -1;
081:
082: // Count the number of hyperlinks. We could check their
083: // correctness as well, though.
084:
085: while ((index = res.indexOf("<a", index + 1)) != -1) {
086: count++;
087: }
088:
089: assertEquals(5, count);
090:
091: String expected = "...and 2 more<br />";
092:
093: assertEquals("End", expected, res.substring(res.length()
094: - expected.length()));
095: }
096:
097: public void testReferenceWidth() throws Exception {
098: WikiContext context2 = new WikiContext(engine, new WikiPage(
099: engine, "Foobar"));
100:
101: String res = manager
102: .execute(context2,
103: "{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE maxwidth=5}");
104:
105: assertEquals(mkFullLink("TestP...", "TestPage") + "<br />", res);
106: }
107:
108: public void testInclude() throws Exception {
109: String res = manager.execute(context,
110: "{ReferringPagesPlugin include='*7'}");
111:
112: assertTrue("7", res.indexOf("Foobar7") != -1);
113: assertTrue("6", res.indexOf("Foobar6") == -1);
114: assertTrue("5", res.indexOf("Foobar5") == -1);
115: assertTrue("4", res.indexOf("Foobar4") == -1);
116: assertTrue("3", res.indexOf("Foobar3") == -1);
117: assertTrue("2", res.indexOf("Foobar2") == -1);
118: }
119:
120: public void testExclude() throws Exception {
121: String res = manager.execute(context,
122: "{ReferringPagesPlugin exclude='*'}");
123:
124: assertEquals("...nobody", res);
125: }
126:
127: public void testExclude2() throws Exception {
128: String res = manager.execute(context,
129: "{ReferringPagesPlugin exclude='*7'}");
130:
131: assertTrue(res.indexOf("Foobar7") == -1);
132: }
133:
134: public void testExclude3() throws Exception {
135: String res = manager.execute(context,
136: "{ReferringPagesPlugin exclude='*7,*5,*4'}");
137:
138: assertTrue("7", res.indexOf("Foobar7") == -1);
139: assertTrue("6", res.indexOf("Foobar6") != -1);
140: assertTrue("5", res.indexOf("Foobar5") == -1);
141: assertTrue("4", res.indexOf("Foobar4") == -1);
142: assertTrue("3", res.indexOf("Foobar3") != -1);
143: assertTrue("2", res.indexOf("Foobar2") != -1);
144: }
145:
146: public static Test suite() {
147: return new TestSuite(ReferringPagesPluginTest.class);
148: }
149: }
|