001: package com.ecyrd.jspwiki;
002:
003: import java.util.Collection;
004: import java.util.Properties;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009:
010: import com.ecyrd.jspwiki.attachment.Attachment;
011:
012: public class PageRenamerTest extends TestCase {
013: TestEngine m_engine;
014:
015: protected void setUp() throws Exception {
016: super .setUp();
017:
018: Properties props = new Properties();
019:
020: props.load(TestEngine.findTestProperties());
021:
022: props.setProperty(WikiEngine.PROP_MATCHPLURALS, "true");
023:
024: TestEngine.emptyWorkDir();
025: m_engine = new TestEngine(props);
026: }
027:
028: protected void tearDown() throws Exception {
029: super .tearDown();
030:
031: TestEngine.deleteTestPage("TestPage");
032: TestEngine.deleteTestPage("TestPage2");
033: TestEngine.deleteTestPage("FooTest");
034: TestEngine.deleteTestPage("Test");
035: TestEngine.deleteTestPage("CdauthNew");
036: TestEngine.deleteTestPage("Cdauth");
037:
038: TestEngine.emptyWorkDir();
039: }
040:
041: public void testSimpleRename() throws Exception {
042: // Count the numberof existing references
043: int refCount = m_engine.getReferenceManager().findCreated()
044: .size();
045:
046: m_engine.saveText("TestPage", "the big lazy dog thing");
047:
048: WikiPage p = m_engine.getPage("TestPage");
049:
050: WikiContext context = new WikiContext(m_engine, p);
051:
052: m_engine.renamePage(context, "TestPage", "FooTest", false);
053:
054: WikiPage newpage = m_engine.getPage("FooTest");
055:
056: assertNotNull("no new page", newpage);
057: assertNull("old page not gone", m_engine.getPage("TestPage"));
058:
059: // Refmgr
060:
061: Collection refs = m_engine.getReferenceManager().findCreated();
062:
063: assertEquals("wrong list size", refCount + 1, refs.size());
064: assertTrue(refs.contains("FooTest"));
065: }
066:
067: public void testReferrerChange() throws Exception {
068: m_engine.saveText("TestPage", "foofoo");
069: m_engine.saveText("TestPage2", "[TestPage]");
070:
071: WikiPage p = m_engine.getPage("TestPage");
072:
073: WikiContext context = new WikiContext(m_engine, p);
074:
075: m_engine.renamePage(context, "TestPage", "FooTest", true);
076:
077: String data = m_engine.getPureText("TestPage2",
078: WikiProvider.LATEST_VERSION);
079:
080: assertEquals("no rename", "[FooTest]", data.trim());
081:
082: Collection refs = m_engine.getReferenceManager().findReferrers(
083: "TestPage");
084:
085: assertNull("oldpage", refs);
086:
087: refs = m_engine.getReferenceManager().findReferrers("FooTest");
088: assertEquals("new size", 1, refs.size());
089: assertEquals("wrong ref", "TestPage2", (String) refs.iterator()
090: .next());
091: }
092:
093: public void testReferrerChangeCC() throws Exception {
094: m_engine.saveText("TestPage", "foofoo");
095: m_engine.saveText("TestPage2", "TestPage");
096:
097: WikiPage p = m_engine.getPage("TestPage");
098:
099: WikiContext context = new WikiContext(m_engine, p);
100:
101: m_engine.renamePage(context, "TestPage", "FooTest", true);
102:
103: String data = m_engine.getPureText("TestPage2",
104: WikiProvider.LATEST_VERSION);
105:
106: assertEquals("no rename", "FooTest", data.trim());
107: Collection refs = m_engine.getReferenceManager().findReferrers(
108: "TestPage");
109:
110: assertNull("oldpage", refs);
111:
112: refs = m_engine.getReferenceManager().findReferrers("FooTest");
113: assertEquals("new size", 1, refs.size());
114: assertEquals("wrong ref", "TestPage2", (String) refs.iterator()
115: .next());
116: }
117:
118: public void testReferrerChangeAnchor() throws Exception {
119: m_engine.saveText("TestPage", "foofoo");
120: m_engine.saveText("TestPage2", "[TestPage#heading1]");
121:
122: WikiPage p = m_engine.getPage("TestPage");
123:
124: WikiContext context = new WikiContext(m_engine, p);
125:
126: m_engine.renamePage(context, "TestPage", "FooTest", true);
127:
128: String data = m_engine.getPureText("TestPage2",
129: WikiProvider.LATEST_VERSION);
130:
131: assertEquals("no rename", "[FooTest#heading1]", data.trim());
132: Collection refs = m_engine.getReferenceManager().findReferrers(
133: "TestPage");
134:
135: assertNull("oldpage", refs);
136:
137: refs = m_engine.getReferenceManager().findReferrers("FooTest");
138: assertEquals("new size", 1, refs.size());
139: assertEquals("wrong ref", "TestPage2", (String) refs.iterator()
140: .next());
141: }
142:
143: public void testReferrerChangeMultilink() throws Exception {
144: m_engine.saveText("TestPage", "foofoo");
145: m_engine
146: .saveText(
147: "TestPage2",
148: "[TestPage] [TestPage] [linktext|TestPage] TestPage [linktext|TestPage] [TestPage#Anchor] [TestPage] TestPage [TestPage]");
149:
150: WikiPage p = m_engine.getPage("TestPage");
151:
152: WikiContext context = new WikiContext(m_engine, p);
153:
154: m_engine.renamePage(context, "TestPage", "FooTest", true);
155:
156: String data = m_engine.getPureText("TestPage2",
157: WikiProvider.LATEST_VERSION);
158:
159: assertEquals(
160: "no rename",
161: "[FooTest] [FooTest] [linktext|FooTest] FooTest [linktext|FooTest] [FooTest#Anchor] [FooTest] FooTest [FooTest]",
162: data.trim());
163:
164: Collection refs = m_engine.getReferenceManager().findReferrers(
165: "TestPage");
166:
167: assertNull("oldpage", refs);
168:
169: refs = m_engine.getReferenceManager().findReferrers("FooTest");
170: assertEquals("new size", 1, refs.size());
171: assertEquals("wrong ref", "TestPage2", (String) refs.iterator()
172: .next());
173: }
174:
175: public void testReferrerNoWikiName() throws Exception {
176: m_engine.saveText("Test", "foo");
177: m_engine
178: .saveText("TestPage2",
179: "[Test] [Test#anchor] test Test [test] [link|test] [link|test]");
180:
181: WikiPage p = m_engine.getPage("TestPage");
182:
183: WikiContext context = new WikiContext(m_engine, p);
184:
185: m_engine.renamePage(context, "Test", "TestPage", true);
186:
187: String data = m_engine.getPureText("TestPage2",
188: WikiProvider.LATEST_VERSION);
189:
190: assertEquals(
191: "wrong data",
192: "[TestPage] [TestPage#anchor] test Test [TestPage] [link|TestPage] [link|TestPage]",
193: data.trim());
194: }
195:
196: public void testAttachmentChange() throws Exception {
197: m_engine.saveText("TestPage", "foofoo");
198: m_engine.saveText("TestPage2",
199: "[TestPage/foo.txt] [linktext|TestPage/bar.jpg]");
200:
201: m_engine.addAttachment("TestPage", "foo.txt", "testing"
202: .getBytes());
203: m_engine
204: .addAttachment("TestPage", "bar.jpg", "pr0n".getBytes());
205: WikiPage p = m_engine.getPage("TestPage");
206:
207: WikiContext context = new WikiContext(m_engine, p);
208:
209: m_engine.renamePage(context, "TestPage", "FooTest", true);
210:
211: String data = m_engine.getPureText("TestPage2",
212: WikiProvider.LATEST_VERSION);
213:
214: assertEquals("no rename",
215: "[FooTest/foo.txt] [linktext|FooTest/bar.jpg]", data
216: .trim());
217:
218: Attachment att = m_engine.getAttachmentManager()
219: .getAttachmentInfo("FooTest/foo.txt");
220: assertNotNull("footext", att);
221:
222: att = m_engine.getAttachmentManager().getAttachmentInfo(
223: "FooTest/bar.jpg");
224: assertNotNull("barjpg", att);
225:
226: att = m_engine.getAttachmentManager().getAttachmentInfo(
227: "TestPage/bar.jpg");
228: assertNull("testpage/bar.jpg exists", att);
229:
230: att = m_engine.getAttachmentManager().getAttachmentInfo(
231: "TestPage/foo.txt");
232: assertNull("testpage/foo.txt exists", att);
233:
234: Collection refs = m_engine.getReferenceManager().findReferrers(
235: "TestPage/bar.jpg");
236:
237: assertNull("oldpage", refs);
238:
239: refs = m_engine.getReferenceManager().findReferrers(
240: "FooTest/bar.jpg");
241: assertEquals("new size", 1, refs.size());
242: assertEquals("wrong ref", "TestPage2", (String) refs.iterator()
243: .next());
244: }
245:
246: public void testSamePage() throws Exception {
247: m_engine.saveText("TestPage", "[TestPage]");
248:
249: rename("TestPage", "FooTest");
250:
251: WikiPage p = m_engine.getPage("FooTest");
252:
253: assertNotNull("no page", p);
254:
255: assertEquals("[FooTest]", m_engine.getText("FooTest").trim());
256: }
257:
258: public void testBrokenLink1() throws Exception {
259: m_engine.saveText("TestPage", "hubbub");
260: m_engine.saveText("TestPage2", "[TestPage|]");
261:
262: rename("TestPage", "FooTest");
263:
264: WikiPage p = m_engine.getPage("FooTest");
265:
266: assertNotNull("no page", p);
267:
268: // Should be no change
269: assertEquals("[TestPage|]", m_engine.getText("TestPage2")
270: .trim());
271: }
272:
273: public void testBrokenLink2() throws Exception {
274: m_engine.saveText("TestPage", "hubbub");
275: m_engine.saveText("TestPage2", "[|TestPage]");
276:
277: WikiPage p;
278: rename("TestPage", "FooTest");
279:
280: p = m_engine.getPage("FooTest");
281:
282: assertNotNull("no page", p);
283:
284: assertEquals("[|FooTest]", m_engine.getText("TestPage2").trim());
285: }
286:
287: private void rename(String src, String dst) throws WikiException {
288: WikiPage p = m_engine.getPage(src);
289:
290: WikiContext context = new WikiContext(m_engine, p);
291:
292: m_engine.renamePage(context, src, dst, true);
293: }
294:
295: public void testBug25() throws Exception {
296: String src = "[Cdauth/attach.txt] [link|Cdauth/attach.txt] [cdauth|Cdauth/attach.txt]"
297: + "[CDauth/attach.txt] [link|CDauth/attach.txt] [cdauth|CDauth/attach.txt]"
298: + "[cdauth/attach.txt] [link|cdauth/attach.txt] [cdauth|cdauth/attach.txt]";
299:
300: String dst = "[CdauthNew/attach.txt] [link|CdauthNew/attach.txt] [cdauth|CdauthNew/attach.txt]"
301: + "[CDauth/attach.txt] [link|CDauth/attach.txt] [cdauth|CDauth/attach.txt]"
302: + "[CdauthNew/attach.txt] [link|CdauthNew/attach.txt] [cdauth|CdauthNew/attach.txt]";
303:
304: m_engine.saveText("Cdauth", "xxx");
305: m_engine.saveText("TestPage", src);
306:
307: m_engine.addAttachment("Cdauth", "attach.txt", "Puppua"
308: .getBytes());
309:
310: rename("Cdauth", "CdauthNew");
311:
312: assertEquals(dst, m_engine.getText("TestPage").trim());
313: }
314:
315: public void testBug21() throws Exception {
316: String src = "[Link to TestPage2|TestPage2]";
317:
318: m_engine.saveText("TestPage", src);
319: m_engine.saveText("TestPage2", "foo");
320:
321: rename("TestPage2", "Test");
322:
323: assertEquals("[Link to Test|Test]", m_engine
324: .getText("TestPage").trim());
325: }
326:
327: public void testExtendedLinks() throws Exception {
328: String src = "[Link to TestPage2|TestPage2|target='_new']";
329:
330: m_engine.saveText("TestPage", src);
331: m_engine.saveText("TestPage2", "foo");
332:
333: rename("TestPage2", "Test");
334:
335: assertEquals("[Link to Test|Test|target='_new']", m_engine
336: .getText("TestPage").trim());
337: }
338:
339: public void testBug85_case1() throws Exception {
340: // renaming a non-existing page
341: // This fails under 2.5.116, cfr. with http://bugs.jspwiki.org/show_bug.cgi?id=85
342: // m_engine.saveText( "TestPage", "blablahblahbla" );
343: try {
344: rename("TestPage123", "Main8887");
345: rename("Main8887", "TestPage123");
346: } catch (NullPointerException npe) {
347: npe.printStackTrace();
348: System.out.println("NPE: Bug 85 caught?");
349: fail();
350: }
351: }
352:
353: public void testBug85_case2() throws Exception {
354: try {
355: // renaming a non-existing page, but we call m_engine.saveText() before renaming
356: // this does not fail under 2.5.116
357: m_engine.saveText("TestPage1234", "blablahblahbla");
358: rename("TestPage1234", "Main8887");
359: rename("Main8887", "TestPage1234");
360: } catch (NullPointerException npe) {
361: npe.printStackTrace();
362: System.out.println("NPE: Bug 85 caught?");
363: fail();
364: }
365: }
366:
367: public void testBug85_case3() throws Exception {
368: try {
369: // renaming an existing page
370: // this does not fail under 2.5.116
371: // m_engine.saveText( "Main", "blablahblahbla" );
372: rename("Main", "Main8887");
373: rename("Main8887", "Main");
374: } catch (NullPointerException npe) {
375: npe.printStackTrace();
376: System.out.println("NPE: Bug 85 caught?");
377: fail();
378: }
379: }
380:
381: public void testBug85_case4() throws Exception {
382: try {
383: // renaming an existing page, and we call m_engine.saveText() before renaming
384: // this does not fail under 2.5.116
385: m_engine.saveText("Main", "blablahblahbla");
386: rename("Main", "Main8887");
387: rename("Main8887", "Main");
388: } catch (NullPointerException npe) {
389: npe.printStackTrace();
390: System.out.println("NPE: Bug 85 caught?");
391: fail();
392: }
393: }
394:
395: public static Test suite() {
396: return new TestSuite(PageRenamerTest.class);
397: }
398:
399: }
|