1 package org.tigris.scarab.om; 2 3 48 49 import java.io.File ; 50 51 import org.apache.commons.configuration.BaseConfiguration; 52 import org.apache.commons.configuration.Configuration; 53 import org.apache.commons.fileupload.DefaultFileItemFactory; 54 import org.apache.commons.fileupload.FileItem; 55 import org.apache.commons.fileupload.FileItemFactory; 56 import org.apache.torque.om.NumberKey; 57 import org.tigris.scarab.test.BaseScarabTestCase; 58 59 60 66 public class AttachmentTest extends BaseScarabTestCase 67 { 68 private Attachment comment = null; 69 private Attachment fileAttachment = null; 70 private Issue issue = null; 71 72 public void setUp()throws Exception { 73 super.setUp(); 74 comment = AttachmentManager.getInstance(); 75 fileAttachment = AttachmentManager.getInstance(); 76 issue = IssueManager.getInstance(new NumberKey("1")); 77 78 } 79 80 81 public void testSaveComment() throws Exception 82 { 83 comment.setName("comment"); 85 comment.setData("Test comment"); 86 comment.setTextFields(getUser1(),issue, Attachment.COMMENT__PK); 87 comment.save(); 88 89 Attachment comment2 = AttachmentManager.getInstance(comment.getAttachmentId()); 93 assertEquals(comment2.getName(), comment.getName()); 94 95 } 96 97 public void saveFile() throws Exception 98 { 99 FileItemFactory factory = new DefaultFileItemFactory(6480, null); 101 String textFieldName = "textField"; 102 103 FileItem fileItem = factory.createItem( 104 textFieldName, 105 "image/jpeg", 106 true, 107 "logo.gif" 108 ); 109 fileAttachment.setFile(fileItem); 110 fileAttachment.setName(fileItem.getName()); 111 fileAttachment.setMimeType("image/jpeg"); 112 fileAttachment.setCreatedBy(getUser1().getUserId()); 113 issue.addFile(fileAttachment, getUser1()); 114 issue.save(); 115 issue.doSaveFileAttachments(getUser1()); 117 System.out.println("filename=" + fileAttachment.getFileName()); 118 } 119 120 public void testGetRepositoryDirectory() throws Exception 121 { 122 123 Configuration c = new BaseConfiguration(); 124 c.addProperty("scarab.attachments.repository","WEB-INF/attachements"); 125 Attachment.setConfiguration(c); 126 String control = new String ("WEB-INF" + File.separator + "attachments"); 127 File testPath = new File (Attachment.getRepositoryDirectory()); 128 assertTrue("testpath was:" + testPath.getPath(),testPath.getPath().endsWith(control)); 129 } 130 131 public void testGetRelativePath() throws Exception 132 { 133 saveFile(); 134 File control = new File ("mod" + issue.getModuleId().toString() 135 + "/" + issue.getIdCount()/1000 + "/" 136 + issue.getUniqueId() + "_" 137 + fileAttachment.getQueryKey() 138 + "_" + fileAttachment.getFileName()); 139 File testPath = new File (fileAttachment.getRelativePath()); 140 assertEquals(control.getPath(), testPath.getPath()); 141 } 142 143 public void testGetFullPath() throws Exception 144 { 145 saveFile(); 146 File control = new File (fileAttachment.getFullPath()); 147 File testPath = new File (Attachment.getRepositoryDirectory(), 148 fileAttachment.getRelativePath()); 149 assertEquals(control.getPath(), testPath.getPath()); 150 } 151 152 public void testSaveUrl() throws Exception 153 { 154 Attachment url = AttachmentManager.getInstance(); 156 url.setIssue(issue); 157 url.setTypeId(AttachmentTypePeer.URL_PK); 158 url.setMimeType(""); 159 url.setName("foo"); 160 url.setData("www.foo.com"); 161 url.save(); 162 assertEquals(url.getName(),"foo"); 163 assertEquals(url.getData(),"http://www.foo.com"); 164 165 url.setData("mailto:admin@foo.com"); 166 url.save(); 167 assertEquals(url.getData(),"mailto:admin@foo.com"); 168 } 169 } 170 | Popular Tags |