Using Jice to load Objects Graphs

Jice is an (excellent) lightweight dependency injection framework now embedded into JTestCase.
Using Jice it is possible to bulk load into JTestCase arbitrary graphs of Java objects.

This section explains how with a specific example, using the class java.text.SimpleDateFormat .
I'd recommend to read further about Jice on the jice site, even independently of JTestCase !.

SimpleDateFormat Example



In this example we will load an SimpleDateFormat object in a JTestCase test.
SimpleDateFormat is a JDK class in the package java.text

It has two methods, toPattern() and toLocalizedPattern() , that return the date formatting pattern with which the instance object has been initialized.

In our case, a SimpleDateFormat will be instantiated in the jtestcase xml file using jice element , with the date pattern dd.MM.yyyy HH:mm:ss z.

When SimpleDateFormat is instantiated, it is passed another embedded class , java.util.Locale , specifying the finnish locale FI, and finnish language fi.

The toPattern() method will be tested to return the following string    dd.MM.yyyy HH:mm:ss z
The toLocalizedPattern() method will be tested to return the string    jj.nn.aaaa HH:mm:ss z .

Jice Elements



    <method name="testGetJice" test-case="standard">
       <params>
          <param name="jice" type="" use-jice="yes">
             <jice>
                <dateFormat
                  xmlns="http://www.jicengine.org/jic/2.0"
                  class="java.text.SimpleDateFormat" args="pattern,locale">
                    <pattern>dd.MM.yyyy HH:mm:ss z /pattern>
                    <locale class="java.util.Locale"
                      args="language,country">
                        <language>fi </language>
                        <country>FI </country>
                    </locale>
                </dateFormat>
              </jice>
          </param>
       </params>
    </method>
        

Jice Tests



  /**
   * Tests getting jice elements as parameters
   
   @throws Exception
   */
  public void testGetJice() throws Exception {

    Vector testCases = null;
    HashMap params = null;
    String dataFile = "test/org/jtestcase/data/GetParamTest1.xml";
    _jtestcase = new JTestCase(dataFile, "GetParamTest");

    testCases = _jtestcase
        .getTestCasesInstancesInMethod("testGetJice");

    // for each test case
    for (int i = 0; i < testCases.size(); i++) {
      TestCaseInstance testCase = ((TestCaseInstancetestCases
          .elementAt(i));
      params = testCase.getTestCaseParams();
      java.text.SimpleDateFormat dateFormat = (java.text.SimpleDateFormatparams.get("jice");
      assertTrue("date format pattern is not correct""dd.MM.yyyy HH:mm:ss z".equals(dateFormat.toPattern()));
      assertTrue("date format pattern is not correctly localized ""jj.nn.aaaa HH:mm:ss z".equals(dateFormat.toLocalizedPattern()));      
    }
  }