Author: tchemit Date: 2012-07-25 12:17:39 +0200 (Wed, 25 Jul 2012) New Revision: 2374 Url: http://nuiton.org/repositories/revision/nuiton-utils/2374 Log: refs #2206: Some tests are not windows OS compiliant Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/StringUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java trunk/nuiton-utils/src/test/java/org/nuiton/util/ResourceTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/StringUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/StringUtil.java 2012-07-17 13:55:41 UTC (rev 2373) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/StringUtil.java 2012-07-25 10:17:39 UTC (rev 2374) @@ -25,6 +25,8 @@ package org.nuiton.util; +import org.apache.commons.lang3.SystemUtils; + import java.awt.Color; import java.lang.reflect.Field; import java.security.MessageDigest; @@ -754,4 +756,18 @@ return result; } + + /** + * + * @return the file separator escaped for a regex regarding the os used. + */ + public static String getFileSeparatorRegex() { + String result; + if(SystemUtils.IS_OS_WINDOWS) { + result = "\\\\"; + } else { + result = "/"; + } + return result; + } } Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java 2012-07-17 13:55:41 UTC (rev 2373) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/FileUtilTest.java 2012-07-25 10:17:39 UTC (rev 2374) @@ -26,9 +26,8 @@ package org.nuiton.util; import org.apache.commons.io.FileUtils; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TestName; import java.io.File; import java.io.IOException; @@ -41,13 +40,12 @@ * Created: 22 nov. 2004 * * @author Benjamin Poussin <poussin@codelutin.com> - * @version $Revision$ - * <p/> - * Mise a jour: $Date$ - * par : */ public class FileUtilTest { // FileUtilTest + @Rule + public final TestName testName = new TestName(); + @Test public void testFind() throws Exception { List<File> result = FileUtil.find(new File("."), ".*FileUtil.*", true); @@ -148,6 +146,8 @@ public void testSedSingleFile() throws IOException { //FIXME tchemit 20100924 Do tests in target, not in /tmp please! + ResourceTest.assumeNotUnderWindows(getClass(), testName); + // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test"); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); @@ -185,6 +185,8 @@ //FIXME tchemit 20100924 Do tests in target, not in /tmp please! + ResourceTest.assumeNotUnderWindows(getClass(), testName); + // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test"); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); @@ -215,6 +217,8 @@ @Test public void testSedComment() throws IOException { + ResourceTest.assumeNotUnderWindows(getClass(), testName); + // try to not make sed in real src dir ;) File testDirectory = FileUtil.createTempDirectory("sed", "test", parent); FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/ResourceTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/ResourceTest.java 2012-07-17 13:55:41 UTC (rev 2373) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/ResourceTest.java 2012-07-25 10:17:39 UTC (rev 2374) @@ -25,13 +25,11 @@ package org.nuiton.util; +import org.apache.commons.lang3.SystemUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TestName; import java.io.File; import java.lang.reflect.Field; @@ -45,10 +43,7 @@ * Test class for {@link Resource}. * * @author chatellier - * @version $Revision$ - * <p/> - * Last update : $Date$ - * By : $Author$ + * @author tchemit <chemit@codelutin.com> */ public class ResourceTest { // ResourceTest @@ -57,15 +52,19 @@ @Test public void testaddDefaultClassLoader() throws Exception { - Assert.assertNull(ResourceTest.class.getResource("/bin/java")); + String javaExecFilename = getJavaExecName(); + Assert.assertNull(ResourceTest.class.getResource("/bin/" + javaExecFilename)); + File repository = new File(System.getProperty("java.home")); Resource.addDefaultClassLoader(repository.toURI().toURL()); - File result = new File(repository, "bin" + File.separator + "java"); + //FIXME-tchemit-2012-07-23 On windows os java does not eixsts but java.exe does + File result = new File(repository, "bin" + File.separator + javaExecFilename); + Assert.assertTrue(result.exists()); - URL resultURL = Resource.getURL("bin/java"); + URL resultURL = Resource.getURL("bin/" + javaExecFilename); Assert.assertEquals(result.toURI().toURL(), resultURL); } @@ -73,24 +72,29 @@ public void testGetURL() throws Exception { URL url; + String javaExecFilename = getJavaExecName(); + url = Resource.getURL("README.txt"); Assert.assertNotNull(url); try { - Resource.getURL("bin/java"); + Resource.getURL("bin/" + javaExecFilename); Assert.fail(); } catch (ResourceNotFoundException e) { Assert.assertTrue(true); } File repository = new File(System.getProperty("java.home")); Resource.addDefaultClassLoader(repository.toURI().toURL()); - url = Resource.getURL("bin/java"); + url = Resource.getURL("bin/" + javaExecFilename); Assert.assertNotNull(url); } @Test public void testGetURLsFromDirectory() throws Exception { + + assumeNotUnderWindows(getClass(), testName); + List<URL> list = new ArrayList<URL>(); File repository = new File(System.getProperty("java.home")); @@ -148,11 +152,14 @@ log.info(Arrays.asList(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs())); } + String javaExecFilename = getJavaExecName(); + String fileSeparatorRegex = StringUtil.getFileSeparatorRegex(); File repository = new File(System.getProperty("java.home")); Resource.addDefaultClassLoader(repository.toURI().toURL()); List<URL> result; - result = Resource.getURLs(".*bin/java"); + // comes + result = Resource.getURLs(".*bin" + fileSeparatorRegex + javaExecFilename); Assert.assertNotNull(result); Assert.assertEquals(1, result.size()); @@ -193,6 +200,9 @@ */ @Test public void testGetResourcesJarClassPath() throws Exception { + + assumeNotUnderWindows(getClass(), testName); + // ce test peut echoué a chaque changement dans les dépendances List<URL> urlsPattern = Resource.getResources("org/nuiton/util/Version.*Test\\.class"); List<URL> urlsClass1 = Resource.getResources("org/nuiton/util/VersionTest.class"); @@ -225,6 +235,9 @@ */ @Test public void testGetTestResourcesClassPath() throws Exception { + + assumeNotUnderWindows(getClass(), testName); + URL url1; URL url2; url1 = getClass().getResource("/org/nuiton/util/fileUtilData.txt"); @@ -249,4 +262,24 @@ Assert.assertTrue(urls.contains(url2)); } + protected String getJavaExecName() { + String result; + if (SystemUtils.IS_OS_WINDOWS) { + result = "java.exe"; + } else { + result = "java"; + } + return result; + } + + @Rule + public final TestName testName = new TestName(); + + public static void assumeNotUnderWindows(Class<?> testClass, TestName testName) { + if (SystemUtils.IS_OS_WINDOWS) { + log.warn("This test " + testClass.getName() + "#" + testName.getMethodName() + + " is still not compatible with windows OS"); + Assume.assumeTrue(false); + } + } } // ResourceTest