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

Play Framework/Scala example source code file (DBSpec.scala)

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

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

Play Framework tags/keywords

api, connectioncallable, database, db, dbspec, fakeapplication, java, map, play, play framework, string, test

The DBSpec.scala Play Framework example source code

package play.db;

import play.api.test.FakeApplication

object DBSpec extends org.specs2.mutable.Specification {
  "Java DB utility" title

  "DB" should {
    "execute block with default connection" in {
      val id = s"withConnection-${System.identityHashCode(this)}"

      DB.withConnection(callable(id), fakeApp).
        aka("connection block result") must_== id

    }

    "execute block with connection from specified datasource" in {
      val id = s"withConnection-${System.identityHashCode(this)}"

      DB.withConnection("default", callable(id), fakeApp).
        aka("connection block result") must_== id

    }

    "execute block with transaction for default connection" in {
      val id = s"withConnection-${System.identityHashCode(this)}"

      DB.withTransaction(callable(id), fakeApp).
        aka("transaction block result") must_== id

    }

    "execute block with transaction from specified datasource" in {
      val id = s"withConnection-${System.identityHashCode(this)}"

      DB.withTransaction("default", callable(id), fakeApp).
        aka("connection block result") must_== id

    }
  }

  // ---

  def callable(res: String) = new ConnectionCallable[String] {
    def call(con: java.sql.Connection) = res
  }

  lazy val fakeApp = {
    acolyte.jdbc.Driver.register("test", acolyte.jdbc.CompositeHandler.empty())
    FakeApplication(additionalConfiguration = Map(
      "db.default.driver" -> "acolyte.jdbc.Driver",
      "db.default.url" -> "jdbc:acolyte:test?handler=test"))

  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework DBSpec.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.