Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Groovy, Grails, & Gr8Ladies Women Techmakers: Minneapolis

Groovy, Grails, & Gr8Ladies Women Techmakers: Minneapolis

An introduction to Groovy and Grails given at Women Techmakers: Minneapolis

jlstrater

March 22, 2014
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1.  Prerequisites  Java jdk  try ‘java –version’ to

    see if it’s already installed  http://www.grails.org/Installation  To Install Groovy and Grails:  Gvmtool.net  curl –s get.gvmtool.net | bash  gvm install groovy  gvm install grails  gvm use grails 2.3.4
  2.  An organization for the support of women in the

    Gr8 stack community  Goals:  Education  Networking  Professional Development  Contact Us  @Gr8Ladies on twitter  Gr8Ladies.org  LinkedIn, Facebook search Gr8Ladies  Contribute to our website via GitHub
  3.  Groovy – the language  Grails – MVC framework

     Gradle –build automation tool  Used to build Android Apps  Griffon – desktop app framework  Spock – testing framework  Many other minor frameworks and development tools
  4.  Dynamic compiled language  Flexible  optional typing 

    default scope  optional getters/setters  Rapid Development  Integrates with existing JAVA libraries and infrastructure  #groovylang
  5.  Ranges  A collection of numbers defined by boundaries

     Spread dot  Applies a function to every element of a collection and returns the result
  6.  Null safe  user?.chapter?.name?.toUpperCase()  will return null if

    user or chapter is null and will not return an error
  7.  MVC framework  similar to Symphony in PHP, Django

    in Python, or Ruby on Rails  Convention over configuration  #grailsfw
  8.  Domain Classes  Grails Object Relationship Mapping(GORM)  Uses

    Hibernate  Class names map to tables  Field names map to columns  Dynamic finders  User.findAllByGenderAnd ChapterIsNotNull(‘F’)  Events  beforeInsert, afterUpdate, beforeDelete  Alternatives  Mongo and Redis
  9.  Mapped to urls by name  /user/list UserController.list() 

    Url Parameters map to controller  /user/show/id UserController.sho w(id)
  10.  GSP (Groovy Server Page)  Directory structure by convention

     UserController.list() renders /views/user/list.gsp  Special tags for forms, links, looping and other tasks  Reference model data by ${model.attribute}
  11. • www.gebish.org • http://docs.spockframework.org/en/spock-0.7-groovy- 2.0/ • For Grails, in BuildConfig.groovy:

    dependencies { test "org.seleniumhq.selenium:selenium-support:2.40.0" test "org.seleniumhq.selenium:selenium-firefox-driver:2.40.0" test "org.gebish:geb-spock:0.9.2” } plugins { test ":geb:0.9.2" compile ":remote-control:1.4“ // For accessing the context of the // grails app from tests // Spock is already included in Grails 2.3.6 }
  12. package com.allison.pages import geb.Page class EmployeeListPage extends Page { //

    the link that is navigated to // on the 'to' call static url = "employee/list" // the closure that is verified on // the 'at' call static at = { pageTitle.text() == 'Employee List Page' } // The navigator objects static content = { pageTitle(wait: true) { $('h2.title') } employeeLink(wait: true, to: EmployeeShowPage) { id -> $('a.employeeLink', 'id': "${id}") } } // It is good practice to wrap all the // navigator objects in methods boolean employeeLinkIsPresent(Long id) { employeeLink(id).isDisplayed() } EmployeeShowPage clickEmployeeLink(Long id) { employeeLink(id).click() return browser.page } } <html> <head> <title>Employee</title> </head> <body> <h2 class="title">Employee List Page</h2> <ul> <li> <a href="/gebDemo/employee/show/1" id="1” class="employeeLink"> Alice LastName </a> </li> <li> <a href="/gebDemo/employee/show/2" id="2” class="employeeLink"> Bob LastName2 </a> </li> <li> <a href="/gebDemo/employee/show/3" id="3” class="employeeLink"> Charles LastName3 </a> </li> </ul> </body> </html>
  13. package com.allison.functional import com.allison.pages.EmployeeListPage import com.allison.remote.EmployeeRemoteControl import geb.spock.GebReportingSpec class firstSpec

    extends GebReportingSpec { EmployeeRemoteControl remote = new EmployeeRemoteControl() Long employeeId_1 Long employeeId_2 Long employeeId_3 def setup() { EmployeeRemoteControl remote = new EmployeeRemoteControl() employeeId_1 = remote.createEmployee('Alice', 'LastName') employeeId_2 = remote.createEmployee('Bob', 'LastName2') employeeId_3 = remote.createEmployee('Charles', 'LastName3') } CONTINUED  def 'A descriptive name for the feature'() { given: 'Setup stuff' // Can be blank when: 'The employee list page is navigated to' EmployeeListPage employeeListPage = to EmployeeListPage then: 'The employee list page header is displayed' assert at(EmployeeListPage) // All statements in a // then-block are implicitly // asserted and: 'The employee is listed' assert employeeListPage.employeeLinkIsPresent( employeeId_1) when: 'The employee is clicked' employeeListPage.clickEmployeeLink(employeeId_1) then: 'At the show employee page' assert at(EmployeeShowPage) } def cleanup() { remote.deleteEmployee(employeeId_1) remote.deleteEmployee(employeeId_2) remote.deleteEmployee(employeeId_3) } }
  14.  GVM!  Groovy enVironment Manager  curl –s get.gvmtool.net

    | bash  gvm install groovy  gvm install grails  Use grails 2.3.4  Choose an IDE  IntelliJ  Eclipse  Netbeans
  15.  grails create-app appName  auto generates app structure 

    grails create-domain-class User  Creates domain class and test file  grails generate-all appName.User  Auto generates views and controllers based on the properties of the domain class
  16.  Grails war  Defaults to target/appName-0.1.war  Deploy to

    JVM  Example via Amazon ElasticBeanstalk
  17.  Making Java Groovy – Ken Kousen  Email [email protected]

    for coupon code  Programming Grails – Bert Beckwith (O’reilly animal book)  Groovy In Action - Dierk Koenig with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet  Many Tutorials available on grails.org
  18.  Come to Groovy Users of Minnesota(GUM) every second Tuesday

    of the month at Smart Things  Join Gr8Ladies for hack sessions and contribute to the gr8ladies website on github  Gr8Ladies.org or @Gr8Ladies on twitter