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

Play Framework/Scala example source code file (build.sbt)

This example Play Framework source code file (build.sbt) 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

expected, inputkey, resource, runtimeexception

The build.sbt Play Framework example source code

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := Option(System.getProperty("scala.version")).getOrElse("2.10.4")

PlayKeys.playInteractionMode := play.StaticPlayNonBlockingInteractionMode

InputKey[Unit]("verify-reloads") := {
  val expected = Def.spaceDelimited().parsed.head.toInt
  val actual = IO.readLines(target.value / "reload.log").count(_.nonEmpty)
  if (expected == actual) {
    println(s"Expected and got $expected reloads")
  } else {
    throw new RuntimeException(s"Expected $expected reloads but got $actual")
  }
}

InputKey[Unit]("verify-resource-contains") := {
  val args = Def.spaceDelimited("<path> <status> <words> ...").parsed
  val path = args.head
  val status = args.tail.head.toInt
  val assertions = args.tail.tail
  val url = new java.net.URL("http://localhost:9000" + path)
  val conn = url.openConnection().asInstanceOf[java.net.HttpURLConnection]
  if (status == conn.getResponseCode) {
    println(s"Resource at $path returned $status as expected")
  } else {
    throw new RuntimeException(s"Resource at $path returned ${conn.getResponseCode} instead of $status")
  }
  val is = if (conn.getResponseCode >= 400) {
    conn.getErrorStream
  } else {
    conn.getInputStream
  }
  val contents = IO.readStream(is)
  is.close()
  conn.disconnect()
  assertions.foreach { assertion =>
    if (contents.contains(assertion)) {
      println(s"Resource at $path contained $assertion")
    } else {
      throw new RuntimeException(s"Resource at $path didn't contain '$assertion':\n$contents")
    }
  }
}

Other Play Framework source code examples

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