KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > services > email > VelocityEmailServiceTest


1 package org.tigris.scarab.services.email;
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
50 import java.io.BufferedReader JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.StringReader JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.ResourceBundle JavaDoc;
55
56 import org.apache.fulcrum.TurbineServices;
57 import org.apache.fulcrum.localization.LocalizationService;
58 import org.apache.velocity.VelocityContext;
59 import org.tigris.scarab.test.BaseTurbineTestCase;
60 import org.tigris.scarab.tools.ScarabLocalizationTool;
61 import org.tigris.scarab.util.ScarabConstants;
62
63 /**
64  * A Testing Suite for the VelocityEmailService class.
65  *
66  * @author <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh </a>
67  * @version $Id: VelocityEmailServiceTest.java,v 1.1 2004/04/07 20:11:27 dep4b
68  * Exp $
69  */

70 public class VelocityEmailServiceTest extends BaseTurbineTestCase
71 {
72
73     /**
74      * @author pti
75      *
76      * MOCK object to calculate the Issue Link in the templates
77      */

78     public class MockLink {
79         /**
80          * Calculate a dummy test url from the issue.
81          *
82          * @param issue An issue 'hashmap'
83          * @return A test url based on the issue id.
84          */

85         public String JavaDoc getIssueIdLink(HashMap JavaDoc issue) {
86             return "http://issue.test.org/" + issue.get("UniqueId");
87         }
88     }
89     
90     private HashMap JavaDoc issue;
91     private HashMap JavaDoc user;
92     private HashMap JavaDoc rModuleIssueType;
93     private VelocityContext context;
94     private VelocityEmailService ves;
95     private ResourceBundle JavaDoc l10n;
96     private MockLink link;
97
98
99     /* (non-Javadoc)
100      * @see junit.framework.TestCase#setUp()
101      */

102     public void setUp() throws Exception JavaDoc {
103         super.setUp();
104         
105         ves = (VelocityEmailService)TurbineServices.getInstance().getService("EmailService");
106         
107         // Create a new context for Velocity
108
context = new VelocityContext();
109         
110         // Get a suitable resource bundle and put it in the context
111
ScarabLocalizationTool slt = new ScarabLocalizationTool();
112         slt.init(ScarabLocalizationTool.DEFAULT_LOCALE);
113         context.put(ScarabConstants.LOCALIZATION_TOOL,slt);
114         
115         // Create a user mock object based on a hashmap to represent the modifier
116
user = new HashMap JavaDoc();
117         user.put("Name","tester");
118         user.put("UserName", "Ted E. Ster");
119         user.put("Email", "tester@test.org");
120
121         // Create an RModuleIssueType mock object based on a hashmap
122
rModuleIssueType = new HashMap JavaDoc();
123         rModuleIssueType.put("DisplayName","Defect");
124
125         // Create an issue mock object based on a hashmap and add it to the context
126
issue = new HashMap JavaDoc();
127         context.put("issue", issue);
128         issue.put("ModifiedBy", user);
129         issue.put("DefaultText", "This is the default issue text.");
130         issue.put("UniqueId", "TEST1");
131         issue.put("RModuleIssueType",rModuleIssueType);
132
133         // Create a link mock object and add it to the context
134
link = new MockLink();
135         context.put("link", link);
136         
137     }
138     
139     /**
140      * Count the number of lines in a message
141      *
142      * @param msg The email message whose lines will be counted
143      * @return The number of lines in the message
144      * @throws IOException
145      */

146     private int countLines(String JavaDoc msg) throws IOException JavaDoc {
147         BufferedReader JavaDoc rdr = new BufferedReader JavaDoc(new StringReader JavaDoc(msg));
148         int i = 0;
149         while (rdr.readLine() != null) {
150             i++;
151         }
152         return i;
153     }
154     
155     public void testModifyIssueMessage() throws Exception JavaDoc {
156         String JavaDoc msg = ves.handleRequest(context, "email/ModifyIssue.vm");
157         assertNotNull("Null ModifyIssue Message Returned",msg);
158         assertTrue("Unexpected few lines in ModifyIssue Message : " + countLines(msg),countLines(msg) > 5);
159         assertTrue("Name expected in ModifyIssue Message.",
160                     msg.indexOf((String JavaDoc)user.get("Name")) > 0);
161   // assertTrue("UserName expected in ModifyIssue Message.",
162
// msg.indexOf((String)user.get("UserName")) > 0);
163
assertTrue("Email expected in ModifyIssue Message.",
164                 msg.indexOf((String JavaDoc)user.get("Email")) > 0);
165         assertTrue("IssueId expected in ModifyIssue Message.",
166                 msg.indexOf((String JavaDoc)issue.get("UniqueId")) > 0);
167         assertTrue("DefaultText expected in ModifyIssue Message.",
168                 msg.indexOf((String JavaDoc)issue.get("DefaultText")) > 0);
169         assertTrue("IssueType expected in ModifyIssue Message.",
170                 msg.indexOf((String JavaDoc)rModuleIssueType.get("DisplayName")) > 0);
171         assertTrue("Link url to issue expected in ModifyIssue Message.",
172                 msg.indexOf(link.getIssueIdLink(issue)) > 0);
173         
174         System.out.println(msg);
175     }
176 }
177
178
Popular Tags