Tuesday, 3 April 2012


Rails 3 + Rcov Test Coverage
For the first time in a long time I added a new category to the blog. The new category is "Ruby/Rails Test Coverage" .
Writing tests is simple, but I wanted to make sure I had good coverage. This led to me to rcov which does a good job of running my tests and showing me a nice HTML output of the coverage. It was still a pain to go and run rcov and hunt down the report .
So I did some searching and ran across a nice rake plugin called rails rcov that does it all for me. Now I can run rake test:test:rcov from TextMate and see all my tests run and view my coverage.
I created the following rake file in lib/tasks/coverage.rake:
namespace :coverage do
 
  task :clean do
    rm_f "test/coverage"
    rm_f "test/coverage.data"
    Rcov = "cd test && rcov --rails --aggregate coverage.data -Ilib \
                   --text-summary -x 'bundler/*,gems/*'"
  end
 
  def display_coverage
    system("sensible-browser test/coverage/index.html")
  end
 
  desc 'Measures unit test coverage'
  task :unit => :clean do
    system("#{Rcov} --html unit/*_test.rb")
    display_coverage
  end
 
  desc 'Measures functional test coverage'
  task :func => :clean do
    system("#{Rcov} --html functional/*_test.rb")
    display_coverage
  end
 
  desc 'All unit test coverage'
  task :all => :clean do
    system("#{Rcov} --html */*_test.rb")
    display_coverage
  end
 
end
 
task :coverage do
  Rake::Task["coverage:all"].invoke
end
This creates the following rake tasks you might be interested in:
coverage:unit – runs coverage on tests in test/unit
coverage:func – runs coverage on tests in test/functional
coverage:all – runs all unit tests
coverage – synonym for coverage:all
Note that the */*_test.rb syntax may not work depending on your shell.
Of course, you need rcov, so I added the following to my Gemfile:
gem 'rcov'
Do a bundle install, and you should be able to run rake coverage. After running coverage, this script will automatically open your web browser to look at your coverage; It should look something like this, hopefully with more green bars than my just-getting-started project:

Happy Coding.

Tuesday, 7 February 2012


Roodi [ Ruby Object Oriented Design Inferometer ]

Roodi stands for Ruby Object Oriented Design Inferometer. It parses your Ruby code and warns you about design issues you have based on the checks that is has configured.

Very easy to install and use with your code.

# gem install roodi

We can check one or more files using the default configuration that comes with roodi.

Check all ruby files in a rails app:
    roodi "rails_app/**/*.rb"

Check one controller and one model file in a rails app:
    roodi app/controller/sample_controller.rb app/models/sample.rb

Check one controller and all model files in a rails app:
    roodi app/controller/sample_controller.rb "app/models/*.rb"

Check all ruby files in a rails app with a custom configuration file:
    roodi -config=my_roodi_config.yml "rails_app/**/*.rb"

CUSTOM CONFIGURATION

To change the set of checks included, or to change the default values of the checks, you can provide your own config file. The config file is a YAML file that lists the checks to be included. Each check can optionally include a hash of options that are passed to the check to configure it. For example, the default config file looks like this:

    AssignmentInConditionalCheck:    { }
    CaseMissingElseCheck:            { }
    ClassLineCountCheck:             { line_count: 300 }
    ClassNameCheck:                  { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
    CyclomaticComplexityBlockCheck:  { complexity: 4 }
    CyclomaticComplexityMethodCheck: { complexity: 8 }
    EmptyRescueBodyCheck:            { }
    ForLoopCheck:                    { }
    MethodLineCountCheck:            { line_count: 20 }
    MethodNameCheck:                 { pattern: !ruby/regexp /^[_a-z<>=\[\]|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/ }
    ModuleLineCountCheck:            { line_count: 300 }
    ModuleNameCheck:                 { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
    ParameterNumberCheck:            { parameter_count: 5 }

PostgresSQL Commands for CentOS/Linux & pg_hba.conf configuration

Most of the time we can not install or use pgadmin. I tend to forget simple commands for administering the database. Hopefully this blog will help as reference when working with PostgreSQL

  • Login as "postgres" to start using database:
    # su - postgres
    
  • Create a new database:
    $ createdb mydb
    
  • Drop database:
    $ dropdb mydb
    
  • Access database:
    $ psql mydb
    
  • Get help:
    mydb=# \h
    
  • Quit:
    mydb=# \q
    
  • Read command from file:
    mydb=# \i input.sql
    
  • To dump a database:
    $ pg_dump mydb > db.out
    
  • To reload the database:
    $ psql -d database -f db.out
    
  • Dump all database:
    # su - postgres
    # pg_dumpall > /var/lib/pgsql/backups/dumpall.sql
    
  • Restore database:
    # su - postgres
    # psql -f /var/lib/pgsql/backups/dumpall.sql mydb
    
  • Show databases:
    #psql -l
    or
    mydb=# \l;
    
  • Show users:
    mydb=# SELECT * FROM "pg_user";
    
  • Show tables:
    mydb=# SELECT * FROM "pg_tables";
    
  • Set password:
    mydb=# UPDATE pg_shadow SET passwd = 'new_password' where usename = 'username';
    
  • Clean all databases (Should be done via a daily cron):
    $ vacuumdb --quiet --all

  • The pg_hba.conf file states who is allowed to connect to the database server and which authentication method must be used to establish the connection.This configuration is very useful when we try to connect database through pgadmin client.

    Default pg_hba.conf
    # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
    
    # "local" is for Unix domain socket connections only
    local   all         all                               trust
    # IPv4 local connections:
    host    all         all         127.0.0.1/32          trust
    # IPv6 local connections:
    host    all         all         ::1/128               trust

    Wednesday, 30 November 2011



    When one of my colleague asked me What is 'Java Generations'?

    I don’t know how many Java techies know about Java Generations. We never think about memory management and garbage collection.
    This article is very useful who really face these question:
               1)     How heap and non-heap works in Java?
               2)    What are the Generations in Java? 
               3)    How Garbage collection works with Objects?






    At initialization, a maximum address space is virtually reserved but not allocated to physical memory unless it is needed.
    The complete address space reserved for object memory can be divided into the young and tenured generations.
    The young generation consists of eden plus two survivor spaces. Objects are initially allocated in eden. One survivor space is empty at any time, and serves as a destination of the next, copying collection of any live objects in eden and the other survivor space. Objects are copied between survivor spaces in this way until they are old enough to be tenured, or copied to the tenured generation.
    Other virtual machines, including the production virtual machine for the J2SE Platform version 1.2 for the Solaris Operating System, used two equally sized spaces for copying rather than one large eden plus two small spaces. This means the options for sizing the young generation are not directly comparable.
    A third generation closely related to the tenured generation is the permanent generation. The permanent generation is special because it holds data needed by the virtual machine to describe objects that do not have equivalence at the Java language level. For example objects describing classes and methods are stored in the permanent generation.

    Summary:
    • Eden Space (heap): The pool from which memory is initially allocated for most objects.
    • Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.
    • Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.
    • Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
    • Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

    Source:
    http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

    Tuesday, 29 November 2011

    How to Change Default Author Name for JavaDocs in Eclipse?


    In Eclipse we always try to generate the Java docs at the class level or method level. But when we generate the Java docs it picks the user name from the system user. This could result in weird author names in your code files as in an organization usernames are usually as per organizational naming conventions.For example my user name that I use to login is ORG001239 something like  and you will agree that it wouldn't look good as an author name in a Java file and might not make any sense to most other viewers of the code.

    /**
     * Test default author in JavaDocs
     * @author ORG001239
     */
    public class TestClass {
    }

    Here is a quick way to change the default author name in your Eclipse projects. Simply edit your eclipse.ini file found in the root directory where you placed Eclipse. I have Eclipse at C:\eclipse, so my path would be C:\eclipse\eclipse.ini. Once editing this file add the following line and save.

    -Duser.name=Saket Saraf

    After saving restart Eclipse and when you do a JavaDoc comment and use the author attribute by typing @author and pressing enter on the autocomplete you will see something like this:


    /**
     * Test default author in JavaDocs
     * @author Saket Saraf
     */

    public class TestClass {
    }


    Source
    http://www.javagyan.com