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.DataInputStream;
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileWriter;
30  import java.io.InputStream;
31  
32  import static org.junit.Assert.*;
33  
34  
35  /**
36   * This is a description of what this class does - chang don't forget to fill this in. 
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class AttachmentTest extends KRADTestCase {
42  	
43  	Attachment dummyAttachment;
44  	
45  	@Override
46  	public void setUp() throws Exception {
47  		super.setUp();
48  		dummyAttachment = new Attachment();
49  		
50  	}
51  
52  	@Override
53  	public void tearDown() throws Exception {
54  		super.tearDown();
55  		dummyAttachment = null;
56  	}
57  
58  	@Test
59  	public void testNoteIdentifier(){
60  		dummyAttachment.setNoteIdentifier((long)12345);
61  		assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
62  	}
63  	
64  	@Test
65  	public void testAttachmentMimeTypeCode(){
66  		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
67  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
68  	}
69  	
70  	@Test
71  	public void testAttachmentFileName(){
72  		dummyAttachment.setAttachmentFileName("FILE_NM");
73  		assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
74  	}
75  	
76  	@Test
77  	public void testAttachmentIdentifier(){
78  		dummyAttachment.setAttachmentIdentifier("Att_ID");
79  		assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
80  	}
81  	
82  	@Test
83  	public void testAttachmentFileSize(){
84  		dummyAttachment.setAttachmentFileSize((long)12345);
85  		assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
86  	}
87  	
88  
89  	@Test
90  	public void testAttachmentTypeCode(){
91  		dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
92  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
93  	}
94  	
95  
96  	@Test
97  	public void testNote(){
98  		Note dummyNote = new Note();
99  		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 }