Groovy Programming Cookbook I
Total Page:16
File Type:pdf, Size:1020Kb
Groovy Programming Cookbook i Groovy Programming Cookbook Groovy Programming Cookbook ii Contents 1 Groovy Script Tutorial for Beginners 1 1.1 Environment......................................................1 1.2 Scripting Basics....................................................1 1.3 Example - Order Processing.............................................1 1.4 Planning and approach................................................2 1.5 Groovy Scripts for Business Objects.........................................2 1.5.1 Order POGO.................................................2 1.5.2 First Level Unit Test.............................................4 1.5.3 Test Order - Random Entries Onetime....................................5 1.5.4 Test Order - Random Entries at Intervals...................................6 1.6 Groovy Scripts for Database.............................................9 1.6.1 Configuration file to hold the database properties..............................9 1.6.2 Parsing the configuration file......................................... 10 1.6.3 Create Table................................................. 11 1.6.4 Insert Rows from a Single File........................................ 12 1.6.5 Select Total Count.............................................. 14 1.6.6 Select All Rows................................................ 14 1.6.7 Insert Rows from the matching files at interval............................... 16 1.6.8 Truncate Table................................................ 19 1.6.9 Drop Table.................................................. 19 1.7 Conclusion...................................................... 20 1.8 Code References................................................... 20 1.9 Download the Source Code.............................................. 20 2 Groovy Dictionary Example 21 2.1 Environment...................................................... 21 2.2 Concept behind Dictionary.............................................. 21 2.3 Examples....................................................... 21 2.3.1 Custom Dictionary - Illustration....................................... 22 2.3.2 Country Captial Dictionary - Realistic Example............................... 23 2.4 Code References................................................... 26 2.5 Download the Source Code.............................................. 26 Groovy Programming Cookbook iii 3 Groovy Json Example 27 3.1 Environment...................................................... 27 3.2 Important Classes................................................... 27 3.3 JSON Examples.................................................... 28 3.3.1 Producing JSON data from a literal String.................................. 28 3.3.2 Producing JSON data from a POGO..................................... 28 3.3.3 Producing JSON data via JSONSlurper using literal String......................... 29 3.3.4 Producing JSON data via JSONSlurper using POGO Instance(s)...................... 30 3.3.5 Producing JSON data via JSONSlurper and verify the data type of attributes................ 31 3.3.6 Parsing JSON data via JsonSlurper by reading an input file......................... 31 3.3.7 Creating JSON data via JSONBuilder from POGO............................. 32 3.3.8 Creating JSON data via JSONBuilder and Write to a File.......................... 34 3.3.9 Code References............................................... 36 3.4 Download the Source Code.............................................. 36 4 Groovy String Example 37 4.1 Introduction...................................................... 37 4.2 Warmup........................................................ 37 4.3 Concatenation vs GString............................................... 37 4.4 Operators....................................................... 38 4.5 Multiple Lined Strings................................................ 39 4.6 String Tokenize.................................................... 39 4.7 Conclusion...................................................... 39 5 Groovy Closure Example 41 5.1 Introduction...................................................... 41 5.2 Closure Declaration.................................................. 41 5.3 Closure Parameters.................................................. 41 5.4 VarArgs........................................................ 42 5.5 Closure Passing.................................................... 43 5.6 Closure Composition................................................. 43 5.7 Conclusion...................................................... 43 6 Groovy Regex Example 45 6.1 Introduction...................................................... 45 6.2 Quick Start...................................................... 45 6.3 Advanced Usage................................................... 46 6.4 Matchers....................................................... 47 6.5 Conclusion...................................................... 48 Groovy Programming Cookbook iv 7 Groovy Collect Example 49 7.1 Introduction...................................................... 49 7.2 Collect......................................................... 49 7.3 Collect with Supplied Collector............................................ 50 7.4 Conclusion...................................................... 51 8 Groovy Date Example 52 8.1 Introduction...................................................... 52 8.2 Warmup........................................................ 52 8.3 Date Parsing...................................................... 52 8.4 Date Formatting.................................................... 53 8.5 Date Arithmetic.................................................... 53 8.6 Subscript Operators.................................................. 54 8.7 Conclusion...................................................... 55 9 Groovy Array Example 56 9.1 Introduction...................................................... 56 9.2 Array Declaration................................................... 56 9.3 Access Array Items.................................................. 57 9.4 Add Item Exception.................................................. 57 9.5 Array Length..................................................... 57 9.6 Array Min & Max Value............................................... 58 9.7 Remove Item..................................................... 58 9.8 Array Ordering.................................................... 59 9.9 Array Lookup..................................................... 59 9.10 Conversion...................................................... 60 9.11 Conclusion...................................................... 60 10 Groovy Console Example 61 10.1 Introduction...................................................... 61 10.2 Action Buttons.................................................... 61 10.3 Quick Start...................................................... 62 10.4 Run Selection..................................................... 65 10.5 Interruption...................................................... 66 10.6 Embedding Console.................................................. 67 10.7 Conclusion...................................................... 68 Groovy Programming Cookbook v 11 Grails tutorial for beginners 69 11.1 Groovy........................................................ 69 11.2 Grails MVC...................................................... 69 11.2.1 Controller................................................... 70 11.2.2 Domain.................................................... 70 11.2.3 Groovy server pages............................................. 70 11.3 Grails Simple Application.............................................. 70 11.3.1 Controller................................................... 71 11.3.2 Model..................................................... 72 11.3.3 View..................................................... 72 11.3.4 Run the web application........................................... 73 11.3.4.1 Index page............................................. 74 11.3.4.2 User page............................................. 75 11.4 Download the source code.............................................. 75 Groovy Programming Cookbook vi Copyright(c) Exelixis MediaP.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced intoa retrieval system, or transmitted, in any form or by any means(electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of the copyright owner. Groovy Programming Cookbook vii Preface Apache Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform, is dynamically compiled to Java Virtual Machine (JVM) bytecode, and interoperates with other Java code and libraries. Groovy uses a Java-like curly-bracket syntax. Most Java code is also syntactically valid Groovy, although semantics may be different. Groovy 1.0 was released on January 2, 2007, and Groovy 2.0 in July, 2012. Since version 2, Groovy can also be compiled statically, offering type inference and performance very close to that of Java. Groovy 2.4 was the last major release under Pivotal Software’s sponsorship which ended in March 2015. Groovy has since changed its governance structure