!  Purpose: Test the ARCHIVE command
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  if (reg.main.M.logger NREXISTS) then  ! Since 3.7.1, only run this test if not logging
    set logfile test_archive.log
    archive on logfile /echo=off
    set expText "archive_test_text"
    say expText
    archive off

    assert/text="^reg.main.name: log file exists?"/stat=tempStatus logfile FEXISTS
    if tempstatus istrue then
      status logfile size=logsize
      assert/text="^reg.main.name: log file size" logsize EQ 18
      file open tag logfile
      file read tag logcontent
      assert/text="^reg.main.name: log file contents" expText EQS logcontent
    endif

    erase/warn=n logfile

    ! test BUG 2422
    set z:gotValidTextMsg false
    ! 1. archive messages to main macro (this)
    archive to MAIN
    ! 2. send info message
    info expText
    ! 5. turn echo ON (back to normal) and so we can see output of next ASSERT
    archive/echo=on off
    assert/text="^reg.main.name: Got valid 'TEXT' message"/stat=gotValidTextMsg gotValidTextMsg isTrue
    ! end test BUG 2422

    call testBug2983_NPE

  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "TEXT" then
    ! test BUG 2422
    ! 3. need to turn OFF archive with echo ON so that we get output of ASSERT
    archive/echo=on off
    assert/text="^reg.main.name: Got 'TEXT' message with correct data"/stat=gotValidTextMsg expText SUBS msg.data
    ! 4. turn off echo so that info message does NOT get printed
    archive/echo=off off
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!*****************************************************************************!
! test NPE during Archive ON when unable to open log file
!*****************************************************************************!
procedure testBug2983_NPE
  invoke tempFile java.io.File.createTempFile("test_archive", ".log")
  invoke madereadonly tempFile.setReadOnly()
  
  assert/text="^reg.main.name: created read only file: ^tempFile." madereadonly isTrue

  info "!!!!!  Warning below expected i.e. 'ARCHIVE: Unable to open (for output) ...' !!!!!"
  archive on "^tempFile" /echo=on
  archive off

  erase "^tempFile"
return