001: package com.ecyrd.jspwiki;
002:
003: import junit.framework.*;
004: import java.io.*;
005: import java.util.*;
006:
007: import com.ecyrd.jspwiki.providers.*;
008: import com.ecyrd.jspwiki.attachment.*;
009:
010: public class WikiEngineTest extends TestCase {
011: public static final String NAME1 = "Test1";
012: public static final long PAGEPROVIDER_RESCAN_PERIOD = 2;
013:
014: Properties props = new Properties();
015:
016: TestEngine m_engine;
017:
018: public WikiEngineTest(String s) {
019: super (s);
020: }
021:
022: public static Test suite() {
023: return new TestSuite(WikiEngineTest.class);
024: }
025:
026: public static void main(String[] args) {
027: junit.textui.TestRunner
028: .main(new String[] { WikiEngineTest.class.getName() });
029: }
030:
031: public void setUp() throws Exception {
032: props.load(TestEngine.findTestProperties());
033:
034: props.setProperty(WikiEngine.PROP_MATCHPLURALS, "true");
035: // We'll need a shorter-than-default consistency check for
036: // the page-changed checks. This will cause additional load
037: // to the file system, though.
038: props.setProperty(CachingProvider.PROP_CACHECHECKINTERVAL, Long
039: .toString(PAGEPROVIDER_RESCAN_PERIOD));
040:
041: TestEngine.emptyWorkDir();
042: m_engine = new TestEngine(props);
043: }
044:
045: public void tearDown() {
046: String files = props
047: .getProperty(FileSystemProvider.PROP_PAGEDIR);
048:
049: if (files != null) {
050: File f = new File(files);
051:
052: TestEngine.deleteAll(f);
053: }
054:
055: TestEngine.emptyWorkDir();
056: }
057:
058: public void testNonExistantDirectory() throws Exception {
059: String tmpdir = System.getProperties().getProperty(
060: "java.io.tmpdir");
061: String dirname = "non-existant-directory";
062:
063: String newdir = tmpdir + File.separator + dirname;
064:
065: props.setProperty(FileSystemProvider.PROP_PAGEDIR, newdir);
066:
067: new TestEngine(props);
068:
069: File f = new File(newdir);
070:
071: assertTrue("didn't create it", f.exists());
072: assertTrue("isn't a dir", f.isDirectory());
073:
074: f.delete();
075: }
076:
077: public void testNonExistantDirProperty() throws Exception {
078: props.remove(FileSystemProvider.PROP_PAGEDIR);
079:
080: try {
081: new TestEngine(props);
082:
083: fail("Wiki did not warn about missing property.");
084: } catch (WikiException e) {
085: // This is okay.
086: }
087: }
088:
089: /**
090: * Check that calling pageExists( String ) works.
091: */
092: public void testNonExistantPage() throws Exception {
093: String pagename = "Test1";
094:
095: assertEquals("Page already exists", false, m_engine
096: .pageExists(pagename));
097: }
098:
099: /**
100: * Check that calling pageExists( WikiPage ) works.
101: */
102: public void testNonExistantPage2() throws Exception {
103: WikiPage page = new WikiPage(m_engine, "Test1");
104:
105: assertEquals("Page already exists", false, m_engine
106: .pageExists(page));
107: }
108:
109: public void testFinalPageName() throws Exception {
110: m_engine.saveText("Foobar", "1");
111: m_engine.saveText("Foobars", "2");
112:
113: assertEquals("plural mistake", "Foobars", m_engine
114: .getFinalPageName("Foobars"));
115:
116: assertEquals("singular mistake", "Foobar", m_engine
117: .getFinalPageName("Foobar"));
118: }
119:
120: public void testFinalPageNameSingular() throws Exception {
121: m_engine.saveText("Foobar", "1");
122:
123: assertEquals("plural mistake", "Foobar", m_engine
124: .getFinalPageName("Foobars"));
125: assertEquals("singular mistake", "Foobar", m_engine
126: .getFinalPageName("Foobar"));
127: }
128:
129: public void testFinalPageNamePlural() throws Exception {
130: m_engine.saveText("Foobars", "1");
131:
132: assertEquals("plural mistake", "Foobars", m_engine
133: .getFinalPageName("Foobars"));
134: assertEquals("singular mistake", "Foobars", m_engine
135: .getFinalPageName("Foobar"));
136: }
137:
138: public void testPutPage() throws Exception {
139: String text = "Foobar.\r\n";
140: String name = NAME1;
141:
142: m_engine.saveText(name, text);
143:
144: assertEquals("page does not exist", true, m_engine
145: .pageExists(name));
146:
147: assertEquals("wrong content", text, m_engine.getText(name));
148: }
149:
150: public void testPutPageEntities() throws Exception {
151: String text = "Foobar. "\r\n";
152: String name = NAME1;
153:
154: m_engine.saveText(name, text);
155:
156: assertEquals("page does not exist", true, m_engine
157: .pageExists(name));
158:
159: assertEquals("wrong content", "Foobar. "\r\n",
160: m_engine.getText(name));
161: }
162:
163: /**
164: * Cgeck that basic " is changed.
165: */
166: public void testPutPageEntities2() throws Exception {
167: String text = "Foobar. \"\r\n";
168: String name = NAME1;
169:
170: m_engine.saveText(name, text);
171:
172: assertEquals("page does not exist", true, m_engine
173: .pageExists(name));
174:
175: assertEquals("wrong content", "Foobar. "\r\n", m_engine
176: .getText(name));
177: }
178:
179: public void testGetHTML() throws Exception {
180: String text = "''Foobar.''";
181: String name = NAME1;
182:
183: m_engine.saveText(name, text);
184:
185: String data = m_engine.getHTML(name);
186:
187: assertEquals("<i>Foobar.</i>\n", data);
188: }
189:
190: public void testEncodeNameLatin1() {
191: String name = "abc\u00e5\u00e4\u00f6";
192:
193: assertEquals("abc%E5%E4%F6", m_engine.encodeName(name));
194: }
195:
196: public void testEncodeNameUTF8() throws Exception {
197: String name = "\u0041\u2262\u0391\u002E";
198:
199: props.setProperty(WikiEngine.PROP_ENCODING, "UTF-8");
200:
201: WikiEngine engine = new TestEngine(props);
202:
203: assertEquals("A%E2%89%A2%CE%91.", engine.encodeName(name));
204: }
205:
206: public void testReadLinks() throws Exception {
207: String src = "Foobar. [Foobar]. Frobozz. [This is a link].";
208:
209: Object[] result = m_engine.scanWikiLinks(
210: new WikiPage(m_engine, "Test"), src).toArray();
211:
212: assertEquals("item 0", "Foobar", result[0]);
213: assertEquals("item 1", "This is a link", result[1]);
214: }
215:
216: public void testBeautifyTitle() {
217: String src = "WikiNameThingy";
218:
219: assertEquals("Wiki Name Thingy", m_engine.beautifyTitle(src));
220: }
221:
222: /**
223: * Acronyms should be treated wisely.
224: */
225: public void testBeautifyTitleAcronym() {
226: String src = "JSPWikiPage";
227:
228: assertEquals("JSP Wiki Page", m_engine.beautifyTitle(src));
229: }
230:
231: /**
232: * Acronyms should be treated wisely.
233: */
234: public void testBeautifyTitleAcronym2() {
235: String src = "DELETEME";
236:
237: assertEquals("DELETEME", m_engine.beautifyTitle(src));
238: }
239:
240: public void testBeautifyTitleAcronym3() {
241: String src = "JSPWikiFAQ";
242:
243: assertEquals("JSP Wiki FAQ", m_engine.beautifyTitle(src));
244: }
245:
246: public void testBeautifyTitleNumbers() {
247: String src = "TestPage12";
248:
249: assertEquals("Test Page 12", m_engine.beautifyTitle(src));
250: }
251:
252: /**
253: * English articles too.
254: */
255: public void testBeautifyTitleArticle() {
256: String src = "ThisIsAPage";
257:
258: assertEquals("This Is A Page", m_engine.beautifyTitle(src));
259: }
260:
261: /**
262: * English articles too, pathological case...
263: */
264: /*
265: public void testBeautifyTitleArticle2()
266: {
267: String src = "ThisIsAJSPWikiPage";
268:
269: assertEquals("This Is A JSP Wiki Page", m_engine.beautifyTitle( src ) );
270: }
271: */
272:
273: public void testLatestGet() throws Exception {
274: props.setProperty("jspwiki.pageProvider",
275: "com.ecyrd.jspwiki.providers.VerySimpleProvider");
276: props.setProperty("jspwiki.usePageCache", "false");
277:
278: WikiEngine engine = new TestEngine(props);
279:
280: WikiPage p = engine.getPage("test", -1);
281:
282: VerySimpleProvider vsp = (VerySimpleProvider) engine
283: .getPageManager().getProvider();
284:
285: assertEquals("wrong page", "test", vsp.m_latestReq);
286: assertEquals("wrong version", -1, vsp.m_latestVers);
287: assertNotNull("null", p);
288: }
289:
290: public void testLatestGet2() throws Exception {
291: props.setProperty("jspwiki.pageProvider",
292: "com.ecyrd.jspwiki.providers.VerySimpleProvider");
293: props.setProperty("jspwiki.usePageCache", "false");
294:
295: WikiEngine engine = new TestEngine(props);
296:
297: String p = engine.getText("test", -1);
298:
299: VerySimpleProvider vsp = (VerySimpleProvider) engine
300: .getPageManager().getProvider();
301:
302: assertEquals("wrong page", "test", vsp.m_latestReq);
303: assertEquals("wrong version", -1, vsp.m_latestVers);
304: assertNotNull("null", p);
305: }
306:
307: public void testLatestGet3() throws Exception {
308: props.setProperty("jspwiki.pageProvider",
309: "com.ecyrd.jspwiki.providers.VerySimpleProvider");
310: props.setProperty("jspwiki.usePageCache", "false");
311:
312: WikiEngine engine = new TestEngine(props);
313:
314: String p = engine.getHTML("test", -1);
315:
316: VerySimpleProvider vsp = (VerySimpleProvider) engine
317: .getPageManager().getProvider();
318:
319: assertEquals("wrong page", "test", vsp.m_latestReq);
320: assertEquals("wrong version", 5, vsp.m_latestVers);
321: assertNotNull("null", p);
322: }
323:
324: public void testLatestGet4() throws Exception {
325: props.setProperty("jspwiki.pageProvider",
326: "com.ecyrd.jspwiki.providers.VerySimpleProvider");
327: props.setProperty("jspwiki.usePageCache", "true");
328:
329: WikiEngine engine = new TestEngine(props);
330:
331: String p = engine.getHTML(VerySimpleProvider.PAGENAME, -1);
332:
333: CachingProvider cp = (CachingProvider) engine.getPageManager()
334: .getProvider();
335: VerySimpleProvider vsp = (VerySimpleProvider) cp
336: .getRealProvider();
337:
338: assertEquals("wrong page", VerySimpleProvider.PAGENAME,
339: vsp.m_latestReq);
340: assertEquals("wrong version", -1, vsp.m_latestVers);
341: assertNotNull("null", p);
342: }
343:
344: /**
345: * Checks, if ReferenceManager is informed of new attachments.
346: */
347: public void testAttachmentRefs() throws Exception {
348: ReferenceManager refMgr = m_engine.getReferenceManager();
349: AttachmentManager attMgr = m_engine.getAttachmentManager();
350:
351: m_engine.saveText(NAME1, "fooBar");
352:
353: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
354: att.setAuthor("FirstPost");
355: attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
356:
357: try {
358: // and check post-conditions
359: Collection c = refMgr.findUncreated();
360: assertTrue("attachment exists: " + c, c == null
361: || c.size() == 0);
362:
363: c = refMgr.findUnreferenced();
364: assertEquals("unreferenced count", 2, c.size());
365: Iterator i = c.iterator();
366: String first = (String) i.next();
367: String second = (String) i.next();
368: assertTrue("unreferenced", (first.equals(NAME1) && second
369: .equals(NAME1 + "/TestAtt.txt"))
370: || (first.equals(NAME1 + "/TestAtt.txt") && second
371: .equals(NAME1)));
372: } finally {
373: // do cleanup
374: String files = props
375: .getProperty(FileSystemProvider.PROP_PAGEDIR);
376: TestEngine.deleteAll(new File(files, NAME1
377: + BasicAttachmentProvider.DIR_EXTENSION));
378: }
379: }
380:
381: /**
382: * Is ReferenceManager updated properly if a page references
383: * its own attachments?
384: */
385:
386: /*
387: FIXME: This is a deep problem. The real problem is that the reference
388: manager cannot know when it encounters a link like "testatt.txt" that it
389: is really a link to an attachment IF the link is created before
390: the attachment. This means that when the attachment is created,
391: the link will stay in the "uncreated" list.
392:
393: There are two issues here: first of all, TranslatorReader should
394: able to return the proper attachment references (which I think
395: it does), and second, the ReferenceManager should be able to
396: remove any links that are not referred to, nor they are created.
397:
398: However, doing this in a relatively sane timeframe can be a problem.
399: */
400:
401: public void testAttachmentRefs2() throws Exception {
402: ReferenceManager refMgr = m_engine.getReferenceManager();
403: AttachmentManager attMgr = m_engine.getAttachmentManager();
404:
405: m_engine.saveText(NAME1, "[TestAtt.txt]");
406:
407: // check a few pre-conditions
408:
409: Collection c = refMgr.findReferrers("TestAtt.txt");
410: assertTrue("normal, unexisting page", c != null
411: && ((String) c.iterator().next()).equals(NAME1));
412:
413: c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
414: assertTrue("no attachment", c == null || c.size() == 0);
415:
416: c = refMgr.findUncreated();
417: assertTrue("unknown attachment", c != null && c.size() == 1
418: && ((String) c.iterator().next()).equals("TestAtt.txt"));
419:
420: // now we create the attachment
421:
422: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
423: att.setAuthor("FirstPost");
424: attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
425: try {
426: // and check post-conditions
427: c = refMgr.findUncreated();
428: assertTrue("attachment exists: ", c == null
429: || c.size() == 0);
430:
431: c = refMgr.findReferrers("TestAtt.txt");
432: assertTrue("no normal page", c == null || c.size() == 0);
433:
434: c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
435: assertTrue("attachment exists now", c != null
436: && ((String) c.iterator().next()).equals(NAME1));
437:
438: c = refMgr.findUnreferenced();
439: assertTrue("unreferenced", c.size() == 1
440: && ((String) c.iterator().next()).equals(NAME1));
441: } finally {
442: // do cleanup
443: String files = props
444: .getProperty(FileSystemProvider.PROP_PAGEDIR);
445: TestEngine.deleteAll(new File(files, NAME1
446: + BasicAttachmentProvider.DIR_EXTENSION));
447: }
448: }
449:
450: /**
451: * Checks, if ReferenceManager is informed if a link to an attachment is added.
452: */
453: public void testAttachmentRefs3() throws Exception {
454: ReferenceManager refMgr = m_engine.getReferenceManager();
455: AttachmentManager attMgr = m_engine.getAttachmentManager();
456:
457: m_engine.saveText(NAME1, "fooBar");
458:
459: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
460: att.setAuthor("FirstPost");
461: attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
462:
463: m_engine.saveText(NAME1, " [" + NAME1 + "/TestAtt.txt] ");
464:
465: try {
466: // and check post-conditions
467: Collection c = refMgr.findUncreated();
468: assertTrue("attachment exists", c == null || c.size() == 0);
469:
470: c = refMgr.findUnreferenced();
471: assertEquals("unreferenced count", c.size(), 1);
472: assertTrue("unreferenced", ((String) c.iterator().next())
473: .equals(NAME1));
474: } finally {
475: // do cleanup
476: String files = props
477: .getProperty(FileSystemProvider.PROP_PAGEDIR);
478: TestEngine.deleteAll(new File(files, NAME1
479: + BasicAttachmentProvider.DIR_EXTENSION));
480: }
481: }
482:
483: /**
484: * Checks, if ReferenceManager is informed if a third page references an attachment.
485: */
486: public void testAttachmentRefs4() throws Exception {
487: ReferenceManager refMgr = m_engine.getReferenceManager();
488: AttachmentManager attMgr = m_engine.getAttachmentManager();
489:
490: m_engine.saveText(NAME1, "[TestPage2]");
491:
492: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
493: att.setAuthor("FirstPost");
494: attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
495:
496: m_engine.saveText("TestPage2", "[" + NAME1 + "/TestAtt.txt]");
497:
498: try {
499: // and check post-conditions
500: Collection c = refMgr.findUncreated();
501: assertTrue("attachment exists", c == null || c.size() == 0);
502:
503: c = refMgr.findUnreferenced();
504: assertEquals("unreferenced count", c.size(), 1);
505: assertTrue("unreferenced", ((String) c.iterator().next())
506: .equals(NAME1));
507: } finally {
508: // do cleanup
509: String files = props
510: .getProperty(FileSystemProvider.PROP_PAGEDIR);
511: TestEngine.deleteAll(new File(files, NAME1
512: + BasicAttachmentProvider.DIR_EXTENSION));
513: new File(files, "TestPage2" + FileSystemProvider.FILE_EXT)
514: .delete();
515: }
516: }
517:
518: public void testDeletePage() throws Exception {
519: m_engine.saveText(NAME1, "Test");
520:
521: String files = props
522: .getProperty(FileSystemProvider.PROP_PAGEDIR);
523: File saved = new File(files, NAME1
524: + FileSystemProvider.FILE_EXT);
525:
526: assertTrue("Didn't create it!", saved.exists());
527:
528: WikiPage page = m_engine.getPage(NAME1,
529: WikiProvider.LATEST_VERSION);
530:
531: m_engine.deletePage(page.getName());
532:
533: assertFalse("Page has not been removed!", saved.exists());
534: }
535:
536: public void testDeletePageAndAttachments() throws Exception {
537: m_engine.saveText(NAME1, "Test");
538:
539: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
540: att.setAuthor("FirstPost");
541: m_engine.getAttachmentManager().storeAttachment(att,
542: m_engine.makeAttachmentFile());
543:
544: String files = props
545: .getProperty(FileSystemProvider.PROP_PAGEDIR);
546: File saved = new File(files, NAME1
547: + FileSystemProvider.FILE_EXT);
548:
549: String atts = props
550: .getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
551: File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
552:
553: assertTrue("Didn't create it!", saved.exists());
554:
555: assertTrue("Attachment dir does not exist", attfile.exists());
556:
557: WikiPage page = m_engine.getPage(NAME1,
558: WikiProvider.LATEST_VERSION);
559:
560: m_engine.deletePage(page.getName());
561:
562: assertFalse("Page has not been removed!", saved.exists());
563: assertFalse("Attachment has not been removed", attfile.exists());
564: }
565:
566: public void testDeletePageAndAttachments2() throws Exception {
567: m_engine.saveText(NAME1, "Test");
568:
569: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
570: att.setAuthor("FirstPost");
571: m_engine.getAttachmentManager().storeAttachment(att,
572: m_engine.makeAttachmentFile());
573:
574: String files = props
575: .getProperty(FileSystemProvider.PROP_PAGEDIR);
576: File saved = new File(files, NAME1
577: + FileSystemProvider.FILE_EXT);
578:
579: String atts = props
580: .getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
581: File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
582:
583: assertTrue("Didn't create it!", saved.exists());
584:
585: assertTrue("Attachment dir does not exist", attfile.exists());
586:
587: WikiPage page = m_engine.getPage(NAME1,
588: WikiProvider.LATEST_VERSION);
589:
590: assertNotNull("page", page);
591:
592: att = m_engine.getAttachmentManager().getAttachmentInfo(
593: NAME1 + "/TestAtt.txt");
594:
595: m_engine.deletePage(att.getName());
596:
597: m_engine.deletePage(NAME1);
598:
599: assertNull("Page not removed", m_engine.getPage(NAME1));
600: assertNull("Att not removed", m_engine.getPage(NAME1
601: + "/TestAtt.txt"));
602:
603: Collection refs = m_engine.getReferenceManager().findReferrers(
604: NAME1);
605:
606: assertNull("referrers", refs);
607: }
608:
609: public void testDeleteVersion() throws Exception {
610: props.setProperty("jspwiki.pageProvider",
611: "VersioningFileProvider");
612:
613: TestEngine engine = new TestEngine(props);
614: engine.saveText(NAME1, "Test1");
615: engine.saveText(NAME1, "Test2");
616: engine.saveText(NAME1, "Test3");
617:
618: WikiPage page = engine.getPage(NAME1, 3);
619:
620: engine.deleteVersion(page);
621:
622: assertNull("got page", engine.getPage(NAME1, 3));
623:
624: String content = engine.getText(NAME1,
625: WikiProvider.LATEST_VERSION);
626:
627: assertEquals("content", "Test2", content.trim());
628: }
629:
630: public void testDeleteVersion2() throws Exception {
631: props.setProperty("jspwiki.pageProvider",
632: "VersioningFileProvider");
633:
634: TestEngine engine = new TestEngine(props);
635: engine.saveText(NAME1, "Test1");
636: engine.saveText(NAME1, "Test2");
637: engine.saveText(NAME1, "Test3");
638:
639: WikiPage page = engine.getPage(NAME1, 1);
640:
641: engine.deleteVersion(page);
642:
643: assertNull("got page", engine.getPage(NAME1, 1));
644:
645: String content = engine.getText(NAME1,
646: WikiProvider.LATEST_VERSION);
647:
648: assertEquals("content", "Test3", content.trim());
649:
650: assertEquals("content1", "", engine.getText(NAME1, 1).trim());
651: }
652:
653: /**
654: * Assumes that CachingProvider is in use.
655: */
656: public void testExternalModificationRefs() throws Exception {
657: ReferenceManager refMgr = m_engine.getReferenceManager();
658:
659: m_engine.saveText(NAME1, "[Foobar]");
660: m_engine.getText(NAME1); // Ensure that page is cached.
661:
662: Collection c = refMgr.findUncreated();
663: assertTrue(
664: "Non-existent reference not detected by ReferenceManager",
665: Util.collectionContains(c, "Foobar"));
666:
667: Thread.sleep(2000L); // Wait two seconds for filesystem granularity
668:
669: String files = props
670: .getProperty(FileSystemProvider.PROP_PAGEDIR);
671:
672: File saved = new File(files, NAME1
673: + FileSystemProvider.FILE_EXT);
674:
675: assertTrue("No file!", saved.exists());
676:
677: FileWriter out = new FileWriter(saved);
678: FileUtil.copyContents(new StringReader("[Puppaa]"), out);
679: out.close();
680:
681: Thread.sleep(2000L * PAGEPROVIDER_RESCAN_PERIOD); // Wait five seconds for CachingProvider to wake up.
682:
683: String text = m_engine.getText(NAME1);
684:
685: assertEquals("wrong contents", "[Puppaa]", text);
686:
687: c = refMgr.findUncreated();
688:
689: assertTrue("Non-existent reference after external page change "
690: + "not detected by ReferenceManager", Util
691: .collectionContains(c, "Puppaa"));
692: }
693:
694: /**
695: * Assumes that CachingProvider is in use.
696: */
697: public void testExternalModificationRefsDeleted() throws Exception {
698: ReferenceManager refMgr = m_engine.getReferenceManager();
699:
700: m_engine.saveText(NAME1, "[Foobar]");
701: m_engine.getText(NAME1); // Ensure that page is cached.
702:
703: Collection c = refMgr.findUncreated();
704: assertEquals("uncreated count", 1, c.size());
705: assertEquals("wrong referenced page", "Foobar", (String) c
706: .iterator().next());
707:
708: Thread.sleep(2000L); // Wait two seconds for filesystem granularity
709:
710: String files = props
711: .getProperty(FileSystemProvider.PROP_PAGEDIR);
712:
713: File saved = new File(files, NAME1
714: + FileSystemProvider.FILE_EXT);
715:
716: assertTrue("No file!", saved.exists());
717:
718: saved.delete();
719:
720: assertFalse("File not deleted!", saved.exists());
721:
722: Thread.sleep(2000L * PAGEPROVIDER_RESCAN_PERIOD); // Wait five seconds for CachingProvider to catch up.
723:
724: WikiPage p = m_engine.getPage(NAME1);
725:
726: assertNull("Got page!", p);
727:
728: String text = m_engine.getText(NAME1);
729:
730: assertEquals("wrong contents", "", text);
731:
732: c = refMgr.findUncreated();
733: assertEquals("NEW: uncreated count", 0, c.size());
734: }
735:
736: /**
737: * Assumes that CachingProvider is in use.
738: */
739: public void testExternalModification() throws Exception {
740: m_engine.saveText(NAME1, "Foobar");
741:
742: m_engine.getText(NAME1); // Ensure that page is cached.
743:
744: Thread.sleep(2000L); // Wait two seconds for filesystem granularity
745:
746: String files = props
747: .getProperty(FileSystemProvider.PROP_PAGEDIR);
748:
749: File saved = new File(files, NAME1
750: + FileSystemProvider.FILE_EXT);
751:
752: assertTrue("No file!", saved.exists());
753:
754: FileWriter out = new FileWriter(saved);
755: FileUtil.copyContents(new StringReader("Puppaa"), out);
756: out.close();
757:
758: // Wait for the caching provider to notice a refresh.
759: Thread.sleep(2000L * PAGEPROVIDER_RESCAN_PERIOD);
760:
761: // Trim - engine.saveText() may append a newline.
762: String text = m_engine.getText(NAME1).trim();
763: assertEquals("wrong contents", "Puppaa", text);
764: }
765:
766: /**
767: * Tests BugReadingOfVariableNotWorkingForOlderVersions
768: * @throws Exception
769: */
770: public void testOldVersionVars() throws Exception {
771: Properties pr = new Properties();
772: pr.load(TestEngine
773: .findTestProperties("/jspwiki_vers.properties"));
774:
775: pr.setProperty(PageManager.PROP_USECACHE, "true");
776:
777: TestEngine engine = new TestEngine(pr);
778:
779: engine.saveText(NAME1, "[{SET foo=bar}]");
780:
781: engine.saveText(NAME1, "[{SET foo=notbar}]");
782:
783: WikiPage v1 = engine.getPage(NAME1, 1);
784:
785: WikiPage v2 = engine.getPage(NAME1, 2);
786:
787: assertEquals("V1", "bar", v1.getAttribute("foo"));
788:
789: // FIXME: The following must run as well
790: assertEquals("V2", "notbar", v2.getAttribute("foo"));
791:
792: engine.deletePage(NAME1);
793: }
794:
795: public void testSpacedNames1() throws Exception {
796: m_engine.saveText("This is a test", "puppaa");
797:
798: assertEquals("normal", "puppaa", m_engine.getText(
799: "This is a test").trim());
800: assertEquals("lowercase", "puppaa", m_engine.getText(
801: "this is a test").trim());
802: assertEquals("randomcase", "puppaa", m_engine.getText(
803: "ThiS Is a teSt").trim());
804: }
805:
806: public void testParsedVariables() throws Exception {
807: m_engine.saveText("TestPage",
808: "[{SET foo=bar}][{SamplePlugin text='{$foo}'}]");
809:
810: String res = m_engine.getHTML("TestPage");
811:
812: assertEquals("bar\n", res);
813: }
814:
815: /**
816: * Tests BugReferenceToRenamedPageNotCleared
817: *
818: * @throws Exception
819: */
820: public void testRename() throws Exception {
821: m_engine.saveText("RenameBugTestPage",
822: "Mary had a little generic object");
823: m_engine.saveText("OldNameTestPage",
824: "Linked to RenameBugTestPage");
825:
826: Collection pages = m_engine.getReferenceManager()
827: .findReferrers("RenameBugTestPage");
828: assertEquals("has one", "OldNameTestPage", pages.iterator()
829: .next());
830:
831: WikiContext ctx = new WikiContext(m_engine, m_engine
832: .getPage("OldNameTestPage"));
833:
834: m_engine.renamePage(ctx, "OldNameTestPage", "NewNameTestPage",
835: true);
836:
837: assertFalse("did not vanish", m_engine
838: .pageExists("OldNameTestPage"));
839: assertTrue("did not appear", m_engine
840: .pageExists("NewNameTestPage"));
841:
842: pages = m_engine.getReferenceManager().findReferrers(
843: "RenameBugTestPage");
844:
845: assertEquals("wrong # of referrers", 1, pages.size());
846:
847: assertEquals("has wrong referrer", "NewNameTestPage", pages
848: .iterator().next());
849: }
850:
851: public void testChangeNoteOldVersion2() throws Exception {
852: WikiPage p = new WikiPage(m_engine, NAME1);
853:
854: WikiContext context = new WikiContext(m_engine, p);
855:
856: context.getPage().setAttribute(WikiPage.CHANGENOTE,
857: "Test change");
858:
859: m_engine.saveText(context, "test");
860:
861: for (int i = 0; i < 5; i++) {
862: WikiPage p2 = (WikiPage) m_engine.getPage(NAME1).clone();
863: p2.removeAttribute(WikiPage.CHANGENOTE);
864:
865: context.setPage(p2);
866:
867: m_engine.saveText(context, "test" + i);
868: }
869:
870: WikiPage p3 = m_engine.getPage(NAME1, -1);
871:
872: assertEquals(null, p3.getAttribute(WikiPage.CHANGENOTE));
873: }
874:
875: }
|