View Javadoc

1   /*
2    * Copyright 2007-2010 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.kns.bo;
17  
18  
19  
20  import java.io.DataInputStream;
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileWriter;
24  import java.io.InputStream;
25  import java.util.LinkedHashMap;
26  
27  import org.junit.Test;
28  import org.kuali.rice.kns.UserSession;
29  import org.kuali.rice.kns.service.KNSServiceLocator;
30  import org.kuali.rice.kns.util.GlobalVariables;
31  import org.kuali.test.KNSTestCase;
32  
33  
34  /**
35   * This is a description of what this class does - chang don't forget to fill this in. 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class AttachmentTest extends KNSTestCase{
41  	
42  	Attachment dummyAttachment;
43  	
44  	@Override
45  	public void setUp() throws Exception {
46  		super.setUp();
47  		dummyAttachment = new Attachment();
48  		
49  	}
50  
51  	@Override
52  	public void tearDown() throws Exception {
53  		super.tearDown();
54  		dummyAttachment = null;
55  	}
56  
57  	@Test
58  	public void testNoteIdentifier(){
59  		dummyAttachment.setNoteIdentifier((long)12345);
60  		assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
61  	}
62  	
63  	@Test
64  	public void testAttachmentMimeTypeCode(){
65  		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
66  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
67  	}
68  	
69  	@Test
70  	public void testAttachmentFileName(){
71  		dummyAttachment.setAttachmentFileName("FILE_NM");
72  		assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
73  	}
74  	
75  	@Test
76  	public void testAttachmentIdentifier(){
77  		dummyAttachment.setAttachmentIdentifier("Att_ID");
78  		assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
79  	}
80  	
81  	@Test
82  	public void testAttachmentFileSize(){
83  		dummyAttachment.setAttachmentFileSize((long)12345);
84  		assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
85  	}
86  	
87  
88  	@Test
89  	public void testAttachmentTypeCode(){
90  		dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
91  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
92  	}
93  	
94  
95  	@Test
96  	public void testNote(){
97  		Note dummyNote = new Note();
98  		dummyNote.setNoteText("Hello");
99  		dummyAttachment.setNote(dummyNote);
100 		assertEquals("Testing Note of Attachment in AttachmentTest","Hello", dummyAttachment.getNote().getNoteText());
101 	}
102 	
103 	@Test
104 	public void testComplete(){
105 	
106 		dummyAttachment.setAttachmentIdentifier("Att_ID");
107 		dummyAttachment.setAttachmentFileName("FILE_NM");
108 		dummyAttachment.setAttachmentFileSize(new Long(12345));
109 		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
110 		assertTrue("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
111 		{
112 			dummyAttachment.setAttachmentFileName(null);
113 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
114 	
115 		}
116 		{
117 			dummyAttachment.setAttachmentFileSize((long)0);
118 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
119 		}
120 		{
121 			dummyAttachment.setAttachmentIdentifier(null);
122 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
123 		}
124 		{
125 			dummyAttachment.setAttachmentMimeTypeCode(null);
126 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
127 		}
128 		
129 	}
130 	
131 	
132 	@Test
133 	public void testAttachmentContents(){
134 		
135 		
136 		try{
137 			 
138 			FileWriter out = new FileWriter("dummy.txt");
139 			out.write("Hello testAttachmentContent");
140 			out.close();
141 						
142 			File dummyFile = new File("dummy.txt");  
143 			Note dummyNote = new Note();
144 			InputStream inStream = new FileInputStream("dummy.txt");
145 		
146 			GlobalVariables.setUserSession(new UserSession("quickstart"));
147 			
148 			PersistableBusinessObject parentNote = KNSServiceLocator.getNoteService().createNote(dummyNote, dummyAttachment);
149 			dummyAttachment = KNSServiceLocator.getAttachmentService().createAttachment( parentNote,
150 																					   	 "dummy.txt", 
151 																					     "MimeTypeCode",
152 																					     (int) (long) dummyFile.length(), 
153 																					     inStream,
154 																					     "AttachmentTypeCode");
155 			String result ="";
156 			DataInputStream in =  new DataInputStream(dummyAttachment.getAttachmentContents());
157 		
158 			while (in.available() != 0) {
159 				   result += in.readLine();
160 			}
161 			inStream.close();
162 			assertEquals("Testing attachmentContents in AttachmentTest","Hello testAttachmentContent",result );
163 		}
164 		catch(Exception e){
165 			fail("Testing testAttachmentContents fails because IllegalArgumentException : " + e.toString());
166 		}
167 		finally{
168 			new File("dummy.txt").delete();
169 		}
170 	}
171 	
172 	@Test
173 	public void testToStringMapper(){
174 		dummyAttachment.setNoteIdentifier((long)12345);
175 		LinkedHashMap dummyMap = dummyAttachment.toStringMapper();
176 		assertEquals("Testing toStringMapper of Attachment in AttachmentTest",(long)12345, dummyMap.get("noteIdentifier"));
177 	}
178 }