View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.notes;
18  
19  import java.io.FileReader;
20  import java.io.StringWriter;
21  import java.sql.Timestamp;
22  import java.util.Date;
23  
24  import org.junit.Test;
25  import org.kuali.rice.kew.notes.Attachment;
26  import org.kuali.rice.kew.notes.Note;
27  import org.kuali.rice.kew.notes.service.NoteService;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.kuali.rice.kew.test.TestUtilities;
31  
32  
33  public class NoteServiceTest extends KEWTestCase {
34  		
35      @Test public void testAttachmentSave() throws Exception {
36  		Note note = new Note();
37  		note.setNoteAuthorWorkflowId("fakeyUser");
38  		note.setRouteHeaderId(new Long(2));
39  		note.setNoteCreateDate(new Timestamp(new Date().getTime()));
40  		note.setNoteText("i like notes");
41  		
42  		Attachment attachment = new Attachment();
43  		attachment.setNote(note);
44  		attachment.setMimeType("mimeType");
45  		attachment.setFileName("attachedFile.txt");
46  		attachment.setAttachedObject(TestUtilities.loadResource(this.getClass(), "attachedFile.txt"));
47  		
48  		note.getAttachments().add(attachment);
49  		
50  		NoteService noteService = KEWServiceLocator.getNoteService();
51  		noteService.saveNote(note);
52  		assertNotNull("Note should have a id", note.getNoteId());
53  		assertNotNull("Note should have a version number", note.getLockVerNbr());
54  		
55  		assertNotNull("Attachment should have a id", attachment.getAttachmentId());
56  		assertNotNull("Attachment should have version number", attachment.getLockVerNbr());
57  		assertNotNull("Attachment file loc should reflect file system location", attachment.getFileLoc());
58  		
59  		FileReader fileReader = new FileReader(noteService.findAttachmentFile(attachment));
60  		StringWriter stringWriter = new StringWriter();
61  		int c;
62          while ((c = fileReader.read()) != -1) {
63          	stringWriter.write(c);
64          }
65          //i'm being lazy and knowing what's in the source file
66          assertEquals("Attached file content should equal source file content", "I'm an attached file", stringWriter.getBuffer().toString());
67  	}
68  
69  }