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

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

This example Play Framework source code file (MigrationHelperSpec.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, fakeapplication, logbuffer, map, migrationhelper, migrationhelperspec, none, play, play framework, playspecification, result, some, string, test

The MigrationHelperSpec.scala Play Framework example source code

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

import play.api.test._
import play.it.{LogBuffer, LogTester}
import ch.qos.logback.classic.Level
import org.specs2.execute.Result
import play.api.Play

class MigrationHelperSpec extends PlaySpecification {

  sequential

  def logMessage(logs: LogBuffer) = {
    logs.find(level = Some(Level.WARN), logger = Some("play"), messageContains = Some("session.maxAge")).headOption
  }

  def assertWasLogged(logs: LogBuffer): Result = {
    if (logMessage(logs).isDefined) {
      ok
    } else {
      ko("Could not find log message in \n" + logs.find().mkString("\n"))
    }
  }

  def assertWasNotLogged(logs: LogBuffer): Result = {
    logMessage(logs) match {
      case Some(msg) => ko("Expected no log message but found " + msg)
      case None => ok
    }
  }

  def runTest[T](config: (String, String)*)(block: LogBuffer => T) = {
    val app = FakeApplication(additionalConfiguration = Map(config:_*))
    try {
      LogTester.withLogBuffer { logs =>
        Play.start(app)
        block(logs)
      }
    } finally {
      Play.stop()
    }

  }

  "MigrationHelper" should {
    "warn when session timeout is too low raw value" in runTest("session.maxAge" -> "3600") { logs =>
      assertWasLogged(logs)
    }
    "not warn when session timeout is not set" in runTest() { logs =>
      assertWasNotLogged(logs)
    }
    "not warn when session timeout has units" in runTest("session.maxAge" -> "3600s") { logs =>
      assertWasNotLogged(logs)
    }
    "not warn when session timeout is significantly large" in runTest("session.maxAge" -> "36000000") { logs =>
      assertWasNotLogged(logs)
    }
    "not warn when migration helper is disabled" in runTest("session.maxAge" -> "3600", "play.migrationhelper" -> "disabled") { logs =>
      assertWasNotLogged(logs)
    }
  }

}

Other Play Framework source code examples

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