KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > AttachmentTest


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.File JavaDoc;
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 /**
61  * A Testing Suite for the om.Attachment class.
62  *
63  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
64  * @version $Id: AttachmentTest.java 9271 2004-11-23 08:35:00Z dep4b $
65  */

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 JavaDoc {
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 JavaDoc
82     {
83         // save comment
84
comment.setName("comment");
85         comment.setData("Test comment");
86         comment.setTextFields(getUser1(),issue, Attachment.COMMENT__PK);
87         comment.save();
88
89         //
90
// Make sure the comment was persisted correctly.
91
//
92
Attachment comment2 = AttachmentManager.getInstance(comment.getAttachmentId());
93         assertEquals(comment2.getName(), comment.getName());
94
95     }
96
97     public void saveFile() throws Exception JavaDoc
98     {
99         //FileItem fileItem = new DefaultFileItem(scarab/images/", "logo.gif", "image/jpeg", 6480, 10000);
100
FileItemFactory factory = new DefaultFileItemFactory(6480, null);
101         String JavaDoc 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         // need to save the attachments AFTER the issue has been created
116
issue.doSaveFileAttachments(getUser1());
117         System.out.println("filename=" + fileAttachment.getFileName());
118     }
119
120     public void testGetRepositoryDirectory() throws Exception JavaDoc
121     {
122         
123         Configuration c = new BaseConfiguration();
124         c.addProperty("scarab.attachments.repository","WEB-INF/attachements");
125         Attachment.setConfiguration(c);
126         String JavaDoc control = new String JavaDoc("WEB-INF" + File.separator + "attachments");
127         File JavaDoc testPath = new File JavaDoc(Attachment.getRepositoryDirectory());
128         assertTrue("testpath was:" + testPath.getPath(),testPath.getPath().endsWith(control));
129     }
130
131     public void testGetRelativePath() throws Exception JavaDoc
132     {
133         saveFile();
134         File JavaDoc control = new File JavaDoc("mod" + issue.getModuleId().toString()
135                       + "/" + issue.getIdCount()/1000 + "/"
136                       + issue.getUniqueId() + "_"
137                       + fileAttachment.getQueryKey()
138                       + "_" + fileAttachment.getFileName());
139         File JavaDoc testPath = new File JavaDoc(fileAttachment.getRelativePath());
140         assertEquals(control.getPath(), testPath.getPath());
141     }
142
143     public void testGetFullPath() throws Exception JavaDoc
144     {
145         saveFile();
146         File JavaDoc control = new File JavaDoc(fileAttachment.getFullPath());
147         File JavaDoc testPath = new File JavaDoc(Attachment.getRepositoryDirectory(),
148                                  fileAttachment.getRelativePath());
149         assertEquals(control.getPath(), testPath.getPath());
150     }
151
152     public void testSaveUrl() throws Exception JavaDoc
153     {
154         // save comment
155
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