001: package com.ecyrd.jspwiki.attachment;
002:
003: import junit.framework.*;
004: import java.io.*;
005: import java.util.*;
006:
007: import com.ecyrd.jspwiki.*;
008:
009: public class AttachmentManagerTest extends TestCase {
010: public static final String NAME1 = "TestPage";
011: public static final String NAMEU = "TestPage\u00e6";
012:
013: Properties props = new Properties();
014:
015: TestEngine m_engine;
016: AttachmentManager m_manager;
017:
018: static String c_fileContents = "ABCDEFGHIJKLMNOPQRSTUVWxyz";
019:
020: public AttachmentManagerTest(String s) {
021: super (s);
022: }
023:
024: public void setUp() throws Exception {
025: props.load(TestEngine.findTestProperties());
026:
027: m_engine = new TestEngine(props);
028: m_manager = m_engine.getAttachmentManager();
029:
030: m_engine.saveText(NAME1, "Foobar");
031: m_engine.saveText(NAMEU, "Foobar");
032: }
033:
034: private File makeAttachmentFile() throws Exception {
035: File tmpFile = File.createTempFile("test", "txt");
036: tmpFile.deleteOnExit();
037:
038: FileWriter out = new FileWriter(tmpFile);
039:
040: FileUtil.copyContents(new StringReader(c_fileContents), out);
041:
042: out.close();
043:
044: return tmpFile;
045: }
046:
047: public void tearDown() {
048: TestEngine.deleteTestPage(NAME1);
049: TestEngine.deleteTestPage(NAMEU);
050:
051: m_engine.deleteAttachments(NAME1);
052: m_engine.deleteAttachments(NAMEU);
053:
054: TestEngine.emptyWorkDir();
055: }
056:
057: public void testEnabled() {
058: assertTrue("not enabled", m_manager.attachmentsEnabled());
059: }
060:
061: public void testSimpleStore() throws Exception {
062: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
063:
064: att.setAuthor("FirstPost");
065:
066: m_manager.storeAttachment(att, makeAttachmentFile());
067:
068: Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(
069: m_engine, new WikiPage(m_engine, NAME1)), "test1.txt");
070:
071: assertNotNull("attachment disappeared", att2);
072: assertEquals("name", att.getName(), att2.getName());
073: assertEquals("author", att.getAuthor(), att2.getAuthor());
074: assertEquals("size", c_fileContents.length(), att2.getSize());
075:
076: InputStream in = m_manager.getAttachmentStream(att2);
077:
078: assertNotNull("stream", in);
079:
080: StringWriter sout = new StringWriter();
081: FileUtil.copyContents(new InputStreamReader(in), sout);
082:
083: in.close();
084: sout.close();
085:
086: assertEquals("contents", c_fileContents, sout.toString());
087: }
088:
089: public void testSimpleStoreSpace() throws Exception {
090: Attachment att = new Attachment(m_engine, NAME1,
091: "test file.txt");
092:
093: att.setAuthor("FirstPost");
094:
095: m_manager.storeAttachment(att, makeAttachmentFile());
096:
097: Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(
098: m_engine, new WikiPage(m_engine, NAME1)),
099: "test file.txt");
100:
101: assertNotNull("attachment disappeared", att2);
102: assertEquals("name", att.getName(), att2.getName());
103: assertEquals("author", att.getAuthor(), att2.getAuthor());
104: assertEquals("size", c_fileContents.length(), att2.getSize());
105:
106: InputStream in = m_manager.getAttachmentStream(att2);
107:
108: assertNotNull("stream", in);
109:
110: StringWriter sout = new StringWriter();
111: FileUtil.copyContents(new InputStreamReader(in), sout);
112:
113: in.close();
114: sout.close();
115:
116: assertEquals("contents", c_fileContents, sout.toString());
117: }
118:
119: public void testSimpleStoreByVersion() throws Exception {
120: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
121:
122: att.setAuthor("FirstPost");
123:
124: m_manager.storeAttachment(att, makeAttachmentFile());
125:
126: Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(
127: m_engine, new WikiPage(m_engine, NAME1)), "test1.txt",
128: 1);
129:
130: assertNotNull("attachment disappeared", att2);
131: assertEquals("version", 1, att2.getVersion());
132: assertEquals("name", att.getName(), att2.getName());
133: assertEquals("author", att.getAuthor(), att2.getAuthor());
134: assertEquals("size", c_fileContents.length(), att2.getSize());
135:
136: InputStream in = m_manager.getAttachmentStream(att2);
137:
138: assertNotNull("stream", in);
139:
140: StringWriter sout = new StringWriter();
141: FileUtil.copyContents(new InputStreamReader(in), sout);
142:
143: in.close();
144: sout.close();
145:
146: assertEquals("contents", c_fileContents, sout.toString());
147: }
148:
149: public void testMultipleStore() throws Exception {
150: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
151:
152: att.setAuthor("FirstPost");
153:
154: m_manager.storeAttachment(att, makeAttachmentFile());
155:
156: att.setAuthor("FooBar");
157: m_manager.storeAttachment(att, makeAttachmentFile());
158:
159: Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(
160: m_engine, new WikiPage(m_engine, NAME1)), "test1.txt");
161:
162: assertNotNull("attachment disappeared", att2);
163: assertEquals("name", att.getName(), att2.getName());
164: assertEquals("author", att.getAuthor(), att2.getAuthor());
165: assertEquals("version", 2, att2.getVersion());
166:
167: InputStream in = m_manager.getAttachmentStream(att2);
168:
169: assertNotNull("stream", in);
170:
171: StringWriter sout = new StringWriter();
172: FileUtil.copyContents(new InputStreamReader(in), sout);
173:
174: in.close();
175: sout.close();
176:
177: assertEquals("contents", c_fileContents, sout.toString());
178:
179: //
180: // Check that first author did not disappear
181: //
182:
183: Attachment att3 = m_manager.getAttachmentInfo(new WikiContext(
184: m_engine, new WikiPage(m_engine, NAME1)), "test1.txt",
185: 1);
186: assertEquals("version of v1", 1, att3.getVersion());
187: assertEquals("name of v1", "FirstPost", att3.getAuthor());
188: }
189:
190: public void testListAttachments() throws Exception {
191: Attachment att = new Attachment(m_engine, NAME1, "test1.txt");
192:
193: att.setAuthor("FirstPost");
194:
195: m_manager.storeAttachment(att, makeAttachmentFile());
196:
197: Collection c = m_manager.listAttachments(new WikiPage(m_engine,
198: NAME1));
199:
200: assertEquals("Length", 1, c.size());
201:
202: Attachment att2 = (Attachment) c.toArray()[0];
203:
204: assertEquals("name", att.getName(), att2.getName());
205: assertEquals("author", att.getAuthor(), att2.getAuthor());
206: }
207:
208: public void testSimpleStoreWithoutExt() throws Exception {
209: Attachment att = new Attachment(m_engine, NAME1, "test1");
210:
211: att.setAuthor("FirstPost");
212:
213: m_manager.storeAttachment(att, makeAttachmentFile());
214:
215: Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(
216: m_engine, new WikiPage(m_engine, NAME1)), "test1");
217:
218: assertNotNull("attachment disappeared", att2);
219: assertEquals("name", att.getName(), att2.getName());
220: assertEquals("author", "FirstPost", att2.getAuthor());
221: assertEquals("size", c_fileContents.length(), att2.getSize());
222: assertEquals("version", 1, att2.getVersion());
223:
224: InputStream in = m_manager.getAttachmentStream(att2);
225:
226: assertNotNull("stream", in);
227:
228: StringWriter sout = new StringWriter();
229: FileUtil.copyContents(new InputStreamReader(in), sout);
230:
231: in.close();
232: sout.close();
233:
234: assertEquals("contents", c_fileContents, sout.toString());
235: }
236:
237: public void testExists() throws Exception {
238: Attachment att = new Attachment(m_engine, NAME1, "test1");
239:
240: att.setAuthor("FirstPost");
241:
242: m_manager.storeAttachment(att, makeAttachmentFile());
243:
244: assertTrue("attachment disappeared", m_engine.pageExists(NAME1
245: + "/test1"));
246: }
247:
248: public void testExists2() throws Exception {
249: Attachment att = new Attachment(m_engine, NAME1, "test1.bin");
250:
251: att.setAuthor("FirstPost");
252:
253: m_manager.storeAttachment(att, makeAttachmentFile());
254:
255: assertTrue("attachment disappeared", m_engine.pageExists(att
256: .getName()));
257: }
258:
259: public void testExistsSpace() throws Exception {
260: Attachment att = new Attachment(m_engine, NAME1,
261: "test file.bin");
262:
263: att.setAuthor("FirstPost");
264:
265: m_manager.storeAttachment(att, makeAttachmentFile());
266:
267: assertTrue("attachment disappeared", m_engine.pageExists(NAME1
268: + "/test file.bin"));
269: }
270:
271: public void testExistsUTF1() throws Exception {
272: Attachment att = new Attachment(m_engine, NAME1,
273: "test\u00e4.bin");
274:
275: att.setAuthor("FirstPost");
276:
277: m_manager.storeAttachment(att, makeAttachmentFile());
278:
279: assertTrue("attachment disappeared", m_engine.pageExists(att
280: .getName()));
281: }
282:
283: public void testExistsUTF2() throws Exception {
284: Attachment att = new Attachment(m_engine, NAMEU,
285: "test\u00e4.bin");
286:
287: att.setAuthor("FirstPost");
288:
289: m_manager.storeAttachment(att, makeAttachmentFile());
290:
291: assertTrue("attachment disappeared", m_engine.pageExists(att
292: .getName()));
293: }
294:
295: public static Test suite() {
296: return new TestSuite(AttachmentManagerTest.class);
297: }
298:
299: }
|