TestXml.java

 
// Lapetus Ltd Java Class.  Copyright (c) Lapetus Systems Ltd, 2009, 2010. 
// ----------------------------------------------------------------------- 
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece. 
// -------------------------------------------------------------------------- 
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu 
// ------------------------------------------------------- 
// $LastChangedRevision: 1168 $ 
// $LastChangedDate:: 2010-11-06 16:41:19#$ 
// ---------------------------------------- 
 
 
import com.lapetus_ltd._2009.xml.licencetype.ObjectFactory; 
import com.lapetus_ltd._2009.xml.licencetype.XLptsLicenceType; 
import com.lapetus_ltd.api.TLptsMainJut; 
import com.lapetus_ltd.api.common.TLptsXmlUtil; 
import com.lapetus_ltd.api.xml.types.TLptsLicenceType; 
import junit.framework.TestCase; 
 
import java.util.LinkedList; 
import java.util.List; 
 
 
// ###################################################################################################### 
// ####    Test code for the JUT build process.  This code will stop the build process on ERROR      #### 
// ###################################################################################################### 
 
 
// Class Description : This module tests the XML manager. 
// 
// The XML manager deals with XML data to and from files. 
// This test verifies the marshaling and unmarshaling capabilities of the XML manager. 
// We also test a jar file as a source of retrieving XML files. 
// 
 
 
public class TestXml extends TestCase 
{ 
  public static void main(String[] args) 
  { 
    TestXml test = new TestXml(); 
    test.runAllTests(false); 
  } 
 
  public void runAllTests(boolean isExit) 
  { 
    testIdentifier(); 
    testUnmarshalMarshal(); 
    testParameterList(); 
    if (isExit) 
      System.exit(0); 
  } 
 
 
  public void testIdentifier() 
  { 
    TLptsMainJut.init(); 
 
    try 
    { 
      assertNotNull("-->Id must not be bull", TLptsXmlUtil.getIdentifier()); 
      assertTrue("-->Id should be 36 characters long", TLptsXmlUtil.getIdentifier().length()==36);  //ie: 95f42369-49e2-3b38-7f4c-04fc4c90c0aa 
    } catch (Throwable e) 
    { 
      System.out.println(e.getMessage()); 
    } 
  } 
 
 
  public void testUnmarshalMarshal() 
  { 
    TLptsMainJut.init(); 
    try 
    { 
      ObjectFactory of = new ObjectFactory(); 
 
      TLptsLicenceType licenceType = new TLptsLicenceType(); 
      licenceType.setId(TLptsXmlUtil.getIdentifier()); 
      licenceType.setDescription("Try this description"); 
      licenceType.setLicencedTo("This test"); 
      TLptsXmlUtil.marshal("test.licence.xml", XLptsLicenceType.class,of.createXLptsLicenceType(licenceType)); 
      XLptsLicenceType licenceType1 = (XLptsLicenceType) TLptsXmlUtil.unmarshal("test.licence.xml", XLptsLicenceType.class); 
 
      if (!licenceType.getId().equals(licenceType1.getId())) 
        fail("FAILED: XML File not same after marshal and unmarshal"); 
      if (!licenceType.getDescription().equals(licenceType1.getDescription())) 
        fail("FAILED: XML File not same after marshal and unmarshal"); 
      if (!licenceType.getLicencedTo().equals(licenceType1.getLicencedTo())) 
        fail("FAILED: XML File not same after marshal and unmarshal"); 
 
      // read the xml file from offline-tests.jar  
      XLptsLicenceType licenceType2 = (XLptsLicenceType) TLptsXmlUtil.unmarshal("jar:file:offline-tests.jar!/test.licence.xml", XLptsLicenceType.class); 
      if (licenceType2==null) 
        fail("FAILED: Could not unmarshall licence from JAR file"); 
      if (!licenceType2.getLicenceType().equals("Demonstration")) 
        fail("FAILED: Invalid data in licence file"); 
 
      System.out.println("XML successfully completed"); 
 
    } catch (Throwable e) 
    { 
      System.out.println(e.getMessage()); 
      fail("Fail : Exception Thrown during XML processing"); 
    } 
  } 
 
  public void testParameterList() 
  { 
    TLptsMainJut.init(); 
 
    List<String> keys = new LinkedList<String>(); 
    List<String> values = new LinkedList<String>(); 
    List<String> tokens = new LinkedList<String>(); 
    String test1 = ";test1=test2;;test3=;=test5;"; 
    String test2 = ""; 
    String test3= ";"; 
 
    keys.add("test1"); 
    keys.add("test3"); 
    keys.add(""); 
 
    values.add("test2"); 
    values.add(""); 
    values.add("test5"); 
 
    tokens.add(""); 
    tokens.add("test1=test2"); 
    tokens.add(""); 
    tokens.add("test3="); 
    tokens.add("=test5"); 
    tokens.add(""); 
 
    try 
    { 
      assertTrue("-->Key Lists must be the same", TLptsXmlUtil.getDelimitedKeyList(test1,';').equals(keys)); 
      assertTrue("-->Value Lists must be the same", TLptsXmlUtil.getDelimitedValueList(test1,';').equals(values)); 
      assertTrue("-->Token Lists must be the same", TLptsXmlUtil.getTokens(test1,';').equals(tokens)); 
      keys.clear(); 
      values.clear(); 
      tokens.clear(); 
      assertTrue("-->Key Lists must be the same (Empty List)", TLptsXmlUtil.getDelimitedKeyList(test2,';').equals(keys)); 
      assertTrue("-->Value Lists must be the same (Empty List)", TLptsXmlUtil.getDelimitedValueList(test2,';').equals(values)); 
      assertTrue("-->Token Lists must be the same (Empty List)", TLptsXmlUtil.getTokens(test2,';').equals(tokens)); 
      tokens.add(""); 
      tokens.add(""); 
      assertTrue("-->Key Lists must be the same (One delimiter)", TLptsXmlUtil.getDelimitedKeyList(test3,';').equals(keys)); 
      assertTrue("-->Value Lists must be the same (One delimiter)", TLptsXmlUtil.getDelimitedValueList(test3,';').equals(values)); 
      assertTrue("-->Token Lists must be the same (One delimiter)", TLptsXmlUtil.getTokens(test3,';').equals(tokens)); 
 
    } catch (Throwable e) 
    { 
      System.out.println(e.getMessage()); 
    } 
  } 
}