001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport;
019:
020: import junit.framework.TestCase;
021: import org.apache.avalon.framework.configuration.DefaultConfiguration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.james.test.util.Util;
024: import org.apache.james.transport.mailets.MailetLoaderTestMailet;
025: import org.apache.mailet.Mailet;
026: import org.apache.mailet.MailetConfig;
027:
028: import javax.mail.MessagingException;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031:
032: public class JamesMailetLoaderTest extends TestCase {
033: private JamesMailetLoader m_jamesMailetLoader = new JamesMailetLoader();
034: private JamesMailetLoaderConfiguration m_conf = new JamesMailetLoaderConfiguration();
035:
036: private class JamesMailetLoaderConfiguration extends
037: DefaultConfiguration {
038: private ArrayList m_packageNames = new ArrayList();
039:
040: public JamesMailetLoaderConfiguration() {
041: super ("mailetpackages");
042: }
043:
044: public void init() {
045: for (Iterator iterator = m_packageNames.iterator(); iterator
046: .hasNext();) {
047: String packageName = (String) iterator.next();
048: addChild(Util.getValuedConfiguration("mailetpackage",
049: packageName));
050: }
051: }
052:
053: public void addStandardPackages() {
054: add("org.apache.james.transport.mailets");
055: add("org.apache.james.transport.mailets.smime");
056: }
057:
058: public void add(String packageName) {
059: m_packageNames.add(packageName);
060: }
061:
062: }
063:
064: private void setUpLoader() throws ConfigurationException {
065: m_conf.init();
066: m_jamesMailetLoader.configure(m_conf);
067: }
068:
069: private void assetIsNullMailet(Mailet mailet) {
070: assertNotNull("Null mailet loaded", mailet);
071: assertTrue(
072: "Null mailet is expected class",
073: mailet instanceof org.apache.james.transport.mailets.Null);
074: }
075:
076: public void testUsingEmtpyConfig() throws ConfigurationException {
077: setUpLoader();
078: }
079:
080: public void testFullQualifiedUsingFakeConfig()
081: throws ConfigurationException, MessagingException {
082: m_conf.add("none.existing.package"); // has to be here so the Loader won't choke
083: setUpLoader();
084:
085: Mailet mailet = m_jamesMailetLoader.getMailet(
086: "org.apache.james.transport.mailets.Null", null);
087: assetIsNullMailet(mailet);
088: }
089:
090: public void testStandardMailets() throws ConfigurationException,
091: MessagingException {
092: m_conf.addStandardPackages();
093: setUpLoader();
094:
095: // use standard package
096: Mailet mailetNull1 = m_jamesMailetLoader
097: .getMailet("Null", null);
098: assetIsNullMailet(mailetNull1);
099:
100: // use full qualified package in parallel
101: Mailet mailetNull2 = m_jamesMailetLoader.getMailet(
102: "org.apache.james.transport.mailets.Null", null);
103: assetIsNullMailet(mailetNull2);
104:
105: }
106:
107: public void testTestMailets() throws ConfigurationException,
108: MessagingException {
109: m_conf.addStandardPackages();
110: setUpLoader();
111:
112: checkTestMailet("MailetLoaderTestMailet");
113:
114: checkTestMailet("MailetLoaderTestSMIMEMailet");
115:
116: }
117:
118: private void checkTestMailet(String mailetName)
119: throws MessagingException {
120: // use standard package
121: DefaultConfiguration configuration = new DefaultConfiguration(
122: "mailetLoaderTest");
123: configuration.addChild(Util.getValuedConfiguration(
124: "testMailetKey", "testMailetValue"));
125:
126: Mailet mailet = m_jamesMailetLoader.getMailet(mailetName,
127: configuration);
128: assertTrue("MailetLoaderTestMailet mailet is expected class",
129: mailet instanceof MailetLoaderTestMailet);
130: MailetLoaderTestMailet mailetLoaderTestMailet = ((MailetLoaderTestMailet) mailet);
131: assertTrue("init was called by loader", mailetLoaderTestMailet
132: .assertInitCalled());
133: MailetConfig mailetConfig = mailetLoaderTestMailet
134: .getMailetConfig();
135: assertEquals("init was called w/ right config",
136: "testMailetValue", mailetConfig
137: .getInitParameter("testMailetKey"));
138: }
139:
140: }
|