Author: tchemit Date: 2013-02-26 08:51:48 +0100 (Tue, 26 Feb 2013) New Revision: 2514 Url: http://nuiton.org/projects/nuiton-utils/repository/revisions/2514 Log: fixes #2548: At FileUtil#getTestSpecificDirectory method to get a directory to execute write test data Removed: trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/ApplicationConfigTest.java trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java 2013-02-26 07:22:43 UTC (rev 2513) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java 2013-02-26 07:51:48 UTC (rev 2514) @@ -37,6 +37,8 @@ package org.nuiton.util; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -1265,4 +1267,40 @@ return result; } + /** + * Obtain a directory and creates it if required to place some test data. + * <p/> + * The directory will be : + * <p/> + * <pre> + * java.io.tmpdir/testclassName.fqn/methodName[/classifier]/timestamp + * </pre> + * + * @param testClassName test class name + * @param methodName method name + * @param classifier optional classifier + * @param timestamp timestamp + * @return the computed and created if required directory. + * @since 2.6.10 + */ + public static File getTestSpecificDirectory(Class<?> testClassName, + String methodName, + String classifier, + long timestamp) { + File tempDirFile = SystemUtils.getJavaIoTmpDir(); + + // create the directory to store database data + String dataBasePath = testClassName.getName() + + File.separator // a directory with the test class name + + methodName; // a sub-directory with the method name + + if (StringUtils.isNotBlank(classifier)) { + dataBasePath += classifier; + } + dataBasePath += '_' + + timestamp; // and a timestamp + File databaseFile = new File(tempDirFile, dataBasePath); + return databaseFile; + } + } // FileUtil Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/ApplicationConfigTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2013-02-26 07:22:43 UTC (rev 2513) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2013-02-26 07:51:48 UTC (rev 2514) @@ -83,8 +83,8 @@ @BeforeClass public static void setUpClass() { - // Initialize DIR_TESTS_DATA to target/surefire-data - DIR_TESTS_DATA = FileUtil.getFileFromFQN(TestHelper.createDefaultTestsDataDirectory(), ApplicationConfigTest.class.getName() + "." + System.nanoTime()); + // Initialize test data directory + DIR_TESTS_DATA = FileUtil.getTestSpecificDirectory(ApplicationConfigTest.class, "", "", System.nanoTime()); oldHome = SystemUtils.getUserHome().getAbsolutePath(); } Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java 2013-02-26 07:22:43 UTC (rev 2513) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java 2013-02-26 07:51:48 UTC (rev 2514) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2004 - 2010 CodeLutin + * Copyright (C) 2004 - 2010 CodeLutin, Tony Chemit * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -26,7 +26,10 @@ package org.nuiton.util; import org.apache.commons.io.FileUtils; -import org.junit.*; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; import org.junit.rules.TestName; import java.io.File; @@ -40,12 +43,26 @@ * Created: 22 nov. 2004 * * @author Benjamin Poussin <poussin@codelutin.com> + * @author tchemit <chemit@codelutin.com> */ public class FileUtilTest { // FileUtilTest + public static final long TIMESTAMP = System.nanoTime(); + @Rule public final TestName testName = new TestName(); + protected File parent; + + @Before + public void setUp() throws Exception { + + parent = FileUtil.getTestSpecificDirectory( + getClass(), + testName.getMethodName(), null, TIMESTAMP); + + } + @Test public void testFind() throws Exception { List<File> result = FileUtil.find(new File("."), ".*FileUtil.*", true); @@ -238,14 +255,6 @@ FileUtils.deleteDirectory(testDirectory); } - static File parent; - - @BeforeClass - public static void beforeTest() { - parent = TestHelper.createTestsDataDirectory( - "surefire-workdir" + File.separator + "FileUtil"); - } - public void testChangeExtension() throws IOException { String name = "toto.yo"; String exepectedName = "toto.ya"; @@ -276,17 +285,15 @@ @Test public void testGetFileFromPaths() { - File rootDirectory = TestHelper.getBaseDir(); - File actual = FileUtil.getFileFromPaths(rootDirectory, "target", "surefire-workdir", "FileUtil"); - File expected = new File(rootDirectory, "target" + File.separator + "surefire-workdir" + File.separator + "FileUtil"); + File actual = FileUtil.getFileFromPaths(parent, "target", "surefire-workdir", "FileUtil"); + File expected = new File(parent, "target" + File.separator + "surefire-workdir" + File.separator + "FileUtil"); Assert.assertEquals(expected, actual); } @Test public void testGetFileFromFQN() { - File rootDirectory = TestHelper.getBaseDir(); - File actual = FileUtil.getFileFromFQN(rootDirectory, "target.surefire-workdir.FileUtil"); - File expected = new File(rootDirectory, "target" + File.separator + "surefire-workdir" + File.separator + "FileUtil"); + File actual = FileUtil.getFileFromFQN(parent, "target.surefire-workdir.FileUtil"); + File expected = new File(parent, "target" + File.separator + "surefire-workdir" + File.separator + "FileUtil"); Assert.assertEquals(expected, actual); } Deleted: trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java 2013-02-26 07:22:43 UTC (rev 2513) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java 2013-02-26 07:51:48 UTC (rev 2514) @@ -1,102 +0,0 @@ -/* - * #%L - * Nuiton Utils :: Nuiton Utils - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2011 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.util; - -import org.apache.commons.logging.*; -import org.apache.commons.logging.Log; -import org.junit.Ignore; - -import java.io.File; -import java.io.IOException; - -/** - * Usefeull methods to do tests. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ -@Ignore -public class TestHelper { - - private static Log log = LogFactory.getLog(TestHelper.class); - - public static final String TESTS_DATA_DEFAULT_DIR = "surefire-workdir"; - - /** - * Create a directory in target directory for maven projects. The - * directory name will be the default one defined by {@link #TESTS_DATA_DEFAULT_DIR} - * - * @return the directory created - * @see #createTestsDataDirectory(String) - */ - public static File createDefaultTestsDataDirectory() { - return createTestsDataDirectory(TESTS_DATA_DEFAULT_DIR); - } - - /** - * Create a directory in target directory for maven projects. The - * directory will be called {@code dirName}. This directory will be useful - * to put all data created during tests. - * - * @param dirName name of the directory to create - * @return the directory created - */ - public static File createTestsDataDirectory(String dirName) { - // Search basedir from maven environment - String basedirPath = System.getenv("basedir"); - if (basedirPath == null) { - basedirPath = new File("").getAbsolutePath(); - } - - File result = new File(basedirPath, "target" + File.separator + dirName); - - try { - FileUtil.createDirectoryIfNecessary(result); - } catch (IOException eee) { - String errorMessage = "Error during tests data directory creation"; - if (log.isErrorEnabled()) { - log.error(errorMessage, eee); - } - throw new Error(errorMessage, eee); - } - - if (log.isDebugEnabled()) { - log.debug("Create tests data directory : " + result.getAbsolutePath()); - } - - 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; - } -}