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

Akka/Scala example source code file (MessageScalaTest.scala)

This example Akka source code file (MessageScalaTest.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

a, akka, b, c, camel, camelmessage, funsuite, inputstreamcache, map, matchers, sharedcamelsystem, string

The MessageScalaTest.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package akka.camel

import java.io.InputStream
import org.apache.camel.NoTypeConversionAvailableException
import akka.camel.TestSupport.{ SharedCamelSystem }
import org.scalatest.FunSuite
import org.scalatest.Matchers
import org.apache.camel.converter.stream.InputStreamCache

class MessageScalaTest extends FunSuite with Matchers with SharedCamelSystem {
  implicit def camelContext = camel.context
  test("mustConvertDoubleBodyToString") {
    CamelMessage(1.4, Map.empty).bodyAs[String] should be("1.4")
  }

  test("mustThrowExceptionWhenConvertingDoubleBodyToInputStream") {
    intercept[NoTypeConversionAvailableException] {
      CamelMessage(1.4, Map.empty).bodyAs[InputStream]
    }
  }

  test("mustConvertDoubleHeaderToString") {
    val message = CamelMessage("test", Map("test" -> 1.4))
    message.headerAs[String]("test").get should be("1.4")
  }

  test("mustReturnSubsetOfHeaders") {
    val message = CamelMessage("test", Map("A" -> "1", "B" -> "2"))
    message.headers(Set("B")) should be(Map("B" -> "2"))
  }

  test("mustTransformBodyAndPreserveHeaders") {
    CamelMessage("a", Map("A" -> "1")).mapBody((body: String) ⇒ body + "b") should be(CamelMessage("ab", Map("A" -> "1")))
  }

  test("mustConvertBodyAndPreserveHeaders") {
    CamelMessage(1.4, Map("A" -> "1")).withBodyAs[String] should be(CamelMessage("1.4", Map("A" -> "1")))
  }

  test("mustSetBodyAndPreserveHeaders") {
    CamelMessage("test1", Map("A" -> "1")).copy(body = "test2") should be(
      CamelMessage("test2", Map("A" -> "1")))
  }

  test("mustSetHeadersAndPreserveBody") {
    CamelMessage("test1", Map("A" -> "1")).copy(headers = Map("C" -> "3")) should be(
      CamelMessage("test1", Map("C" -> "3")))
  }

  test("mustBeAbleToReReadStreamCacheBody") {
    val msg = CamelMessage(new InputStreamCache("test1".getBytes("utf-8")), Map.empty)
    msg.bodyAs[String] should be("test1")
    // re-read
    msg.bodyAs[String] should be("test1")
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka MessageScalaTest.scala source code file:

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.