KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > JamesMailetLoaderTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20
21
22 package org.apache.james.transport;
23
24 import junit.framework.TestCase;
25 import org.apache.avalon.framework.configuration.DefaultConfiguration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.james.test.mock.avalon.MockLogger;
28 import org.apache.james.test.util.Util;
29 import org.apache.james.transport.mailets.MailetLoaderTestMailet;
30 import org.apache.mailet.Mailet;
31 import org.apache.mailet.MailetConfig;
32
33 import javax.mail.MessagingException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 public class JamesMailetLoaderTest extends TestCase {
38     private JamesMailetLoader m_jamesMailetLoader = new JamesMailetLoader();
39     private JamesMailetLoaderConfiguration m_conf = new JamesMailetLoaderConfiguration();
40
41     private class JamesMailetLoaderConfiguration extends DefaultConfiguration {
42         private ArrayList JavaDoc m_packageNames = new ArrayList JavaDoc();
43         
44         public JamesMailetLoaderConfiguration() {
45             super("mailetpackages");
46         }
47
48         public void init() {
49             for (Iterator JavaDoc iterator = m_packageNames.iterator(); iterator.hasNext();) {
50                 String JavaDoc packageName = (String JavaDoc) iterator.next();
51                 addChild(Util.getValuedConfiguration("mailetpackage", packageName));
52             }
53         }
54
55         public void addStandardPackages() {
56             add("org.apache.james.transport.mailets");
57             add("org.apache.james.transport.mailets.smime");
58         }
59
60         public void add(String JavaDoc packageName) {
61             m_packageNames.add(packageName);
62         }
63
64     }
65
66     private void setUpLoader() throws ConfigurationException {
67         m_conf.init();
68         m_jamesMailetLoader.enableLogging(new MockLogger());
69         m_jamesMailetLoader.configure(m_conf);
70     }
71
72     private void assetIsNullMailet(Mailet mailet) {
73         assertNotNull("Null mailet loaded", mailet);
74         assertTrue("Null mailet is expected class", mailet instanceof org.apache.james.transport.mailets.Null);
75     }
76
77
78     public void testUsingEmtpyConfig() throws ConfigurationException {
79         setUpLoader();
80     }
81
82     public void testFullQualifiedUsingFakeConfig() throws ConfigurationException, MessagingException JavaDoc {
83         m_conf.add("none.existing.package"); // has to be here so the Loader won't choke
84
setUpLoader();
85
86         Mailet mailet = m_jamesMailetLoader.getMailet("org.apache.james.transport.mailets.Null", null);
87         assetIsNullMailet(mailet);
88     }
89
90     public void testStandardMailets() throws ConfigurationException, MessagingException JavaDoc {
91         m_conf.addStandardPackages();
92         setUpLoader();
93
94         // use standard package
95
Mailet mailetNull1 = m_jamesMailetLoader.getMailet("Null", null);
96         assetIsNullMailet(mailetNull1);
97
98         // use full qualified package in parallel
99
Mailet mailetNull2 = m_jamesMailetLoader.getMailet("org.apache.james.transport.mailets.Null", null);
100         assetIsNullMailet(mailetNull2);
101
102     }
103
104     public void testTestMailets() throws ConfigurationException, MessagingException JavaDoc {
105         m_conf.addStandardPackages();
106         setUpLoader();
107
108         checkTestMailet("MailetLoaderTestMailet");
109         
110         checkTestMailet("MailetLoaderTestSMIMEMailet");
111
112     }
113
114     private void checkTestMailet(String JavaDoc mailetName) throws MessagingException JavaDoc {
115         // use standard package
116
DefaultConfiguration configuration = new DefaultConfiguration("mailetLoaderTest");
117         configuration.addChild(Util.getValuedConfiguration("testMailetKey", "testMailetValue"));
118
119         Mailet mailet = m_jamesMailetLoader.getMailet(mailetName, configuration);
120         assertTrue("MailetLoaderTestMailet mailet is expected class", mailet instanceof MailetLoaderTestMailet);
121         MailetLoaderTestMailet mailetLoaderTestMailet = ((MailetLoaderTestMailet) mailet);
122         assertTrue("init was called by loader", mailetLoaderTestMailet.assertInitCalled());
123         MailetConfig mailetConfig = mailetLoaderTestMailet.getMailetConfig();
124         assertEquals("init was called w/ right config", "testMailetValue", mailetConfig.getInitParameter("testMailetKey"));
125     }
126
127 }
128
Popular Tags