Thursday, January 13, 2011

Iterate over JAVA HashMap in FreeMarker

Java HashMap can be iterated in FreeMarker by using a <#list> tag as follows:

<#list hashMap?keys as key>
        ${key}=${hashMap[key]}
<#list>


If you are using the above code with default FreeMarker Configuration then there would be some different behavior during iteration. For example if the HashMap is:

HashMap hashMap = new HashMap();
hashMap.put("1","one");
hashMap.put("2","two"); 

By iterating on hashMap with default Configuration it will output following keys in result:

put
remove
entrySet
hashCode
productElement
clear
isEmpty
1
values
empty
underlying
productElements
copy
getClass
get
copy$default$1
equals
productPrefix
class
canEqual
keySet
size
2
containsKey
productArity
containsValue
productIterator
toString
putAll        


This result shows that the iteration is done on all the available hashMap properties.

To get rid of this you have to set the FreeMarker Configuration as follows:


Configuration configuration = new Configuration();
BeansWrapper wrapper = (BeansWrapper) this.configuration.getObjectWrapper();
wrapper.setSimpleMapWrapper(true);
this.configuration.setObjectWrapper(wrapper);

Here the BeanWrapper's simpleMapWrapper property is set as 'true'. After setting this configuration the results over iterating on hashMap will give only the defined key-value pair.

rizzz86

Wednesday, January 12, 2011

MD5 Hashing in J2ME

MD5 (Message Digest) is a hashing algorithm that can be used to ensure data integrity, save and protect passwords/codes, generate unique keys of limited length against a long literal etc. It takes input as String and generates the Hash of that string.

In JAVA there is a built-in MD5 support in its Standard Edition (J2SE). But if we talk about its Micro Edition (J2ME) their is no support for MD5. The package of JAVA Standard Edition can also be used in Micro Edition if and only if the platform supports the SATSA-Crypto Optional Package (How to know that your platform supports SATSA-Crypto).

I have found and tested the Third-Party implementation that can generate MD5 hash and can be used in JAVA Micro Edition. You can get the MD5 source files from here.

rizzz86

Wednesday, December 29, 2010

J2ME Default Emulator without asking for Permission

While running J2ME project with Default Emulator it will ask for Permissions in case of File Read/Write, HTTP Connectivity etc. To get rid of this Permission on every access you have to change the Security options of Emulator. In my case I am using NetBeans IDE, so to change these options I will go through following steps:
  1. Open Project Properties
  2. Click on Manage Emulators Button
  3. Go to Tools & Extension Tab and Click Open Preferences Option
  4. On Preferences Window click on Security option
  5. Select Security Policy to 'JTWI'
  6. Select Security Domain as 'trusted'
  7. Now run the project it will not ask for access Permissions

 rizzz86

Friday, September 10, 2010

Unit Testing for J2ME - JMUnit

In professional development environment it is a common practice to create Unit Tests. On Java platform there are lots of research and development done on Unit Tests for Enterprise Edition. As far as Java Micro Edition is concerned one of the framework available is JMUnit.

Creating and executing a test case using jmunit is quite simple and can be done without wasting much time to explore it. Following steps will guide you how to work with jmunit:

  •  In your mobile application right click on package (where you want to create a unit test) and select option 'New > Empty JMUnit Test'.
  • It will create a unit test class for you that will look like this:
    /*
     * ABCTest.java
     * JMUnit based test
     *
     */
    
    package hello;
    
    
    import jmunit.framework.cldc10.*;
    
    /**
     * @author rizwan
     */
    public class ABCTest extends TestCase {
        
        public ABCTest() {
            //The first parameter of inherited constructor is the number of test cases
            super(0,"ABCTest");
        }            
    
        public void test(int testNumber) throws Throwable {
        }    
    }  

    • Next you will create a test method in same class and call it in test() using switch cases.
    • Total count of test cases will also be mentioned in unit class constructor.
    • Now the code will look like this:

    /*
     * ABCTest.java
     * JMUnit based test
     *
     */
    
    package hello;
    
    
    import jmunit.framework.cldc10.*;
    
    /**
     * @author rizwan
     */
    public class ABCTest extends TestCase {
        
        public ABCTest() {
            //The first parameter of inherited constructor is the number of test cases
            super(1,"ABCTest");
        }            
    
        public void test(int testNumber) throws Throwable {
            switch(testNumber){
                case 0: 
                    testOne();
                    break;
            }
        }    
    
        private void testOne() throws Exception{
            String tmp = "Hello Rizwan";
            assertEquals(tmp, "Hello Rizwan");
        }
    } 

    • After that you have to mention 'ABCTest' as your Midlet.
    • Go into 'project properties > Application Descriptor > MIDlets' and define unit test name and package here.
    • Run application and you will also get Test option on screen:  
    • Test the project and it will show you the results




    Your mobile test case is ready !

    rizzz86

    Saturday, September 4, 2010

    Open Transport Tycoon Deluxe (TTD)

    TTD is one of my most favorite games that I have played and when I saw the 'Open' with it, I was really excited to install it on my Ubuntu OS.

    The installation of Open TTD is difficult and you need to be a bit keera of shell scripting to do this job. I have followed all the steps on this link and managed to install OTTD successfully. After completing installation and launching the game I was really amazed by watching the same TTD main screen as what I was playing on my 'Windows OS' four years back.


    The game is upgraded now and contains four different types of maps with each map containing different industries, disasters, vehicles etc. Maps are also really interesting with environment like a Desert area map, Snow area map, Toyland area map and a default one as well. I have some getty images of the game that are as follows:


    All TTD lovers do enjoy this on your Linux OS.

    rizzz86

    Friday, September 3, 2010

    Amir, Asif & Salman - From Hero to Zero


    Pakistani cricketers are again under a controversy and this time it is a serious one - the FIXING case :|

    The trio who are Hero of last week are now Zero. In my last blog I am really excited of Pakistan's fighting win over England and now I am not much interested to even watch a cricket match.

    Is it a non-professionalism or a trap or anything else - the trio-players has taken part in this show of money. The future of these players is still not clear and are at stake. But as a Pakistani I am still hoping these players will not have a life ban and will play cricket after completing their punishments.

    rizzz86

    Sunday, August 22, 2010

    Pakistan beat England after 9 Years


    A victory dedicated to the flood victims of Pakistan.

    Pakistan beat England in England after 9 years in a Test Match is another huge achievement by a rookie team within a month. Previously they beat Australia after 15  years in a Test Match.

    A good team work by Pakistani bowlers as well as batsmen has done the work for Pakistan. Taking a first inning lead and excellent comeback by Paki bowlers in the 2nd innings has provided them a victory. There are lots of moments and achievements in a match that really boost the team moral. Some of them includes 1) Wahab Riaz's superb debut 5 wicket hall, 2) M. Yousuf solid comeback in a International side, 3) M. Amir becomes the youngest bowler to take 5 wicket hall in England in a Test Match and become a Man of the Match, 4) Saeed Ajmal's Spin magic, 5) Azhar Ali's unbeaten rock knock of 92* in the first innings and 6) Kamran Akmal's shocking 8 catches to reach his 200 dismissals in a Test Match.

    Also a big relief for a new captain Salman Butt to still remain in the series. We all are hoping to win the Lord's Test to level the series.


    Yousuf is a class act.

    rizzz86