Wednesday, June 27, 2012

Java - Task interval scheduling

In some application some tasks need to be run periodically for example GUI screen should update the data from server periodically.
This java tips illustrate a method of scheduling a task periodically. Developers may use it for repetitive invoking of a method as per their requirements.
For this aim, first you have to make a class extending TimerTask abstract class and write code in run method, you want to run repetitively.
import java.util.TimerTask;

public class HeartBeatTask extends TimerTask{

  private int timerInterval;

  public HeartBeatTask(int timeInterval){
    this.timerInterval=timeInterval;
  }

  public void run() {
    // add the task here
  }

}
In your main program you can call this code to schedule task:
  java.util.Timer t1 = new java.util.Timer();

  HeartBeatTask tt = new HeartBeatTask(timeInterval);
  t1.schedule(tt, 0, timeInterval);
Now the task will repeat after the fixed time interval.

Thursday, June 21, 2012

VMWare Web Service FAQs

http://www.vmware.com/support/developer/vc-sdk/WS_FAQs.html
What is the VMware VirtualCenter Web service?
The VMware VirtualCenter Web service is a programming interface that exposes the functionality of VMware VirtualCenter to customer-written or third party applications. How do I get the VirtualCenter Web service?
The VirtualCenter Web service is part of VirtualCenter. It is distributed and installed along with the rest of VirtualCenter and there is no additional cost or licensing required. The VirtualCenter Web service is not available prior to VirtualCenter 1.1. How do I use the VirtualCenter Web service?
A number of vendors are integrating their products with VirtualCenter through the VirtualCenter Web service interface. To use the VirtualCenter Web service with such a product, simply configure the application with the host name and port of the Web service. For documentation about writing your own programs that access the Web service, refer to the VMware SDK package available at http://www.vmware.com/support/developer/vc-sdk. This package also contains sample code in Java, C#, Visual Basic, and Perl. Can I manage both ESX Server and GSX Server using the VirtualCenter Web service?
Yes. Beginning with the VirtualCenter 1.2 release, the Web service interface supports the management of GSX Server hosts, as well as ESX Server hosts. Is the VirtualCenter Web service secure?
Yes. The VirtualCenter Web service relies on the same authentication and authorization scheme as the rest of VirtualCenter. In addition, traffic to and from the Web service is encrypted with SSL. Do I have to install the VirtualCenter Web service?
If you prefer not to install the VirtualCenter Web service, you can use the Custom option when installing VirtualCenter. How do I check that the VirtualCenter Web service is installed properly?
Using a Web browser, open the document "Documents And Settings\All Users\Application Data\VMware\VMware VirtualCenter\VMA\login\login.html." Fill in the three fields: user name, password, and the full Web service URL; for example, https://localhost:8443. If an XML document appears in your browser, the Web service is running and responding to queries. Can I use a non-SSL port with the VirtualCenter Web service?
Yes. For example, if you want to use the non-SSL port 8080, add the following entry to the vmaConfig.xml file, located in C:/Documents And Settings/All Users/Application Data/VMware/VMware VirtualCenter/VMA.

  1. Open vmaConfig.xml and look for the <service> element.
  2. Add the 8080 port number.
    <port>8080</port>
  3. Restart the Web service for any changes to vmaConfig.xml to take effect.
Can I install the Web service on a different box than the rest of VirtualCenter?
The VirtualCenter server and the VirtualCenter Web service should reside on the same machine. This is the only supported configuration. How can I access the WSDL file programatically?
Access the vim.wsdl file by placing ?wsdl after the Web service URL; for example, https://localhost:8443/sdk/vim.wsdl?wsdl.

Monday, June 18, 2012

Javadoc

Introduction

  • Javadoc is a tool that generates html documentation (similar to the reference pages at java.sun.com) from Javadoc comments in the code.  In this tutorial we will go over how to write basic Javadoc comments and how to use features of Javadoc to generate html documentation.

Javadoc Comments

  • Javadoc recognizes special comments  /** .... */ which are highlighted blue by default in Eclipse (regular comments // and /* ... */ are highlighted green).
  • Javadoc allows you to attach descriptions to classes, constructors, fields, interfaces and methods in the generated html documentation by placing Javadoc comments directly before their declaration statements.  
  • Here's an example using Javadoc comments to describe a class, a field and a constructor: 
Javadoc Tags
  • Tags are keywords recognized by Javadoc which define the type of information that follows.
  • Tags come after the description (separated by a new line).
  • Here are some common pre-defined tags:
    • @author [author name] - identifies author(s) of a class or interface.
    • @version [version] - version info of a class or interface.
    • @param [argument name] [argument description] - describes an argument of method or constructor.
    • @return [description of return] - describes data returned by method (unnecessary for constructors and void methods).
    • @exception [exception thrown] [exception description] - describes exception thrown by method.
    • @throws [exception thrown] [exception description] - same as @exception.
       
       
       
      /**
       * Java class example
       * The class illustrates how to write comments used
       * to generate JavaDoc documentation
       *
       * @author Catalin
       * @version 2.00, 23 Dec 2010
       */
      public class MyClass {
          /**
           *
           * Simple method.
           *
           * The method prints a received message on the Console
           *
           * @param message String variable to be printed
           * @see MyClass
           * @deprecated
           * @since version 1.00
           */
          public void MyMethod(String message)
          {
              System.out.printf(message);
          }
       
              /**
           *
           * Simple method example.
           * The method prints a received message on the Console
           *
           * @param message String variable to be printed
           * @since version 1.00
           */
          public void printMessage(String message)
          {
              System.out.printf(message);
          }
           /**
           *
           * Simple method example.
           *
           * The methods adds 2 numbers and return the result
           *
           * @param val1 the first value
           * @param val2 the second value
           * @return sum between val1 and val2
           * @since version 2.00
           */
          public int add(int val1, int val2)
          {
             return val1+val2;
          }
      }
       
       

      How to generate JavaDoc in Eclipse Helios

      1. Open the Eclipse project
      2. Select Project –> Generate JavaDoc
      Generate JavaDoc in Eclipse
      Generate JavaDoc in Eclipse
      3. At first step of the wizard, you can define settings for:
      3.1 path for the javadoc.exe tool from the JDK;
      3.2 project resources for which to generate JavaDoc;
      3.3 classes and methods for which to generate JavaDoc based on their visibility;
      3.4 location of the JavaDoc (by default it will be placed in the doc folder in the project location)
      Generate JavaDoc in Eclipse - Step 1
      Generate JavaDoc in Eclipse - Step 1
      4. At second step you can make settings regarding:
      4.1 documentation structure;
      4.2 JavaDoc tags to be processed;
      4.3 other resources(archives, projects) used in project to be included in the documentation;
      4.4 another CSS style sheet for the documentation;
      Generate JavaDoc in Eclipse - Step 2
      Generate JavaDoc in Eclipse - Step 2
      5. At the last step you can save the settings in an Ant script for future use.
      Generate JavaDoc in Eclipse - Step 3
      Generate JavaDoc in Eclipse - Step 3
       

Wednesday, June 13, 2012

swixml

http://www.swixml.org/index.html

, I tried, but could not understand its logic for JTree or JTable

Monday, June 11, 2012

Dynamically create Swing GUI from generic XML

http://stackoverflow.com/questions/8503661/dynamically-create-swing-gui-from-generic-xml

I think I know what you're asking for...
How about using SwiXML plus Java's inbuilt XML APIs.
SwiXML would solve the Swing form generation, and Java's built-in XML APIs could bridge the gap between this and your 'generic xml format'. The Java DOM API would provide everything you need for transforming and generating XML.
Overview:
  1. You could transform your 'generic xml' document into SwiXML's XML format, using one of Java's XML APIs such as DOM or SAX (or even an XSLT transformation).
  2. Then you could invoke SwiXML on the resulting SwiXML markup.
  3. Finally, you'd get your Submit button's ActionListener to generate the 'generic output XML' using e.g. Java's DOM API.
Hope this helps!

Thursday, June 7, 2012

http://theskyisthelimit.se/bunker-roy-learning-from-a-barefoot-movement/


Adding fields dynamically in ASP.NET

http://howto.jamescarnley.com/2008/05/adding-fields-dynamically-in-aspnet.html

Adding a control to a page dynamically is a very powerful tool when you want to allow users to add varying amounts of data to their submission. These controls should keep their state through multiple postbacks.

I recently had to solve this problem to allow professors to upload any number of files to our library server using a web form. It seemed simple enough to solve, but as I searched around the net, it seemed many people were having a lot of trouble with this topic or were using some pretty hackish things to get it working. I hope this post helps someone out there.

Using Session

The main problem of adding new controls to a page is that the new controls will disappear whenever there is a new postback. To solve this problem we simply store the control in the users Session which allows them to keep their data as long as their session stays alive.

We can store the controls in a List to keep track of them. When the page is first created, we store the list inside of the Session, then when the page is loaded, we get the list from the Session. Getting the list from the session when the page is loaded guarantees that we have the list of controls for the current user.

Adding Controls Back to Page After Postback

Whenever a postback occurs, all of the controls on a page are recreated. Since a control spawned within the page cycle is not bound to anything, it is simply destroyed.

In order for the dynamically added controls to show up, they must be added at the beginning of each page in the Page_Load method. In this example, we simply iterate through the list and add the control to a placeholder.

Adding a New Control

Whenever a new control is added inside of a button click method, it will not show up on the page immediately. What happens is that it is actually created and added to the array, but the page cycle is already past the Page_Load method, so there is no chance to add the control to the page from the array. To counter this, we simply have to add the control to the page manually inside of the method. In the next postback, the control will be added automatically like the others.

Example Files

Unfortunately, I cannot host files on Blogger so I will copy the contents of the files directly to this post. These files were tested with ASP.Net 3.5 and Visual Studio 2008, but they are simple enough to work on pretty much any platform.

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Adding Dynamic Controls to a Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
</div>
<p>
    <asp:Button ID="addControl" runat="server" Text="Button"
        onclick="addControl_Click" />
</p>
</form>
</body>
</html>

Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
 List<Control> controls = new List<Control>();
 protected void Page_Load(object sender, EventArgs e)
 {
     if (Page.IsPostBack == false)
         Session["controls"] = controls;
     else
         controls = (List<Control>)Session["controls"];

     /* The controls must be added back to the page each time there
         * is a postback, otherwise the controls disappear.*/
     foreach (Control control in controls)
         myPlaceholder.Controls.Add(control);
 }
 protected void addControl_Click(object sender, EventArgs e)
 {
     TextBox box = new TextBox();
     box.ID = "box" + controls.Count;
     controls.Add(box);

     /* We add the box manually first since the Page_Load 
           method has already finished. The box will only show up
           after the next postback otherwise.*/
     myPlaceholder.Controls.Add(box);
 }
}

To access html control without runat=”server” in C# code.

http://praveenbattula.blogspot.com/2009/04/to-access-html-control-without.html

In ASP.NET coding, I don't think it is needed to create or declare only ASP.NET server-side controls. Some cases, to make our page more efficient and faster we can write HTML controls by adding runat="server" to access them on server side code [C#].

But, there are some special requirements where we need to create HTML controls dynamically in c# and add them in a string and write the string to a page. And whenever some event raised like button click event, on server side code, we need to retrieve the values of those HTML controls. As it is not declared as runat="server" on the page, we can't take the values very simple. In that type of scenarios, this solution works. Please follow the solution below to get the values of the HTML controls which doesn't have runat="server" attribute defined.

Example:
HTML declaration:
<input type="text" name="txtName" />

C# Code:
string strValue = Page.Request.Form["name of the control"].ToString();

Note:
To get the values in server side code of HTML control, we need to follow below points.
  • The tag should have an attribute called NAME. Because it is used as key in form[].
  • The form method should be of type POST.
That's it!!! Please let me know, if you have any issues with this approach. Hope this helps...

Call a Web service from Javascript utilizing SOAP without any extra libraries.

http://www.hendricksongroup.com/code_call_web_service_javascript.aspx
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr

 

 Call a Web service from Javascript utilizing SOAP without any extra libraries.




Environment: ASP .NET C#, and VB

What is all the buzz about AJAX?  It's all about calling a web service from your client web site without posting your web page to the host.  Well AJAX is more than that but in this example we'll consume a web service using a Javascript client.  What is AJAX? Well it's really a group of interrelated techniques used to send and receive data asynchronously from the host, keeping your browser responsive while gathering small amounts of data without refreshing the entire web page.   You call the web service using the XMLHttpRequest object built into your browser's DOM (IE 7+). Most newer browsers support the DOM object, but in the case of IE5 and IE6 we will use the ActiveXObject  Microsoft.XMLHTTP . Calling a web service asynchronously from Javascript can significantly reduce the round trip time required to get a small amount of data from the host in using background processing.

If you are familiar with .NET web services you understand how efficient these calls to the host are, no html web page, just the data you want returned in (often) XML format. ASP .NET allows you to build powerful data driven applications...but all that reposting to the host costs a lot of bandwidth.  The XMLHttpRequest object has been around since 2000 and is found on virtually every box running a Microsoft Windows OS in use today, in addition to most modern browsers. In this example we will call an ASP .NET web service written in C# using a SOAP (Simple Object Access Protocol) envelope from a Javascript client. Download the Javascript code (.zip file) for this example.

The code that allows you to call a web service from javascript:

First the Javacript code to encapsulate the XMLHttpRequest object. This is a Javascript object with which we will create a new instance and and make the asynchronous call to the host.

function WebSvc()   // Class Signature
{
    
    // Encapsulates the elements of a XMLHttpRequest to an ASP .Net Web Service
    
    
    WebSvc.prototype.CallWebService = function(url, soapXml, callback)
    {
        
        // Calls web service with XMLHttpRequest object. Utilizes a SOAP envelope for
        // transport to and from Server. This is an asynchronous call.
        // PARAM url - Fully qualified url to web service .asmx file
        // PARAM sopXml - String containing SOAP envelope with request
        // PARAM callback - Supplied callback with signature callback(result,data) when call returns.
        

        var xmlDoc = null;

        if (window.XMLHttpRequest)
        {
            xmlDoc = new XMLHttpRequest(); //Newer browsers
        }
        else if (window.ActiveXObject) //IE 5, 6
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (xmlDoc)
        {
            //callback for readystate when it returns
            var self = this;
            xmlDoc.onreadystatechange = function() { self.StateChange(xmlDoc, callback); };

            //set up the soap xml web service call
            xmlDoc.open("POST", url, true);
            xmlDoc.setRequestHeader("Content-Type", "text/xml");
            xmlDoc.setRequestHeader("Content-Length", soapXml.length);
            xmlDoc.send(soapXml);
        }
        else
        {
            if (callback)
            {
                callback(false, "unable to create XMLHttpRequest object");
            }
        }
    };

    WebSvc.prototype.StateChange = function(xmlDoc, callback)
    {
        // Callback supplied for XMLHttpRequest Object to monitor state and retrieve data when returns.
        // PARAM xmlDoc - XMLHttpRequest Object we're watching
        // PARAM callback - Callback function for returning data, signature CallBack(result,data)
        
        if (xmlDoc.readyState === 4)
        {
            var text = "";

            if (xmlDoc.status === 200)
            {
                text = xmlDoc.responseText;
            }

            // Perform callback with data if callback function signature was provided, 
            if (callback !== null)
            {
                callback(xmlDoc.status === 200, text);
            }
        }
    };
}
    


Next create a simple web service that we will be calling.  In this case it's a simple method that takes a string as input and returns an XML document with a <date_time> element containing the string we passed as input and the date and time.  Obviously you would want to return a status element that allowed you to determine whether the call was successful, but in this case there's not much that can go wrong.  An easy way to return an XML document from a web service is to return an XmlNode which is easily obtained from the XmlDocument.DocumentElement;

This is the C# ASP .NET web service we will call with from our web page Javascript. The method requires a string as input.
    [WebMethod]
    public XmlNode GetTime(string input) 
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<envelope><date_time>" + input + "  " + DateTime.Now.ToLongDateString() + 
                    DateTime.Now.ToLongTimeString() + "</date_time></envelope>");
        
        return doc.DocumentElement;
    }                
    


When we call the web service we will be using a SOAP envelope to communicate our request.  This simple javascript function creates the SOAP envelope and includes the input parameter that the C# Web Service will be expecting.

    


We supply a callback function so we can execute the request asyncronously from our web page.  This allows the web page to continue to be responsive while the client to host request is made.  When the call to the host completes the callback function will display the result.  If we don't want the callback function we supply a null as a second parameter to the call. In the following case a string parameter is passed into the web service. In this example we are using the XMLHttpRequest.responseText value and will receive the result in the form of plain text, and therefore will parse out the <date_time> tag ourselves to get the result returned by the server. The XMLHttpRequest object can also return an XML document but that is beyond the scope of this article.
    
    // This is the callback function that displays the result of the web service call
    function callComplete(result, data)
    {
        if (result)
        {
            alert("SUCCESS " + getTagValue(data, "date_time"));
        }
        else
        {
            alert("Error occurred calling web service.");
        }
    }
    

    
    
    // This function is called with a text input that is formatted with the server date and time
    // when the web service call returns.
    function getTime(message)
    {
        var soap = createSoapHeader(message);
        

        //create the web service object and make the call
        var webServiceCall = new WebSvc();
        webServiceCall.CallWebService("services/WebService.asmx", soap, callComplete);
        
    }
    

    
    // Simple Javascript function that returns data contained within a set of tags. 
    function getTagValue(inputStr, tagName)
    {
        // Simple function to search for tagged element in a string,
        // this function will not recurse and simply finds first ocurrence
        // of tag in document.
        
        // PARAM inputStr - string containing tagged document
        // PARAM tagName - name of element to locate
        // RETURNS string data between tagName element or "" if not found

        var stag = "<" + tagName + ">";
        var etag = "<" + "/" + tagName + ">";

        var startPos = inputStr.indexOf(stag, 0);
        if (startPos >= 0)
        {
            var endPos = inputStr.indexOf(etag, startPos);
            if (endPos > startPos)
            {
                startPos = startPos + stag.length;
                return inputStr.substring(startPos, endPos);
            }
        }

        return "";
    }


    

Call the web service from your web page as follows, supplying a parameter to the web service.

   
onclick="getTime(getElementById('echoText').value)
    

Monday, June 4, 2012

JUnit Test Patterns

http://www.abracadabrasolutions.com/JUnitArticle.htm 
 
Unit testing is one of the most important techniques available in the development of quality software. When you start to write unit tests however, it’s not long before you realize just how much effort is involved. Fortunately, a good unit-testing framework can really help, and one such framework is JUnit, which was originally developed by Kent Beck as part of his eXtreme Programming methodology.

Even with a good framework, however, you must still write a lot of repetitive and mundane code just to support your test harness. In this article, I show how I used Rational® XDE™ to create a design pattern that automates the creation of test cases and suites, using the JUnit framework.

The goals of this project were:
1)       Automate the generation of all housekeeping code. I wanted my pattern to generate all required structural and behavioral code that doesn’t need  human input (i.e., doesn’t require thinking).
2)       To make the generated code compile and run even when the pattern is applied multiple times.

This was the first iteration of my pattern, so to keep the scope manageable I restricted the pattern to working with one class at a time. I also did not  do anything fancy, like trying to keep the test cases in sync with the application code. If a class name is changed, the test code will break, and it’s a manual job to fix it.

JUnit

JUnit is a test framework. It provides a set of classes and tools that can be used to create and run tests for Java applications. The core of the JUnit framework is the TestCase class, which  provides basic functionality to create and run tests. The normal usage scenario is to create a sub-class of TestCase, and then add methods to it that test something.

In order to run the tests, the new TestCase is added to a TestSuite, and the method TestSuite.run is called. The TestSuite then uses reflection to run the test methods of the TestCase. Any method in the TestCase that begins with “test” is treated as a test case and executed. So you use Java code to test Java code.

The Test Harness

The test harness is all the code that is there simply to do testing. It adds nothing to the functionality of the released application. Any piece of code that I want to test is called the “Unit Under Test,” or UUT for short. With Java, the UUT is generally a class, because in Java the class is the smallest unit of stand-alone code.

The test harness code can easily take up as much code as the application under test itself, so it makes good sense to structure the harness in a way that makes it easy to manage and modify. The best way I have found to do this is to create a subclass of TestCase for each UUT, and a subclass of TestSuite that allows you to run all test cases in one go. Each TestCase will invoke methods on the UUT and check that the values returned are as expected.

For example, if I had two classes, Adder and Subtractor, I would create two test cases called TestAdder and TestSubtractor, respectively, plus a test suite called something like AllTests. The UML representation of the test harness structure would look something like Figure 1.

Figure 1: UML Class Diagram of Test Harness

To make it simple to test a single class, I normally add a main method to the TestCase that simply runs the tests in the class and outputs the results to standard out. In this way I can just compile and run the test case to check whether the code I’ve just developed works or not. The JUnit framework provides everything I need to do this, so the main method for a class like TestAdder would look something like this:

   public static void main(String[] args) {
      junit.textui.TestRunner.run(AdderTest.class);
   }

I also put a main method on the TestSuite class, so that I can easily run all my tests from the command line. I generally make running this TestSuite a part of the build process, so that no application is said to build correctly until it has compiled and all its test cases have executed successfully.

The main method of the test suite would look something like this:

   public static void main(String[] args) {
      AllTests suite = new AllTests();
      junit.textui.TestRunner.run(suite);
   }


The TestSuite constructor also needs to do a bit of work. It must add all known TestCases to the suite, so that the main method can execute them. So my test suite constructor would look something like this:

   public AllTests() {
      this.addTest(new TestSuite(TestAdder.class));
      this.addTest(new TestSuite(TestSubtractor.class));
   }


Then I can finally start adding test methods to the TestCases to exercise the behavior of the classes being tested. Supposing my Adder class had a method named add that returned the sum of two numbers passed to it; a test for this method might look something like this:


public void testAdd()
{
    Adder uut = new Adder();
    assertTrue(“1+1!=2”, uut.add(1,1) == 2);
}

This method instantiates a new Adder and checks that the result of the call to add equals two by calling the JUnit framework method assertTrue. If the second parameter passed to add does not evaluate to true, JUnit throws an exception and records this as a test failure.

Only the code in the testAdd method is really specific to testing the Adder class. So even with a framework like JUnit, there is a lot of housekeeping code that needs to go in place before I can actually write the tests.  Another painful thing is that every time I create a new test case, I have to go and add it to my TestSuite. These are the kinds of tasks I’d like to automate.

In summary, the tasks to be automated are:
1.       Write the structural code for a new TestCase.
2.       Write the TestSuite code if we don’t have one yet.
3.       Add the code to add an instance of the new TestCase to the TestSuite at run time.
4.       Write a main method for the new TestCase that runs the TestCase and outputs the results to standard out.
5.       Write a main method for the TestSuite that runs all TestCases and outputs the results to standard out.

The Structural Pattern

I decided to put the pattern and the JUnit framework classes together in a model, which can be reused with any application I’m developing. That way, I can just open the model and apply the pattern as required. All I have to remember to do is add a reference from my application model to my JUnit pattern model, so that the code will generate and compile correctly.

First, I must reverse engineer the JUnit framework, to get access to the classes it contains for my pattern. To do this, I create a new Java Modeling Project named JUnit, and copy the JUnit framework archive, junit.jar, into the root folder of the project. In the model navigator, I right click on the model and select from the menu:

                More Java Actions | Add / Remove Modeled Files…

In the Add/Remove Modeled Files dialog  shown in Figure 2, I set the File Types combo to “*.jar” and click the Add Recursively button. XDE pulls junit.jar into the project. When I click the OK button, XDE reverse engineers all of the classes in the .jar file.


Figure 2: Reverse Engineering JUnit

Abstracting out a general design pattern from the main usage scenario above, I come up with two parameters for our pattern. The first is the class I want to test, and the second is the suite, which may or may not exist.

In Rational XDE, creating a new design pattern is easy. I just right click on the JUnit model in the Model Explorer and select

                Add UML | Pattern Asset

from the menu. XDE presents me with a dialog box to specify how I want the pattern asset created, as in Figure 3. Rational XDE will create a new package with the stereotype <<Asset>>. Inside that will be a new Parametized Collaboration.



Figure 3: Creating a Pattern Asset


The Parametized Collaboration is a UML element I can use to specify any collaboration of UML elements that will modify or use a set of input parameters in some way. In this case, the input parameters are a class to be tested, and a test suite. The pattern will use these input parameters to generate the structure of a test harness. I add these parameters to the Collaboration by right clicking on it in the Model Explorer and selecting

                Add UML | Template Parameter

from the menu. To specify that the input parameters are classes, I then select each one, right click on it, and select

                Add UML | Type | Class

from the menu. This gives me a structure, as shown in Figure 4.


Figure 4: Parametized Collaboration Structure in the Model Explorer

Creating the structural aspect of the design pattern is straightforward. I simply create a new class diagram in the <<Asset>> package and put some classes on it with relationships between them. In this case, I need to add the TestCase and TestSuite classes from the JUnit framework, the classes that are parameters to the collaboration, any classes that will be generated as part of the pattern expansion, and then the required relationships between them. So my initial design looks like the class diagram in Figure 5. Now, when I apply the pattern, XDE prompts me for the class to be tested and the TestSuite, and then adds any other classes in the diagram to my model, including any relationships I have specified between these classes.




Figure 5: Initial Structural Aspect of Pattern



But here’s where I have a problem. I’ve specified that the pattern should create a sub class of TestCase called MyTestCase, to be the test case for the UUT. However, that name is not very descriptive, and if I apply the pattern twice, I’ll get two classes called MyTestCase. What I really want is to pre-pend “Test” to the name of the class I’m trying to test, so that my test case for Adder would be TestAdder, for example.

Luckily, XDE provides this capability through the use of scriptlets. A scriptlet is a little piece of JavaScript that will be executed when the pattern is applied. To tell the pattern engine to execute the scriptlet, I just need to enclose it in <%…%> markers. To tell the pattern engine to use the output of the execution as a replacement string, I just include a  = (equals symbol) as the first character in the execution.  Notice that this is very similar to the use of JavaScript in an ASP or JSP page.

I make this change and apply the pattern to a class. The pattern expansion creates a new test case and test suite class, and also a class diagram showing the expansion in the target package, as expected. Unfortunately, it also creates a copy of the framework classes in the destination package, but what I really want is for the expanded classes to reference the framework classes in the JUnit model.

To rectify this situation, I need to modify the merge behavior of the pattern via the Pattern Explorer. When I select the class diagram in the Pattern Explorer, I can view its Pattern Properties, one of which is the merge behavior. The different ways a UML element in a pattern can be merged into a model during pattern expansion are described in Table 1.

Table 1: Merge Behaviors for Pattern Elements

Value
Meaning
Merge
Create a new element if it doesn’t exist; update if it’s already there.
Preserve
Create a new element if it doesn’t exist; don’t touch  if it’s already there.
Replace
Always create a new element; overwrite if it’s already there.
No Copy
Never create a new element; never update if it’s already there.


By changing the merge behavior of the diagram from “Replace” (the default) to “No Copy,” the diagram is not created in the model at expansion time; and, as a side effect, the framework classes that are present on the diagram are not re-created in the model when the pattern is expanded.

Adding Behavior

Okay. Now I have a pattern that will create the structural aspect of a test-harness architecture. But architecture is more than just structure. What about behavior?

The actual tests that I will write to test my code will be different for each class, so they are not good candidates to automate as part of the pattern. But there are a lot of things I need to do for my test cases and suites that stay the same each time. For example, I always add a main method to each test case and the suite, and these main methods do pretty much the same things every time, the only difference being the class they’re operating on.

XDE provides me with a way to automate this kind of behavioral code through the use of code templates. With a code template, I can bind a piece of Java code to an operation on a class. When I apply the pattern, the Java code is squirted into the implementation body for that class. What’s even better is that I can use scriptlets in these code templates, and I can specify parameters to pass to the scriptlet when it is expanded.

For example, for my TestCase, I add a main method, and then right click on that method in the model explorer and select

Code Templates | Bind


from the menu to bind a new code template to the method. The code template editor appears, and I can specify any existing model elements to use as parameters to the code template expansion. In this case, I want the code template to run the TestCase, so I specify the TestCase as a parameter to the code template. This can be seen in the Code Template Editor in Figure 6. I specify that a parameter named “TestCase” will be passed to the code template expansion, and that its value is expanded from the scriptlet Test<%=UUT%>, which is the name of the new TestCase the pattern creates.


Figure 6: The Code Template Editor


Inside the code template body, I use scriptlets to access these parameters. So the code template I bind to the main method of my TestCase looks like this:

      junit.textui.TestRunner.run(<%=TestCase%>.class);

When the pattern is applied to the Adder class, this will expand to:

      junit.textui.TestRunner.run(TestAdder.class);

which is exactly what I want.

When I bind a code template to an operation, XDE gives me the option of specifying if it is a one-time expansion or not. If I select one-time expansion, then the code is generated, and I can modify it at will. If I don’t select one-time expansion, the code will be generated with a couple of marker comments around it. Then, any code inside those marker comments is overwritten by XDE whenever the code and model are synchronized.

In my pattern, I’ve made use of both ways of binding templates. For my main method, I’m not anticipating any customizations to the code, so I haven’t made one-time expansions. Any changes made inadvertently will be cleaned up next time I synchronize.

I’ve also automated the addition of the TestCases to the TestSuite. First, I define a method called addAllTests in the TestSuite and bind a code template to the constructor of the TestSuite that calls this method. Then, I bind the following piece of code to the addAllTests method:

      this.addTest(new TestSuite(<%=TestCase%>.class));



As you can see in Figure 6, I’ve set the parameter TestCase to be Test<%=UUT%> so it expands to the name of the TestCase class that’s just been generated, and binds the resulting piece of code to the addAllTests method.  I’m anticipating that this method might be customized, so I’ve bound the code template as a one-time expansion.

Just to provide the mandatory scope creep, I’ll also automate the instantiation of the UUT. In my usage scenario above, I explicitly create a new Adder just before I test it. With my pattern, I add a method to do that for me; JUnit allows you to provide a method on your test case named setUp, which it will execute before it calls each test… method. I’ve added the method and bound the following code template to it:

    mUut = new <%=UUT%>();

This expands to calling the default constructor for the UUT and storing the object in a data member named mUut.

JUnit also allows you to provide a tearDown method, which it calls immediately after calling each test… method. To this method, I’ve bound the following code template:

    mUut = null;

To support these two method bodies, I change the dependency between the test case and the UUT into a private aggregation, with the role name mUut. XDE generates a field reference for this. I can use the generated setUp and tearDown methods as starting points or leave them as is.

The last thing I’ll add is a default test operation named testDoSomething, and bind a code template to it that simply throws a failure back to the JUnit framework when executed. This is useful, because it doesn’t mislead me into thinking I’ve got a test for something when I haven’t. When I implement the test, I can replace the code that throws the failure with a valid test.

Merging the Pattern

So now we have a pattern that creates both the structure and behavior of a test harness. We have satisfied the first goal of the project: to generate all housekeeping code automatically. Unfortunately, the second goal requires a bit more work.

If the pattern is expanded at this point, the resulting code won’t compile. The generated TestSuite is missing import statements for the JUnit framework classes it requires; Test and TestResult. I could add dependencies to both of these classes, but I really want the TestSuite to have access to all classes in the junit.framework package, as other classes may be needed when I start coding inside the TestSuite. I really want the statement

      import junit.framework.*

in the TestSuite class. To do this I create an <<access>dependency between the Java component the test suite resides in and the junit.framework package. The resulting class diagram is shown in Figure 7.


Figure 7: The Final Structural Pattern



Now the generated code will compile and run, although the test case will fail with a “Not implemented” message. But as soon as the pattern is applied to a second class using the same TestSuite, the first TestCase is no longer executed. What’s happened? The addAllTests method of the TestSuite has stopped working. It now only adds the last TestCase created to the TestSuite. The second application of the pattern has overwritten the existing method definition with its own definition. What I really want is for the body of the second code template to be appended, so that both TestCases are added. I do this, by delving into the merge options for the pattern again.

By default, the merge value for the addAllTests operation is set to “Replace,” so if there is an existing element in the target model with the same name, it is overwritten by the element from the pattern application. This is why the body of addAllTests gets replaced when the pattern is applied to a suite the second time. By changing the merge value to “Merge”, the code templates bound to addAllTests will actually be merged into one method body, so all test cases will be added when the method is called.

To help organize my model, I want to put test cases and suites into a package separate from the real application code. It is easiest to place them into a subpackage of the package where the UUT resides. I’ll name this subpackage test, so I’ll know what is in there.

To do this, I need to open the pattern in the Pattern Explorer again, and go to the

                Advanced Properties | Default Expansion Location

property.  I change the property to expand to the package of the template parameter UUT, and append “test” to that, as shown in Figure 8.

Figure 8: Setting the Default Expansion Location for the Pattern.

The last tweak I’ll make is to give the user the option of either selecting an existing TestSuite class from the model, or providing a name to generate a new one. To do this, I need to set the pattern properties for the Suite template parameter. So in the Pattern Explorer, I browse to the

Suite | Advanced Properties | Value Sources

property, and change the value source. The possible values for this are summarized in Table 2. The value I want to set it to is “User or Generated,” which gives me the option of selecting an existing model element or entering the name for a new TestSuite.

Table 2: Value Sources for Pattern Template Parameters

Value
Meaning
User
Parameter must be selected from model.
Generated
Parameter must be typed as a string.
Collection
Parameter must be a collection of an element selected from model.
User or Generated
Parameter may either be selected from the model or typed in.
User or Collection
Parameter may either be an element selected from model or a collection selected from model.
Generated or Collection
Parameter may either be typed in or a collection of an element selected from model
Any
Parameter can be entered in any one of the three ways



By default, XDE would still prompt me for the expansion location and default it to the value I have specified here. To make my life easier, I’ve also specified that the prompt for this value and the bindings location should be suppressed. To do this, I browse to the

                Advanced Properties | Application Wizard Properties


node in the Pattern explorer and check both the Suppress Bind Location Dialog and Suppress Expand Location in Dialog checkboxes.

The Final Product

Finally, the acid test. The first time I apply the pattern, I am presented with the dialog shown in Figure 9.

Figure 9: Selecting a Unit Under Test (UUT)

I select the class I want to test as the UUT, and click next. But when prompted for the suite, there is no existing class in my model that is a TestSuite, so I select the generated value option, enter the name of my new TestSuite, and click the Add button, as shown in Figure 10.

Figure 10: Specifying a New Test Suite


My  pattern generates a TestCase called TestAdder, and a TestSuite called AllTests. When I compile and run AllTests, I get one failure because the test testDoSomething has not being implemented yet. So far so good.

When I generate a TestCase for Subtractor, I select the TestSuite I’ve just created. When I compile and run the TestSuite AllTests, two tests are executed. Success! Now that XDE has done the donkeywork, I can go and add some real test methods to my two new TestCases.

Next Steps

My pattern deals with only one simple but common use of the JUnit framework. There are other common uses that could be implemented fairly easily - for example, creating a TestCase as a nested class of the UUT. Also, there are many places this particular pattern could go. Selecting a package and generating TestCases for all the classes inside is one suggested enhancement. Generating a default test method for each public method of the UUT is another.

All of these options are outside the scope of my project, however, so they will have to wait for another article. Meanwhile, my project did create a small but useful pattern and afforded a good dig around the XDE pattern engine in the process. Keep in mind that the pattern engine is extremely powerful, and I’ve really only scratched the surface of its potential. I hope this article will serve as an introduction to what is possible and set your creative juices flowing.

References

To get more information about JUnit and download the latest version of the framework, visit http://www.junit.org.
For information on eXtreme programming, visit http://www.extremeprogramming.org or http://www.xp.co.nz.

Acknowledgements

My deepest thanks to Yves Holvoet for his help in producing both this article and the XDE model it is based on.

Friday, June 1, 2012

Remove XML Name Space

I was working on a 2 versions of a BPM product the older one without support for namespaces and the newer one supporting namespaces. The messages from external systems were with namespaces. When the old system was called it was required to strip the namespaces. Initially we thought of using xslt to achieve this. But then again regular expression came to our rescue. The cute little function below can remove namespaces from xml string


public static String removeXmlStringNamespaceAndPreamble(String xmlString) {


return xmlString.replaceAll("(<\\?[^<]*\\?>)?", ""). /* remove preamble */
replaceAll("xmlns.*?(\"|\').*?(\"|\')", "") /* remove xmlns declaration */
.replaceAll("(<)(\\w+:)(.*?>)", "$1$3") /* remove opening tag prefix */
.replaceAll("(</)(\\w+:)(.*?>)", "$1$3"); /* remove closing tags prefix */


}
Love the power of regular expression :)