Writing A Standalone Java Test Client for Entity Object

  1. In the Package Explorer View make sure the Calculator project is selected.

  2. Create a new Java class. Open menu File > New > Other... and choose Java > Class.

  3. The package will be by.iba.client and the class name PersistentCalculatorClient.

    Leave the default options selected and be sure that public static void main(...) method is checked.

    Click the Finish button.

    PersistentCalculatorClient class

  4. Add the following code to the public static void main method body:

    try {
    	Context jndiContext = new InitialContext();
    	Object ref = jndiContext.lookup("PersistentCalculatorBean/remote");
    	PersistentCalculator calc = (PersistentCalculator) PortableRemoteObject
    		.narrow(ref, PersistentCalculator.class);
    
    	System.out.println("4 + 3 = " + calc.add(4, 3));
    	System.out.println("4 - 3 = " + calc.subtract(4, 3));
    	System.out.println("4 * 3 = " + calc.multiply(4, 3));
    	System.out.println("4 / 3 = " + calc.divide(4, 3));
    	System.out.println("6 * 3 = " + calc.multiply(6, 3));
    	System.out.println("7 / 3 = " + calc.divide(7, 3));
    			
    	System.out.println("   History before cleanup:");
    	System.out.println(calc.getHistory());
    	
    	calc.clearHistory();
    	
    	System.out.println("   History after cleanup:");
    	System.out.println(calc.getHistory());
    			
    } catch (NamingException ne) {
    	ne.printStackTrace();
    }
    								

    NOTE: To resolve imports problems press Shift + Ctrl + O. Press Ctrl + S to save the Java class.

  5. Run the Java class by selecting menu Run > Run As > Java Application.

    NOTE: Make sure the PersistentCalculatorClient.java is selected in Package Explorer View.

    You should see the following result in the Java client's Console View:

    4 + 3 = 7.0
    4 - 3 = 1.0
    4 * 3 = 12.0
    4 / 3 = 1.3333333333333333
    6 * 3 = 18.0
    7 / 3 = 2.3333333333333335
       History before cleanup:
    [ 
    2006-11-15 13:32:52.159 : 7.0 divide 3.0 is 2.3333333333333335,  
    2006-11-15 13:32:52.143 : 6.0 multiply 3.0 is 18.0,  
    2006-11-15 13:32:52.143 : 4.0 divide 3.0 is 1.3333333333333333,  
    2006-11-15 13:32:52.127 : 4.0 multiply 3.0 is 12.0,  
    2006-11-15 13:32:52.112 : 4.0 subtract 3.0 is 1.0]
       History after cleanup:
    []
    								

  6. OPTIONALLY: You can see the DB table created by JBoss for the Entity Object.

    1. Comment out the history cleanup code line in the client:

      System.out.println("   History before cleanup:");
      System.out.println(calc.getHistory());
      
      // calc.clearHistory();
      ...
      											

    2. Re-run PersistentCalculatorClient client (to populate the table).

    3. Open a browser, go to JMX Console http://localhost:8080/jmx-console:

      JMX Console

    4. Go to Hypersonic SQL service and click on startDatabaseManager()

      Hypersonic SQL service

    5. The HSQL Database Manager will come up, displaying the database table OPERATION.

      OPERATION

    6. If you execute an SQL query on the table, the data from the OPERATION table will be displayed as shown below:

      SELECT * FROM PUBLIC.OPERATION ORDER BY TIMESTAMP ASC
      											

      SELECT query

Congratulations ! You have succesfully tested EJB 3.0 application with Entity Object !!!

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine