View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * AttachmentTest tests {@link Attachment} getters, setters and service methods
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * tests {@link Attachment#getNoteIdentifier()} and {@link Attachment#setNoteIdentifier(Long)}
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       * tests {@link Attachment#getAttachmentMimeTypeCode()} and {@link Attachment#setAttachmentMimeTypeCode(String)}
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       * tests {@link Attachment#getAttachmentFileName()} and {@link Attachment#setAttachmentFileName(String)}
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       * tests {@link Attachment#getAttachmentIdentifier()} and {@link Attachment#setAttachmentIdentifier(String)}
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       * tests {@link Attachment#getAttachmentFileSize()} and {@link Attachment#setAttachmentFileSize(Long)}
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      * tests {@link Attachment#getAttachmentTypeCode()} and {@link Attachment#setAttachmentTypeCode(String)}
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      * tests {@link Attachment#getNote()} and {@link Attachment#setNote(Note)}
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      * tests {@link org.kuali.rice.krad.bo.Attachment#isComplete()}
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      * tests {@link Attachment#getAttachmentContents()}
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 }