package ucar.nc2;

import junit.framework.*;
import junit.extensions.TestSetup;

import java.util.List;
import java.util.Properties;

import ucar.unidata.io.RandomAccessFile;

/**
 * TestSuite that runs all nj22 unit tests.
 *
 */
public class TestAll {

  public static long startTime;

  static {
    Properties env = System.getProperties();
    String path = env.getProperty( "unidata.upc.share.path" );
    if ( path == null )     
    {
      System.out.println( "**No \"unidata.upc.share.path\"property, using \"/upc/share/\"." );
      path = "/upc/share/";
    }
    if ((! path.endsWith( "/")) && ! path.endsWith( "\\"))
    {
      path = path + "/";
    }
    upcShareDir = path;
  }

  public static String upcShareDir = "R:/";
  public static String upcShareTestDataDir = upcShareDir + "testdata/";

  public static String cdmTestDataDir = "./src/test/data/";
  // public static String testdataDir = "/upc/share/testdata/";

  public static String temporaryDataDir = "./target/test/tmp/";

  public static junit.framework.Test suite ( ) {
    RandomAccessFile.setDebugLeaks( true);

    TestSuite suite= new TestSuite();
    suite.addTest( ucar.nc2.units.TestUnitsAll.suite());
    suite.addTest( ucar.nc2.TestNC2.suite());

    suite.addTest( ucar.ma2.TestMA2.suite());
    suite.addTest( ucar.nc2.TestH5.suite()); //
    suite.addTest( ucar.nc2.TestIosp.suite());   //

    suite.addTest( ucar.nc2.dataset.TestDataset.suite());  //
    suite.addTest( ucar.nc2.ncml.TestNcML.suite());  // */

    suite.addTest( ucar.nc2.dt.grid.TestGrid.suite()); //
    suite.addTest( ucar.nc2.dt.TestTypedDatasets.suite());

    suite.addTest( ucar.unidata.geoloc.TestGeoloc.suite());  //
    suite.addTest( ucar.nc2.dods.TestDODS.suite()); // */

    suite.addTest( thredds.catalog.TestCatalogAll.suite()); // */

    TestSetup wrapper = new TestSetup(suite) {

      protected void setUp() {
        //NetcdfFileCache.init();
        //NetcdfDatasetCache.init();
        RandomAccessFile.setDebugLeaks(true);
        startTime = System.currentTimeMillis();
      }

      protected void tearDown() {
        checkLeaks();
        //NetcdfFileCache.clearCache( true);
        //NetcdfDatasetCache.clearCache( true);
        checkLeaks();

        double took = (System.currentTimeMillis() - startTime) * .001;
        System.out.println(" that took= "+took+" secs");
      }
    };

    return wrapper;
  }

  static private void checkLeaks() {
    System.out.println("RandomAccessFile still open");
    List openFiles = RandomAccessFile.openFiles;
    for (int i = 0; i < openFiles.size(); i++) {
      String o = (String) openFiles.get(i);
      System.out.println(" open= " + o);
    }
  }

  static public boolean closeEnough( double d1, double d2) {
    if (d1 < 1.0e-5) return Math.abs(d1-d2) < 1.0e-5;
    return Math.abs((d1-d2)/d1) < 1.0e-5;
  }

  static public boolean closeEnough( float d1, float d2) {
    if (d1 < 1.0e-5) return Math.abs(d1-d2) < 1.0e-5;
    return Math.abs((d1-d2)/d1) < 1.0e-5;
  }

}
@@ -125,16 +125,97 @@
 perties;
+%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -381,111 +381,1077 @@
     
-Properties env = System.getProperties(
+// Determine how /upc/share is mounted by reading system or THREDDS property.%0A    String upcSharePropName = %22unidata.upc.share.path%22;%0A    String threddsPropFileName = %22thredds.properties%22;%0A%0A    // Get system property%0A    String path = System.getProperty( upcSharePropName 
 );%0A    
-String
+if (
  path =
- env.getProperty( %22unidata.upc.share.path%22 );
+= null )%0A    %7B%0A      // Get user property.%0A      File userHomeDirFile = new File( System.getProperty( %22user.home%22 ) );%0A      File userThreddsPropsFile = new File( userHomeDirFile, threddsPropFileName );%0A      if ( userThreddsPropsFile.exists() && userThreddsPropsFile.canRead() )%0A      %7B%0A        Properties userThreddsProps = new Properties();%0A        try%0A        %7B%0A          userThreddsProps.load( new FileInputStream( userThreddsPropsFile ) );%0A        %7D%0A        catch ( IOException e )%0A        %7B%0A          System.out.println( %22**Failed loading user THREDDS property file: %22 + e.getMessage() );%0A        %7D%0A        if ( userThreddsProps != null && ! userThreddsProps.isEmpty() )%0A        %7B%0A          path = userThreddsProps.getProperty( upcSharePropName );%0A        %7D%0A      %7D%0A    %7D%0A
 %0A   
@@ -1474,19 +1474,41 @@
 ll )
+%0A
     
- 
+%7B
 %0A    
-%7B
+  // Get default path.
 %0A   
@@ -1576,13 +1576,21 @@
 ty, 
-using
+defaulting to
  %5C%22/
@@ -1637,24 +1637,65 @@
 re/%22;%0A    %7D%0A
+    // Make sure path ends with a slash.%0A
     if ((! p
@@ -1845,16 +1845,8 @@
 eDir
- = %22R:/%22
 ;%0A  

Clean up use of "/upc/share/testdata", "thredds/cdm/src/test/data", and a temporary directory (now "thredds/cdm/target/test/tmp").