OpenTTCN/Knowledge base/XML IO operations

From OpenTTCN

Jump to: navigation, search

  OpenTTCN DocZone

  Home | Developer's corner | Knowledge base | Working documents | Documentation | OpenTTCN IDE | Tutorials | Training | How do I | Frequently asked questions | Technical support


XML I/O Operations in TTCN-3



This article discusses OpenTTCN Tester 2011 capabilities related to reading and writing TTCN-3 values as XML file in the file system, on the TTCN-3 source code level. This allows for example serializing TTCN-3 variable state to a file for its later use in the TTCN-3 test suite. This functionality can be found in OpenTTCN Tester 2011 4.1.4 and later. Programming examples of using this functionality are also provided, together with example encodings.


Contents


Function signatures

Here are the signatures of the two new TTCN-3 predefined functions found in OpenTTCN Tester 2011.


writeToXmlFile



function writeToXmlFile(opentype val, universal charstring fileName) return integer;


Saves the specified TTCN-3 value to a file with the specified name in XML format.


Parameters

val

   

TTCN-3 value of any legal TTCN-3 type to be saved as XML to a file with the specified name.

fileName

   

Name of the file to which the specified TTCN-3 value is saved in XML format. If the file already exists, it is overwritten. If it does not exist, it is created.


Return values

Returns 0 on success, non-zero on failure.


Remarks

Opentype is not to be confused with TTCN-3 anytype. Opentype does not exist in TTCN-3 and is used as a pseudo notation to indicate a placeholder for any legal TTCN-3 type in its place.


readFromXmlFile



function readFromXmlFile(universal charstring fileName) return opentype;


Reads a TTCN-3 value in XML format from a file with the specified name and returns the resulting TTCN-3 value.


Parameters

fileName

   

Name of the file in XML format from which the returned TTCN-3 value is read. If the file does not exist or is corrupt, a test case error is thrown.


Return values

Returns the TTCN-3 value read from the specified file in XML format.


Example of use

The following example in TTCN-3 illustrates the use of XML I/O operations.

type component EmptyComponent
{
}

type record FOA
{
    integer frequency,
    float offset,
    float attenuation
}

type record of FOA FOAList;

testcase TC_WriteAndReadXmlFile() runs on EmptyComponent
{
    var universal charstring fileNameBase := "C:\openttcn_test_temp.xml";
    var universal charstring fileName := fileNameBase & ".1";

    /////////////////////////////////////////////////////////
    // integer

    var integer iv := 5;

    var integer result := writeToXmlFile(iv, fileName);

    if (result != 0) { setverdict(fail, "In writeToXmlFile()."); stop; }

    var integer iv2 := readFromXmlFile(fileName);

    log("iv2 = ", iv2);
    if (iv2 != 5) { setverdict(fail); stop; }

    /////////////////////////////////////////////////////////
    // float

    fileName := fileNameBase & ".2";

    var float fv := -15.5;
    result := writeToXmlFile(fv, fileName);
    if (result != 0) { setverdict(fail, "In writeToXmlFile()."); stop; }

    var float fv2 := readFromXmlFile(fileName);
    log("fv2 = ", fv2);
    if (fv2 != -15.5) { setverdict(fail); stop; }

    /////////////////////////////////////////////////////////
    // record

    fileName := fileNameBase & ".3";

    var FOA rec_v :=
    {
        frequency := 10000,
        offset := 0.125,
        attenuation := -1.2
    }

    result := writeToXmlFile(rec_v, fileName);
    if (result != 0) { setverdict(fail, "In writeToXmlFile()."); stop; }

    var FOA rec_v2 := readFromXmlFile(fileName);
    log("rec_v2 = ", rec_v2);
    if (rec_v2 != rec_v) { setverdict(fail); stop; }

    /////////////////////////////////////////////////////////
    // record of

    fileName := fileNameBase & ".4";

    rec_v2.offset := 0.375;
    rec_v2.frequency := 17000;

    var FOAList rof_v := { rec_v, rec_v2 }

    result := writeToXmlFile(rof_v, fileName);
    if (result != 0) { setverdict(fail, "In writeToXmlFile()."); stop; }

    var FOAList rof_v2 := readFromXmlFile(fileName);

    log("rof_v2 = ", rof_v2);
    if (rof_v2 != rof_v) { setverdict(fail); stop; }

    setverdict(pass);
}

Running the above test case produces the following campaign log:

*****************************************************************************
*** RUNNING TEST CASE TC_WriteAndReadXmlFile

Tester : {12:59:11.902} : // Time: 12:59:11.901. Date: 23/Jun/2011. MOT version: TC: 4.1.3.
mtc : {12:59:12.055} : // CASE TC_WriteAndReadXmlFile STARTED
mtc : {12:59:12.063} : log("iv2 = ", 5);
mtc : {12:59:12.072} : log("fv2 = ", -15.5);
mtc : {12:59:12.083} : log("rec_v2 = ", { frequency := 10000, offset := 0.125, attenuation := -1.2 });
mtc : {12:59:12.099} : log("rof_v2 = ", { { frequency := 10000, offset := 0.125, attenuation := -1.2 }, { frequency := 17000, offset := 0.375, attenuation := -1.2 } });
mtc : {12:59:12.101} : setverdict(pass);
mtc : {12:59:12.107} : // CASE TC_WriteAndReadXmlFile FINISHED WITH PASS

/////////////////////////////////////////////////////////////////////////////
/// TEST CASE TC_WriteAndReadXmlFile COMPLETE VERDICT PASS

Example encodings

The TC_WriteAndReadXmlFile test case from the previous section produces the following XML encodings.

File openttcn_test_temp.xml.1:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<document>5</document>

File openttcn_test_temp.xml.2:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<document>-15.5</document>

File openttcn_test_temp.xml.3:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<document>
  <FOA>
    <frequency>10000</frequency>
    <offset>0.125</offset>
    <attenuation>-1.2</attenuation>
  </FOA>
</document>

File openttcn_test_temp.xml.3:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<document>
  <FOAList>
    <element>
      <frequency>10000</frequency>
      <offset>0.125</offset>
      <attenuation>-1.2</attenuation>
    </element>
    <element>
      <frequency>17000</frequency>
      <offset>0.375</offset>
      <attenuation>-1.2</attenuation>
    </element>
  </FOAList>
</document>


The following table summarizes XML encoding rules for various TTCN-3 data types.

Table 1: Encodings of TTCN-3 data types in XML
TTCN-3 type class TTCN-3 constructed type TTCN-3 value Encoding (XML)

integer

-

5

<document>5</document>

float

-

-15.5

<document>-15.5</document>

boolean

type record of boolean
BooleanList;

{ true, false }

<document>
  <BooleanList>
    <element>true</element>
    <element>false</element>
  </BooleanList>
</document>

bitstring

-

'1101011'B

<document>1101011</document>

octetstring

-

'1F2DE3'O

<document>1F2DE3</document>

hexstring

-

'1F2D3'H

<document>1F2D3</document>

charstring

-

"Test string."

<document>Test string.</document>

universal charstring

-

"Это тест."

<document>Это тест.</document>

verdicttype

type record of verdicttype
VerdictList;

{
    pass,
    inconc,
    none,
    error,
    fail
}
<document>
  <VerdictList>
    <element>pass</element>
    <element>inconc</element>
    <element>none</element>
    <element>error</element>
    <element>fail</element>
  </VerdictList>
</document>

enumerated

type enumerated EnumType
{
    E_ACCEPT,   // 0
    E_RELEASE,  // 2
    E_INDICATE, // 3
    E_REQUEST   (1)
}

type record of EnumType
EnumTypeList;
{
E_ACCEPT,
E_RELEASE,
E_INDICATE
}
<document>
  <EnumTypeList>
    <element>E_ACCEPT</element>
    <element>E_RELEASE</element>
    <element>E_INDICATE</element>
  </EnumTypeList>
</document>

union

type union UnionType
{
    bitstring b,
    charstring c,
    hexstring h,
    RecordType r
}

type record RecordType
{
bitstring bs optional,
octetstring os optional
}
{ r := {
bs := '10'B,
os := omit
} }
<document>
  <UnionType>
    <r>
      <bs>10</bs>
    </r>
  </UnionType>
</document>

record

type record FOA
{
    integer freq,
    float off,
    float att
}
{
freq := 10000,
off := 0.125,
att := -1.2
}
<document>
  <FOA>
    <freq>10000</freq>
    <off>0.125</off>
    <att>-1.2</att>
  </FOA>
</document>

record of

type record of FOA
FOAList;

{
{
freq := 10000,
off := 0.125,
att := -1.2
},
{
freq := 17000,
off := 0.375,
att := -1.2
}
}
<document>
  <FOAList>
    <element>
      <freq>10000</freq>
      <off>0.125</off>
      <att>-1.2</att>
    </element>
    <element>
      <freq>17000</freq>
      <off>0.375</off>
      <att>-1.2</att>
    </element>
  </FOAList>
</document>

set

type set TYPE_set_opt
{
bitstring bs optional,
hexstring hs optional,
octetstring os optional,
charstring cs optional
}
{
os := 'A1D2'O,
hs := '1F722'H
}
<document>
  <TYPE_set_opt>
    <os>A1D2</os>
    <hs>1F722</hs>
  </TYPE_set_opt>
</document>

set of

type set of charstring
CharstringList;

{
    "AB",
    omit,
    "CD",
    "DE"
}
<document>
  <CharstringList>
    <element>AB</element>
    <element>CD</element>
    <element>DE</element>
  </CharstringList>
</document>
Views
Personal tools