Archive for February 17, 2009

Stubbing a Test Mail Server

Django has built in support for sending email. We make use of this in our app, but when testing, we wanted to be able to access the emails sent so we could assert on their content, and pull data out of the body. In the unit tests, that’s easily solved by mocking the email client call, but we wanted to do this as a black-box regression test. That’s where the Python built in smtpd.SMTPServer comes in handy:

import smtpd, asyncore, threading

email_server = None

class FakeServer(smtpd.SMTPServer):

def __init__(self, localaddr, remoteaddr):
self.server = smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
self.emails = {}

def process_message(self, peer, mailfrom, rcpttos, data):
for recipient in rcpttos:
idx = recipient.replace(‘@’,'_at_’).replace(‘.’,'_dot_’)
existing_emails = emails.get(idx, [])
existing_emails.append(data)
emails[idx] = existing_emails

def stop(self):
self.server.close()

def main()
email_server = FakeServer((‘localhost’, 10920), None)
def start_email():
asyncore.loop()
thread = threading.Thread(target=start_email)
thread.setDaemon(True)
thread.start()

if __name__==’__main__’:
main()

This starts an email server on port 10910, which on receipt of a new email, stores it in a dictionary keyed by a modified version of the email address. Multiple emails to the same address get appended to the list. There is no reason in this context for replacing the ‘.’ and ‘@’ symbols from the email address in this context, but it was appropriate for our usage.

We run the stub mailserver on a separate box, and make the emails available via a get request on a test HTTP server, which allows us to run tests scripted entirely written in Selenium. I will blog again with details of how we manage our test HTTP server. You can use the this code as the basis for embedding the mail server in a unittest setup too.

By: Chris Tarttelin

Comments (1) »

Defaulting To JDK 1.6 In NetBeans 6.5 On OSX

We try to stay current with our technologies at Point2. An example of this is using the most recent Java platform, Java 6 (JDK 1.6) and modern IDEs such as NetBeans 6.5. However, staying up to date with technology is not always as simple as it could be. Java 6 was released in December 2006 and NetBeans 6.5 was released almost 2 years later in November 2008 and yet it still wants to use Java 5 (JDK 1.5) as the default Java Platform, regardless of what you have set in the OSX Java preferences. Here is how you can set NetBeans 6.5 to use JDK 1.6 by default.

  • Ensure that JDK 1.6 and NetBeans 6.5 are installed
  • Locate the NetBeans config file, typically located at “/Applications/NetBeans/NetBeans 6.5.app/Contents/Resources/NetBeans/etc/netbeans.conf” and open the file in your favourite editor
  • Set the “netbeans_jdkhome” parameter to the JDK 1.6 home directory (ie. netbeans_jdkhome=
    /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home)
  • In NetBeans, right click on the project in the project window and select “Properties”
  • Ensure the “Source/Binary Format” is set to “1.6″
  • Click on “Tools” on the toolbar and select “Java Platforms”
  • Under “J2SE” the default platform should now be “JDK 1.6″

By: Damien Gabrielson

Comments (13) »