0% found this document useful (0 votes)
103 views24 pages

Groovy Script

Uploaded by

tsreddy.sr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views24 pages

Groovy Script

Uploaded by

tsreddy.sr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

GROOVY SCRIPT

What is Groovy Features and Overview: :


Groovy is nothing but programmable language that can be compatible with java

If you want to work on groovy then you must have java in your system

The Apache Groovy programming language (groovy-lang.org)

DYNAMIC PROGRAMMING LANGUAGE : In dynamic programming language we can check for errors
only at runtime

Static programming : these are known and checked for correctness before running your programme
like java compile error etc

IDE and TEXT Editors for Groovy :


For doing programming in groovy we need a platform where we can write the groovy
code ,and run it and check the result so for that we need either IDE or Text Editors

IDE—Integrated Development Environment :


This is one the platform where we can write , run,compile and check the result for groovy
script and it is a complete platform

Text Editors also used for groovy but has less features

There are some IDE s which we will discus

1 . Groovy Eclipse : -

Step 1: how to install groovy on Eclipse :

Install Eclipse
Using Groovy script we can

Set and get body

Set and get properties

Set and get headers

Libraries = groovy packages = groovy classes

The purpose of get body method is read message body

Get body =read message body

Set body=create/update message body

Get header=read msg headers,get(“oldheader”)

Set header=create/update header message

Get properties = read message properties, get(“oldheader”)


Variables ::

Variables are used to store information, this information is of can be different types
examples String, int, Boolean

To define the variables we use a keyword “ def “

When we use single quotation ‘ , it will take exactly same what we can write

When we use double Quotes “ it will take name whatever we defined

You can use Letters, Digits , Underscore in your variable name

Groovy is case sensitive

If you want to print in separate lines then use println


Groovy is dynamically typed that means the type of the variables is resolved at run time i.e.
it will identify errors at run time

Data types : There are different types of data types are there Byte, Shorty,*Int,
loag,float , char etc

In above screen you can see the printing of min and max values of Byte and Short

Char is used to store a single character

Operators ::
Operators in are symbols that tell the compiler to perform some specific operations

There are different types of operators are there

1 .arithmetic operators

2. unary operators

3. increment and decrement operators


In groovy we have control structure is there

1. Conditional statements
2. Looping
3. Exception handling

Conditional statements ::
These are also called as Decision making statements

There are two types of conditional statements

1 . if –else

2 . Switch- case

1 . if – else :

We will use curly braces for if and else seperately


For nested if cases we will use switch case

You have to define the case in curly braces only otherwise it will give an error

Loops ::
There are three types of loops are there

1 . for

2 . for in

3 . while
And three keywords we use in these loops

1 upto 2 times 3 step

1 . for :

We will use any number of conditions in for loop , for every condition it will chwck and give
the output
Here in above screen it will check i=1 then check if it it is less than 5 or not if 2 nd condition
satisfies then it will go to 3rd condition and increments the value of i and check the next
condition , this loop will continue upto condition lapses

For other condition and keywords the syntax will be like below

In UPTO it will take from to

In TIMES it will take from to

In STEP (10 ,2) means that it will be incremented every 2 number upto 10 i.e. 1,3,5,9
For WHILE loop the syntax should be like below

Exception handling ::
In exception handling we have three methods

1 Try-catch
2 Try-catch-finally
3 Try-finally

.
Strings ::

In single quoted string we can not refer a variable , this property is called intertpolation
Whwenever you want to use multiple lines then we will use triple multicoated ones i.e triple
double qupoted or triple single quoted

We can also do some other functions

Name .length() function to count the length of the string

If we want to print the position of the index character then we can use the number in square
brackets i.e. for ex. [2]. If you ewant to count from the reverse i.e. from right to left then we
will use [-2] and also we can use .IndexOf(char) to findout the position of that character
We will use println name[0..2] then it will print the latters from 0 to 2 i.e Rag in above name

We can also print from the right and also from substring
Methods ::

Method is named as block of code

Makes code modular and reusable

It can take parameters and can have a return type


Closures ::

It is a block of code that can take parameters, refer variables, return values and can be
passed as a parameter in a method

First we define the closure then you have to call it like below i.e.your closure name.call

Lists : :
List is a structure to store collection of data types
It is an ordered collection of data

Syntax : [ Obj1 , Obj2 , Obj3 ……], this Obj is of any data type , it can be int or string or char or
floating type there can be nested list also

There are different functions we will use in list

See examples below screens


Maps ::
Maps is key value pair and it is unordered collection of data

Maps can be maintained in [] We can define simple map as [:]

We can see different types of functions below screens and we caln also clone the entire map
Ranges ::

Ranges is used to creates a list of sequential values

And it is denoted by first and last values of the sequence

Example 1…10 , a…z . 10…1 and we can use assert whether the statement is
correct or not if it is correct it will not throw any exception , if it is incorrect then
it will throw an exception
Ranges are of instanceof java.util.List

Input and output ::

For this error copy the path and goto cmd and give that path

How to read files as string, list, array etc:

To read files as String we will use “.text “


To read the files as lists we will use .collect{it}

You might also like