1 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 ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 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 m_packageNames = new ArrayList (); 43 44 public JamesMailetLoaderConfiguration() { 45 super("mailetpackages"); 46 } 47 48 public void init() { 49 for (Iterator iterator = m_packageNames.iterator(); iterator.hasNext();) { 50 String packageName = (String ) 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 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 { 83 m_conf.add("none.existing.package"); 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 { 91 m_conf.addStandardPackages(); 92 setUpLoader(); 93 94 Mailet mailetNull1 = m_jamesMailetLoader.getMailet("Null", null); 96 assetIsNullMailet(mailetNull1); 97 98 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 { 105 m_conf.addStandardPackages(); 106 setUpLoader(); 107 108 checkTestMailet("MailetLoaderTestMailet"); 109 110 checkTestMailet("MailetLoaderTestSMIMEMailet"); 111 112 } 113 114 private void checkTestMailet(String mailetName) throws MessagingException { 115 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 |