Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Thursday, February 21, 2013

Groovy - Validate Email and Date

1. Validate email
Code to validate email in groovy


def isEmail(email) {
        def instantEmailValidate = EmailValidator.getInstance()
        return instantEmailValidate.isValid(email)
    }

2. Validate date
Code to validate date in groovy


def isDate(date){
        if (date != ''){
            def instantDateValidate = DateValidator.getInstance()
            return instantDateValidate.isValid(date, "yyyy-MM-dd", true)
        }else{
            return false
        }
    }

Good luck to you

Thursday, December 6, 2012

Unit Test in Grails

To test Unit Test we need 2 steps:
- Create new Object in setUp function.
- Create some function tests.

Step 1:
In this example, I create class from abstract class. With class Feed is abstract class.


void setUp() {
        feed = [jsonForRelatedEntity: { -> 
                [
                    id: 1,
                    title: "Feed Title", 
                    date: new Date('2010/10/11')                 
                ]
            }] as Feed
    }

Step 2:
Create some function test.


/**
    * Test function json
    * @author tien.nguyen
    */
    void testJSON() {
        def json = feed.json()
        
        assertEquals "Feed Title",  json.title
        assertEquals new Date('2010/10/11'), json.date        
        
        println json
    }

Good luck to you.

Monday, November 12, 2012

Lesion 1: Introduction about Groovy on Grails

What is Grails ?
The same Ruby on Rails. Grails is web server to run application by groovy. You do not choose web server to run application Grails. Default will choose Jetty Serverlet.
Grails auto use database HSQLDB this is database rude java.

What is Groovy?
Groovy is programming language was inherit from java. It don't replace Java that it is support java. It help some java developer short their coding.

Install Grails?
Download Grails from http://grails.org/Download.
- Extract file that you download
- Set Environment variable JAVA_HOME and GRAILS_HOME: Please go to /etc/environment add JAVA_HOME and GRAILS_HOME (is path go to folder content java and grails that you extract). Remember set PATH for JAVA_HOME and GRAILS_HOME go to bin folder. Continue run "source   /etc/environment" to update new environment variable.
(Please see introduction in link from Grails: http://grails.org/Installation)

Create new application
Create new folder and use command line
grails create-app {project-name}
Grails will auto generate structure project.

Run application
Please go to folder your project. run command line grails run-app after open browser with domain http://localhost:8080/{project-name}

Integrate Groovy in Netbean

Active Groovy
Go to plugin install Groovy and Active Groovy. (Tool -> Plugin)

Configuration Groovy in Netbean
Open options Tools -> Options. Choose tab Miscellaneous -> choose Groovy. set path go grails that you download
Example: /home/tiennguyen/Softwares/Webserver/grails-2.1.1