class zcl_guitar definition public final create public .
public section.
data: guitar_record type zguitars read-only.
"! <p class="shorttext synchronized" lang="en"></p>
"! Creates a guitar object from a guitar structure
"! @parameter i_guitar_record | <p class="shorttext synchronized" lang="en"> Guitar structure </p>
methods constructor importing i_guitar_record type zguitars.
protected section.
private section.
endclass.
class zcl_guitar implementation.
method constructor.
me->guitar_record = i_guitar_record.
endmethod.
endclass.
class ltcl_inventory definition final for testing duration short
risk level harmless.
private section.
methods:
setup,
add_guitar_test for testing raising cx_static_check.
data inventory type ref to zcl_inventory.
endclass.
class ltcl_inventory implementation.
method setup.
inventory = new zcl_inventory( ).
endmethod.
method add_guitar_test.
data(guitar_record) = value zguitars( serialnumber = 'FE34000'
price = '1745.43'
builder = 'Fender'
model = 'Stratocaster'
type = 'Electric'
backwood = 'Maple'
topwood = 'Maple' ).
inventory->add_guitar( new zcl_guitar( guitar_record ) ).
cl_abap_unit_assert=>assert_subrc( exp = 0 act = sy-subrc ).
endmethod.
endclass.
types: begin of ty_guitar,
serial_number type z_serial_number,
guitar type ref to zcl_guitar,
end of ty_guitar.
types: guitars_tab type hashed table of ty_guitar with unique key
serial_number.
class zcl_inventory definition public
final create public.
public section.
"! <p class="shorttext synchronized" lang="en"></p>
"! Adds a guitar to the inventory
"! @parameter i_guitar | <p class="shorttext synchronized" lang="en"> Guitar object to add</p>
"! @parameter r_ok | <p class="shorttext synchronized" lang="en"> Guitar was added to inventory</p>
methods add_guitar importing i_guitar type ref to zcl_guitar
raising zcx_guitar.
protected section.
private section.
data guitars type guitars_tab.
endclass.
method add_guitar.
data(guitar_record) = value ty_guitar( serial_number = i_guitar->guitar_record-serialnumber
guitar = i_guitar ).
try.
insert guitar_record into table me->guitars.
catch cx_sy_itab_duplicate_key.
raise exception type zcx_guitar
exporting
textid = zcx_guitar=>duplicate_record.
endtry.
endmethod.
class ltcl_inventory definition final for testing duration short
risk level harmless.
private section.
methods:
setup,
add_guitar_test for testing raising cx_static_check,
duplicate_guitar for testing raising zcx_guitar.
data inventory type ref to zcl_inventory.
endclass.
class ltcl_inventory implementation.
method setup.
inventory = new zcl_inventory( ).
endmethod.
method add_guitar_test.
data(guitar_record) = value zguitars( serialnumber = 'FE34000'
price = '1745.43'
builder = 'Fender'
model = 'Stratocaster'
type = 'Electric'
backwood = 'Maple'
topwood = 'Maple' ).
inventory->add_guitar( new zcl_guitar( guitar_record ) ).
cl_abap_unit_assert=>assert_subrc( exp = 0 act = sy-subrc ).
endmethod.
method duplicate_guitar.
data(guitar_record) = value zguitars( serialnumber = 'FE34000'
price = '1599.95'
builder = 'Fender'
model = 'Stratocaster'
type = 'Electric'
backwood = 'Maple'
topwood = 'Maple' ).
data(guitar1) = new zcl_guitar( guitar_record ).
data(guitar2) = new zcl_guitar( guitar_record ).
inventory->add_guitar( guitar1 ).
inventory->add_guitar( guitar2 ).
* Supposed to fai the test only if no exception has been thrown by the previous method call
* Seems not to work
cl_abap_unit_assert=>fail( msg = 'ZCX_GUITAR not raised'
level = if_aunit_constants=>critical ).
endmethod.
endclass.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |