All files / src/xml zcl_abapgit_xml_output.clas.testclasses.abap

49.51% Statements 51/103
100% Branches 1/1
50% Functions 1/2
49.51% Lines 51/103

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                         1x 1x  
CLASS ltcl_xml_output DEFINITION DEFERRED.
CLASS zcl_abapgit_xml_output DEFINITION LOCAL FRIENDS ltcl_xml_output.
 
CLASS ltcl_xml_output DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
 
  PUBLIC SECTION.
    METHODS:
      render_xml_string FOR TESTING
        RAISING zcx_abapgit_exception,
      add_simple_object FOR TESTING
        RAISING zcx_abapgit_exception.
 
    TYPES: BEGIN OF ty_old,
             foo TYPE i,
             bar TYPE c LENGTH 1,
           END OF ty_old.
 
ENDCLASS.
CLASS ltcl_xml_output IMPLEMENTATION.
 
  METHOD add_simple_object.
 
    DATA: ls_input       TYPE ty_old,
          ls_result      TYPE ty_old,
          lv_value       TYPE string,
          li_xml_element TYPE REF TO if_ixml_element,
          lo_output      TYPE REF TO zcl_abapgit_xml_output.
 
    ls_input-foo = '2'.
    ls_input-bar = 'A'.
 
    CREATE OBJECT lo_output.
    lo_output->zif_abapgit_xml_output~add( iv_name = 'DATA'
                    ig_data = ls_input ).
 
    li_xml_element = lo_output->mi_xml_doc->find_from_name( 'FOO' ).
    lv_value = li_xml_element->get_value( ).
    ls_result-foo = lv_value.
    li_xml_element = lo_output->mi_xml_doc->find_from_name( 'BAR' ).
    lv_value = li_xml_element->get_value( ).
    ls_result-bar = lv_value.
 
    cl_abap_unit_assert=>assert_equals(
      act = ls_input
      exp = ls_result ).
 
  ENDMETHOD.
 
  METHOD render_xml_string.

    DATA: ls_input           TYPE ty_old,
          lv_value           TYPE string,
          lv_xml             TYPE string,
          lo_output          TYPE REF TO zcl_abapgit_xml_output,
          lo_conv_in_string  TYPE REF TO cl_abap_conv_in_ce,
          lo_conv_out_string TYPE REF TO cl_abap_conv_out_ce,
          lv_encoding        TYPE abap_encoding,
          lv_xstring         TYPE xstring,
          lv_bom             TYPE xstring.

    ls_input-foo = '2'.
    ls_input-bar = 'A'.

    lv_value =
      '<?xml version="1.0" encoding="utf-16"?>#<abapGit version="v1.0.0">#' &
      ' <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">#' &
      '  <asx:values>#   <DATA>#    <FOO>2</FOO>#    <BAR>A' &
      '</BAR>#   </DATA>#  </asx:values># </asx:abap>#</abapGit>#'.

    REPLACE ALL OCCURRENCES OF '#' IN lv_value WITH cl_abap_char_utilities=>newline.

    CREATE OBJECT lo_output.
    lo_output->zif_abapgit_xml_output~add( iv_name = 'DATA'
                                           ig_data = ls_input ).

    lv_xml = lo_output->zif_abapgit_xml_output~render( ).

    lv_encoding = cl_abap_codepage=>sap_codepage( `UTF-16LE` ). "4103

    lo_conv_out_string = cl_abap_conv_out_ce=>create(
      encoding    = lv_encoding
      ignore_cerr = 'X' ).

    lo_conv_out_string->write( data = lv_value ).

    lv_xstring = lo_conv_out_string->get_buffer( ).

    lv_bom = cl_abap_char_utilities=>byte_order_mark_little. "UTF-16LE, 4103
    CONCATENATE lv_bom lv_xstring INTO lv_xstring IN BYTE MODE.

    lo_conv_in_string = cl_abap_conv_in_ce=>create(
      encoding = lv_encoding
      input    = lv_xstring ).

    lo_conv_in_string->read( IMPORTING data = lv_value ).

    cl_abap_unit_assert=>assert_equals(
      act = lv_xml
      exp = lv_value ).

  ENDMETHOD.
 
ENDCLASS.