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

Lift Framework example source code file (OneShot.scala)

This example Lift Framework source code file (OneShot.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

containervars, full, int, int, jettytestserver, net, network, oneshot, oneshot, requestkit, requestkit, specification, specification, url

The Lift Framework OneShot.scala source code

/*
 * Copyright 2010-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb
package webapptest

import org.specs.Specification

import util._
import http._
import testing._
import Helpers._

import java.net.{URL, InetAddress}

import common.Full
import snippet.Counter


object OneShot extends Specification with RequestKit {

  private def reachableLocalAddress = {
    val l = InetAddress.getLocalHost
    tryo { l.isReachable(50) } match {
      case Full(true) => l.getHostAddress
      case _          => "127.0.0.1"
    }
  }

  private val host_ = System.getProperty("net.liftweb.webapptest.oneshot.host", reachableLocalAddress)
  private val port_ = System.getProperty("net.liftweb.webapptest.oneshot.port", "8181").toInt

  private lazy val baseUrl_ = new URL("http://%s:%s".format(host_, port_))

  private lazy val jetty = new JettyTestServer(Full(baseUrl_))

  def baseUrl = jetty.baseUrl.toString

  doBeforeSpec(jetty.start())

  "ContainerVars" should {

    setSequential()

    "have correct int default" in {
      val tmp = LiftRules.sessionCreator
      try {
        LiftRules.sessionCreator = LiftRules.sessionCreatorForMigratorySessions
        
        val bx = 
          for {
            resp <- get("/cv_int")
            xml <- resp.xml
          } yield xml
        
        bx.open_! must ==/ (<int>45).when(jetty.running)
      } finally {
        LiftRules.sessionCreator = tmp
      }
    }

    "be settable as Int" in {
      val tmp = LiftRules.sessionCreator
      try {
        LiftRules.sessionCreator = LiftRules.sessionCreatorForMigratorySessions
        
      val bx = 
        for {
          resp <- get("/cv_int/33")
          resp2 <- resp.get("/cv_int")
          xml <- resp2.xml
        } yield xml

      bx.open_! must ==/ (<int>33).when(jetty.running)
      } finally {
        LiftRules.sessionCreator = tmp
      }
    }

    "be session aware" in {
      val tmp = LiftRules.sessionCreator
      try {
        LiftRules.sessionCreator = LiftRules.sessionCreatorForMigratorySessions
        
      val bx = 
        for {
          resp <- get("/cv_int/33")
          resp2 <- resp.get("/cv_int")
          xml <- resp2.xml
          resp3 <- get("/cv_int")
          xml2 <- resp3.xml
        } yield (xml, xml2)

      bx.open_!._1 must ==/ (<int>33).when(jetty.running)
      bx.open_!._2 must ==/ (<int>45).when(jetty.running)
      } finally {
        LiftRules.sessionCreator = tmp
      }
    }

    "support multiple vars" in {
      val tmp = LiftRules.sessionCreator
      try {
        LiftRules.sessionCreator = LiftRules.sessionCreatorForMigratorySessions

        val bx =
          for {
            resp <- get("/cv_int/33")
            resp2 <- resp.get("/cv_int")
            respx <- resp.get("/cv_str/meow")
            resp3 <- resp.get("/cv_str")
            xml <- resp2.xml
            xml2 <- resp3.xml
          } yield (xml, xml2)

        bx.open_!._1 must ==/(<int>33).when(jetty.running)
        bx.open_!._2 must ==/(<str>meow).when(jetty.running)

      } finally {
        LiftRules.sessionCreator = tmp
      }
    }
  }

  "OneShot" should {

    setSequential()

    "fire once for oneshot" in {
      Counter.x = 0

      for {
        resp <- get("/oneshot")
        xml <- resp.xml
        span <- (xml \\ "span").filter(x => (x \ "@id").text == "one")
        in <- (span \\ "input")
        name <- in \ "@name"
      } {
        resp.get("/oneshot?" + urlEncode(name.text) + "=3")
        resp.get("/oneshot?" + urlEncode(name.text) + "=3")
      }

      Counter.x must be_==(1).when(jetty.running)
    }

    "fire multiple times for normal" in {
      Counter.x = 0

      for {
        resp <- get("/oneshot")
        xml <- resp.xml
        span <- (xml \\ "span").filter(x => (x \ "@id").text == "two")
        in <- (span \\ "input")
        name <- in \ "@name"
      } {
        resp.get("/oneshot?" + urlEncode(name.text) + "=3")
        resp.get("/oneshot?" + urlEncode(name.text) + "=3")
      }


      Counter.x must be_==(2).when(jetty.running)
    }
  }

  doAfterSpec {
    tryo {
      jetty.stop()
    }
  }


}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework OneShot.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.