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