Skip to content

Commit

Permalink
Fixed #1333 - replace open_! for openOrThrowException()
Browse files Browse the repository at this point in the history
  • Loading branch information
fmpwizard committed Nov 17, 2012
1 parent 0f4f82d commit 9281228
Show file tree
Hide file tree
Showing 51 changed files with 209 additions and 197 deletions.
63 changes: 31 additions & 32 deletions core/actor/src/main/scala/net/liftweb/actor/LiftActor.scala
Expand Up @@ -211,20 +211,19 @@ trait SpecializedLiftActor[T] extends SimpleActor[T] {
}
}

/**
* A list of LoanWrappers that will be executed around the evaluation of mailboxes
*/
protected def aroundLoans: List[CommonLoanWrapper] = Nil

/**
* You can wrap calls around the evaluation of the mailbox. This allows you to set up
* the environment
*/
protected def around[R](f: => R): R = aroundLoans match {
case Nil => f
case xs => CommonLoanWrapper(xs)(f)
}
/**
* A list of LoanWrappers that will be executed around the evaluation of mailboxes
*/
protected def aroundLoans: List[CommonLoanWrapper] = Nil

/**
* You can wrap calls around the evaluation of the mailbox. This allows you to set up
* the environment
*/
protected def around[R](f: => R): R = aroundLoans match {
case Nil => f
case xs => CommonLoanWrapper(xs)(f)
}
private def proc2(ignoreProcessing: Boolean) {
var clearProcessing = true
baseMailbox.synchronized {
Expand Down Expand Up @@ -257,27 +256,27 @@ protected def around[R](f: => R): R = aroundLoans match {

while (keepOnDoingHighPriory) {
val hiPriPfBox = highPriorityReceive
if (hiPriPfBox.isDefined) {
val hiPriPf = hiPriPfBox.open_!
findMailboxItem(baseMailbox.next, mb => testTranslate(hiPriPf.isDefinedAt)(mb.item)) match {
case Full(mb) =>
mb.remove()
try {
execTranslate(hiPriPf)(mb.item)
} catch {
case e: Exception => if (eh.isDefinedAt(e)) eh(e)
}
case _ =>
baseMailbox.synchronized {
if (msgList.isEmpty) {
keepOnDoingHighPriory = false
hiPriPfBox.map{
hiPriPf =>
findMailboxItem(baseMailbox.next, mb => testTranslate(hiPriPf.isDefinedAt)(mb.item)) match {
case Full(mb) =>
mb.remove()
try {
execTranslate(hiPriPf)(mb.item)
} catch {
case e: Exception => if (eh.isDefinedAt(e)) eh(e)
}
else {
putListIntoMB()
case _ =>
baseMailbox.synchronized {
if (msgList.isEmpty) {
keepOnDoingHighPriory = false
}
else {
putListIntoMB()
}
}
}
} }
else {keepOnDoingHighPriory = false}
}
}.openOr{keepOnDoingHighPriory = false}
}

val pf = messageHandler
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/main/scala/net/liftweb/common/LRU.scala
Expand Up @@ -85,7 +85,7 @@ class LRUMap[K, V](initMaxSize: Int, loadFactor: Box[Float], expiredFunc: ((K, V
Full(v.value2)
}

def apply(key: K) = get(key).open_!
def apply(key: K) = get(key).openOrThrowException("Simulating what happens with a regular Map, use contains(key) to check if it is present or not.")

def contains(key: K): Boolean = localMap.containsKey(key)

Expand Down
6 changes: 3 additions & 3 deletions core/common/src/test/scala/net/liftweb/common/BoxSpec.scala
Expand Up @@ -71,7 +71,7 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
Empty.isDefined must beFalse
}
"be implicitly defined from an Option. The open_! method can be used on an Option for example" in {
Some(1).open_! must_== 1
Some(1).openOrThrowException("This is a test") must_== 1
}
"be defined from some legacy code (possibly passing null values). If the passed value is not null, a Full(value) is returned" in {
Box.legacyNullTest("s") must_== Full("s")
Expand All @@ -98,7 +98,7 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
Full(1).isDefined must beTrue
}
"return its value when opened" in {
Full(1).open_! must_== 1
Full(1).openOrThrowException("This is a test") must_== 1
}
"return its value when opened with openOr(default value)" in {
Full(1) openOr 0 must_== 1
Expand Down Expand Up @@ -223,7 +223,7 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
Empty.isDefined must beFalse
}
"throw an exception if opened" in {
{Empty.open_!; ()} must throwA[NullPointerException]
{Empty.openOrThrowException("See what happens?, at least we expect it in this case :)"); ()} must throwA[NullPointerException]
}
"return a default value if opened with openOr" in {
Empty.openOr(1) must_== 1
Expand Down
2 changes: 1 addition & 1 deletion core/util/src/main/scala/net/liftweb/util/FatLazy.scala
Expand Up @@ -48,7 +48,7 @@ class FatLazy[T](f: => T) {
value match {
case Full(v) => v
case _ => value = Full(f)
value.open_!
value.openOrThrowException("We just checked that this is a Full box.")
}
}

Expand Down
1 change: 0 additions & 1 deletion core/util/src/main/scala/net/liftweb/util/JSONParser.scala
Expand Up @@ -19,7 +19,6 @@ package util

import scala.util.parsing.combinator.{Parsers, ImplicitConversions}
import common._

object JSONParser extends SafeSeqParser with ImplicitConversions {
implicit def strToInput(in: String): Input = new scala.util.parsing.input.CharArrayReader(in.toCharArray)
type Elem = Char
Expand Down
Expand Up @@ -190,7 +190,7 @@ object BindHelpersSpec extends Specification {
e => e.attribute("id").
filter(_.text == "3").
map(i => e)
}.open_! must ==/ (<woof id="3"/>)
}.openOrThrowException("Test") must ==/ (<woof id="3"/>)
}

"not find an ide" in {
Expand Down
Expand Up @@ -36,7 +36,7 @@ object BundleBuilderSpec extends Specification {
<div name="dog" lang="en">Dog</div>
<div name="dog" lang="fr">Chien</div>
<div name="cat"><div>hi</div></div>
</div>, Locale.US).open_!
</div>, Locale.US).openOrThrowException("Test")

b.getObject("dog") must_== "Dog"
b.getObject("cat").asInstanceOf[NodeSeq] must ==/ (<div>hi</div>)
Expand All @@ -47,7 +47,7 @@ object BundleBuilderSpec extends Specification {
<div name="dog" lang="zz">Dog</div>
<div name="dog" lang="fr" default="true" >Chien</div>
<div name="cat"><div>hi</div></div>
</div>, Locale.US).open_!
</div>, Locale.US).openOrThrowException("Test")

b.getObject("dog") must_== "Chien"
b.getObject("cat").asInstanceOf[NodeSeq] must ==/ (<div>hi</div>)
Expand Down
Expand Up @@ -174,10 +174,10 @@ object ClassHelpersSpec extends Specification {
createInvoker("length", null) must_== Empty
}
"return a Full can with the function from Unit to a can containing the result of the method to invoke" in {
createInvoker("length", "").open_!.apply().get must_== 0
createInvoker("length", "").openOrThrowException("Test").apply().get must_== 0
}
"The invoker function will throw the cause exception if the method can't be called" in {
createInvoker("get", "").open_!.apply must throwA[Exception]
createInvoker("get", "").openOrThrowException("Test").apply must throwA[Exception]
}
}

Expand Down
14 changes: 7 additions & 7 deletions core/util/src/test/scala/net/liftweb/util/Html5ParserSpec.scala
Expand Up @@ -54,12 +54,12 @@ object Html5ParserSpec extends Specification with PendingUntilFixed with Html5Pa
val (page1, page2, page3) = (new String(p._1), new String(p._2), new String(p._3))

"parse valid page type1" in {
val parsed = parse(page1).open_!
val parsed = parse(page1).openOrThrowException("Test")
(parsed \\ "script").length must be >= 4
}

"parse valid page type2" in {
val parsed = parse(page2).open_!
val parsed = parse(page2).openOrThrowException("Test")
(parsed \\ "script").length must be >= 4
}

Expand All @@ -76,7 +76,7 @@ object Html5ParserSpec extends Specification with PendingUntilFixed with Html5Pa
}

"change <lift:head> to <head>" in {
val parsed = parse("<div><lift:head>123</lift:head></div>").open_!
val parsed = parse("<div><lift:head>123</lift:head></div>").openOrThrowException("Test")
val heads = parsed \\ "head"
heads.length must_== 1
heads.text must_== "123"
Expand All @@ -85,17 +85,17 @@ object Html5ParserSpec extends Specification with PendingUntilFixed with Html5Pa

"Parse stuff with lift: namespace" in {
val parsed = parse("""<lift:surround with="dog"><div/></lift:surround>""")
val e = parsed.open_!.asInstanceOf[Elem]
val e = parsed.openOrThrowException("Test").asInstanceOf[Elem]
e.prefix must_== "lift"
e.label must_== "surround"
(parsed.open_! \ "@with").text must_== "dog"
(parsed.openOrThrowException("Test") \ "@with").text must_== "dog"
}

"Parse stuff without lift: namespace" in {
val parsed = parse("""<div with="dog"><div/></div>""")
val e = parsed.open_!.asInstanceOf[Elem]
val e = parsed.openOrThrowException("Test").asInstanceOf[Elem]
e.label must_== "div"
(parsed.open_! \ "@with").text must_== "dog"
(parsed.openOrThrowException("Test") \ "@with").text must_== "dog"
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/util/src/test/scala/net/liftweb/util/JsonParserSpec.scala
Expand Up @@ -95,26 +95,26 @@ object JsonParserSpec extends Specification {
"Parse doubles" in {
val p2 = JSONParser.parse("51.348484")
p2.isDefined must be_==(true)
p2.open_! must_== 51.348484
p2.openOrThrowException("Test") must_== 51.348484
}

"Parse negative doubles" in {
val p2 = JSONParser.parse("-51.348484")
p2.isDefined must be_==(true)
p2.open_! must_== -51.348484
p2.openOrThrowException("Test") must_== -51.348484
}

"Parse negative longs" in {
val p2 = JSONParser.parse("-517272833222")
p2.isDefined must be_==(true)
p2.open_! must_== -517272833222L
p2.openOrThrowException("Test") must_== -517272833222L
}

"complex JSON objects #2" in {
val p2 = JSONParser.parse(
"""{"command":"setPoint","params":{"mf":51.3256123,"$a":-0.6379592,"x":-0.6379592,"y":51.3256123}}""")
p2.isDefined must be_==(true)
val params = p2.open_!.asInstanceOf[Map[String, Map[String, Any]]].apply("params")
val params = p2.openOrThrowException("Test").asInstanceOf[Map[String, Map[String, Any]]].apply("params")
params("mf") must_== 51.3256123D
params("$a") must_== -0.6379592D
params("y") must_== 51.3256123D
Expand All @@ -132,7 +132,7 @@ object JsonParserSpec extends Specification {
val p2 = JSONParser.parse(
"""-0.5033""")
p2.isDefined must be_==(true)
p2.open_! must_== -0.5033
p2.openOrThrowException("Test") must_== -0.5033

}

Expand Down
2 changes: 1 addition & 1 deletion core/util/src/test/scala/net/liftweb/util/MailerSpec.scala
Expand Up @@ -44,7 +44,7 @@ object MailerSpec extends Specification {
while (lastMessage.isEmpty) {
MailerSpec.this.wait(100)
}
lastMessage.open_!
lastMessage.openOrThrowException("Test")
}
}

Expand Down
Expand Up @@ -65,15 +65,15 @@ val data3 = """<?xml version="1.0" encoding="UTF-8"?>

"PCDataMarkupParser" should {
"Parse a document with whitespace" in {
PCDataXmlParser(data1).open_! must ==/ (<html>dude</html>)
PCDataXmlParser(data1).openOrThrowException("Test") must ==/ (<html>dude</html>)
}

"Parse a document with doctype" in {
PCDataXmlParser(data2).open_! must ==/ (<html>dude</html>)
PCDataXmlParser(data2).openOrThrowException("Test") must ==/ (<html>dude</html>)
}

"Parse a document with xml and doctype" in {
PCDataXmlParser(data3).open_!.apply(0).label must_== "html"
PCDataXmlParser(data3).openOrThrowException("Test").apply(0).label must_== "html"
}

}
Expand Down
Expand Up @@ -43,7 +43,7 @@ object XmlParserSpec extends Specification {
</lift:surround>

val bis = new ByteArrayInputStream(actual.toString.getBytes("UTF-8"))
val parsed = PCDataXmlParser(bis).open_!
val parsed = PCDataXmlParser(bis).openOrThrowException("Test")
parsed must ==/(expected)
}

Expand Down
Expand Up @@ -89,7 +89,7 @@ trait CouchRecord[MyType <: CouchRecord[MyType]] extends JSONRecord[MyType] {
case Full(database) => database
case _ =>
_database = Full(_calcDatabase)
_database.open_!
_database.openOrThrowException("We just made this a Full Box")
}

/** Set the current database (if any) for this record */
Expand Down
Expand Up @@ -69,7 +69,7 @@ trait Document extends Request with FetchableAsJObject {
def @@ (rev: String): DocumentRevision = at(rev)

/** Refine to a particular revision of the document by getting _rev from a given JObject. */
def at(doc: JObject): DocumentRevision = at(doc._rev.open_!)
def at(doc: JObject): DocumentRevision = at(doc._rev.openOrThrowException("legacy code"))

/** Alias for at */
def @@ (doc: JObject): DocumentRevision = at(doc)
Expand Down Expand Up @@ -198,7 +198,7 @@ class Database(couch: Request, database: String) extends Request(couch / databas
def apply(id: String): Document = new Request(this / id) with Document { }

/** Access a particular document in the database with _id from a given JObject */
def apply(doc: JObject): Document = this(doc._id.open_!.s)
def apply(doc: JObject): Document = this(doc._id.openOrThrowException("legacy code").s)

/** Access a series of documents by ID. */
def apply(ids: Seq[String]): AllDocs = all.includeDocs.keys(ids.map(JString): _*)
Expand Down
Expand Up @@ -27,7 +27,13 @@ object DocumentHelpers {
def forceStore(http: Http, database: Database, doc: JObject): Box[JObject] =
tryo(http(database(doc) fetch)) match {
case Full(existingDoc) =>
tryo(http(database store updateIdAndRev(doc, existingDoc._id.open_!, existingDoc._rev.open_!))).flatMap(b => b)
tryo{
http(database.store(updateIdAndRev(
doc,
existingDoc._id.openOrThrowException("We just got this document, so it has an id"),
existingDoc._rev.openOrThrowException("We just got this document, so it has a rev"))))
}.flatMap(b => b)


case Failure(_, Full(StatusCode(404, _)), _) =>
tryo(http(database store doc)).flatMap(b => b)
Expand Down
Expand Up @@ -47,7 +47,7 @@ object CouchDocumentSpec extends Specification {

private final def verifyAndOpen[A](b: Box[A]): A = {
b.isDefined must_== true
b.open_!
b.openOrThrowException("This is a test")
}

"A document" should {
Expand Down

0 comments on commit 9281228

Please sign in to comment.