Utils { public static String computeFileRelativeLocation( File rootDirectory, File subFile ) { String rootPath = rootDirectory.getAbsolutePath(); String subPath = subFile.getAbsolutePath(); if( ! subPath.startsWith( rootPath )) throw new IllegalArgumentException( "The sub-file must be contained in the directory." ); if( rootDirectory.equals( subFile )) throw new IllegalArgumentException( "The sub-file must be different than the directory." ); return subPath.substring( rootPath.length() + 1 ).replace( '\\', '/' ); } private  Utils(); static boolean isEmptyOrWhitespaces( String s ); static String capitalize( String s ); static String removeFileExtension( String filename ); static List<String> splitNicely( String toSplit, String separator ); static List<String> splitNicelyWithPattern( String toSplit, String patternSeparator ); static List<String> filterEmptyValues( List<String> values ); static String format( Collection<String> items, String separator ); static String expandTemplate(String s, Properties params); static void closeQuietly( InputStream in ); static void closeQuietly( OutputStream out ); static void closeQuietly( Reader reader ); static void closeQuietly( Writer writer ); static void copyStreamUnsafelyUseWithCaution( InputStream in, OutputStream os ); static void copyStreamSafely( InputStream in, ByteArrayOutputStream os ); static void copyStream( InputStream in, File outputFile ); static void copyStream( File inputFile, File outputFile ); static void copyStream( File inputFile, OutputStream os ); static void writeStringInto( String s, File outputFile ); static void appendStringInto( String s, File outputFile ); static String readFileContent( File file ); static String readFileContentQuietly( File file, Logger logger ); static String readUrlContent( String url ); static String readUrlContentQuietly( String url, Logger logger ); static Properties readPropertiesFile( File file ); static Properties readPropertiesFileQuietly( File file, Logger logger ); static Properties readPropertiesQuietly( String fileContent, Logger logger ); static void writePropertiesFile( Properties properties, File file ); static String cleanNameWithAccents( String name ); static void createDirectory( File directory ); static List<File> listAllFiles( File directory ); static String updateProperties( String propertiesContent, Map<String,String> keyToNewValue ); static List<File> listAllFiles( File directory, String fileExtension ); static List<File> listAllFiles( File directory, boolean includeDirectories ); static List<File> listDirectFiles( File directory, String fileExtension ); static List<File> listDirectories( File root ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory, List<String> exclusionPatteners ); static Map<String,String> storeDirectoryResourcesAsString( File directory ); static String computeFileRelativeLocation( File rootDirectory, File subFile ); static void extractZipArchive( File zipFile, File targetDirectory ); static void extractZipArchive( File zipFile, File targetDirectory, String entryPattern, String removedEntryPrefix ); static boolean isAncestor( File ancestorCandidate, File file ); static void deleteFilesRecursively( File... files ); static void deleteFilesRecursivelyAndQuietly( File... files ); static String writeExceptionButDoNotUseItForLogging( Throwable t ); static void logException( Logger logger, Level logLevel, Throwable t, String message ); static void logException( Logger logger, Level logLevel, Throwable t ); static void logException( Logger logger, Throwable t ); static void closeStatement( PreparedStatement ps, Logger logger ); static void closeStatement( Statement st, Logger logger ); static void closeResultSet( ResultSet resultSet, Logger logger ); static void closeConnection( Connection conn, Logger logger ); static boolean isAncestorFile( File potentialAncestor, File file ); static void copyDirectory( File source, File target ); static Map.Entry<String,Integer> findUrlAndPort( String url ); static String getValue(Map<String,String> map, String key, String defaultValue);  }
@Test( expected = IllegalArgumentException.class ) public void testComputeFileRelativeLocation_failure_notASubFile() { final File rootDir = new File( System.getProperty( "java.io.tmpdir" ), "does-not-exist"); Utils.computeFileRelativeLocation( rootDir, new File( "invalid-path" )); }
@Test( expected = IllegalArgumentException.class ) public void testComputeFileRelativeLocation_failure_sameFile() { final File rootDir = new File( System.getProperty( "java.io.tmpdir" )); Utils.computeFileRelativeLocation( rootDir, rootDir ); }
@Test public void testComputeFileRelativeLocation_success() { final File rootDir = new File( System.getProperty( "java.io.tmpdir" )); File directChildFile = new File( rootDir, "woo.txt" ); Assert.assertEquals( directChildFile.getName(), Utils.computeFileRelativeLocation( rootDir, directChildFile )); String indirectChildPath = "dir1/dir2/script.sh"; File indirectChildFile = new File( rootDir, indirectChildPath ); Assert.assertEquals( indirectChildPath, Utils.computeFileRelativeLocation( rootDir, indirectChildFile )); }