1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.mail;
30  
31  import java.util.Locale;
32  
33  import javax.activation.DataSource;
34  import javax.mail.Message;
35  import javax.mail.MessagingException;
36  import javax.mail.Session;
37  import javax.mail.internet.MimeMessage;
38  
39  import org.jcontainer.dna.Configuration;
40  import org.jcontainer.dna.Logger;
41  import org.jcontainer.dna.impl.Log4JLogger;
42  import org.objectledge.context.Context;
43  import org.objectledge.filesystem.FileSystem;
44  import org.objectledge.templating.Templating;
45  import org.objectledge.templating.TemplatingContext;
46  import org.objectledge.templating.velocity.VelocityTemplating;
47  import org.objectledge.threads.ThreadPool;
48  import org.objectledge.utils.LedgeTestCase;
49  
50  /**
51   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
52   *
53   */
54  public class MailSystemTest extends LedgeTestCase
55  {
56      private FileSystem fs = null;
57  
58      private MailSystem mailSystem;
59  
60      public void setUp()
61          throws Exception
62      {
63          super.setUp();
64          fs = getFileSystem();
65          Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(MailSystem.class));
66          Configuration config = 
67              getConfig("config/org.objectledge.templating.velocity.VelocityTemplating.xml");
68          Templating templating = new VelocityTemplating(config, logger, fs);
69          Context context = new Context();
70          config = getConfig("config/org.objectledge.threads.ThreadPool.xml");
71          ThreadPool threadPool = new ThreadPool(null, context,config, logger);
72          config = getConfig("config/org.objectledge.mail.MailSystem.xml");
73          checkSchema(
74              "config/org.objectledge.mail.MailSystem.xml","org/objectledge/mail/MailSystem.rng");
75          mailSystem = new MailSystem(config, logger, fs, templating, threadPool);
76      }
77  
78  
79      public void testMailSystem()
80      {
81          assertNotNull(mailSystem);
82      }
83  
84      /*
85       * Test for Session getSession()
86       */
87      public void testGetSession()
88      {
89          assertNotNull(mailSystem.getSession());
90      }
91  
92      /*
93       * Test for Session getSession(String)
94       */
95      public void testGetSessionString()
96      {
97          assertNull(mailSystem.getSession("foo"));
98      }
99  
100     public void testGetDataSource()
101     {
102         DataSource ds = mailSystem.getDataSource("foo");
103         assertNotNull(ds);
104     }
105 
106     /*
107      * Test for LedgeMessage newMessage()
108      */
109     public void testNewMessage() throws Exception
110     {
111         LedgeMessage message = mailSystem.newMessage();
112         assertNotNull(message);
113         Session session = mailSystem.getSession();
114         Message msg = new MimeMessage(session);
115         message.setMessage(msg);
116         assertEquals(msg, message.getMessage());
117         message = mailSystem.newMessage();
118         message.setText("foo");
119         message.setEncoding("ISO-8859-2");
120         message.prepare();
121         msg = message.getMessage();
122         assertEquals("foo",msg.getContent());
123         assertEquals("text/plain; charset=ISO-8859-2",msg.getContentType());
124         
125         message = mailSystem.newMessage();
126         message.setTemplate(new Locale("pl","PL"), "PLAIN", "Foo");
127         TemplatingContext context = message.getContext();
128         context = message.getContext();
129         assertNotNull(context);
130         context.put("foo","bar");
131         message.prepare();
132         msg = message.getMessage();
133         assertEquals("foo=bar",msg.getContent());
134         
135         message = mailSystem.newMessage();
136         assertEquals(0, message.getAttachments().size());
137         assertEquals(0, message.getRelatedContent().size());
138     }   
139 
140     /*
141      * Test for LedgeMessage newMessage(String)
142      */
143     public void testNewMessageString()
144     {
145         LedgeMessage message = mailSystem.newMessage("foo");
146         assertNotNull(message);
147     }
148 
149     public void testSend()
150         throws Exception
151     {
152         LedgeMessage message = mailSystem.newMessage();
153         try
154         {
155             mailSystem.send(message, true);
156             fail("shoild throw the exception");
157         }
158         catch(MessagingException e)
159         {
160             // ok!
161         }
162     }
163 
164     public void testGetContentType()
165     {
166         assertEquals("text/html", mailSystem.getContentType("foo.html"));
167         assertEquals("application/octet-stream", mailSystem.getContentType("xxx.xxx"));
168     }
169 
170     public void testIsValidEmailAddress()
171     {
172         assertEquals(true, mailSystem.isValidEmailAddress("foo@bar.foo"));
173         assertEquals(false, mailSystem.isValidEmailAddress("foo@bar_foo"));
174     }
175 
176     private Configuration getConfig(String name)
177         throws Exception
178     {
179         return getConfig(fs, name);
180     }
181 
182 }