Sunday, April 12, 2009

More on Grails Mail

by Richard Vowles

Actually testing the grails mail plugin and having it sending email and then decoding the results were a little more difficult that expected. dumpster 1.6 (last updated in 2005) was buggy for MIME messages - it didn't obey the ordering of the message headers or keep the line feeds in emails.

Then it was finding a way to decode the message so I could spelunk through it and ensure it matched what was required. I'm almost finished now - the attachments file files and byte arrays work, but the InputStream is giving me a Bad file descriptor which I think might be related to trying to read the stream multiple times, but it happens in native code so I'm not sure.

I also have to check how to check JNDI data sources and then I thought I would check the JIRAs for other issues.

A sample method looks like this:

void testSendMailViewWithTags() {
def mailService = new MailService ()
mailService.mailSender = mailSender

def ctx = RequestContextHolder.currentRequestAttributes ().servletContext
def applicationContext = ctx[GrailsApplicationAttributes.APPLICATION_CONTEXT]

mailService.groovyPagesTemplateEngine = applicationContext.getBean (GroovyPagesTemplateEngine.BEAN_ID)

mailService.sendMail {
to "fred@g2one.com"
subject "Hello John"
body (view: '/_testemails/tagtest', model: [condition: true])
}

assertEquals 1, server.receivedEmailSize

MimeMessage m = new MimeMessage ((Session) null, new StringBufferInputStream (server.receivedEmails[0].toString ()))

MimeMultipart content = m.content
assertEquals 1, content.count
assertEquals 1, content.getBodyPart(0).getContent().getCount() // only 1, it should be text
assertEquals "text/plain", content.getBodyPart(0).getContent().getBodyPart(0).getContentType().split(";")[0]
assertEquals "Condition is true", content.getBodyPart(0).getContent().getBodyPart(0).getContent().trim()

mailService.sendMail {
to "fred@g2one.com"
subject "Hello John"
body (view: '/_testemails/tagtest', model: [condition: false])
}

assertEquals 2, server.receivedEmailSize

m = new MimeMessage ((Session) null, new StringBufferInputStream (server.receivedEmails[1].toString ()))

content = m.content
assertEquals 1, content.count
assertEquals 1, content.getBodyPart(0).getContent().getCount() // only 1, it should be text
assertEquals "text/plain", content.getBodyPart(0).getContent().getBodyPart(0).getContentType().split(";")[0]
assertEquals "", content.getBodyPart(0).getContent().getBodyPart(0).getContent().trim()
}

Saturday, April 11, 2009

Changing the new grails-mail

by Richard Vowles

So now that I have set up repositories on github and forked them, I am moving my changes into github for others to use. To ensure they are robust though, they need to have testing - and that is usually pretty difficult for SMTP based email servers - but I have come across a library called dumbster that is designed specifically for unit testing of such facilities!

I'll be creating tests for all of my own functionality as well as migrating the tests for Marc and Greame's tests and start to knock off all the defects that are in the repo for the plugin.

I have a post coming up that has detailed the method that I use for using the pageProperty tag in Grails, but I can't get MarsEdit to change my < symbols for me so I can paste XML/HTML into this document. It is extremely annoying! I might write a Griffon app just to try it out.

Friday, April 10, 2009

I have just been updating the Groovy that comes with Grails 1.1 to Groovy 1.6.1 - and found out that there is a whole heap of Swing and Ant stuff in the file. Given this gets uploaded with your war file, this seems pretty silly. I am sure the Ant stuff gets used in Grails with Gant, but Swing?

There must be a way of getting a swingless Groovy. Useful for Griffon, but clearly not for Grails.