1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.bo;
17
18
19 import org.junit.Test;
20 import org.kuali.rice.kim.api.identity.Person;
21 import org.kuali.rice.krad.UserSession;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.test.KRADTestCase;
25
26 import java.io.BufferedReader;
27 import java.io.DataInputStream;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileWriter;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33
34 import static org.junit.Assert.*;
35
36
37
38
39
40
41
42
43 public class AttachmentTest extends KRADTestCase {
44
45 Attachment dummyAttachment;
46
47 @Override
48 public void setUp() throws Exception {
49 super.setUp();
50 dummyAttachment = new Attachment();
51
52 }
53
54 @Override
55 public void tearDown() throws Exception {
56 super.tearDown();
57 dummyAttachment = null;
58 }
59
60 @Test
61
62
63
64 public void testNoteIdentifier(){
65 dummyAttachment.setNoteIdentifier((long)12345);
66 assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
67 }
68
69 @Test
70
71
72
73 public void testAttachmentMimeTypeCode(){
74 dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
75 assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
76 }
77
78 @Test
79
80
81
82 public void testAttachmentFileName(){
83 dummyAttachment.setAttachmentFileName("FILE_NM");
84 assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
85 }
86
87 @Test
88
89
90
91 public void testAttachmentIdentifier(){
92 dummyAttachment.setAttachmentIdentifier("Att_ID");
93 assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
94 }
95
96 @Test
97
98
99
100 public void testAttachmentFileSize(){
101 dummyAttachment.setAttachmentFileSize((long)12345);
102 assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
103 }
104
105
106 @Test
107
108
109
110 public void testAttachmentTypeCode(){
111 dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
112 assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
113 }
114
115
116 @Test
117
118
119
120 public void testNote(){
121 Note dummyNote = new Note();
122 dummyNote.setNoteText("Hello");
123 dummyAttachment.setNote(dummyNote);
124 assertEquals("Testing Note of Attachment in AttachmentTest","Hello", dummyAttachment.getNote().getNoteText());
125 }
126
127 @Test
128
129
130
131 public void testComplete(){
132
133 dummyAttachment.setAttachmentIdentifier("Att_ID");
134 dummyAttachment.setAttachmentFileName("FILE_NM");
135 dummyAttachment.setAttachmentFileSize(new Long(12345));
136 dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
137 assertTrue("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
138 {
139 dummyAttachment.setAttachmentFileName(null);
140 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
141
142 }
143 {
144 dummyAttachment.setAttachmentFileSize((long)0);
145 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
146 }
147 {
148 dummyAttachment.setAttachmentIdentifier(null);
149 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
150 }
151 {
152 dummyAttachment.setAttachmentMimeTypeCode(null);
153 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
154 }
155
156 }
157
158 @Test
159
160
161
162 public void testAttachmentContents() throws Exception {
163
164
165 try{
166
167 FileWriter out = new FileWriter("dummy.txt");
168 out.write("Hello testAttachmentContent");
169 out.close();
170
171 File dummyFile = new File("dummy.txt");
172 Note dummyNote = new Note();
173 InputStream inStream = new FileInputStream("dummy.txt");
174
175 GlobalVariables.setUserSession(new UserSession("quickstart"));
176
177 Person kualiUser = GlobalVariables.getUserSession().getPerson();
178 PersistableBusinessObject parentNote = KRADServiceLocator.getNoteService().createNote(dummyNote, dummyAttachment, kualiUser.getPrincipalId());
179 dummyAttachment = KRADServiceLocator.getAttachmentService().createAttachment( parentNote,
180 "dummy.txt",
181 "MimeTypeCode",
182 (int) (long) dummyFile.length(),
183 inStream,
184 "AttachmentTypeCode");
185 String result ="";
186 BufferedReader in = new BufferedReader(new InputStreamReader(dummyAttachment.getAttachmentContents()));
187 String line;
188 while ((line = in.readLine()) != null) {
189 result += line;
190 }
191 inStream.close();
192 assertEquals("Testing attachmentContents in AttachmentTest","Hello testAttachmentContent",result );
193 }
194 finally{
195 new File("dummy.txt").delete();
196 }
197 }
198 }