GuiceMatcherLoader implements MatcherLoader { @Override public Matcher getMatcher(MatcherConfig config) throws MessagingException { try { ClassName className = new ClassName(config.getMatcherName()); Matcher result = genericLoader.<Matcher>withNamingSheme(MATCHER_NAMING_SCHEME) .instantiate(className); result.init(config); return result; } catch (Exception e) { throw new MessagingException("Can not load matcher " + config.getMatcherName(), e); } } @Inject  GuiceMatcherLoader(GuiceGenericLoader genericLoader); @Override Matcher getMatcher(MatcherConfig config);  }
@Test public void getMatcherShouldLoadClass() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMatcherLoader guiceMailetLoader = new GuiceMatcherLoader(genericLoader); Matcher matcher = guiceMailetLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("All") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher).isInstanceOf(All.class); }
@Test public void getMatcherShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMatcherLoader guiceMailetLoader = new GuiceMatcherLoader(genericLoader); Matcher matcher = guiceMailetLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("sub.TestMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher).isInstanceOf(All.class); }
@Test public void getMatcherShouldThrowOnBadType() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(genericLoader); expectedException.expect(MessagingException.class); guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("org.apache.james.transport.mailets.Null") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMatcherShouldLoadClassWhenInExtensionsJars() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(genericLoader); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("CustomMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.matchers.CustomMatcher"); }
@Test public void getMatcherShouldBrowseRecursivelyExtensionJars() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(genericLoader); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("CustomMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.matchers.CustomMatcher"); }
@Test public void getMatcherShouldAllowCustomPackages() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(genericLoader); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("com.custom.matchers.AnotherMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("com.custom.matchers.AnotherMatcher"); }
@Test public void getMatcherShouldThrowOnUnknownMailet() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(genericLoader); expectedException.expect(MessagingException.class); guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("org.apache.james.transport.matchers.Unknown") .mailetContext(FakeMailContext.defaultContext()) .build()); }