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

Lift Framework example source code file (ToHeadUsages.scala)

This example Lift Framework source code file (ToHeadUsages.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

boolean, boolean, exclude, full, jettytestserver, net, network, specification, specification, string, template, template, toheadusages, toheadusages, url

The Lift Framework ToHeadUsages.scala source code

/*
 * Copyright 2007-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 java.net.{URL, InetAddress}

import org.specs.Specification

import common.Full
import util._
import http._
import Helpers.tryo

/**
 * System under specification for ToHeadUsages.
 */
object ToHeadUsages extends Specification("ToHeadUsages Specification") {

  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.toheadusages.port", "8282").toInt

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

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

  doBeforeSpec(jetty.start())

  "lift <head> merger" should {

    setSequential()

    "merge <head> from html fragment" in {
      jetty.browse(
        "/htmlFragmentWithHead", html =>
         html.getElementByXPath("/html/head/script[@id='fromFrag']") must notBeNull.when(jetty.running)
      )
    }

    "merge <head> from html fragment does not include head element in body" in {
      jetty.browse(
        "/htmlFragmentWithHead", html =>
         html.getElementsByXPath("/html/body/script[@id='fromFrag']").size must be_==(0).when(jetty.running)
      )
    }

    "merge <head> from snippet" in {
      jetty.browse(
        "/htmlSnippetWithHead", html =>
         html.getElementByXPath("/html/head/script[@src='snippet.js']") must notBeNull.when(jetty.running)
      )
    }

    "not merge for bodyless html" in {
      jetty.browse(
        "/basicDiv",html => {
          html.getElementById("fruit") must notBeNull.when(jetty.running)
          html.getElementById("bat")   must notBeNull.when(jetty.running)
        }
      )
    }

    "not merge for headless bodyless html" in {
      jetty.browse(
        "/h1",html => {
          html.getElementById("h1") must notBeNull.when(jetty.running)
        }
      )
    }

    "not merge for headless body html" in {
      jetty.browse(
        "/body_no_head",html => {
          // Note: The XPath expression "html/body/head/div" fails here with
          // HtmlUnit 2.5 since "head" is not recognized as a XHTML element
          // due to its incorrect position (under body instead of directly under html)
          html.getElementsByXPath("/html/body//div").size must be_==(1).when(jetty.running)
        }
      )
    }

    "not merge non-html" in {
      jetty.browse(
        "/non_html",html => {
          html.getElementById("frog") must notBeNull.when(jetty.running)
        }
      )
    }

  }

  "pages " should {

    setSequential()

    "Template finder should recognize entities" in {
      val ns = TemplateFinder.findAnyTemplate(List("index")).open_!
      val str = AltXML.toXML(ns(0), false, false, false)

      val idx = str.indexOf("—")
      (idx >= 0) must beTrue.when(jetty.running)
    }

    "Template finder should not recognize entities" in {
      val ns = TemplateFinder.findAnyTemplate(List("index")).open_!
      val str = AltXML.toXML(ns(0), false, true, false)

      val idx = str.indexOf("—")
      (idx >= 0) must beFalse.when(jetty.running)
    }

    /*
    "round trip entities" in {
      JettyTestServer.browse(
        "/index",html => {
          val idx = html.getPageSource.indexOf("—")
          (idx >= 0) must_== true
        }
      )
    }
    */
  }

  "deferred snippets" should {

    setSequential()

    "render" in {
      jetty.browse(
        "/deferred",html => {
          html.getElementById("second") must notBeNull.when(jetty.running)
        }
      )
    }

    "not deferred not in actor" in {
      jetty.browse(
        "/deferred",html => {
          html.getElementByXPath("/html/body/span[@id='whack1']/span[@id='actor_false']") must notBeNull.when(jetty.running)
        }
      )
    }

    "deferred in actor" in {
      jetty.browse(
        "/deferred",html => {
          html.getElementByXPath("/html/body/span[@id='whack2']/span[@id='actor_true']") must notBeNull.when(jetty.running)
        }
      )
    }

    "Exclude from context rewriting" in {
      val first = http.Req.fixHtml("/wombat",
        <span>
          <a href="/foo" id="foo">foo
          <a href="/bar" id="bar">bar
        </span>
      )

      def excludeBar(in: String): Boolean = in.startsWith("/bar")

      val second = LiftRules.excludePathFromContextPathRewriting.doWith(excludeBar _) {
        Req.fixHtml("/wombat",
          <span>
            <a href="/foo" id="foo">foo
            <a href="/bar" id="bar">bar
          </span>
        )
      }

      ((first \\ "a").filter(e => (e \ "@id").text == "foo") \ "@href").text must be_==("/wombat/foo").when(jetty.running)
      ((first \\ "a").filter(e => (e \ "@id").text == "bar") \ "@href").text must be_==("/wombat/bar").when(jetty.running)
      ((second \\ "a").filter(e => (e \ "@id").text == "foo") \ "@href").text must be_==("/wombat/foo").when(jetty.running)
      ((second \\ "a").filter(e => (e \ "@id").text == "bar") \ "@href").text must be_==("/bar").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 ToHeadUsages.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.