Mathjax javascript librry setup

Scala Working Notes

Introduction

This notebook contains my “working notes” written as I read or work on a project. They are organized by date, or by project.

My organized reference notes are kept in:

Logging

The tricky part of logging is not actually implementing in your source code, its setting up a configuring a suitable logging framework.

I’m using a simple set-up from the Scala Cookbook bonus PDF to provide a basic logging capability.

I am using the Grizzled-SLF4J from Clapper.org. There are quite a number of utilities on the site … its worth checking out.

Grizzled-SLF4J uses the SLF4J logging facade, which in turn must be bound to a specific logging framework that supports SLF4J. SLF4J ships with a number of jar files called “bindings”. See the SLF4J User Manuall.

Different Logging Frameworks will have different configuration set-up and configuration requirements.

I’m binding in the Simple Logger implementation which:

  • Outputs to System.err by default
  • Outputs all logs of INFO and higher bu default.
  • Selects a basic log output format by default

Setup

  • Add the following dependencies to the project’s build.sbt file:
1
2
3
libraryDependencies ++= Seq("org.slf4j" % "slf4j-api" % "1.7.5",
                            "org.slf4j" % "slf4j-simple" % "1.7.5",
                            "org.clapper" %% "grizzled-slf4j" % "1.0.1")  // For Scala 2.10 or later
  • The SLF4J Simple Logger can be configured using system properties, or by a simplelogger.properties file which must be on the classpath. I put the following simplelogger.properties file in the src/main/resources directory:
1
2
3
4
5
6
7
8
org.slf4j.simpleLogger.logFile         = System.err
org.slf4j.simpleLogger.defaultLogLevel = info
org.slf4j.simpleLogger.showDateTime    = true
org.slf4j.simpleLogger.dateTimeFormat  = yyyy'/'MM'/'dd' 'HH':'mm':'ss'-'S
org.slf4j.simpleLogger.showThreadName  = true
org.slf4j.simpleLogger.showLogName     = true
org.slf4j.simpleLogger.showShortLogName= false
org.slf4j.simpleLogger.levelInBrackets = true

Using Grizzled-SLF4J

  • Read the documentation and the ScalaDoc

  • Tp keep it simple, mix in the Logging trait, and use log messages as follows
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    import grizzled.slf4j.Logging
    
    object Main extends Logging {
        def main(args: Array[String]) {
        logger.info("Starting Tests")
        logger.trace("This is a trace level log")
        logger.info("Ending Tests")
    
    // When mixing in the logging trait you don't need to use the logger object:
        info("Starting Tests")
        trace("This is a trace level log")
        info("Ending Tests")
    }
    
  • You can also log exceptions, get the name of the current logger, see what log level is enabled, etc.

Other Scala Logging Frameworks

Testing in Scala

Using Scala Test

  • The book Testing in Scala is an excellent, concise tutorial on testing in Scala using:
    • ScalaTest - With ScalaTest, you can test either Scala or Java code. By offering deep integration with tools such as JUnit, TestNG, Ant, Maven, sbt, ScalaCheck, JMock, EasyMock, Mockito, ScalaMock, Selenium, Eclipse, NetBeans, and IntelliJ, ScalaTest makes it easy to take your testing to a higher, more productive level in new or existing Scala or Java projects.
    • Specs2 - specs2 is a library for writing executable software specifications. With specs2 you can write software specifications for one class (unit specifications) or a full system (acceptance specifications).
    • ScalaCheck - ScalaCheck is a library written in the Scala Programming Language and is used for automated specification-based testing of Scala or Java software applications. ScalaCheck was originally inspired by the Haskell library QuickCheck, but has also ventured into its own.

Scala Test

Installation and Set-up

Running Scala Test

SBT Triggered Execution

You can make a command run when certain files change by prefixing the command with ~

Monitoring is terminated when enter is pressed
To have SBT watch for any changes to files, and automatically re-run tests use the SBT command ~ test

Writing Tests With Scala Test

Setting up my Scala Projects

This assumes that Eclipse and SBT are already set-up, as per Scala Tools These notes are for setting up a specific Scala project

Directory Structure

  • SBT and Eclipse use the standard Maven directory structure:

    project-root .build.sbt - SBT build file .gitignore - files for git to ignore scalastyle_config.xml - ScalaStyle config file

    src
        main
            java
            scala
                ca
                    bashd
                        my-package
            resources
        test
            java
            scala
                ca
                    bashd
                        my-package
  • set up the directory and:

1
2
3
cd project-root
sbt
sbt

ScalaCheck

  • ScalaCheck is a library written used for automated property-based testing of Scala or Java programs. ScalaCheck was originally inspired by the Haskell library QuickCheck, but has also ventured into its own.
  • It can be run on its own, or with ScalaTest or Specs2
  • There are three main components to ScalaCheck:
    • a Properties class that defines tests and runs them through a test harness called Prop. Properties can be mixed and matched with various groupings and combinations, as well as filtered to provide only the data needed for the test.
    • a Gen object, which is a generator class that provides much of the fake data and allows you to control the kind of data created. For instance, if you want only positive integers, you can use Gen to eliminate negative integers and zero.
    • the Arbitrary class is used for custom types, which of course are useful because your programs are made up of more than primitive types.

Excerpt From: Daniel Hinojosa. “Testing in Scala.” iBooks. https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=212D3448DB9CA6A91FFB94E4A42648C8

Comments