SuperPlan(1)TaoBao Winner - Prepare the Play
SuperPlan(1)TaoBao Winner - Prepare the Play
1. Upgrade the play framework
First steps, update the play framework to the latest version. 2.1.1
>play new taobaowinner
>cd taobaowinner
>play eclipse
Run and Verify the environments.
>play
>run
It works fine.
2. Prepare the Sample
But I want to take some old projects as references. So I want to upgrade the old projects to new version.
>vi project/plugins.sbt
// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.1.0")
>vi project/Build.scala
//import PlayProject._
import play.Project._
//val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
val main = play.Project(appName, appVersion, appDependencies).settings(
>vi project/build.properties
sbt.version=0.12.2
Once finish the configuration changes, clean and recompile the project.
>play clean
>play run
>play eclipse
Update the IDE to view the codes.
http://download.scala-ide.org/nightly-scala-ide-juno-210x
Compile Error Message
not found: object anorm
not found: value DB
not found: value get
object db is not a member of package play.api
Solution:
Change the import packages lines as follow:
import anorm._
import anorm.SqlParser._
import play.api.Play.current
import play.api.db.DB
3. Build and Learn from computer-database
Seq is a collection, Pk is the primary key of the table in anorm
I understand the Models part now. It is much easier, just directly use the SQL, scala is really the powerful language.
Try to understand the Test Case
http://www.playframework.com/documentation/2.1.1/ScalaTest
Test Your Application
Run all the test cases
play>test
Run the specified test case
play>test-only com.sillycat.MySpec
Using Specs2
http://etorreborre.github.io/specs2/
Download the sample and understand the unit tests.
>git clone https://github.com/etorreborre/specs2-test.git
>mvn eclipse:eclipse
Import to eclipse and then we can see a lot of samples.
Scala Function Test
http://www.playframework.com/documentation/2.1.1/ScalaTest
http://www.playframework.com/documentation/2.1.1/ScalaFunctionalTest
The test class should be look like this:
package unit
import org.specs2.mutable._
class HelloWorldUnitSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size (11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
And I will run the command
play>test-only unit.HelloWorldUnitSpec
I suddenly found that my play version is 2.1.0, I plan to update that to 2.1.1.
>play clean
>play eclipse
Running multiple examples inside the same Specification
"Computer model" should {
"be retrieved by id" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val Some(macintosh) = Computer.findById(21)
macintosh.name must equalTo("Macintosh")
macintosh.introduced must beSome.which(dateIs(_, "1984-01-24"))
}
}
"be listed along its companies" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val computers = Computer.list()
computers.total must equalTo(574)
computers.items must have length(10)
}
}
…snip…
All the tests are based on the memory database and it is based on the SQL files.
Unit Testing Controllers
"Application" should {
"redirect to the computer list on /" in {
valresult = controllers.Application.index(FakeRequest())
status(result) must equalTo(SEE_OTHER)
redirectLocation(result) must beSome.which(_ == "/computers")
}
…snip…
Integration Test
"Application" should {
"work from within a browser" in {
running(TestServer(3333), HTMLUNIT) { browser =>
browser.goTo("http://localhost:3333/")
browser.$("header h1").first.getText must equalTo("Play 2.0 sample application — Computer database")
browser.$("section h1").first.getText must equalTo("574 computers found")
browser.$("#pagination li.current").first.getText must equalTo("Displaying 1 to 10 of 574")
browser.$("#pagination li.next a").click()
browser.$("#pagination li.current").first.getText must equalTo("Displaying 11 to 20 of 574")
browser.$("#searchbox").text("Apple")
browser.$("#searchsubmit").click()
References:
http://sillycat.iteye.com/blog/1753450
http://sillycat.iteye.com/blog/1754139
http://sillycat.iteye.com/blog/1754381
http://sillycat.iteye.com/blog/1757098
http://sillycat.iteye.com/blog/1774803
http://www.playframework.com/documentation/2.1.0/SBTSubProjects
http://www.playframework.com/documentation/2.1.0/Migration