!******************************************************************************!
! Tests for using nxm.sys.lib.BaseFile as a Library.
!
! @bug     2207 JAR: Strange error if using nxm-sys.jar without NMROOT set
! @bug     2497 BaseFile.getQualifiers() should not return null
! @author  Neon Ngo
! @since   NeXtMidas 2.8.2
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  invoke javaHome    nxm.sys.lib.Shell.getJavaHome()
  call setDatDir

  call test_getQualifiers
  call test_setDefaultQualifier

  invoke GcontrolExists reg.shell.M.dictionary.isDictionaryEntry(GCONTROL)
  if interAct AND GcontrolExists then
    ! Setup test filenames
    set testfile ^{datDir}.sv1.prm
    invoke testfile testfile.toLowerCase()
    set testfileSlash ^{datDirSlash}/sv1.prm
    invoke testfileSlash testfileSlash.toLowerCase()

    ! Check if running from a jar file - to determine test case output - NXM-3845 since NeXtMidas 4.1.1
    if ^{env.NMROOT} EQS "classpath:" then
      invoke pathToJar nxm.sys.lib.Shell.getPathToOptJar(sys)
      if env.osname eqss "Windows" then
        invoke jarsIndex pathToJar.indexOf(\jars\)
      else
        invoke jarsIndex pathToJar.indexOf("/jars/")
      endif
      res cpath "\"^{pathToJar.substring(0,^{jarsIndex}+6).concat("*")}\""

      ! Check if running in Gradle mode - to determine output for test case 2 - NXM-3757
      set gradleTestResourcesDir ""
      invoke isGradleBuild nxm.sys.lib.Shell.isGradleBuild()
      if isGradleBuild then
        set gradleTestResourcesDir "sys/src/test/resources/"
      endif
      set inTestCase2 "^{pathToJar.substring(0,^{jarsIndex})}/^{gradleTestResourcesDir}^{testfileSlash}"
      set cpTestCase3 cpath
    else
      file name cpath (^env.nmroot,jars) nxm-sys jar
      set inTestCase2 "^{env.nmroot}^{testfileSlash}"
      set cpTestCase3 env.nmroot
    endif

    if env.osname eqss "Windows" then
      invoke cpath       cpath.replace("/",\)
      invoke inTestCase2 inTestCase2.replace("/",\)
      invoke cpTestCase3 cpTestCase3.replace("/",\)
    endif

    invoke vercomp nxm.sys.lib.StringUtil.compareVersions("^env.nmversion","2.9.0")

    ! Add extra JVM argument if Java major version is greater than or equal to 17 - since NeXtMidas 4.1.1
    set jvmArgLogging ""
    invoke javaMajVer nxm.sys.lib.Shell.getJavaMajorVersion()
    if javaMajVer ge 17 then set jvmArgLogging " --add-opens java.logging/java.util.logging=ALL-UNNAMED"

    import "JAVAHOME" javaHomeDir
    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label  macro   "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}

      textdisplay/id=tdisp text=""
      message send tdisp,,"APPENDL",,"In another OS terminal do the following:"
      message send tdisp,,"APPENDL",," "
      message send tdisp,,"APPENDL",,"1. ^{javaHome}^{jvmArgLogging} -cp ^cpath nxm.sys.test.TestBaseFile ^testfile"
      message send tdisp,,"APPENDL",," "

      if vercomp lt 0 then
        ! note: the nicer error message is for version 2.8.2+ in the 2.8.x series
        message send tdisp,,"APPENDL",,"*** Verify that the error message says NMROOT is not defined."
      else ! for version 2.9.0+
        message send tdisp,,"APPENDL",,"*** Verify that the NeXtMidas version info is displayed, followed by the file size, some header info, and some data in the file."
      endif

      message send tdisp,,"APPENDL",," "
      message send tdisp,,"APPENDL",,"2. ^{javaHome}^{jvmArgLogging} -DNMROOT=bogusValue -cp ^cpath nxm.sys.test.TestBaseFile ^inTestCase2"
      message send tdisp,,"APPENDL",," "
      message send tdisp,,"APPENDL",,"*** Verify that the file size is displayed, followed by some header info, and some data in the file."
      message send tdisp,,"APPENDL",,"    NOTE: You should not see the NeXtMidas version & don't worry about the WARN: No commands.cnf found..."

      message send tdisp,,"APPENDL",," "
      message send tdisp,,"APPENDL",,"3. ^{javaHome}^{jvmArgLogging} -DNMROOT=^env.nmroot -cp ^cpTestCase3 nxm.sys.test.TestBaseFile ^testfile"
      message send tdisp,,"APPENDL",," "
      message send tdisp,,"APPENDL",,"*** Verify that the NeXtMidas version info is displayed, followed by the file size, some header info, and some data in the file."

    pipe off
  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: user indicated test" msg.data eqss "PASS"
    pipe stop
  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Test BUG 2497 BaseFile.getQualifiers() should not return null
procedure test_getQualifiers fn:fname
  file open tag ^{datDir}.sinewave.prm
  invoke quals tag.getQualifiers()
  file close tag

  assert/text="^reg.main.name: BaseFile.getQualifiers() should not return null" quals NISNULL

return

!******************************************************************************!
! Test BUG 2518 BaseFile.setDefaultQualifier()
procedure test_setDefaultQualifier
  ! updated in 3.5.1 to use the updated timelinetest Primitive
  set L:expOutType1 2000
  set outfile1 "ram:test_basefile_default_quals"
  nxm.sys.test.timelinetester IN=^{datDir}.apenny.prm OUT=outfile1
  status outfile1 type=outType1
  assert/text="^reg.main.name: BaseFile.setDefaultQualifier() default type (^outType1)" outType1 eq expOutType1
  erase outfile1

  set L:expOutType2 2001
  set outfile2 "ram:test_basefile_default_quals{TYPE=^expOutType2}"
  nxm.sys.test.timelinetester IN=^{datDir}.apenny.prm OUT=outfile2
  status outfile2 type=outType2
  assert/text="^reg.main.name: BaseFile.setDefaultQualifier() user overridden type" outType2 eq expOutType2
  erase outfile2
return
