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

Lift Framework example source code file (BsonDSLSpec.scala)

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

bsondsl, convert, convert, date, dbobject, dbobject, jobject, jobject, list, list, mongo, nil, pattern, regex, regex, util

The Lift Framework BsonDSLSpec.scala source code

/*
 * Copyright 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 mongodb

import BsonDSL._
import json._

import scala.collection.JavaConversions._
import scala.util.matching.Regex

import java.util.{Date, UUID}
import java.util.regex.Pattern

import org.bson.types.ObjectId
import org.specs.Specification

import com.mongodb.{BasicDBList, DBObject}

object BsonDSLSpec extends Specification("BsonDSL Specification") {
  "BsonDSL" should {
    "Convert ObjectId properly" in {
      val oid: ObjectId = ObjectId.get
      val qry: JObject = ("id" -> oid)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)

      dbo.get("id") must_== oid
    }

    "Convert List[ObjectId] properly" in {
      val oidList = ObjectId.get :: ObjectId.get :: ObjectId.get :: Nil
      val qry: JObject = ("ids" -> oidList)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
      val oidList2: List[ObjectId] = dbo.get("ids").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[ObjectId])

      oidList2 must_== oidList
    }

    "Convert Pattern properly" in {
      val ptrn: Pattern = Pattern.compile("^Mongo", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE)
      val qry: JObject = ("ptrn" -> ptrn)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
      val ptrn2: Pattern = dbo.get("ptrn").asInstanceOf[Pattern]

      ptrn2.pattern must_== ptrn.pattern
      ptrn2.flags must_== ptrn.flags
    }

    "Convert List[Pattern] properly" in {
      val ptrnList =
        Pattern.compile("^Mongo1", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE) ::
        Pattern.compile("^Mongo2", Pattern.CASE_INSENSITIVE) ::
        Pattern.compile("^Mongo3") :: Nil
      val qry: JObject = ("ptrns" -> ptrnList)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
      val ptrnList2: List[Pattern] = dbo.get("ptrns").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[Pattern])

      for (i <- 0 to 2) {
        ptrnList(i).pattern must_== ptrnList2(i).pattern
        ptrnList(i).flags must_== ptrnList2(i).flags
      }
    }

    "Convert Regex properly" in {
      val regex: Regex = "^Mongo".r
      val qry: JObject = ("regex" -> regex)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
      val ptrn: Pattern = dbo.get("regex").asInstanceOf[Pattern]

      regex.pattern.pattern must_== ptrn.pattern
      regex.pattern.flags must_== ptrn.flags
    }

    "Convert UUID properly" in {
      val uuid: UUID = UUID.randomUUID
      val qry: JObject = ("uuid" -> uuid)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)

      dbo.get("uuid") must_== uuid
    }

    "Convert List[UUID] properly" in {
      val uuidList = UUID.randomUUID :: UUID.randomUUID :: UUID.randomUUID :: Nil
      val qry: JObject = ("ids" -> uuidList)
      val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
      val uuidList2: List[UUID] = dbo.get("ids").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[UUID])

      uuidList2 must_== uuidList
    }

    "Convert Date properly" in {
      implicit val formats = DefaultFormats.lossless
      val dt: Date = new Date
      val qry: JObject = ("now" -> dt)
      val dbo: DBObject = JObjectParser.parse(qry)

      dbo.get("now") must_== dt
    }

    "Convert List[Date] properly" in {
      implicit val formats = DefaultFormats.lossless
      val dateList = new Date :: new Date :: new Date :: Nil
      val qry: JObject = ("dts" -> dateList)
      val dbo: DBObject = JObjectParser.parse(qry)
      val dateList2: List[Date] = dbo.get("dts").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[Date])

      dateList2 must_== dateList
    }
  }
}

Other Lift Framework examples (source code examples)

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