r1920 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: tchemit Date: 2010-09-25 01:58:04 +0200 (Sat, 25 Sep 2010) New Revision: 1920 Url: http://nuiton.org/repositories/revision/nuiton-utils/1920 Log: add getBaseDir in TestUil add more methods to FileUtil Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java trunk/src/main/java/org/nuiton/util/TestUtil.java trunk/src/test/java/org/nuiton/util/FileUtilTest.java Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-09-23 19:31:22 UTC (rev 1919) +++ trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-09-24 23:58:04 UTC (rev 1920) @@ -643,6 +643,74 @@ return result; } + /** + * Recupère le fichier dans le même répertoire que le fichier donné et dont + * on a changé l'extension. + * + * @param file le fichier d'origine + * @param newExtension la nouvelle extension à utiliser + * @param extchars la liste des extensions possibles + * @return le fichier dont on a changé l'extension + * @throws IOException si aucune extension trouvé dans le fichier d'origine + * @since 1.4.2 + */ + static public File changeExtension(File file, + String newExtension, + String... extchars) throws IOException { + String name = file.getName(); + String newName = changeExtension(name, newExtension, extchars); + File newFile = new File(file.getParentFile(), newName); + return newFile; + } + + /** + * Change l'extension du fichier en entrée avec la nouvelle extension + * + * @param name le nom de fichier à transformer + * @param newExtension la nouvelle extension à utiliser + * @param extchars la liste des extensions possibles + * @return le nouveau nom de fichier + * @throws IOException si aucune extension trouvé dans le fichier d'origine + * @since 1.4.2 + */ + static public String changeExtension(String name, + String newExtension, + String... extchars) throws IOException { + String extension = extension(name, extchars); + if (extension == null) { + throw new IOException("Could not find extension for name " + name + " within " + Arrays.toString(extchars)); + } + String nameWithoutExtension = name.substring(0, name.length() - extension.length()); + String newName = nameWithoutExtension + newExtension; + return newName; + } + + /** + * Recupère le fichier mirroir du fichier {@code file} donnée qui est dans + * l'arborescence de {@code inputDirectory} dans le répertoire + * {@code ouputDirectory}. + * + * @param inputDirectory le répertoire de départ + * @param outputDirectory le répertoire cible + * @param file le fichier + * @return le fichier mirroir dans le répertoire cible + * @since 1.4.2 + */ + static public File getRelativeFile(File inputDirectory, + File outputDirectory, + File file) { + String inputPath = inputDirectory.getAbsolutePath(); + String s = file.getAbsolutePath(); + int index = s.indexOf(inputPath); + if (index == -1) { + throw new IllegalArgumentException( + "File " + file + " is not in " + inputDirectory); + } + String relativePath = s.substring(inputPath.length()); + File result = new File(outputDirectory, relativePath); + return result; + } + public interface FileAction { boolean doAction(File f); } Modified: trunk/src/main/java/org/nuiton/util/TestUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/TestUtil.java 2010-09-23 19:31:22 UTC (rev 1919) +++ trunk/src/main/java/org/nuiton/util/TestUtil.java 2010-09-24 23:58:04 UTC (rev 1920) @@ -90,4 +90,15 @@ return result; } + + public static File getBaseDir() { + // Search basedir from maven environment + String basedirPath = System.getenv("basedir"); + if (basedirPath == null) { + basedirPath = new File("").getAbsolutePath(); + } + + File result = new File(basedirPath); + return result; + } } Modified: trunk/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2010-09-23 19:31:22 UTC (rev 1919) +++ trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2010-09-24 23:58:04 UTC (rev 1920) @@ -31,6 +31,7 @@ import java.util.Map; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; /** @@ -130,6 +131,8 @@ @Test public void testFileToByteToFile() throws Exception { + //FIXME tchemit 20100924 Do tests in target, not in /tmp please! + String content = "testFileToByteToFile"; File file = FileUtil.getTempFile(content); @@ -193,7 +196,8 @@ */ @Test public void testSedSingleFile() throws IOException { - + //FIXME tchemit 20100924 Do tests in target, not in /tmp please! + // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test"); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); @@ -229,6 +233,8 @@ @Test public void testSedMultiple() throws IOException { + //FIXME tchemit 20100924 Do tests in target, not in /tmp please! + // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test"); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); @@ -259,6 +265,7 @@ @Test public void testSedComment() throws IOException { + //FIXME tchemit 20100924 Do tests in target, not in /tmp please! // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test"); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); @@ -278,4 +285,39 @@ FileUtil.deleteRecursively(testDirectory); } + static File parent; + + @BeforeClass + public static void beforeTest() { + parent = TestUtil.createTestsDataDirectory("FileUtil"); + } + + public void testChangeExtension() throws IOException { + String name = "toto.yo"; + String exepectedName = "toto.ya"; + String newExtension = "ya"; + String actualName = FileUtil.changeExtension(name, newExtension); + + Assert.assertEquals(exepectedName, actualName); + + File file = new File(parent, name); + File expectedFile = new File(parent, exepectedName); + File actualFile = FileUtil.changeExtension(file, newExtension); + Assert.assertEquals(expectedFile, actualFile); + } + + public void testGetRelativeFile() { + File inputDirectory = new File(parent, "in"); + File outputDirectory = new File(parent, "out"); + File file = new File(inputDirectory, "yoyo.to"); + File expectedFile = new File(outputDirectory, file.getName()); + File actualFile = FileUtil.getRelativeFile(inputDirectory, outputDirectory, file); + Assert.assertEquals(expectedFile, actualFile); + + file = new File(inputDirectory, "rep" + File.separator + "yoyo.to"); + expectedFile = new File(outputDirectory, "rep" + File.separator + file.getName()); + actualFile = FileUtil.getRelativeFile(inputDirectory, outputDirectory, file); + Assert.assertEquals(expectedFile, actualFile); + } + } // FileUtilTest
participants (1)
-
tchemit@users.nuiton.org