Day-2 Sessions
Groovy in 2014 and Beyond - Guillaume Laforge
The very first session I chose to attend was Groovy in 2014 and Beyond by Guillaume Laforge, the Head of Groovy Development. It was very informative session giving details on Groovy's recent neat features and future possibilities and directions.
My notes
Grails 3.0 Preview - Graeme Rocher
My notes
Groovy in 2014 and Beyond - Guillaume Laforge
Groovy in 2014 and Beyond - Guillaume Lafarge |
My notes
- Groovy news letter is out every Tue
- Google+ groovy page google.com/+groovy
- Google+ groovy community
- Closures vs. Lambdas
- Traits
- New: trait keyword
- Like interfaces but with method bodies
- Multiple inheritance without the <<Diamond>> problem
- When a class implements traits, methods are available and visible, traits properties are also visible making the class stateful
- Inheritance: trait can extend another trait and inherit properties
- New: @TailRecursive
- New: @Sortable - contributed by Griffon, Makes class comparable, can specify includes, excludes.
- @BaseScript improvement, custom internal abstract method
- NIO2 module
- JDK7+ NIO2 Path
- All familiar methods like withReader, eachLine, << , readLines etc. in GDK on File have been retrofitted on Path as well
- JSON
- Rewrote and performance improvement, 3 to 4 times faster than Jackson and GSON JsonSlurper for configuration files
- Markup Template Engine
- GroovyDoc 2.3.6 and up - nicer looking javadoc
- beta.groovy-lang.org - generated from groovy template engine, groovy code, ascii doctor
- Groovy 2.4
- Android support bit.ly/nyt-groovy
- Groovy Macros - Authoring AST transformations is verbose, with Groovy macros it is simpler
- Groovy 3.0
- New MOP goals: leverage JDK 7+ invoke dynamic, get java-like performance even for dynamic code
- Antlr v4 grammar - Google summer code. student helping
- Groovy still uses antlr v2, but v3 and v4 are out, harder to evolve with v2 as groovy grammar evolved from Java grammar
- Java 8 Support
- Lambdas, Stream API, Date and Time API, method references, default methods in interfaces, annotations on types and repeated annotations
Grails 3.0 Preview |
My notes
- Grails 2.4.3 is the recent
- Grails 3.0 - The Future
- is the master branch @Github
- Goals: embrace Gradle, Reach outside the Servlet Container, deployment with runnable jars, Build on Spring Boot, Support micro services, many improvements
- Grails is one of the entry layers along with Spring XD, with Groovy being at the foundation of Spring IO platform
- Gradle as the build system
- Demo
- Benefit of building upon Spring boot is - IDE tooling doesn’t need any special efforts. Project can just be imported as Gradle project into IntelliJ community edition and the app can just be run as an application. Grails 3.0 has Application class which extends Spring boot class with run method. This makes the application runnable from the IDE.
- Spring Boot is going to handle Embedded Servers, Runnable JARs, WAR packaging, Scripting and Micro services, includes monitoring and health checkes, Grails takes advantage of Spring Boot.
- Full Boot powered micro services
- Servlet 3.x only and no web.xml
- Simplifications (just DispatchedServlet and GrailsController)
- GrailsPageFilter, UrlMappingFilter are gone
- Less code to maintain, better performance, no internal forwarding, better integration with spring
- Deprecations /Removals
- Servlet 2.5 Support, no web.xml
- GDoc replaced with AsciiDoctor
- Gant & current build system is gone
- Filters replaced by new mechanisms
- Evolution of Metaprogramming
- Grails 1.x: runtime meta programming ExpandoMetaClass, Grails 2.x - compile time, Grails 3.0- Traits and Transforms
- Most code previously added by AST Transforms will now be added by Traits
- Application profiles
- Grails 3.0 Challenges: Compatibility-plugins, build system, Modularization-servlet API independence. Refactoring.
- The whole package structure has changed, package renaming, separating out new public api and old api etc.
Testing Java, Groovy, Spring and Web applications with Spock - Peter Neiderwieser
Testing Java, Groovy, Apring and Web applications with Spock - Peter Neiderweiser |
My Notes
- Spock Web console: https://meetspock.appspot.com/
- expect block. Everything in this block is treated as assertions. So you don’t need special assertion FW or assert statements.
- given: when: then: blocks
- multiple when: then: blocks
- Mocking FW built in
- Async support
- Extensions: Spring, Guice, Tapestry, Unitils, JUnit rules etc.
- Default Grails FW testing (since 2.3)
- Diff Dialog Window: Shows differences of assertion failures in a separate window. IntelliJ and Eclipse will have clickable links to get to this window.
- Mocking- Mock(MyClass)
- def sub1 = Mock(Subscriber)
- sub1.receive(_) >> {throw new Exception()} //throws an exception whenever receive method is called on sub1 that takes no arguments. (_) groovy notation to indicate that the method takes no arguments
- Stubs and Spies
- Stub - weaker form of mock object
- Spy- can spy on class, creates a real object under the spy
- Advanced Mocking for testing Groovy code. GroovyMock for groovy code that has dynamic code, GroovySpy, GroovyStub. The call to mock object has to be a groovy call.
- Testing Spring Applications
- Full support for Spring Test Context Framework (2.5 - 4.1, the very latest)
- Use spock-spring module
- @SpringApplicationConfiguration
- Testing web applications
- Spock + Geb (Groovy Browser Automation, pronounced jeb)- Groovy lib on top of Selenium, easy to script your browser
- Functional Web Testing
- Business Friendly Reporting
- gradle dependencies: geb-spock, geb-junit4
- MySpec extends GebReportingSpec
- @Stepwise spock annotation- when annotated spock treats each method as a step instead of a test case.
- go “/login” //navigate to login page
- $(“h1”).text == ‘Login Successful” //Geb offers jQuery like accessing html elements
- when one step fails, it wouldn’t even run the other steps.
- Page Object pattern: a small abstraction that abstracts the page for the purpose of testing. LoginPage extends Page {}
- TIP: def page = to LoginPage //tell intelliJ what page object is for code completion
- alertDialog, alertMessage, alertClosedButton, alertDialogIsClosed
- Spock report
- @Title annotation for a testcase
- @Narrative multi-line
- @Issue(http://...) //will show up in the report
- GebConfig.groovy (baseUrl = “”, reportsDir=”geb-reports”, driver = “chrome”)
Spring Boot for the Web Tier - Dave Syer, Philwebb
Spring Boot for the Web Tier - Dave Syer, Phil Webb |
My Notes
- Conventions: static content
- webjars- client side libs like jQuery packaged as jar and served as static content, gives all dependency management advantages, transitive, version etc.
- Grunt toolchain - Javascript toolchain
- Wro4j - Web resource optimizer
- Dynamic content - Templating Support
- Thymeleaf (modern templating fw) - Spring Boot uses this
- Groovy Templating Language (DSL based)
- Freemaker, Velocity
- JSP(Not recommended)
- Conventions
- Choose specific locale using spring.mvc.locale property
- Choose specific date format using spring.mvc.date-format property
- Hidden Gems: RequestContextHolder,
- HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, Converter, Formatter etc.
- spring.mvc.message-codes-resolver-format to add a MessageCodesResolver- prefix_error_code or postfix_error_code
- Embedded Server: Default is Tomcat
- When using WARs a ServletContainerInitializer (Tomcat) creates Spring ApplicationContext
- When using embedded server, ApplicationContext creates the server.
- Embedded server customization, Tomcat, Jetty specific. TomcatConnectorCustomizer, TomcatContextCustomizer
- Embedding Ratpak
Making Spring Boot even Groovier - Graeme Rocher
Making Spring Boot even Groovier - Graeme Rocher |
My Notes
- Spring Boot is not dependent on Groovy but already has groovy
- Spring Boot Components
- Boot CLI
- Spring Groovy Templates
- Spring Boot AutoConfiguration
- Adding Groovy to Spring Boot
- Starting Slow: Gradle for the build, Spock for testing
- Digging deeper: Groovy controllers, Groovy templates
- Going Groovy all the way: GORM, GSP
- Step 1: Adding a powerful build system: Flexible and Powerful, Based on Groovy DSL, extremely well documented, Used by Android, Better than Maven ;)
- Builds are not static. Maven builds are static, it is not designed to be changed.
- Step 2: Spock (Path to enlightenment ;))
- Data driven tests, power assertions with detailed diagnostics of failures, Integrated mocking / stubbing
- Add Spock as a test scoped dependency testCompile(“spock ….”)
- Step 3: Groovy Everywhere
- Seamlessly mixed with Java code, with @CompileStatic now there is no longer performance cost. Performance difference between Groovy and Java with @CompileStatic is basically ZERO.
- Just annotate classes with @CompileStatic and the job is all done ;)
- When you use @CompileStatic, meta programming cannot be used. If there is any particular method that needs dynamic behavior, just annotate that particular method with @CompileDynamic, that makes that method dymanic leaving the class compiled to static.
- With tooling being good, it is now easier than ever to introduce Groovy into the Java organizations.
- Why Groovy? : Get Java 8 Lambdas but deployable to any JVM (1.5+), Extensive Groovy SDK, Easier to learn, Android support, static or dynamic compilation
- Step 4: Groovy Templates- Writing views with Groovy
- MarkupTemplateEngine introduced in Groovy 2.3
- Add groovy-templates as a dependency
- Add templates with .tpl extension into src/main/templates/layouts and views into src/main/templates/views
- Elegant, readable views expressed in Groovy
- Step 5: GORM
- Powerful multi-datastore query layer (HIBERNATE, MongoDB, redis, Cassandra, Neo4j)
- Dynamic finders, criteria and persistence methods, Automatic mapping of entities to underlying database
- Add dependency gorm-hibernate4-spring-boot
- Each GORM entity needs to be annotated with grails.persistence.Entity @Entity. With this Spring Boot will detect the class as GORM entity. That’s all you need to do.
- Data source configurations remain same, GORM will pick it up.
- with @GrailsCompileStatic, dynamic finders in GORM work. Not with @CompileStatic ;)
- GORM for HIBERNATE is trivial annotate with Grails @Entity
- Powerful query composition with detached criteria
- GORM for MongoDB
- Geospacial querying
- GeoJSON models, full text search, stateless and stateful modes etc.
- Step 6: GSP
- view rendering engine from Grails
- Supports tag libraries, xss/double encoding prevention
- Layout, templates and views
- More designer friendly
- XSS prevention, development and precompiled mode
- Impossible in GSP to double escape data, GSP escapes all data rendered.
- Just add grails-gsp-spring-boot dependency
- Add templates with .gsp extension in src/main/resources/templates/views/
- main.gsp gets applied to every page
- can do all normal grails things for composing views.
- Each gsp is a fully formed page with <html>...</html> tags, though main.gsp is taken as the template and only <title> and <body> is taken from the page. It makes designers to work on individual pages.
- easy definition of tag libraries in Groovy code
- Elegant markup based views
No comments:
Post a Comment