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