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

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

This example Akka source code file (DefaultCamelTest.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

actor, akka, camel, defaultcamel, defaultcameltest, event, matchers, mockitosugar, runtimeexception, sharedcamelsystem, wordspec

The DefaultCamelTest.scala Akka example source code

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

package akka.camel

import akka.camel.TestSupport.SharedCamelSystem
import internal.DefaultCamel
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.apache.camel.{ CamelContext, ProducerTemplate }
import org.scalatest.WordSpec
import akka.event.LoggingAdapter
import akka.actor.ActorSystem.Settings
import com.typesafe.config.ConfigFactory
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.spi.Registry
import akka.actor.{ ExtendedActorSystem, ActorSystem }

class DefaultCamelTest extends WordSpec with SharedCamelSystem with Matchers with MockitoSugar {

  import org.mockito.Mockito.{ when, verify }
  val sys = mock[ExtendedActorSystem]
  val config = ConfigFactory.defaultReference()
  when(sys.dynamicAccess) thenReturn system.asInstanceOf[ExtendedActorSystem].dynamicAccess
  when(sys.settings) thenReturn (new Settings(this.getClass.getClassLoader, config, "mocksystem"))
  when(sys.name) thenReturn ("mocksystem")

  def camelWithMocks = new DefaultCamel(sys) {
    override val log = mock[LoggingAdapter]
    override lazy val template = mock[ProducerTemplate]
    override lazy val context = mock[DefaultCamelContext]
    override val settings = mock[CamelSettings]
  }

  "during shutdown, when both context and template fail to shutdown" when {
    val camel = camelWithMocks

    when(camel.context.stop()) thenThrow new RuntimeException("context")
    when(camel.template.stop()) thenThrow new RuntimeException("template")
    val exception = intercept[RuntimeException] {
      camel.shutdown()
    }

    "throws exception thrown by context.stop()" in {
      exception.getMessage() should be("context");
    }

    "tries to stop both template and context" in {
      verify(camel.template).stop()
      verify(camel.context).stop()
    }

  }

  "during start, if template fails to start, it will stop the context" in {
    val camel = camelWithMocks

    when(camel.template.start()) thenThrow new RuntimeException

    intercept[RuntimeException] {
      camel.start
    }

    verify(camel.context).stop()

  }
}

Other Akka source code examples

Here is a short list of links related to this Akka DefaultCamelTest.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.