public void testFoo() throws Exception { Support support = new Support() { @Override protected void execute(TestPluginLifeCycle lifeCycle, Wiser wiser) throws IOException, MessagingException, ExecutionException, InterruptedException { Shell shell = lifeCycle.createShell(); lifeCycle.bindClass("produce", Commands.ProduceValue.class); lifeCycle.bindClass("consume", Commands.ConsumeObject.class); Commands.list.clear(); BaseProcessContext process = BaseProcessContext.create(shell, "produce | mail send -s the_subject -b admin@gmail.com | consume").execute(); ShellResponse.Ok ok = assertInstance(ShellResponse.Ok.class, process.getResponse()); Assert.assertEquals(1, wiser.getMessages().size()); WiserMessage msg = wiser.getMessages().get(0); Assert.assertEquals("foo@gmail.com", msg.getEnvelopeSender()); Assert.assertEquals("admin@gmail.com", msg.getEnvelopeReceiver()); Assert.assertEquals("the_subject", msg.getMimeMessage().getSubject()); String data = new String(msg.getData()); String content = (String)msg.getMimeMessage().getContent(); assertTrue(content.contains("<value>abc</value>")); assertTrue(data.contains("Content-Type: text/html;charset=UTF-8")); System.out.println("data = " + data); assertEquals(Arrays.<Object>asList(new Value("abc")), Commands.list); } }; support.doTest(); } }
public void testSendPlain() throws Exception { Support support = new Support(); support.doTest(); }
public void testSendHtml() throws Exception { Support support = new Support() { @Override protected Future<Boolean> send(MailPlugin plugin) throws IOException, MessagingException { return plugin.send(Arrays.asList("dst@gmail.com"), "Testing Subject", "<html><body>hello wolrd</body></html>", "text/html;charset=UTF-8"); } }; support.doTest(); }
public void testSendAttachment() throws Exception { final File f = new File(MailPluginTestCase.class.getResource("image.png").toURI()); Support support = new Support() { @Override protected Future<Boolean> send(MailPlugin plugin) throws IOException, MessagingException { return plugin.send(Arrays.asList("dst@gmail.com"), "Testing Subject", "Dear Mail Crawler," + "\n\n No spam to my email, please!", new FileDataSource(f)); } @Override protected void assertMessage(WiserMessage msg) throws MessagingException { super.assertMessage(msg); String data = new String(msg.getData()); String match = "name=image.png"; assertTrue("Was expecting " + data + " to contain <" + match + ">", data.contains(match)); } }; support.doTest(); } }
public void testAuthFailed() throws Exception { final AtomicBoolean done = new AtomicBoolean(); Support support = new Support() { @Override protected TestPluginLifeCycle createLifeCycle() throws Exception { TestPluginLifeCycle lifeCycle = super.createLifeCycle(); lifeCycle.setProperty(MailPlugin.SMTP_USERNAME, "foo"); lifeCycle.setProperty(MailPlugin.SMTP_PASSWORD, "bar"); return lifeCycle; } @Override protected SMTPServer createServer(MessageHandlerFactory mhf) { SMTPServer smtpServer = super.createServer(mhf); smtpServer.setAuthenticationHandlerFactory(new LoginAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { done.set(true); throw new LoginFailedException(); } })); return smtpServer; } @Override protected void assertResponse(Wiser wiser, Future<Boolean> response) throws MessagingException, ExecutionException, InterruptedException { assertFalse(response.get()); } }; support.doTest(); assertEquals(true, done.get()); }
support.doTest();
public void testAuth() throws Exception { final AtomicReference<String> usernameRef = new AtomicReference<String>(); final AtomicReference<String> passwordRef = new AtomicReference<String>(); Support support = new Support() { @Override protected TestPluginLifeCycle createLifeCycle() throws Exception { TestPluginLifeCycle lifeCycle = super.createLifeCycle(); lifeCycle.setProperty(MailPlugin.SMTP_USERNAME, "foo"); lifeCycle.setProperty(MailPlugin.SMTP_PASSWORD, "bar"); return lifeCycle; } @Override protected SMTPServer createServer(MessageHandlerFactory mhf) { SMTPServer smtpServer = super.createServer(mhf); smtpServer.setAuthenticationHandlerFactory(new LoginAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { usernameRef.set(username); passwordRef.set(password); } })); return smtpServer; } }; support.doTest(); assertEquals("foo", usernameRef.get()); assertEquals("bar", passwordRef.get()); }