groovy in 15 minutes
DESCRIPTION
CADEC 2006 presentation with a quick introduction to Groovy.TRANSCRIPT
![Page 2: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/2.jpg)
CADEC2006, Groovy, Slide 2Copyright 2006, Callista Enterprise AB
Today…
• Building systems requires broad and deep skills
• Complex applications requires a complex platform
• But… same mechanisms used for building simpler applications as for more complex.
![Page 3: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/3.jpg)
CADEC2006, Groovy, Slide 3Copyright 2006, Callista Enterprise AB
PHP with MySQL
![Page 4: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/4.jpg)
CADEC2006, Groovy, Slide 4Copyright 2006, Callista Enterprise AB
Java keeping up…
• Integrating existing script languages• Beanshell• Building a new script lanugage -
Groovy
![Page 5: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/5.jpg)
CADEC2006, Groovy, Slide 5Copyright 2006, Callista Enterprise AB
What´s Groovy?
- Standard - JSR #241 - A scripting language that is tightly
integrated into the Java platform.- A dynamic, agile OO language for the
JVM- Full access to the Java API- Groovy Scripts can be compiled into
Java bytecode- Static or dynamic typing
![Page 6: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/6.jpg)
CADEC2006, Groovy, Slide 6Copyright 2006, Callista Enterprise AB
Java
public class Utils {
public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("Dupond");list.add("Dupont");list.add("Tintin");List<String> filteredList = Utils.findAll("Dup", list);for (String name : filteredList) {
System.out.println(name);}
}
public static List<String> findAll(String filter, List<String> items) {List<String> result = new ArrayList<String>();for (String item : items) {
if (item.contains(filter)) { result.add(item);
}}return result;
}}
![Page 7: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/7.jpg)
CADEC2006, Groovy, Slide 7Copyright 2006, Callista Enterprise AB
Groovy
list = ["Dupond","Dupont","Tintin"]
duponts = list.findAll { it.contains("Dup") }
duponts.each { println it }
![Page 8: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/8.jpg)
CADEC2006, Groovy, Slide 8Copyright 2006, Callista Enterprise AB
Strings
GStringsname = ”Tintin"message = ”Hello ${name}”
MultilinesomeXML = ”””
<character><name>Tintin</
name></character>”””
![Page 9: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/9.jpg)
CADEC2006, Groovy, Slide 9Copyright 2006, Callista Enterprise AB
Collections - list
list = [1,2,3]longerlist = list + [”four”,”five”]longerlist now equals [1,2,3,”four”,”five”]
indexing:
list[2..3] equals [3,”four”]list[-1] equals ”five”
![Page 10: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/10.jpg)
CADEC2006, Groovy, Slide 10Copyright 2006, Callista Enterprise AB
Collections - Map
niceMap = ["tintin":true,"Rastapopulous":false]
println niceMap[”tintin”]> True
niceMap.calculus = truePrintln niceMap> ["tintin":true,"Rastapopulous":false,”calculus”:true]
![Page 11: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/11.jpg)
CADEC2006, Groovy, Slide 11Copyright 2006, Callista Enterprise AB
Closures
def list = [1,2,3]
def square = { numberToSquare ->numberToSquare*numberToSquare }
def squaredlist = list.collect (square)
squaredlist equals [1,4,9]
newlist.each { println it }149
![Page 12: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/12.jpg)
CADEC2006, Groovy, Slide 12Copyright 2006, Callista Enterprise AB
Example
• Extract data from a database table into XML
![Page 13: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/13.jpg)
CADEC2006, Groovy, Slide 13Copyright 2006, Callista Enterprise AB
def sql = groovy.sql.Sql.newInstance("jdbc:hsqldb:hsql://localhost/groovy",
"sa","","org.hsqldb.jdbcDriver")
def xml = new groovy.xml.MarkupBuilder()
xml.product_catalogue() { sql.eachRow("select * from product") { row ->
xml.product() { name(row.name) description(row.description)}
}}
From database into XML
![Page 14: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/14.jpg)
CADEC2006, Groovy, Slide 14Copyright 2006, Callista Enterprise AB
The XML
<product-catalogue> <product> <name>iPod</name> <description>mp3 player with video</description> </product> <product> <name>MacBook Pro</name> <description>Intel based Apple</description> </product></product-catalogue>
![Page 15: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/15.jpg)
CADEC2006, Groovy, Slide 15Copyright 2006, Callista Enterprise AB
What about the other way?
def file = new java.io.File("/groovy/products.xml")
def products = new groovy.util.XmlParser().parseText(file.getText())
def sql = groovy.sql.Sql.newInstance( "jdbc:hsqldb:hsql://localhost/groovy", "sa","","org.hsqldb.jdbcDriver")
products.each { product ->sql.execute("insert into product values (?,?)”, [product.name.text(),product.description.text()])
}
![Page 16: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/16.jpg)
CADEC2006, Groovy, Slide 16Copyright 2006, Callista Enterprise AB
Unit testing
• JUnit built into runtime => script your tests for Groovy and Java classes with Groovy syntax
• Groovy provides several new assertions
• Easily scripted with Ant or Maven• Integrate Groovy unit tests with your
existing suite
![Page 17: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/17.jpg)
CADEC2006, Groovy, Slide 17Copyright 2006, Callista Enterprise AB
Unit testing
class StringSplitTest extends GroovyTestCase {void testFullSplit() { splitArray = StringSplitter.split(
"groovy.util.GroovyTestCase", ".")
expect = ["groovy", "util", ”GroovyTestCase"].toArray()
assertArrayEquals(expect, splitAr) }}
![Page 18: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/18.jpg)
CADEC2006, Groovy, Slide 18Copyright 2006, Callista Enterprise AB
Other features
•Ant Scripting, Templates•Groovlets, GSP, Swing, SWT, XMLRPC•GroovyBeans
class Product {
@Property String name
@Property String description
}
![Page 19: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/19.jpg)
CADEC2006, Groovy, Slide 19Copyright 2006, Callista Enterprise AB
Grails
• ”Coding by convention”-paradigm• A toolkit of Spring, Hibernate, SiteMesh…• Smaller applications
– forums, blogs etc.
![Page 20: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/20.jpg)
CADEC2006, Groovy, Slide 20Copyright 2006, Callista Enterprise AB
What else can you do?
• Configuration• Simple tasks• Prototypes• Building and testing• Agile development• Rules for rules engines• ESB transformations• …
![Page 21: Groovy in 15 minutes](https://reader033.vdocuments.mx/reader033/viewer/2022061217/54b43a444a7959261f8b45ce/html5/thumbnails/21.jpg)
CADEC2006, Groovy, Slide 21Copyright 2006, Callista Enterprise AB
Q&A
[email protected]://www.callistaenterprise.se/