!******************************************************************************!
! Testing the createTarFile(...) method in TarFile.java
!
!
! @bug      <2979> <TarFile: Add ability to create TAR files>
! @author:  Peter Quan
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  call setDatDir

  pipe on
    call test_call_create_tar_with_files
  pipe off

endmacro

!******************************************************************************!
! 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

!******************************************************************************!
procedure test_call_create_tar_with_files

  ! Construct new java objects
  new nxm.sys.lib.BaseFile() baseFile
  new nxm.sys.lib.TarFile()  tarFile

  ! Get file names
  invoke fileNamePenny  baseFile.getFileNameFor(this.M, ^{datDir}.apenny.prm)
  invoke fileNameAlaska baseFile.getFileNameFor(this.M, ^{datDir}.alaska.prm) 

  ! Create an instance of a USTAR header
  new nxm.sys.lib.TarFile$TarEntryWithData("apenny.prm",this.M,fileNamePenny)  tarEntryWithDataPenny
  new nxm.sys.lib.TarFile$TarEntryWithData("alaska.prm",this.M,fileNameAlaska) tarEntryWithDataAlaska

  ! Test createTarFile(..) with data in arrayList
  new java.util.ArrayList() arrayList
  invoke arrayList.add(tarEntryWithDataPenny)
  invoke arrayList.add(tarEntryWithDataAlaska)
  invoke createTarFile tarFile.createTarFile(this.M,"createdTarFile^{env.pid}.tar", arrayList)

  ! Open Tar File, Get tar entries, and Parse tar entries
  file OPEN labelTarFile createdTarFile^{env.pid}.tar{AUX=1}{FG=TAR}
  new java.lang.String(labelTarFile.getEntries(0).toString()) tarEntryPennyStr
  new java.lang.String(labelTarFile.getEntries(1).toString()) tarEntryAlaskaStr
  invoke apennyParsedStr tarEntryPennyStr.substring(52, 62)
  invoke alaskaParsedStr tarEntryAlaskaStr.substring(53, 63)

  ! Create String to prepend and Get URL for createdTarFile^{env.pid}.tar
  new java.lang.String("tar:") prependTarStr
  invoke createTarFileUrl labelTarFile.getURL()
  invoke createTarFileLocalFilename nxm.sys.lib.FileUtil.toLocalFileName(createTarFileUrl)

  ! Construct file location strings
  invoke tarPrepended prependTarStr.concat(createTarFileLocalFilename.toString())
  invoke apennyAppendedTarFile tarPrepended.concat("!apenny.prm")
  invoke alaskaAppendedTarFile tarPrepended.concat("!alaska.prm")

  assert/text="^reg.main.name: checking tared file name" apennyParsedStr eqss "apenny.prm"
  assert/text="^reg.main.name: checking tared file name" alaskaParsedStr eqss "alaska.prm"
  assert/text="^reg.main.name: FEQ/B check on tared file" apennyAppendedTarFile FEQ/B ^{datDir}.apenny.prm
  assert/text="^reg.main.name: FEQ/B check on tared file" alaskaAppendedTarFile FEQ/B ^{datDir}.alaska.prm
  file CLOSE labelTarFile



  ! Test createTarFile with empty arrayList
  new java.util.ArrayList() arrayListEmpty
  invoke createTarFileEmpty tarFile.createTarFile(this.M,"createdTarFileEmpty^{env.pid}.tar", arrayListEmpty)
  file OPEN labelTarFileEmpty createdTarFileEmpty^{env.pid}.tar{AUX=1}{FG=TAR}
  assert/text="^reg.main.name: numberOfEntries=1024 (empty tar file test)" labelTarFileEmpty.getSize() EQ 1024

  ! Get URL for createTarFileEmpty^{env.pid}.tar
  invoke createTarFileEmptyUrl labelTarFileEmpty.getURL()
  invoke createTarFileEmptyLocalFilename nxm.sys.lib.FileUtil.toLocalFileName(createTarFileEmptyUrl)
  file CLOSE labelTarFileEmpty



  set OperatingSystem ENV GET OSNAME
  ! Test untaring the file -> tar xvf <file> ! 
  if baseline AND OperatingSystem.OSNAME SEQS "Linux" then
    os tar -xvf ^{createTarFileLocalFilename}
    ! Get file locations for untared files
    file OPEN untarApenny apenny.prm{AUX=cwd}
    file OPEN untarAlaska alaska.prm{AUX=cwd}
    invoke untarApennyURL untarApenny.getURL()
    invoke untarAlaskaURL untarAlaska.getURL()
    invoke untarApennyLocalFileName nxm.sys.lib.FileUtil.toLocalFileName(untarApennyURL)
    invoke untarAlaskaLocalFileName nxm.sys.lib.FileUtil.toLocalFileName(untarAlaskaURL)

    ! Parse untared files
    invoke untarApennyParsedStr  untarApennyLocalFileName.substring(10,20)
    invoke untarAlaskaParsedStr  untarAlaskaLocalFileName.substring(10,20)

    assert/text="^reg.main.name: checking untared file name" untarApennyParsedStr  eqss "apenny.prm"
    assert/text="^reg.main.name: checking untared file name" untarAlaskaParsedStr  eqss "alaska.prm"
    assert/text="^reg.main.name: FEQ/B check on untared file" untarApennyLocalFileName FEQ/B ^{datDir}.apenny.prm
    assert/text="^reg.main.name: FEQ/B check on untared file" untarAlaskaLocalFileName FEQ/B ^{datDir}.alaska.prm

    file CLOSE untarApenny
    file CLOSE untarAlaska

    say ""
    info "Deleting the two untared files that were created in aux.cwd: ^{untarApennyLocalFileName} and ^{untarAlaskaLocalFileName}"
    os rm ^{untarApennyLocalFileName}
    os rm ^{untarAlaskaLocalFileName}
  endif



  ! Remove tar files from aux.1
  say ""
  info "Deleting the two tar files that were created in aux.1: ^{createTarFileLocalFilename} and ^{createTarFileEmptyLocalFilename}
  os rm ^{createTarFileLocalFilename}
  os rm ^{createTarFileEmptyLocalFilename}

return

!******************************************************************************!

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    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
