Integrating Scala Code Coverage Tool (SCCT) in Play Scala project


In my last blog i’ve elaborated the steps to integrate the JaCoCo with your Play Scala project. This time we’ll learn about how to integrate the Scala Code Coverage Tool ( SCCT ) in your Play Scala project.

Just some easy steps and you’re done with SCCT integration. Lets see it in action.

1. Add the following resolvers in your plugin.sbt file.

resolvers += Classpaths.typesafeResolver

resolvers += "scct-github-repository" at "http://mtkopone.github.com/scct/maven-repo"

2. Add this sbt plugin there as well.

addSbtPlugin("reaktor" % "sbt-scct" % "0.2-SNAPSHOT")

3. We have a model object Hello.scala in app/models & its corresponding test HelloTest.scala in test/models

4. After adding these just do :

play clean compile eclipsify run

5. Now its time to have the coverage report for our tests. Run scct:test as shown below
SCCT Test Coverage

6. You’d find the test coverage report for you project in /target/scala-2.9.1/coverage-report/index.html .

Note : Open the index.html file with Firefox browser.

7. Here’s our test coverage report :
SCCT Test Coverage Report

Find the code here on Github

Posted in Scala, Web | Tagged , , , | 1 Comment

Integrate SCCT (Scala Code Coverage Tool) with Lift Scala project


Its always a good practice to have a fine test coverage in any project. I am working on Lift framework. I have tried to integrate the SCCT with my lift scala project.

Here, I am going to elaborate a few easy steps to integrate the SCCT in Lift Scala project. I am using scala 2.10.0 and sbt 0.12 version.

Continue reading

Posted in LiftWeb, Scala | 1 Comment

KnolX Session: Testing Akka Actors Using TestKit


In the presentation which is the part of our ongoing Knolx session, we discussed about how to test Akka actors using Testkit. By using testkit we can test Actor’s behaviour, apply time assertions on actor’s reply and we can also do integration testing of actors. We also discussed how to use TestProbe to test actors which are supposed to send messages to different destinatations.

 

 

Posted in Agile, AJAX, Architecture, Cloud, Scala, Web | Leave a comment

ScalaJobz updates: Job search app for iOS


Knoldus is excited to announce the official release of our award-winning ScalaJobz search app for iOS using Phonegap! The ScalaJobz Search app is now available for free download in the App store.

ScalaJobz is a job aggregator for Scala Jobs across the world. It aggregates latest job offerings from various Job Portals like Indeed, Simply Hired ,Career Builder etc. It provides dedicated Scala job posting avenue for organizations looking for Scala talent. ScalaJobz is a winner of the Typesafe developer competition.

Continue reading

Posted in AJAX, Web | Tagged , , | Leave a comment

Integrating JaCoCo in your Play Scala project


Its always good to have a fine test coverage in any project. I am working on Play framework & this time i’ve tried to integrate the JaCoCo with my play scala project & it’s cool to have it in my project.

I am going to elaborate a few easy steps to integrate the JaCoCo in Play Scala project.

1. Add the following lines of code in your plugin.sbt file. You’ve to add the plugin dependency to your project’s plugins.sbt.

libraryDependencies ++= Seq(
 	"org.jacoco" % "org.jacoco.core" % "0.5.7.201204190339" artifacts(Artifact("org.jacoco.core", "jar", "jar")),
 	"org.jacoco" % "org.jacoco.report" % "0.5.7.201204190339" artifacts(Artifact("org.jacoco.report", "jar", "jar")))

 addSbtPlugin("de.johoop" % "jacoco4sbt" % "1.2.3")

2. Have this import in your Build.scala file.

import de.johoop.jacoco4sbt.JacocoPlugin._

3. Add the following line in your Build.scala file.

lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)

This line adds the jacoco settings to the variable jacoco_settings.

4. Add these settings in to your Play project in Build.scala file as

 val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA, settings = jacoco_settings).settings(
    parallelExecution in jacoco.Config := false)

parallelExecution in jacoco.Config := false makes sure that the ParallelExexution is false during testing.(Look at Point 7)

Note that we’ve a model class with name Test.scala & a test class DemoTest.scala for now.

5. That’s it. Lets run the JaCoCo with command jacoco:cover

6. You’ll find the html file with name index.html which would give you the full test report. You’ll find this file in this path /target/scala-2.9.1/jacoco/html

This is how your coverage report would look like :

null

7. You can check whether the parallel execution if false or not as

Get the CodeCoverage here

Posted in Scala, Web | Tagged , , , | Leave a comment

Pattern Matching – at a glance


I presented types of Pattern Matching at Knolx session at Knoldus. The presentation talks about the type of pattern matching in Scala with small examples.

Posted in Scala | Tagged , , | Leave a comment

Getting Longitude & Latitude for a address using Play Framework 2.1 WS API


This blog post serves the two purposes :

First , We’ll learn about getting the latitude & longitude for an address using Google Geocoding API.

Second , We’ll do the same as mentioned above using Play 2.1 WS API & learn about how to call the web services in Play 2.1.

On order to call the other HTTP services within Play 2.1 we use play.api.libs.ws.WS library. Calls made by using this returns scala.concurrent.Future[play.api.libs.ws.Response].

In order to get the Latitude & Longitude for a address we need to have a GET call with the following query parameters.

- address -> For which the latitude & longitude is required.
- sensor – Indicates whether or not the geocoding request comes from a device with a location sensor. Should be true here

Lets do the same by using Play 2.1 WS API.

Our GET call would looks like

WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=NewDelhi&sensor=true").get()

As mentioned above the result of this GET cal would give us scala.concurrent.Future[play.api.libs.ws.Response] & we’ll map over it to get the results. The code stuff is below :

def fetchLatitudeAndLongitude(address: String): Option[(Double, Double)] = {
    implicit val timeout = Timeout(50000 milliseconds)
    
    // Encoded the address in order to remove the spaces from the address (spaces will be replaced by '+')
    //@purpose There should be no spaces in the parameter values for a GET request
    val addressEncoded = URLEncoder.encode(address, "UTF-8");
    val jsonContainingLatitudeAndLongitude = WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=" +      
    addressEncoded + "&sensor=true").get()
   
    val future = jsonContainingLatitudeAndLongitude map {
      response => (response.json \\ "location")
    }

    // Wait until the future completes (Specified the timeout above)

    val result = Await.result(future, timeout.duration).asInstanceOf[List[play.api.libs.json.JsObject]]
    
    //Fetch the values for Latitude & Longitude from the result of future
    val latitude = (result(0) \\ "lat")(0).toString.toDouble
    val longitude = (result(0) \\ "lng")(0).toString.toDouble
    Option(latitude, longitude)
  }

Lets us elaborate the process step by step :

- We’ve specified the timeout for the Future , within this duration the results from the future would be expected. (Line-2)

- Made a WS GET call with the query parameters which are address & sensor. (Line -7 )

- Let the future calculate the results & wait for the specified timeout duration. (Line 10 & 16 )

- Fetched the values of Latitude & Longitude from the result of future. (Line 19-20)

Now we are ready with our WS GET call to Google Geocoding API in order to get the latitude & longitude for the given address. Lets call the method from our Global.scala when the application starts.

override def onStart(app: Application) {
    Logger.info("Application has started.........!!!!!!")
    FetchLatitudeAndLongitudeUtil.fetchLatitudeAndLongitude("New Delhi")
  }

Here’s the output when the application starts :

We’ve used WS GET call to serve our purpose. A WS POST call looks like :

 WS.url("URL").post(Map("key" -> Seq("value")))

More on Play 2.1.0 WS API .

Code for the above example can be found on Github as well.

Posted in Scala | Tagged , , , | Leave a comment