Permalink
Browse files

Redone the way embedded jetty server is setup, more reliable across b…

…uilds :)
  • Loading branch information...
1 parent 80772e4 commit 60fc7de978955b34871e4cd953a8689f4eda23c2 @indrajitr indrajitr committed Mar 2, 2011
@@ -17,21 +17,23 @@
package net.liftweb
package webapptest
+import java.net.URL
+
import org.mortbay.jetty.Server
import org.mortbay.jetty.webapp.WebAppContext
import net.sourceforge.jwebunit.junit.WebTester
import junit.framework.AssertionFailedError
+import common.Box
+
-object JettyTestServer {
- private val serverPort_ = System.getProperty("SERVLET_PORT", "8989").toInt
- private var baseUrl_ = "http://127.0.0.1:" + serverPort_
+final class JettyTestServer(baseUrlBox: Box[URL]) {
- def baseUrl = baseUrl_
+ def baseUrl = baseUrlBox getOrElse new URL("http://127.0.0.1:8080")
private val (server_, context_) = {
- val server = new Server(serverPort_)
+ val server = new Server(baseUrl.getPort)
val context = new WebAppContext()
context.setServer(server)
context.setContextPath("/")
@@ -45,7 +47,7 @@ object JettyTestServer {
(server, context)
}
- def urlFor(path: String) = baseUrl_ + path
+ def urlFor(path: String) = baseUrl + path
def start() = server_.start()
@@ -61,7 +63,7 @@ object JettyTestServer {
val wc = new WebTester()
try {
wc.setScriptingEnabled(false)
- wc.beginAt(JettyTestServer.urlFor(startPath))
+ wc.beginAt(urlFor(startPath))
f(wc)
} catch {
case exc: AssertionFailedError => {
@@ -24,18 +24,29 @@ import http._
import testing._
import Helpers._
+import java.net.{URL, InetAddress}
+
+import common.Full
import snippet.Counter
object OneShot extends Specification with RequestKit {
- val J = JettyTestServer
+ private val host_ = System.getProperty("net.liftweb.webapptest.oneshot.host", InetAddress.getLocalHost.getHostAddress)
+ private val port_ = System.getProperty("net.liftweb.webapptest.oneshot.port", "8181").toInt
+
+ private lazy val baseUrl_ = new URL("http://%s:%s".format(host_, port_))
- doBeforeSpec(J.start())
+ private lazy val jetty = new JettyTestServer(Full(baseUrl_))
- def baseUrl = JettyTestServer.baseUrl
+ def baseUrl = jetty.baseUrl.toString
+
+ doBeforeSpec(jetty.start())
"ContainerVars" should {
+
+ setSequential()
+
"have correct int default" in {
val tmp = LiftRules.sessionCreator
try {
@@ -47,7 +58,7 @@ object OneShot extends Specification with RequestKit {
xml <- resp.xml
} yield xml
- bx.open_! must ==/ (<int>45</int>).when(J.running)
+ bx.open_! must ==/ (<int>45</int>).when(jetty.running)
} finally {
LiftRules.sessionCreator = tmp
}
@@ -65,7 +76,7 @@ object OneShot extends Specification with RequestKit {
xml <- resp2.xml
} yield xml
- bx.open_! must ==/ (<int>33</int>).when(J.running)
+ bx.open_! must ==/ (<int>33</int>).when(jetty.running)
} finally {
LiftRules.sessionCreator = tmp
}
@@ -85,8 +96,8 @@ object OneShot extends Specification with RequestKit {
xml2 <- resp3.xml
} yield (xml, xml2)
- bx.open_!._1 must ==/ (<int>33</int>).when(J.running)
- bx.open_!._2 must ==/ (<int>45</int>).when(J.running)
+ bx.open_!._1 must ==/ (<int>33</int>).when(jetty.running)
+ bx.open_!._2 must ==/ (<int>45</int>).when(jetty.running)
} finally {
LiftRules.sessionCreator = tmp
}
@@ -108,15 +119,18 @@ object OneShot extends Specification with RequestKit {
xml2 <- resp3.xml
} yield (xml, xml2)
- bx.open_!._1 must ==/ (<int>33</int>).when(J.running)
- bx.open_!._2 must ==/ (<str>meow</str>).when(J.running)
+ bx.open_!._1 must ==/ (<int>33</int>).when(jetty.running)
+ bx.open_!._2 must ==/ (<str>meow</str>).when(jetty.running)
} finally {
LiftRules.sessionCreator = tmp
}
}
}
"OneShot" should {
+
+ setSequential()
+
"fire once for oneshot" in {
Counter.x = 0
@@ -132,7 +146,7 @@ object OneShot extends Specification with RequestKit {
resp.get("/oneshot?"+urlEncode(name.text)+"=3")
}
- Counter.x must be_==(1).when(J.running)
+ Counter.x must be_==(1).when(jetty.running)
}
"fire multiple times for normal" in {
@@ -150,10 +164,10 @@ object OneShot extends Specification with RequestKit {
}
- Counter.x must be_==(2).when(J.running)
+ Counter.x must be_==(2).when(jetty.running)
}
}
- doAfterSpec(J.stop())
+ doAfterSpec(jetty.stop())
}
@@ -17,8 +17,11 @@
package net.liftweb
package webapptest
+import java.net.{URL, InetAddress}
+
import org.specs.Specification
+import common.Full
import util._
import http._
@@ -28,85 +31,96 @@ import http._
*/
object ToHeadUsages extends Specification("ToHeadUsages Specification") {
- val J = JettyTestServer
+ private val host_ = System.getProperty("net.liftweb.webapptest.toheadusages.host", InetAddress.getLocalHost.getHostAddress)
+ 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(J.start())
+ doBeforeSpec(jetty.start())
"lift <head> merger" should {
+
+ setSequential()
+
"merge <head> from html fragment" in {
- J.browse(
+ jetty.browse(
"/htmlFragmentWithHead", html =>
- html.getElementByXPath("/html/head/script[@id='fromFrag']") must notBeNull.when(J.running)
+ 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 {
- J.browse(
+ jetty.browse(
"/htmlFragmentWithHead", html =>
- html.getElementsByXPath("/html/body/script[@id='fromFrag']").size must be_==(0).when(J.running)
+ html.getElementsByXPath("/html/body/script[@id='fromFrag']").size must be_==(0).when(jetty.running)
)
}
"merge <head> from snippet" in {
- J.browse(
+ jetty.browse(
"/htmlSnippetWithHead", html =>
- html.getElementByXPath("/html/head/script[@src='snippet.js']") must notBeNull.when(J.running)
+ html.getElementByXPath("/html/head/script[@src='snippet.js']") must notBeNull.when(jetty.running)
)
}
"not merge for bodyless html" in {
- J.browse(
+ jetty.browse(
"/basicDiv",html => {
- html.getElementById("fruit") must notBeNull.when(J.running)
- html.getElementById("bat") must notBeNull.when(J.running)
+ html.getElementById("fruit") must notBeNull.when(jetty.running)
+ html.getElementById("bat") must notBeNull.when(jetty.running)
}
)
}
"not merge for headless bodyless html" in {
- J.browse(
+ jetty.browse(
"/h1",html => {
- html.getElementById("h1") must notBeNull.when(J.running)
+ html.getElementById("h1") must notBeNull.when(jetty.running)
}
)
}
"not merge for headless body html" in {
- J.browse(
+ 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(J.running)
+ html.getElementsByXPath("/html/body//div").size must be_==(1).when(jetty.running)
}
)
}
"not merge non-html" in {
- J.browse(
+ jetty.browse(
"/non_html",html => {
- html.getElementById("frog") must notBeNull.when(J.running)
+ 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("&mdash;")
- (idx >= 0) must beTrue.when(J.running)
+ (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("&mdash;")
- (idx >= 0) must beFalse.when(J.running)
+ (idx >= 0) must beFalse.when(jetty.running)
}
/*
@@ -122,26 +136,29 @@ object ToHeadUsages extends Specification("ToHeadUsages Specification") {
}
"deferred snippets" should {
+
+ setSequential()
+
"render" in {
- J.browse(
+ jetty.browse(
"/deferred",html => {
- html.getElementById("second") must notBeNull.when(J.running)
+ html.getElementById("second") must notBeNull.when(jetty.running)
}
)
}
"not deferred not in actor" in {
- J.browse(
+ jetty.browse(
"/deferred",html => {
- html.getElementByXPath("/html/body/span[@id='whack1']/span[@id='actor_false']") must notBeNull.when(J.running)
+ html.getElementByXPath("/html/body/span[@id='whack1']/span[@id='actor_false']") must notBeNull.when(jetty.running)
}
)
}
"deferred in actor" in {
- J.browse(
+ jetty.browse(
"/deferred",html => {
- html.getElementByXPath("/html/body/span[@id='whack2']/span[@id='actor_true']") must notBeNull.when(J.running)
+ html.getElementByXPath("/html/body/span[@id='whack2']/span[@id='actor_true']") must notBeNull.when(jetty.running)
}
)
}
@@ -165,13 +182,13 @@ object ToHeadUsages extends Specification("ToHeadUsages Specification") {
)
}
- ((first \\ "a").filter(e => (e \ "@id").text == "foo") \ "@href").text must be_==("/wombat/foo").when(J.running)
- ((first \\ "a").filter(e => (e \ "@id").text == "bar") \ "@href").text must be_==("/wombat/bar").when(J.running)
- ((second \\ "a").filter(e => (e \ "@id").text == "foo") \ "@href").text must be_==("/wombat/foo").when(J.running)
- ((second \\ "a").filter(e => (e \ "@id").text == "bar") \ "@href").text must be_==("/bar").when(J.running)
+ ((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(J.stop())
+ doAfterSpec(jetty.stop())
}

0 comments on commit 60fc7de

Please sign in to comment.