!******************************************************************************!
! Test case description/purpose: test macro for classpath IOResource
! 
! A new resource-type prefix ("classpath") has been created to be used in loading
! the current Midas Context's ClassLoader to access files in the classpath.
! 
! NOTE: We are testing the following classpath:
!       Verify that new IO resource is register and available
!       classpath:nxm/sys/dat/apenny.prm
!       classpath:nxm/sys/version.txt
!       classpath:nxm/sys/test/junit/lib/TableText.java
!       classpath:java/lang/System.class 
!
! @bug  NXM-2901 Need to specify file in class path
! @author:  Juan J Velasquez
! @since 3.7.1
!******************************************************************************!
startmacro
  call setDatDir

  call test_reg_handles_resource
  call test_classpath_prm
  call test_classpath_txt
  if ^{env.NMROOT} EQS "classpath:" then
    info "^reg.main.name: Skipping test_classpath_test_java since running from a jar file &
         (can't read java source files from inside a jar file)."
  else
    call test_classpath_test_java
  endif
  call test_classpath_java_class
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
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

!******************************************************************************!
! 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 if new CLASSPATH resource is available
procedure test_reg_handles_resource
  assert/text="^reg.main.name: Test if reg.handlers.resource.classpath" & 
  ^{reg.handlers.resource.CLASSPATH} EQS nxm.sys.lib.ClassPathResource  
return

!******************************************************************************!
! Tests reading from a PRM file
procedure test_classpath_prm
  assert/text="^{reg.main.name}: Test reading from a PRM file." &
  "classpath:^{datDirSlash}/apenny.prm" FEQ ^{datDir}.apenny.prm
  
  status classpath:^{datDirSlash}/apenny.prm size=pSize
  assert/text="^reg.main.name:test reading a PRM file size: PRM.size" pSize GT 0  
return

!******************************************************************************!
! Tests reading from a TXT file
procedure test_classpath_txt
  res txtFile "classpath:nxm/sys/version.txt"
  assert/text="^reg.main.name: Testing if TXT file exist " ^{txtFile} fexits 

  status ^{txtFile} size=textSize
  assert/text="^reg.main.name:test reading a TXT file size: TEXTFILE.size" textSize GT 0  
return

!******************************************************************************!
! Tests reading from JAVA file
procedure test_classpath_test_java
  assert/text="^reg.main.name: Test reading from a test java file." &
     "classpath:nxm/sys/test/junit/lib/TableTest.java" FEQ nxm.sys.test.junit.lib.TableTest.java

  status classpath:nxm/sys/test/junit/lib/TableTest.java size=tSize
  assert/text="^reg.main.name: Test reading a java file size: JAVA.size" tSize GT 0  
return

!******************************************************************************!
! Tests if JAR/class file exists
procedure test_classpath_java_class
  res clsname "classpath:java/lang/System.class"
  assert/text="^reg.main.name: Testing if jar/class file exist " ^{clsname} fexits 
return