001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.bo;
017
018
019 import org.junit.Test;
020 import org.kuali.rice.kim.api.identity.Person;
021 import org.kuali.rice.krad.UserSession;
022 import org.kuali.rice.krad.service.KRADServiceLocator;
023 import org.kuali.rice.krad.util.GlobalVariables;
024 import org.kuali.test.KRADTestCase;
025
026 import java.io.DataInputStream;
027 import java.io.File;
028 import java.io.FileInputStream;
029 import java.io.FileWriter;
030 import java.io.InputStream;
031
032 import static org.junit.Assert.*;
033
034
035 /**
036 * This is a description of what this class does - chang don't forget to fill this in.
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041 public class AttachmentTest extends KRADTestCase {
042
043 Attachment dummyAttachment;
044
045 @Override
046 public void setUp() throws Exception {
047 super.setUp();
048 dummyAttachment = new Attachment();
049
050 }
051
052 @Override
053 public void tearDown() throws Exception {
054 super.tearDown();
055 dummyAttachment = null;
056 }
057
058 @Test
059 public void testNoteIdentifier(){
060 dummyAttachment.setNoteIdentifier((long)12345);
061 assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
062 }
063
064 @Test
065 public void testAttachmentMimeTypeCode(){
066 dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
067 assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
068 }
069
070 @Test
071 public void testAttachmentFileName(){
072 dummyAttachment.setAttachmentFileName("FILE_NM");
073 assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
074 }
075
076 @Test
077 public void testAttachmentIdentifier(){
078 dummyAttachment.setAttachmentIdentifier("Att_ID");
079 assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
080 }
081
082 @Test
083 public void testAttachmentFileSize(){
084 dummyAttachment.setAttachmentFileSize((long)12345);
085 assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
086 }
087
088
089 @Test
090 public void testAttachmentTypeCode(){
091 dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
092 assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
093 }
094
095
096 @Test
097 public void testNote(){
098 Note dummyNote = new Note();
099 dummyNote.setNoteText("Hello");
100 dummyAttachment.setNote(dummyNote);
101 assertEquals("Testing Note of Attachment in AttachmentTest","Hello", dummyAttachment.getNote().getNoteText());
102 }
103
104 @Test
105 public void testComplete(){
106
107 dummyAttachment.setAttachmentIdentifier("Att_ID");
108 dummyAttachment.setAttachmentFileName("FILE_NM");
109 dummyAttachment.setAttachmentFileSize(new Long(12345));
110 dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
111 assertTrue("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
112 {
113 dummyAttachment.setAttachmentFileName(null);
114 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
115
116 }
117 {
118 dummyAttachment.setAttachmentFileSize((long)0);
119 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
120 }
121 {
122 dummyAttachment.setAttachmentIdentifier(null);
123 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
124 }
125 {
126 dummyAttachment.setAttachmentMimeTypeCode(null);
127 assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
128 }
129
130 }
131
132
133 @Test
134 public void testAttachmentContents() throws Exception {
135
136
137 try{
138
139 FileWriter out = new FileWriter("dummy.txt");
140 out.write("Hello testAttachmentContent");
141 out.close();
142
143 File dummyFile = new File("dummy.txt");
144 Note dummyNote = new Note();
145 InputStream inStream = new FileInputStream("dummy.txt");
146
147 GlobalVariables.setUserSession(new UserSession("quickstart"));
148
149 Person kualiUser = GlobalVariables.getUserSession().getPerson();
150 PersistableBusinessObject parentNote = KRADServiceLocator.getNoteService().createNote(dummyNote, dummyAttachment, kualiUser.getPrincipalId());
151 dummyAttachment = KRADServiceLocator.getAttachmentService().createAttachment( parentNote,
152 "dummy.txt",
153 "MimeTypeCode",
154 (int) (long) dummyFile.length(),
155 inStream,
156 "AttachmentTypeCode");
157 String result ="";
158 DataInputStream in = new DataInputStream(dummyAttachment.getAttachmentContents());
159
160 while (in.available() != 0) {
161 result += in.readLine();
162 }
163 inStream.close();
164 assertEquals("Testing attachmentContents in AttachmentTest","Hello testAttachmentContent",result );
165 }
166 finally{
167 new File("dummy.txt").delete();
168 }
169 }
170 }