GuiceMailetLoader implements MailetLoader { @Override public Mailet getMailet(MailetConfig config) throws MessagingException { try { ClassName className = new ClassName(config.getMailetName()); Mailet result = genericLoader.<Mailet>withNamingSheme(MAILET_NAMING_SCHEME) .instantiate(className); result.init(resolveConfiguration(result, config)); return result; } catch (Exception e) { throw new MessagingException("Can not load mailet " + config.getMailetName(), e); } } @Inject  GuiceMailetLoader(GuiceGenericLoader genericLoader, Set<MailetConfigurationOverride> mailetConfigurationOverrides); @Override Mailet getMailet(MailetConfig config);  }
@Test public void getMailetShouldLoadClass() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("AddFooter") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet).isInstanceOf(AddFooter.class); }
@Test public void getMailetShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("sub.TestMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet).isInstanceOf(TestMailet.class); }
@Test public void getMailetShouldThrowOnBadType() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("org.apache.james.transport.matchers.SizeGreaterThan") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMailetShouldLoadClassWhenInExtensionsJars() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.mailets.CustomMailet"); }
@Test public void getMailetShouldBrowseRecursivelyExtensionsJars() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.mailets.CustomMailet"); }
@Test public void getMailedShouldAllowCustomPackages() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("com.custom.mailets.AnotherMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("com.custom.mailets.AnotherMailet"); }
@Test public void getMailetShouldThrowOnUnknownMailet() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("org.apache.james.transport.mailets.Unknown") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMailetShouldLoadMailetsWithCustomDependencyInConstructor() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailetWithCustomDependencyInConstructor") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.mailets.CustomMailetWithCustomDependencyInConstructor"); }
@Test public void getMailetShouldLoadMailetsWithCustomDependencyInService() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailetWithCustomDependencyInService") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThatCode(() -> mailet.service(FakeMail.defaultFakeMail())).doesNotThrowAnyException(); }
@Test public void getMailetShouldAllowCustomInjections() throws Exception { GuiceGenericLoader genericLoader = new GuiceGenericLoader( Guice.createInjector(), new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM), new ExtensionConfiguration(ImmutableList.of(new ClassName("org.apache.james.transport.mailets.MyExtensionModule")))); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("MyGenericMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThatCode(() -> mailet.service(FakeMail.defaultFakeMail())).doesNotThrowAnyException(); }
@Test public void allMailetsShouldShareTheSameSingleton() throws Exception { GuiceGenericLoader genericLoader = new GuiceGenericLoader( Guice.createInjector(), new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM), new ExtensionConfiguration(ImmutableList.of(new ClassName("org.apache.james.transport.mailets.MyExtensionModule")))); GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); Mailet mailet1 = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("MyGenericMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); Mailet mailet2 = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("MyGenericMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet1).isEqualTo(mailet2); }