01: package org.emforge.attachment;
02:
03: import java.io.InputStream;
04:
05: import org.apache.commons.io.IOUtils;
06: import org.emforge.AttachmentService;
07: import org.emforge.tests.BaseUnitTest;
08: import org.emforge.xfer.AttachmentTO;
09: import org.junit.Before;
10: import org.junit.Test;
11: import static org.junit.Assert.assertEquals;
12: import static org.junit.Assert.assertFalse;
13: import static org.junit.Assert.assertNotNull;
14: import static org.junit.Assert.assertTrue;
15: import static org.junit.Assert.fail;
16: import org.springframework.beans.factory.annotation.Autowired;
17:
18: import com.ecyrd.jspwiki.WikiContext;
19: import com.ecyrd.jspwiki.WikiEngine;
20: import com.ecyrd.jspwiki.WikiPage;
21:
22: public class AttachmentServiceTest extends BaseUnitTest {
23: private static final String PAGE1 = "Page1";
24: private static final String PAGE2 = "Page2";
25:
26: @Autowired
27: AttachmentService attachmentService;
28: @Autowired
29: WikiEngine wikiEngine;
30:
31: @Before
32: public void initData() throws Exception {
33: // make authentication - authenticate as admin user
34: authenticateUser("admin", "admin");
35:
36: // create 2 wiki pages
37: WikiPage page = new WikiPage(wikiEngine, PAGE1);
38:
39: WikiContext wikiContext = new WikiContext(wikiEngine, page);
40: wikiEngine.saveText(wikiContext, "Some text for page 1");
41:
42: page = new WikiPage(wikiEngine, PAGE2);
43: wikiContext = new WikiContext(wikiEngine, page);
44: wikiEngine.saveText(wikiContext, "Some text for page 2");
45: }
46:
47: @Test
48: public void testAttachments() throws Exception {
49: // check list of attachments for both pages - both should have 0 attachments
50: AttachmentTO[] attachments = attachmentService
51: .getAttachments(PAGE1);
52: assertNotNull(attachments);
53: assertEquals(0, attachments.length);
54:
55: attachments = attachmentService.getAttachments(PAGE2);
56: assertNotNull(attachments);
57: assertEquals(0, attachments.length);
58:
59: // read process definition - it will be used as attachment
60: // now, we need to deploy workflow
61: InputStream is = this .getClass().getClassLoader()
62: .getResourceAsStream("processes/TestFixABug.par");
63:
64: // read into byte array
65: byte[] workflowContent = IOUtils.toByteArray(is);
66:
67: // add it as attachment for page 1
68: attachmentService.addAttachment(PAGE1, "TestFixABug.par",
69: workflowContent, "First version of Attachment");
70:
71: // now check list of attachments for pages:
72: attachments = attachmentService.getAttachments(PAGE2);
73: assertNotNull(attachments);
74: assertEquals(0, attachments.length);
75:
76: attachments = attachmentService.getAttachments(PAGE1);
77: assertNotNull(attachments);
78: assertEquals(1, attachments.length);
79:
80: // get content of this attachment
81: byte[] attContent = attachmentService
82: .getAttachmentContent(attachments[0]);
83: //compare contents
84: assertNotNull(attContent);
85: assertEquals(workflowContent.length, attContent.length);
86:
87: // now remove attachment
88: attachmentService.deleteAttachment(attachments[0]);
89:
90: // and check list of attachments again - it should be empty
91: attachments = attachmentService.getAttachments(PAGE1);
92: assertNotNull(attachments);
93: assertEquals(0, attachments.length);
94: }
95: }
|