/*- * Copyright 2015 Diamond Light Source Ltd. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ package org.eclipse.dawnsci.nexus; import java.io.File; import org.eclipse.dawnsci.hdf5.nexus.NexusFileFactoryHDF5; import org.eclipse.dawnsci.hdf5.nexus.NexusFileHDF5; public class TestUtils { /** * Prefix to folder in which test files are to be generated */ public static final String OUTPUT_FOLDER_PREFIX = "test-scratch/"; /** * Helper function to (recursively) delete a directory and all its contents * * @param dir * - path to directory to delete * @return - boolean True if directory no longer exists */ static boolean deleteDir(File dir) { if (!dir.exists()) { return true; } if (dir.isDirectory()) { String[] children = dir.list(); for (String element : children) { boolean success = deleteDir(new File(dir, element)); if (!success) { System.out.println("1. deleteDir could not delete: " + new File(dir, element)); return false; } } } if (dir.delete()){ return true; } System.out.println("2. deleteDir could not delete: " + dir +". Make sure each test creates a uniquely named folder. Otherwise stale NFS locks may prevent folder deletion."); return false; } /** * Generates a (relative) directory name based on a class name. Uses the appropriate separators for the platform. * * @param classname * - the name of the class on which to base the directory name (a value something like * "gda.analysis.io.JPEGTest"). * @return - the derived directory name. */ public static String generateDirectorynameFromClassname(String classname) { // the generated directory name is usable on both Linux and Windows // (which uses \ as a separator). return OUTPUT_FOLDER_PREFIX + classname.replace('.', '/') + '/'; // File tmp = new File(OUTPUT_FOLDER_PREFIX + classname.replace('.', // '/') + '/'); // return tmp.getAbsolutePath(); } /** * Creates an empty directory for use by test code. If the directory exists (from a previous test), the directory * and all its contents are first deleted. * * @param testScratchDirectoryname * - the name of the directory to create. * @throws Exception */ public static void makeScratchDirectory(String testScratchDirectoryname) throws Exception { // delete any remains from a previous run of this test if (!deleteDir(new File(testScratchDirectoryname))) { throw new Exception("Unable to delete old test scratch directory " + testScratchDirectoryname); } // set up for a new run of this test if (!((new File(testScratchDirectoryname)).mkdirs())) { throw new Exception("Unable to create new test scratch directory " + testScratchDirectoryname); } } }