!******************************************************************************!
! Test bean that hangs upon closing with pipe
! @bug      2686 MidasBean / Command / BaseFile: BaseFile.waitForFile() causes infinite loop when used with MidasBean
! @author:  J Nuanes / J Bauer
! @version: $Id: test_midasbean.mm,v 1.9 2013/09/05 19:55:59 jwb Exp $
!******************************************************************************!
startmacro
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
   if ^{env.NMROOT} EQS "classpath:" then
      info "SKIPPING ^reg.main.name Cannot execute build when running from JAR. The jar holds .class files, not .java files. Already built."
      stop
  endif
  ! check /baseline switch because it fails if user does not have write permissions to compile the test class
  if baseline isTrue then

    ! setup the option tree, add it to the path, and make sure the test prim is compiled
    call setupPath
    call setupFile

    pipe on
      assert/text="^reg.main.name: File compiled" nxm.mytestopt1.prim.testbeanpipehangprim.class fexists

      nxm.sys.test.testbeanpipehangbean

      set this.timer(0) 3  ! trigger the assert
    pipe off

  else
    INFO "SKIPPING. Requires /baseline switch to be used when running test."
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "TIMER" then
    assert/text="^reg.main.name: Registry entry cleaned out" reg.TESTBEANPIPEHANGBEAN nrexists
    path rem mytestopt1
    pipe stop

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  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

!******************************************************************************!
! Setup the option tree and add to path
!******************************************************************************!
procedure setupPath
  ! add the option tree where the test command is located
  FILE NAME optDir (^{env.nmroot},nxm,sys,test,nmopts,nxm,mytestopt1) "" ""
  OPT mytestopt1 optDir
  PATH add mytestopt1
return

!******************************************************************************!
! Make sure the needed file is compiled
!******************************************************************************!
procedure setupFile
  ! compile the relevant code, if needed
  if nxm.mytestopt1.prim.testbeanpipehangprim.class nfexists then
    BUILD testbeanpipehangprim
  endif
return
