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: import com.ecyrd.jspwiki.attachment.*;
009:
010: public class BasicAttachmentProviderTest extends TestCase {
011: public static final String NAME1 = "TestPage";
012: public static final String NAME2 = "TestPageToo";
013:
014: Properties props = new Properties();
015:
016: TestEngine m_engine;
017:
018: BasicAttachmentProvider m_provider;
019:
020: /**
021: * This is the sound of my head hitting the keyboard.
022: */
023: private static final String c_fileContents = "gy th tgyhgthygyth tgyfgftrfgvtgfgtr";
024:
025: public BasicAttachmentProviderTest(String s) {
026: super (s);
027: }
028:
029: public void setUp() throws Exception {
030: props.load(TestEngine.findTestProperties());
031:
032: m_engine = new TestEngine(props);
033:
034: m_provider = new BasicAttachmentProvider();
035: m_provider.initialize(m_engine, props);
036:
037: m_engine.saveText(NAME1, "Foobar");
038: m_engine.saveText(NAME2, "Foobar2");
039: }
040:
041: private File makeAttachmentFile() throws Exception {
042: File tmpFile = File.createTempFile("test", "txt");
043: tmpFile.deleteOnExit();
044:
045: FileWriter out = new FileWriter(tmpFile);
046:
047: FileUtil.copyContents(new StringReader(c_fileContents), out);
048:
049: out.close();
050:
051: return tmpFile;
052: }
053:
054: private File makeExtraFile(File directory, String name)
055: throws Exception {
056: File tmpFile = new File(directory, name);
057: FileWriter out = new FileWriter(tmpFile);
058:
059: FileUtil.copyContents(new StringReader(c_fileContents), out);
060:
061: out.close();
062:
063: return tmpFile;
064: }
065:
066: public void tearDown() {
067: TestEngine.deleteTestPage(NAME1);
068: TestEngine.deleteTestPage(NAME2);
069:
070: String tmpfiles = props
071: .getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
072:
073: File f = new File(tmpfiles, NAME1
074: + BasicAttachmentProvider.DIR_EXTENSION);
075:
076: TestEngine.deleteAll(f);
077:
078: f = new File(tmpfiles, NAME2
079: + BasicAttachmentProvider.DIR_EXTENSION);
080:
081: TestEngine.deleteAll(f);
082:
083: TestEngine.emptyWorkDir();
084: }
085:
086: public void testExtension() {
087: String s = "test.png";
088:
089: assertEquals(BasicAttachmentProvider.getFileExtension(s), "png");
090: }
091:
092: public void testExtension2() {
093: String s = ".foo";
094:
095: assertEquals("foo", BasicAttachmentProvider.getFileExtension(s));
096: }
097:
098: public void testExtension3() {
099: String s = "test.png.3";
100:
101: assertEquals("3", BasicAttachmentProvider.getFileExtension(s));
102: }
103:
104: public void testExtension4() {
105: String s = "testpng";
106:
107: assertEquals("bin", BasicAttachmentProvider.getFileExtension(s));
108: }
109:
110: public void testExtension5() {
111: String s = "test.";
112:
113: assertEquals("bin", BasicAttachmentProvider.getFileExtension(s));
114: }
115:
116: public void testExtension6() {
117: String s = "test.a";
118:
119: assertEquals("a", BasicAttachmentProvider.getFileExtension(s));
120: }
121:
122: /**
123: * Can we save attachments with names in UTF-8 range?
124: */
125: public void testPutAttachmentUTF8() throws Exception {
126: File in = makeAttachmentFile();
127:
128: Attachment att = new Attachment(m_engine, NAME1,
129: "\u3072\u3048\u308båäötest.füü");
130:
131: m_provider.putAttachmentData(att, new FileInputStream(in));
132:
133: List res = m_provider.listAllChanged(new Date(0L));
134:
135: Attachment a0 = (Attachment) res.get(0);
136:
137: assertEquals("name", att.getName(), a0.getName());
138: }
139:
140: public void testListAll() throws Exception {
141: File in = makeAttachmentFile();
142:
143: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
144:
145: m_provider.putAttachmentData(att, new FileInputStream(in));
146:
147: Thread.sleep(2000L); // So that we get a bit of granularity.
148:
149: Attachment att2 = new Attachment(m_engine, NAME2, "test2.txt");
150:
151: m_provider.putAttachmentData(att2, new FileInputStream(in));
152:
153: List res = m_provider.listAllChanged(new Date(0L));
154:
155: assertEquals("list size", 2, res.size());
156:
157: Attachment a2 = (Attachment) res.get(0); // Most recently changed
158: Attachment a1 = (Attachment) res.get(1); // Least recently changed
159:
160: assertEquals("a1 name", att.getName(), a1.getName());
161: assertEquals("a2 name", att2.getName(), a2.getName());
162: }
163:
164: /**
165: * Check that the system does not fail if there are extra files in the directory.
166: */
167: public void testListAllExtrafile() throws Exception {
168: File in = makeAttachmentFile();
169:
170: File sDir = new File(m_engine.getWikiProperties().getProperty(
171: BasicAttachmentProvider.PROP_STORAGEDIR));
172: File extrafile = makeExtraFile(sDir, "foobar.blob");
173:
174: try {
175: Attachment att = new Attachment(m_engine, NAME1,
176: "test1.txt");
177:
178: m_provider.putAttachmentData(att, new FileInputStream(in));
179:
180: Thread.sleep(2000L); // So that we get a bit of granularity.
181:
182: Attachment att2 = new Attachment(m_engine, NAME2,
183: "test2.txt");
184:
185: m_provider.putAttachmentData(att2, new FileInputStream(in));
186:
187: List res = m_provider.listAllChanged(new Date(0L));
188:
189: assertEquals("list size", 2, res.size());
190:
191: Attachment a2 = (Attachment) res.get(0); // Most recently changed
192: Attachment a1 = (Attachment) res.get(1); // Least recently changed
193:
194: assertEquals("a1 name", att.getName(), a1.getName());
195: assertEquals("a2 name", att2.getName(), a2.getName());
196: } finally {
197: extrafile.delete();
198: }
199: }
200:
201: /**
202: * Check that the system does not fail if there are extra files in the
203: * attachment directory.
204: */
205: public void testListAllExtrafileInAttachmentDir() throws Exception {
206: File in = makeAttachmentFile();
207:
208: File sDir = new File(m_engine.getWikiProperties().getProperty(
209: BasicAttachmentProvider.PROP_STORAGEDIR));
210: File attDir = new File(sDir, NAME1 + "-att");
211:
212: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
213:
214: m_provider.putAttachmentData(att, new FileInputStream(in));
215:
216: File extrafile = makeExtraFile(attDir, "ping.pong");
217:
218: try {
219: Thread.sleep(2000L); // So that we get a bit of granularity.
220:
221: Attachment att2 = new Attachment(m_engine, NAME2,
222: "test2.txt");
223:
224: m_provider.putAttachmentData(att2, new FileInputStream(in));
225:
226: List res = m_provider.listAllChanged(new Date(0L));
227:
228: assertEquals("list size", 2, res.size());
229:
230: Attachment a2 = (Attachment) res.get(0); // Most recently changed
231: Attachment a1 = (Attachment) res.get(1); // Least recently changed
232:
233: assertEquals("a1 name", att.getName(), a1.getName());
234: assertEquals("a2 name", att2.getName(), a2.getName());
235: } finally {
236: extrafile.delete();
237: }
238: }
239:
240: /**
241: * Check that the system does not fail if there are extra dirs in the
242: * attachment directory.
243: */
244: public void testListAllExtradirInAttachmentDir() throws Exception {
245: File in = makeAttachmentFile();
246:
247: File sDir = new File(m_engine.getWikiProperties().getProperty(
248: BasicAttachmentProvider.PROP_STORAGEDIR));
249: File attDir = new File(sDir, NAME1 + "-att");
250:
251: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
252:
253: m_provider.putAttachmentData(att, new FileInputStream(in));
254:
255: // This is our extraneous directory.
256: File extrafile = new File(attDir, "ping.pong");
257: extrafile.mkdir();
258:
259: try {
260: Thread.sleep(2000L); // So that we get a bit of granularity.
261:
262: Attachment att2 = new Attachment(m_engine, NAME2,
263: "test2.txt");
264:
265: m_provider.putAttachmentData(att2, new FileInputStream(in));
266:
267: List res = m_provider.listAllChanged(new Date(0L));
268:
269: assertEquals("list size", 2, res.size());
270:
271: Attachment a2 = (Attachment) res.get(0); // Most recently changed
272: Attachment a1 = (Attachment) res.get(1); // Least recently changed
273:
274: assertEquals("a1 name", att.getName(), a1.getName());
275: assertEquals("a2 name", att2.getName(), a2.getName());
276: } finally {
277: extrafile.delete();
278: }
279: }
280:
281: public void testListAllNoExtension() throws Exception {
282: File in = makeAttachmentFile();
283:
284: Attachment att = new Attachment(m_engine, NAME1, "test1.");
285:
286: m_provider.putAttachmentData(att, new FileInputStream(in));
287:
288: Thread.sleep(2000L); // So that we get a bit of granularity.
289:
290: Attachment att2 = new Attachment(m_engine, NAME2, "test2.");
291:
292: m_provider.putAttachmentData(att2, new FileInputStream(in));
293:
294: List res = m_provider.listAllChanged(new Date(0L));
295:
296: assertEquals("list size", 2, res.size());
297:
298: Attachment a2 = (Attachment) res.get(0); // Most recently changed
299: Attachment a1 = (Attachment) res.get(1); // Least recently changed
300:
301: assertEquals("a1 name", att.getName(), a1.getName());
302: assertEquals("a2 name", att2.getName(), a2.getName());
303: }
304:
305: public static Test suite() {
306: return new TestSuite(BasicAttachmentProviderTest.class);
307: }
308:
309: }
|