001: package com.ecyrd.jspwiki.providers;
002:
003: import junit.framework.*;
004: import java.io.*;
005: import java.util.*;
006:
007: import com.ecyrd.jspwiki.*;
008:
009: // FIXME: Should this thingy go directly to the VersioningFileProvider,
010: // or should it rely on the WikiEngine API?
011:
012: public class VersioningFileProviderTest extends TestCase {
013: public static final String NAME1 = "Test1";
014:
015: Properties props = new Properties();
016:
017: TestEngine engine;
018:
019: public VersioningFileProviderTest(String s) {
020: super (s);
021: }
022:
023: public void setUp() throws Exception {
024: props.load(TestEngine
025: .findTestProperties("/jspwiki_vers.properties"));
026:
027: engine = new TestEngine(props);
028: }
029:
030: public void tearDown() {
031: String files = props
032: .getProperty(FileSystemProvider.PROP_PAGEDIR);
033:
034: // Remove file
035: File f = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
036: f.delete();
037:
038: f = new File(files, "OLD");
039:
040: TestEngine.deleteAll(f);
041: }
042:
043: /**
044: * Checks if migration from FileSystemProvider to VersioningFileProvider
045: * works by creating a dummy file without corresponding content in OLD/
046: */
047: public void testMigration() throws IOException {
048: String files = props
049: .getProperty(FileSystemProvider.PROP_PAGEDIR);
050:
051: File f = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
052:
053: Writer out = new FileWriter(f);
054: FileUtil.copyContents(new StringReader("foobar"), out);
055: out.close();
056:
057: String res = engine.getText(NAME1);
058:
059: assertEquals("latest did not work", "foobar", res);
060:
061: res = engine.getText(NAME1, 1); // Should be the first version.
062:
063: assertEquals("fetch by direct version did not work", "foobar",
064: res);
065: }
066:
067: public void testMillionChanges() throws Exception {
068: String text = "";
069: String name = NAME1;
070: int maxver = 100; // Save 100 versions.
071:
072: for (int i = 0; i < maxver; i++) {
073: text = text + ".";
074: engine.saveText(name, text);
075: }
076:
077: WikiPage pageinfo = engine.getPage(NAME1);
078:
079: assertEquals("wrong version", maxver, pageinfo.getVersion());
080:
081: // +2 comes from \r\n.
082: assertEquals("wrong text", maxver + 2, engine.getText(NAME1)
083: .length());
084: }
085:
086: public void testCheckin() throws Exception {
087: String text = "diddo\r\n";
088:
089: engine.saveText(NAME1, text);
090:
091: String res = engine.getText(NAME1);
092:
093: assertEquals(text, res);
094: }
095:
096: public void testGetByVersion() throws Exception {
097: String text = "diddo\r\n";
098:
099: engine.saveText(NAME1, text);
100:
101: WikiPage page = engine.getPage(NAME1, 1);
102:
103: assertEquals("name", NAME1, page.getName());
104: assertEquals("version", 1, page.getVersion());
105: }
106:
107: public void testPageInfo() throws Exception {
108: String text = "diddo\r\n";
109:
110: engine.saveText(NAME1, text);
111:
112: WikiPage res = engine.getPage(NAME1);
113:
114: assertEquals(1, res.getVersion());
115: }
116:
117: public void testGetOldVersion() throws Exception {
118: String text = "diddo\r\n";
119: String text2 = "barbar\r\n";
120: String text3 = "Barney\r\n";
121:
122: engine.saveText(NAME1, text);
123: engine.saveText(NAME1, text2);
124: engine.saveText(NAME1, text3);
125:
126: WikiPage res = engine.getPage(NAME1);
127:
128: assertEquals("wrong version", 3, res.getVersion());
129:
130: assertEquals("ver1", text, engine.getText(NAME1, 1));
131: assertEquals("ver2", text2, engine.getText(NAME1, 2));
132: assertEquals("ver3", text3, engine.getText(NAME1, 3));
133: }
134:
135: public void testGetOldVersion2() throws Exception {
136: String text = "diddo\r\n";
137: String text2 = "barbar\r\n";
138: String text3 = "Barney\r\n";
139:
140: engine.saveText(NAME1, text);
141: engine.saveText(NAME1, text2);
142: engine.saveText(NAME1, text3);
143:
144: WikiPage res = engine.getPage(NAME1);
145:
146: assertEquals("wrong version", 3, res.getVersion());
147:
148: assertEquals("ver1", 1, engine.getPage(NAME1, 1).getVersion());
149: assertEquals("ver2", 2, engine.getPage(NAME1, 2).getVersion());
150: assertEquals("ver3", 3, engine.getPage(NAME1, 3).getVersion());
151: }
152:
153: /**
154: * 2.0.7 and before got this wrong.
155: */
156: public void testGetOldVersionUTF8() throws Exception {
157: String text = "åäö\r\n";
158: String text2 = "barbaröö\r\n";
159: String text3 = "Barneyää\r\n";
160:
161: engine.saveText(NAME1, text);
162: engine.saveText(NAME1, text2);
163: engine.saveText(NAME1, text3);
164:
165: WikiPage res = engine.getPage(NAME1);
166:
167: assertEquals("wrong version", 3, res.getVersion());
168:
169: assertEquals("ver1", text, engine.getText(NAME1, 1));
170: assertEquals("ver2", text2, engine.getText(NAME1, 2));
171: assertEquals("ver3", text3, engine.getText(NAME1, 3));
172: }
173:
174: public void testNonexistantPage() {
175: assertNull(engine.getPage("fjewifjeiw"));
176: }
177:
178: public void testVersionHistory() throws Exception {
179: String text = "diddo\r\n";
180: String text2 = "barbar\r\n";
181: String text3 = "Barney\r\n";
182:
183: engine.saveText(NAME1, text);
184: engine.saveText(NAME1, text2);
185: engine.saveText(NAME1, text3);
186:
187: Collection history = engine.getVersionHistory(NAME1);
188:
189: assertEquals("size", 3, history.size());
190: }
191:
192: public void testDelete() throws Exception {
193: engine.saveText(NAME1, "v1");
194: engine.saveText(NAME1, "v2");
195: engine.saveText(NAME1, "v3");
196:
197: PageManager mgr = engine.getPageManager();
198: WikiPageProvider provider = mgr.getProvider();
199:
200: provider.deletePage(NAME1);
201:
202: String files = props
203: .getProperty(FileSystemProvider.PROP_PAGEDIR);
204:
205: File f = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
206:
207: assertFalse("file exists", f.exists());
208:
209: f = new File(files, NAME1 + ".properties");
210:
211: assertFalse("RCS file exists", f.exists());
212: }
213:
214: public void testDeleteVersion() throws Exception {
215: engine.saveText(NAME1, "v1\r\n");
216: engine.saveText(NAME1, "v2\r\n");
217: engine.saveText(NAME1, "v3\r\n");
218:
219: PageManager mgr = engine.getPageManager();
220: WikiPageProvider provider = mgr.getProvider();
221:
222: List l = provider.getVersionHistory(NAME1);
223: assertEquals("wrong # of versions", 3, l.size());
224:
225: provider.deleteVersion(NAME1, 2);
226:
227: l = provider.getVersionHistory(NAME1);
228:
229: assertEquals("wrong # of versions", 2, l.size());
230:
231: assertEquals("v1", "v1\r\n", provider.getPageText(NAME1, 1));
232: assertEquals("v3", "v3\r\n", provider.getPageText(NAME1, 3));
233:
234: try {
235: provider.getPageText(NAME1, 2);
236: fail("v2");
237: } catch (NoSuchVersionException e) {
238: // This is expected
239: }
240: }
241:
242: public void testChangeNote() throws Exception {
243: WikiPage p = new WikiPage(engine, NAME1);
244: p.setAttribute(WikiPage.CHANGENOTE, "Test change");
245: WikiContext context = new WikiContext(engine, p);
246:
247: engine.saveText(context, "test");
248:
249: WikiPage p2 = engine.getPage(NAME1);
250:
251: assertEquals("Test change", p2
252: .getAttribute(WikiPage.CHANGENOTE));
253: }
254:
255: public void testChangeNoteOldVersion() throws Exception {
256: WikiPage p = new WikiPage(engine, NAME1);
257:
258: WikiContext context = new WikiContext(engine, p);
259:
260: context.getPage().setAttribute(WikiPage.CHANGENOTE,
261: "Test change");
262: engine.saveText(context, "test");
263:
264: context.getPage().setAttribute(WikiPage.CHANGENOTE, "Change 2");
265: engine.saveText(context, "test2");
266:
267: WikiPage p2 = engine.getPage(NAME1, 1);
268:
269: assertEquals("Test change", p2
270: .getAttribute(WikiPage.CHANGENOTE));
271:
272: WikiPage p3 = engine.getPage(NAME1, 2);
273:
274: assertEquals("Change 2", p3.getAttribute(WikiPage.CHANGENOTE));
275: }
276:
277: public void testChangeNoteOldVersion2() throws Exception {
278: WikiPage p = new WikiPage(engine, NAME1);
279:
280: WikiContext context = new WikiContext(engine, p);
281:
282: context.getPage().setAttribute(WikiPage.CHANGENOTE,
283: "Test change");
284:
285: engine.saveText(context, "test");
286:
287: for (int i = 0; i < 5; i++) {
288: WikiPage p2 = (WikiPage) engine.getPage(NAME1).clone();
289: p2.removeAttribute(WikiPage.CHANGENOTE);
290:
291: context.setPage(p2);
292:
293: engine.saveText(context, "test" + i);
294: }
295:
296: WikiPage p3 = engine.getPage(NAME1, -1);
297:
298: assertEquals(null, p3.getAttribute(WikiPage.CHANGENOTE));
299: }
300:
301: public static Test suite() {
302: return new TestSuite(VersioningFileProviderTest.class);
303: }
304: }
|