001: package com.ecyrd.jspwiki.providers;
002:
003: import junit.framework.*;
004: import java.io.*;
005: import java.util.*;
006:
007: import org.apache.log4j.*;
008:
009: import com.ecyrd.jspwiki.*;
010:
011: public class FileSystemProviderTest extends TestCase {
012: FileSystemProvider m_provider;
013: FileSystemProvider m_providerUTF8;
014: String m_pagedir;
015: Properties props = new Properties();
016:
017: TestEngine m_engine;
018:
019: public FileSystemProviderTest(String s) {
020: super (s);
021: }
022:
023: public void setUp() throws Exception {
024: m_pagedir = System.getProperties()
025: .getProperty("java.io.tmpdir");
026:
027: Properties props2 = new Properties();
028:
029: props.setProperty(PageManager.PROP_PAGEPROVIDER,
030: "FileSystemProvider");
031: props.setProperty(FileSystemProvider.PROP_PAGEDIR, m_pagedir);
032:
033: props2.load(TestEngine.findTestProperties());
034: PropertyConfigurator.configure(props2);
035:
036: m_engine = new TestEngine(props);
037:
038: m_provider = new FileSystemProvider();
039:
040: m_provider.initialize(m_engine, props);
041:
042: props.setProperty(WikiEngine.PROP_ENCODING, "UTF-8");
043: m_providerUTF8 = new FileSystemProvider();
044: m_providerUTF8.initialize(m_engine, props);
045: }
046:
047: public void tearDown() {
048: TestEngine.deleteAll(new File(m_pagedir));
049: }
050:
051: public void testScandinavianLetters() throws Exception {
052: WikiPage page = new WikiPage(m_engine, "\u00c5\u00e4Test");
053:
054: m_provider.putPageText(page, "test");
055:
056: File resultfile = new File(m_pagedir, "%C5%E4Test.txt");
057:
058: assertTrue("No such file", resultfile.exists());
059:
060: String contents = FileUtil.readContents(new FileInputStream(
061: resultfile), "ISO-8859-1");
062:
063: assertEquals("Wrong contents", contents, "test");
064: }
065:
066: public void testScandinavianLettersUTF8() throws Exception {
067: WikiPage page = new WikiPage(m_engine, "\u00c5\u00e4Test");
068:
069: m_providerUTF8.putPageText(page, "test\u00d6");
070:
071: File resultfile = new File(m_pagedir, "%C3%85%C3%A4Test.txt");
072:
073: assertTrue("No such file", resultfile.exists());
074:
075: String contents = FileUtil.readContents(new FileInputStream(
076: resultfile), "UTF-8");
077:
078: assertEquals("Wrong contents", contents, "test\u00d6");
079: }
080:
081: /**
082: * This should never happen, but let's check that we're protected anyway.
083: * @throws Exception
084: */
085: public void testSlashesInPageNamesUTF8() throws Exception {
086: WikiPage page = new WikiPage(m_engine, "Test/Foobar");
087:
088: m_providerUTF8.putPageText(page, "test");
089:
090: File resultfile = new File(m_pagedir, "Test%2FFoobar.txt");
091:
092: assertTrue("No such file", resultfile.exists());
093:
094: String contents = FileUtil.readContents(new FileInputStream(
095: resultfile), "UTF-8");
096:
097: assertEquals("Wrong contents", contents, "test");
098: }
099:
100: public void testSlashesInPageNames() throws Exception {
101: WikiPage page = new WikiPage(m_engine, "Test/Foobar");
102:
103: m_provider.putPageText(page, "test");
104:
105: File resultfile = new File(m_pagedir, "Test%2FFoobar.txt");
106:
107: assertTrue("No such file", resultfile.exists());
108:
109: String contents = FileUtil.readContents(new FileInputStream(
110: resultfile), "ISO-8859-1");
111:
112: assertEquals("Wrong contents", contents, "test");
113: }
114:
115: public void testAuthor() throws Exception {
116: try {
117: WikiPage page = new WikiPage(m_engine, "\u00c5\u00e4Test");
118: page.setAuthor("Min\u00e4");
119:
120: m_provider.putPageText(page, "test");
121:
122: WikiPage page2 = m_provider.getPageInfo("\u00c5\u00e4Test",
123: 1);
124:
125: assertEquals("Min\u00e4", page2.getAuthor());
126: } finally {
127: File resultfile = new File(m_pagedir, "%C5%E4Test.txt");
128: try {
129: resultfile.delete();
130: } catch (Exception e) {
131: }
132:
133: resultfile = new File(m_pagedir, "%C5%E4Test.properties");
134: try {
135: resultfile.delete();
136: } catch (Exception e) {
137: }
138: }
139: }
140:
141: public void testNonExistantDirectory() throws Exception {
142: String tmpdir = m_pagedir;
143: String dirname = "non-existant-directory";
144:
145: String newdir = tmpdir + File.separator + dirname;
146:
147: Properties pr = new Properties();
148:
149: pr.setProperty(FileSystemProvider.PROP_PAGEDIR, newdir);
150:
151: FileSystemProvider test = new FileSystemProvider();
152:
153: test.initialize(m_engine, pr);
154:
155: File f = new File(newdir);
156:
157: assertTrue("didn't create it", f.exists());
158: assertTrue("isn't a dir", f.isDirectory());
159:
160: f.delete();
161: }
162:
163: public void testDirectoryIsFile() throws Exception {
164: File tmpFile = null;
165:
166: try {
167: tmpFile = FileUtil.newTmpFile("foobar"); // Content does not matter.
168:
169: Properties pr = new Properties();
170:
171: pr.setProperty(FileSystemProvider.PROP_PAGEDIR, tmpFile
172: .getAbsolutePath());
173:
174: FileSystemProvider test = new FileSystemProvider();
175:
176: try {
177: test.initialize(m_engine, pr);
178:
179: fail("Wiki did not warn about wrong property.");
180: } catch (IOException e) {
181: // This is okay.
182: }
183: } finally {
184: if (tmpFile != null) {
185: tmpFile.delete();
186: }
187: }
188: }
189:
190: public void testDelete() throws Exception {
191: String files = props
192: .getProperty(FileSystemProvider.PROP_PAGEDIR);
193:
194: WikiPage p = new WikiPage(m_engine, "Test");
195: p.setAuthor("AnonymousCoward");
196:
197: m_provider.putPageText(p, "v1");
198:
199: File f = new File(files, "Test" + FileSystemProvider.FILE_EXT);
200:
201: assertTrue("file does not exist", f.exists());
202:
203: f = new File(files, "Test.properties");
204:
205: assertTrue("property file does not exist", f.exists());
206:
207: m_provider.deletePage("Test");
208:
209: f = new File(files, "Test" + FileSystemProvider.FILE_EXT);
210:
211: assertFalse("file exists", f.exists());
212:
213: f = new File(files, "Test.properties");
214:
215: assertFalse("properties exist", f.exists());
216: }
217:
218: public static Test suite() {
219: return new TestSuite(FileSystemProviderTest.class);
220: }
221: }
|