alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (MailerSpec.scala)

This example Lift Framework source code file (MailerSpec.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

from, here, here, mailer, mailer, mimemessage, plainmailbodytype, plainmailbodytype, specification, subject, the, the, this, to

The Lift Framework MailerSpec.scala source code

/*
 * Copyright 2006-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb
package util

import javax.mail.internet.{MimeMessage, MimeMultipart}

import org.specs.Specification

import common._


/**
 * Systems under specification for Lift Mailer.
 */
object MailerSpec extends Specification("Mailer Specification") {
  
  MyMailer.touch()

  import MyMailer._
  
  private def doNewMessage(f: => Unit): MimeMessage = {
    lastMessage = Empty

    val ignore = f

    MailerSpec.this.synchronized {
      while (lastMessage.isEmpty) {
        MailerSpec.this.wait(100)
      }
      lastMessage.open_!
    }
  }

  "A Mailer" should {

    "deliver simple messages as simple messages" in {
      val msg = doNewMessage {
        sendMail(
          From("sender@nowhere.com"),
          Subject("This is a simple email"),
          To("recipient@nowhere.com"),
          PlainMailBodyType("Here is some plain text.")
        )
      }

      msg.getContent match {
        case s: String => true must_== true
        case x => fail("The simple message has content type of " + x.getClass.getName)
      }
    }

    "deliver multipart messages as multipart" in {
      val msg = doNewMessage {
        sendMail(
          From("sender@nowhere.com"),
          Subject("This is a multipart email"),
          To("recipient@nowhere.com"),
          PlainMailBodyType("Here is some plain text."),
          PlainMailBodyType("Here is some more plain text.")
        )
      }

      msg.getContent match {
        case mp: MimeMultipart => true must_== true
        case x => fail("The complex message has content type of " + x.getClass.getName)
      }
    }

    "deliver rich messages as multipart" in {
      val msg = doNewMessage {
        sendMail(
          From("sender@nowhere.com"),
          Subject("This is a rich email"),
          To("recipient@nowhere.com"),
          XHTMLMailBodyType(<html> Here is some rich text )
        )
      }

      msg.getContent match {
        case mp: MimeMultipart => true must_== true
        case x => fail("The complex message has content type of " + x.getClass.getName)
      }
    }
  }
}

object MyMailer extends Mailer {
    @volatile var lastMessage: Box[MimeMessage] = Empty

   testModeSend.default.set((msg: MimeMessage) => {
     lastMessage = Full(msg)
//     MailerSpec.this.notifyAll()
   })

  def touch() {
    Props.testMode
    Thread.sleep(10)
  } // do nothing, but force initialization of this class
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework MailerSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.