View Javadoc

1   /************************************************************************
2    * Copyright (c) 2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
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     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  
19  package org.apache.james.transport;
20  
21  import junit.framework.TestCase;
22  import org.apache.avalon.framework.configuration.DefaultConfiguration;
23  import org.apache.avalon.framework.configuration.ConfigurationException;
24  import org.apache.james.test.util.Util;
25  import org.apache.james.transport.mailets.MailetLoaderTestMailet;
26  import org.apache.mailet.Mailet;
27  import org.apache.mailet.MailetConfig;
28  
29  import javax.mail.MessagingException;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  
33  public class JamesMailetLoaderTest extends TestCase {
34      private JamesMailetLoader m_jamesMailetLoader  = new JamesMailetLoader();
35      private JamesMailetLoaderConfiguration m_conf = new JamesMailetLoaderConfiguration();
36  
37      private class JamesMailetLoaderConfiguration extends DefaultConfiguration {
38          private ArrayList m_packageNames = new ArrayList();
39          
40          public JamesMailetLoaderConfiguration() {
41              super("mailetpackages");
42          }
43  
44          public void init() {
45              for (Iterator iterator = m_packageNames.iterator(); iterator.hasNext();) {
46                  String packageName = (String) iterator.next();
47                  addChild(Util.getValuedConfiguration("mailetpackage", packageName));
48              }
49          }
50  
51          public void addStandardPackages() {
52              add("org.apache.james.transport.mailets");
53              add("org.apache.james.transport.mailets.smime");
54          }
55  
56          public void add(String packageName) {
57              m_packageNames.add(packageName);
58          }
59  
60      }
61  
62      private void setUpLoader() throws ConfigurationException {
63          m_conf.init();
64          m_jamesMailetLoader.configure(m_conf);
65      }
66  
67      private void assetIsNullMailet(Mailet mailet) {
68          assertNotNull("Null mailet loaded", mailet);
69          assertTrue("Null mailet is expected class", mailet instanceof org.apache.james.transport.mailets.Null);
70      }
71  
72  
73      public void testUsingEmtpyConfig() throws ConfigurationException {
74          setUpLoader();
75      }
76  
77      public void testFullQualifiedUsingFakeConfig() throws ConfigurationException, MessagingException {
78          m_conf.add("none.existing.package"); // has to be here so the Loader won't choke
79          setUpLoader();
80  
81          Mailet mailet = m_jamesMailetLoader.getMailet("org.apache.james.transport.mailets.Null", null);
82          assetIsNullMailet(mailet);
83      }
84  
85      public void testStandardMailets() throws ConfigurationException, MessagingException {
86          m_conf.addStandardPackages();
87          setUpLoader();
88  
89          // use standard package
90          Mailet mailetNull1 = m_jamesMailetLoader.getMailet("Null", null);
91          assetIsNullMailet(mailetNull1);
92  
93          // use full qualified package in parallel
94          Mailet mailetNull2 = m_jamesMailetLoader.getMailet("org.apache.james.transport.mailets.Null", null);
95          assetIsNullMailet(mailetNull2);
96  
97      }
98  
99      public void testTestMailets() throws ConfigurationException, MessagingException {
100         m_conf.addStandardPackages();
101         setUpLoader();
102 
103         checkTestMailet("MailetLoaderTestMailet");
104         
105         checkTestMailet("MailetLoaderTestSMIMEMailet");
106 
107     }
108 
109     private void checkTestMailet(String mailetName) throws MessagingException {
110         // use standard package
111         DefaultConfiguration configuration = new DefaultConfiguration("mailetLoaderTest");
112         configuration.addChild(Util.getValuedConfiguration("testMailetKey", "testMailetValue"));
113 
114         Mailet mailet = m_jamesMailetLoader.getMailet(mailetName, configuration);
115         assertTrue("MailetLoaderTestMailet mailet is expected class", mailet instanceof MailetLoaderTestMailet);
116         MailetLoaderTestMailet mailetLoaderTestMailet = ((MailetLoaderTestMailet) mailet);
117         assertTrue("init was called by loader", mailetLoaderTestMailet.assertInitCalled());
118         MailetConfig mailetConfig = mailetLoaderTestMailet.getMailetConfig();
119         assertEquals("init was called w/ right config", "testMailetValue", mailetConfig.getInitParameter("testMailetKey"));
120     }
121 
122 }