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

39.53% Statements 51/129
100% Branches 2/2
50% Functions 2/4
39.53% Lines 51/129

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 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.
    METHODS add_simple_object FOR TESTING RAISING zcx_abapgit_exception.
    METHODS long_numc 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_expected        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_expected =
      '<?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_expected 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_expected ).

    lv_xstring = lo_conv_out_string->get_buffer( ).

    " Add BOM for Unicode systems
    IF cl_abap_char_utilities=>charsize > 1.
      lv_bom = cl_abap_char_utilities=>byte_order_mark_little. "UTF-16LE, 4103
      CONCATENATE lv_bom lv_xstring INTO lv_xstring IN BYTE MODE.
    ENDIF.

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

    lo_conv_in_string->read( IMPORTING data = lv_expected ).

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

  ENDMETHOD.
 
  METHOD long_numc.

    DATA: BEGIN OF ls_foo,
            bar TYPE n LENGTH 255,
          END OF ls_foo.

    DATA lo_output TYPE REF TO zcl_abapgit_xml_output.
    DATA lv_xml TYPE string.

* write a bad value into the NUMC field,
    ls_foo = '0009'.

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

    lv_xml = lo_output->zif_abapgit_xml_output~render( ).

    cl_abap_unit_assert=>assert_char_cp(
      act = lv_xml
      exp = '*>0009<*' ).

  ENDMETHOD.
 
ENDCLASS.