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

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

This example Akka source code file (ActorPathSpec.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, actorpath, actorpathspec, address, akka, an, matchers, rootactorpath, some, string, wordspec

The ActorPathSpec.scala Akka example source code

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

import org.scalatest.WordSpec
import org.scalatest.Matchers
import java.net.MalformedURLException

class ActorPathSpec extends WordSpec with Matchers {

  "An ActorPath" must {

    "support parsing its String rep" in {
      val path = RootActorPath(Address("akka.tcp", "mysys")) / "user"
      ActorPath.fromString(path.toString) should be(path)
    }

    "support parsing remote paths" in {
      val remote = "akka://sys@host:1234/some/ref"
      ActorPath.fromString(remote).toString should be(remote)
    }

    "throw exception upon malformed paths" in {
      intercept[MalformedURLException] { ActorPath.fromString("") }
      intercept[MalformedURLException] { ActorPath.fromString("://hallo") }
      intercept[MalformedURLException] { ActorPath.fromString("s://dd@:12") }
      intercept[MalformedURLException] { ActorPath.fromString("s://dd@h:hd") }
      intercept[MalformedURLException] { ActorPath.fromString("a://l:1/b") }
    }

    "create correct toString" in {
      val a = Address("akka.tcp", "mysys")
      RootActorPath(a).toString should be("akka.tcp://mysys/")
      (RootActorPath(a) / "user").toString should be("akka.tcp://mysys/user")
      (RootActorPath(a) / "user" / "foo").toString should be("akka.tcp://mysys/user/foo")
      (RootActorPath(a) / "user" / "foo" / "bar").toString should be("akka.tcp://mysys/user/foo/bar")
    }

    "have correct path elements" in {
      (RootActorPath(Address("akka.tcp", "mysys")) / "user" / "foo" / "bar").elements.toSeq should be(Seq("user", "foo", "bar"))
    }

    "create correct toStringWithoutAddress" in {
      val a = Address("akka.tcp", "mysys")
      RootActorPath(a).toStringWithoutAddress should be("/")
      (RootActorPath(a) / "user").toStringWithoutAddress should be("/user")
      (RootActorPath(a) / "user" / "foo").toStringWithoutAddress should be("/user/foo")
      (RootActorPath(a) / "user" / "foo" / "bar").toStringWithoutAddress should be("/user/foo/bar")
    }

    "create correct toStringWithAddress" in {
      val local = Address("akka.tcp", "mysys")
      val a = local.copy(host = Some("aaa"), port = Some(2552))
      val b = a.copy(host = Some("bb"))
      val c = a.copy(host = Some("cccc"))
      val root = RootActorPath(local)
      root.toStringWithAddress(a) should be("akka.tcp://mysys@aaa:2552/")
      (root / "user").toStringWithAddress(a) should be("akka.tcp://mysys@aaa:2552/user")
      (root / "user" / "foo").toStringWithAddress(a) should be("akka.tcp://mysys@aaa:2552/user/foo")

      //      root.toStringWithAddress(b) should be("akka.tcp://mysys@bb:2552/")
      (root / "user").toStringWithAddress(b) should be("akka.tcp://mysys@bb:2552/user")
      (root / "user" / "foo").toStringWithAddress(b) should be("akka.tcp://mysys@bb:2552/user/foo")

      root.toStringWithAddress(c) should be("akka.tcp://mysys@cccc:2552/")
      (root / "user").toStringWithAddress(c) should be("akka.tcp://mysys@cccc:2552/user")
      (root / "user" / "foo").toStringWithAddress(c) should be("akka.tcp://mysys@cccc:2552/user/foo")

      val rootA = RootActorPath(a)
      rootA.toStringWithAddress(b) should be("akka.tcp://mysys@aaa:2552/")
      (rootA / "user").toStringWithAddress(b) should be("akka.tcp://mysys@aaa:2552/user")
      (rootA / "user" / "foo").toStringWithAddress(b) should be("akka.tcp://mysys@aaa:2552/user/foo")

    }
  }
}

Other Akka source code examples

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