001: package com.ecyrd.jspwiki;
002:
003: import junit.framework.*;
004: import java.util.*;
005: import java.io.*;
006:
007: import com.ecyrd.jspwiki.providers.FileSystemProvider;
008:
009: /**
010: * @author Torsten Hildebrandt.
011: */
012: public class ReferenceManagerTest extends TestCase {
013: Properties props = new Properties();
014: TestEngine engine;
015: ReferenceManager mgr;
016:
017: public ReferenceManagerTest(String s) {
018: super (s);
019: }
020:
021: public void setUp() throws Exception {
022: props.load(TestEngine.findTestProperties());
023: props.setProperty(
024: "jspwiki.translatorReader.matchEnglishPlurals", "true");
025:
026: //
027: // We must make sure that the reference manager cache is cleaned first.
028: //
029: String workDir = props.getProperty("jspwiki.workDir");
030:
031: if (workDir != null) {
032: File refmgrfile = new File(workDir, "refmgr.ser");
033: if (refmgrfile.exists())
034: refmgrfile.delete();
035: }
036:
037: String fileDir = props
038: .getProperty(FileSystemProvider.PROP_PAGEDIR);
039:
040: if (fileDir != null) {
041: TestEngine.deleteAll(new File(fileDir));
042: }
043:
044: engine = new TestEngine(props);
045:
046: engine.saveText("TestPage", "Reference to [Foobar].");
047: engine.saveText("Foobar",
048: "Reference to [Foobar2], [Foobars], [Foobar]");
049:
050: mgr = engine.getReferenceManager();
051: }
052:
053: public void tearDown() throws Exception {
054: engine.deletePage("TestPage");
055: engine.deletePage("Foobar");
056: engine.deletePage("Foobars");
057: engine.deletePage("Foobar2");
058: engine.deletePage("Foobar2s");
059: engine.deletePage("BugCommentPreviewDeletesAllComments");
060: engine.deletePage("FatalBugs");
061: engine.deletePage("RandomPage");
062: engine.deletePage("NewBugs");
063: engine.deletePage("OpenBug");
064: engine.deletePage("OpenBugs");
065: engine.deletePage("NewBug");
066: engine.deletePage("BugOne");
067: engine.deletePage("BugTwo");
068: }
069:
070: public void testNonExistant1() throws Exception {
071: Collection c = mgr.findReferrers("Foobar2");
072:
073: assertTrue(c.size() == 1 && c.contains("Foobar"));
074: }
075:
076: public void testNonExistant2() {
077: Collection c = mgr.findReferrers("TestBug");
078:
079: assertNull(c);
080: }
081:
082: public void testRemove() throws Exception {
083: Collection c = mgr.findReferrers("Foobar2");
084:
085: assertTrue(c.size() == 1 && c.contains("Foobar"));
086:
087: engine.deletePage("Foobar");
088:
089: c = mgr.findReferrers("Foobar2");
090:
091: assertNull(c);
092:
093: engine.saveText("Foobar", "[Foobar2]");
094:
095: c = mgr.findReferrers("Foobar2");
096:
097: assertTrue(c.size() == 1 && c.contains("Foobar"));
098: }
099:
100: public void testUnreferenced() throws Exception {
101: Collection c = mgr.findUnreferenced();
102: assertTrue("Unreferenced page not found by ReferenceManager",
103: Util.collectionContains(c, "TestPage"));
104: }
105:
106: public void testBecomesUnreferenced() throws Exception {
107: engine.saveText("Foobar2", "[TestPage]");
108:
109: Collection c = mgr.findUnreferenced();
110: assertEquals("Wrong # of orphan pages, stage 1", 0, c.size());
111:
112: engine.saveText("Foobar2", "norefs");
113: c = mgr.findUnreferenced();
114: assertEquals("Wrong # of orphan pages", 1, c.size());
115:
116: Iterator i = c.iterator();
117: String first = (String) i.next();
118: assertEquals("Not correct referrers", "TestPage", first);
119: }
120:
121: public void testUncreated() throws Exception {
122: Collection c = mgr.findUncreated();
123:
124: assertTrue(c.size() == 1
125: && ((String) c.iterator().next()).equals("Foobar2"));
126: }
127:
128: public void testReferrers() throws Exception {
129: Collection c = mgr.findReferrers("TestPage");
130: assertNull("TestPage referrers", c);
131:
132: c = mgr.findReferrers("Foobar");
133: assertTrue("Foobar referrers", c.size() == 1
134: && ((String) c.iterator().next()).equals("TestPage"));
135:
136: c = mgr.findReferrers("Foobar2");
137: assertTrue("Foobar2 referrers", c.size() == 1
138: && ((String) c.iterator().next()).equals("Foobar"));
139:
140: c = mgr.findReferrers("Foobars");
141: assertEquals("Foobars referrers", 1, c.size());
142: assertEquals("Foobars referrer 'TestPage'", "TestPage",
143: (String) c.iterator().next());
144: }
145:
146: public void testRefersTo() throws Exception {
147: Collection s = mgr.findRefersTo("Foobar");
148:
149: assertTrue("does not have Foobar", s.contains("Foobar"));
150: // assertTrue( "does not have Foobars", s.contains("Foobars") );
151: assertTrue("does not have Foobar2", s.contains("Foobar2"));
152: }
153:
154: /**
155: * Should fail in 2.2.14-beta
156: * @throws Exception
157: */
158: public void testSingularReferences() throws Exception {
159: engine.saveText("RandomPage", "FatalBugs");
160: engine.saveText("FatalBugs", "<foo>");
161: engine.saveText("BugCommentPreviewDeletesAllComments",
162: "FatalBug");
163:
164: Collection c = mgr.findReferrers("FatalBugs");
165:
166: assertEquals("FatalBugs referrers number", 2, c.size());
167: }
168:
169: /**
170: * Is a page recognized as referenced if only plural form links exist.
171: */
172:
173: // NB: Unfortunately, cleaning out self-references in the case there's
174: // a plural and a singular form of the page becomes nigh impossible, so we
175: // just don't do it.
176: public void testUpdatePluralOnlyRef() throws Exception {
177: engine.saveText("TestPage", "Reference to [Foobars].");
178: Collection c = mgr.findUnreferenced();
179: assertTrue("Foobar unreferenced", c.size() == 1
180: && ((String) c.iterator().next()).equals("TestPage"));
181:
182: c = mgr.findReferrers("Foobar");
183: Iterator it = c.iterator();
184: String s1 = (String) it.next();
185: assertTrue("Foobar referrers", c.size() == 1
186: && s1.equals("TestPage"));
187: }
188:
189: /**
190: * Opposite to testUpdatePluralOnlyRef(). Is a page with plural form recognized as
191: * the page referenced by a singular link.
192: */
193:
194: public void testUpdateFoobar2s() throws Exception {
195: engine.saveText("Foobar2s", "qwertz");
196: assertTrue("no uncreated", mgr.findUncreated().size() == 0);
197:
198: Collection c = mgr.findReferrers("Foobar2s");
199: assertTrue("referrers", c != null && c.size() == 1
200: && ((String) c.iterator().next()).equals("Foobar"));
201: }
202:
203: public void testUpdateBothExist() throws Exception {
204: engine.saveText("Foobars", "qwertz");
205: Collection c = mgr.findReferrers("Foobars");
206: assertEquals("Foobars referrers", 1, c.size());
207: assertEquals("Foobars referrer is not TestPage", "TestPage",
208: ((String) c.iterator().next()));
209: }
210:
211: public void testUpdateBothExist2() throws Exception {
212: engine.saveText("Foobars", "qwertz");
213: engine
214: .saveText("TestPage",
215: "Reference to [Foobar], [Foobars].");
216:
217: Collection c = mgr.findReferrers("Foobars");
218: assertEquals("Foobars referrers count", 1, c.size());
219:
220: Iterator i = c.iterator();
221: String first = (String) i.next();
222:
223: assertTrue("Foobars referrers", first.equals("TestPage"));
224: }
225:
226: public void testCircularRefs() throws Exception {
227: engine.saveText("Foobar2", "ref to [TestPage]");
228:
229: assertTrue("no uncreated", mgr.findUncreated().size() == 0);
230: assertTrue("no unreferenced",
231: mgr.findUnreferenced().size() == 0);
232: }
233:
234: public void testPluralSingularUpdate1() throws Exception {
235: engine.saveText("BugOne", "NewBug");
236: engine.saveText("NewBugs", "foo");
237: engine.saveText("OpenBugs", "bar");
238:
239: engine.saveText("BugOne", "OpenBug");
240:
241: Collection ref = mgr.findReferrers("NewBugs");
242: assertNull("newbugs", ref); // No referrers must be found
243:
244: ref = mgr.findReferrers("NewBug");
245: assertNull("newbug", ref); // No referrers must be found
246:
247: ref = mgr.findReferrers("OpenBugs");
248: assertEquals("openbugs", 1, ref.size());
249: assertEquals("openbugs2", "BugOne", ref.iterator().next());
250:
251: ref = mgr.findReferrers("OpenBug");
252: assertEquals("openbug", 1, ref.size());
253: assertEquals("openbug2", "BugOne", ref.iterator().next());
254:
255: }
256:
257: public void testPluralSingularUpdate2() throws Exception {
258: engine.saveText("BugOne", "NewBug");
259: engine.saveText("NewBug", "foo");
260: engine.saveText("OpenBug", "bar");
261:
262: engine.saveText("BugOne", "OpenBug");
263:
264: Collection ref = mgr.findReferrers("NewBugs");
265: assertNull("newbugs", ref); // No referrers must be found
266:
267: ref = mgr.findReferrers("NewBug");
268: assertNull("newbug", ref); // No referrers must be found
269:
270: ref = mgr.findReferrers("OpenBugs");
271: assertEquals("openbugs", 1, ref.size());
272: assertEquals("openbugs2", "BugOne", ref.iterator().next());
273:
274: ref = mgr.findReferrers("OpenBug");
275: assertEquals("openbug", 1, ref.size());
276: assertEquals("openbug2", "BugOne", ref.iterator().next());
277:
278: }
279:
280: public void testPluralSingularUpdate3() throws Exception {
281: engine.saveText("BugOne", "NewBug");
282: engine.saveText("BugTwo", "NewBug");
283: engine.saveText("NewBugs", "foo");
284: engine.saveText("OpenBugs", "bar");
285:
286: engine.saveText("BugOne", "OpenBug");
287:
288: Collection ref = mgr.findReferrers("NewBugs");
289: assertEquals("newbugs", 1, ref.size());
290: assertEquals("newbugs2", "BugTwo", ref.iterator().next());
291:
292: ref = mgr.findReferrers("NewBug");
293: assertEquals("newbugs", 1, ref.size());
294: assertEquals("newbugs2", "BugTwo", ref.iterator().next());
295:
296: ref = mgr.findReferrers("OpenBugs");
297: assertEquals("openbugs", 1, ref.size());
298: assertEquals("openbugs2", "BugOne", ref.iterator().next());
299:
300: ref = mgr.findReferrers("OpenBug");
301: assertEquals("openbug", 1, ref.size());
302: assertEquals("openbug2", "BugOne", ref.iterator().next());
303:
304: }
305:
306: public static Test suite() {
307: return new TestSuite(ReferenceManagerTest.class);
308: }
309:
310: public static void main(String[] args) {
311: junit.textui.TestRunner
312: .main(new String[] { ReferenceManagerTest.class
313: .getName() });
314: }
315:
316: /**
317: * Test method: dumps the contents of ReferenceManager link lists to stdout.
318: * This method is NOT synchronized, and should be used in testing
319: * with one user, one WikiEngine only.
320: */
321: public static String dumpReferenceManager(ReferenceManager rm) {
322: StringBuffer buf = new StringBuffer();
323: try {
324: buf
325: .append("================================================================\n");
326: buf.append("Referred By list:\n");
327: Set keys = rm.getReferredBy().keySet();
328: Iterator it = keys.iterator();
329: while (it.hasNext()) {
330: String key = (String) it.next();
331: buf.append(key + " referred by: ");
332: Set refs = (Set) rm.getReferredBy().get(key);
333: Iterator rit = refs.iterator();
334: while (rit.hasNext()) {
335: String aRef = (String) rit.next();
336: buf.append(aRef + " ");
337: }
338: buf.append("\n");
339: }
340:
341: buf
342: .append("----------------------------------------------------------------\n");
343: buf.append("Refers To list:\n");
344: keys = rm.getRefersTo().keySet();
345: it = keys.iterator();
346: while (it.hasNext()) {
347: String key = (String) it.next();
348: buf.append(key + " refers to: ");
349: Collection refs = (Collection) rm.getRefersTo()
350: .get(key);
351: if (refs != null) {
352: Iterator rit = refs.iterator();
353: while (rit.hasNext()) {
354: String aRef = (String) rit.next();
355: buf.append(aRef + " ");
356: }
357: buf.append("\n");
358: } else
359: buf.append("(no references)\n");
360: }
361: buf
362: .append("================================================================\n");
363: } catch (Exception e) {
364: buf.append("Problem in dump(): " + e + "\n");
365: }
366:
367: return (buf.toString());
368: }
369:
370: }
|