Make test more suitable for Windows

This commit is contained in:
Roman Shevchenko
2012-07-23 13:21:22 +02:00
parent 948233e1bd
commit 89c7aa083d
3 changed files with 59 additions and 21 deletions
@@ -44,6 +44,19 @@ public class FileSystemUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.io.FileSystemUtil");
private interface Mediator {
@Nullable
FileAttributes getAttributes(@NotNull String path) throws Exception;
@Nullable
String resolveSymLink(@NotNull String path) throws Exception;
void setPermissions(@NotNull String path, int permissions) throws Exception;
@NotNull
String getName();
}
@NotNull
private static Mediator ourMediator = getMediator();
@@ -51,16 +64,16 @@ public class FileSystemUtil {
final boolean forceUseNio2 = SystemProperties.getBooleanProperty(FORCE_USE_NIO2_KEY, false);
final String quickTestPath = SystemInfo.isWindows ? "C:\\" : "/";
// todo: move IdeaWin32 interface to this package, add mediator
if (!forceUseNio2 && (SystemInfo.isLinux || SystemInfo.isMac || SystemInfo.isSolaris || SystemInfo.isFreeBSD)) {
try {
final Mediator mediator = new JnaUnixMediatorImpl();
mediator.getAttributes(quickTestPath);
return mediator;
}
catch (Throwable t) {
LOG.warn(t);
if (!forceUseNio2) {
if (SystemInfo.isLinux || SystemInfo.isMac || SystemInfo.isSolaris || SystemInfo.isFreeBSD) {
try {
final Mediator mediator = new JnaUnixMediatorImpl();
mediator.getAttributes(quickTestPath);
return mediator;
}
catch (Throwable t) {
LOG.warn(t);
}
}
}
@@ -84,6 +97,11 @@ public class FileSystemUtil {
ourMediator = getMediator();
}
@TestOnly
static String getMediatorName() {
return ourMediator.getName();
}
private FileSystemUtil() { }
@Nullable
@@ -161,15 +179,6 @@ public class FileSystemUtil {
setPermissions(file.getAbsolutePath(), permissions);
}
private interface Mediator {
@Nullable
FileAttributes getAttributes(@NotNull String path) throws Exception;
@Nullable
String resolveSymLink(@NotNull String path) throws Exception;
void setPermissions(@NotNull String path, int permissions) throws Exception;
}
// todo[r.sh] remove reflection after migration to JDK 7
@SuppressWarnings("OctalInteger")
@@ -267,6 +276,12 @@ public class FileSystemUtil {
}
}
@NotNull
@Override
public String getName() {
return "NIO2";
}
private static final Map<String, Integer> ATTRIBUTES_MAP;
static {
ATTRIBUTES_MAP = new HashMap<String, Integer>();
@@ -314,6 +329,7 @@ public class FileSystemUtil {
}
}
// thanks to SVNKit for the idea
private static class JnaUnixMediatorImpl implements Mediator {
@SuppressWarnings("OctalInteger")
@@ -390,8 +406,15 @@ public class FileSystemUtil {
public void setPermissions(@NotNull final String path, final int permissions) throws Exception {
myLibC.chmod(path, permissions & LibC.PERM_MASK);
}
@NotNull
@Override
public String getName() {
return "JnaUnix";
}
}
private static class StandardMediatorImpl implements Mediator {
// from java.io.FileSystem
private static final int BA_REGULAR = 0x02;
@@ -452,5 +475,11 @@ public class FileSystemUtil {
@Override
public void setPermissions(@NotNull final String path, final int permissions) throws Exception {
}
@NotNull
@Override
public String getName() {
return "fallback";
}
}
}
@@ -20,6 +20,7 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;
public class FileAttributesNio2ReadingTest extends FileAttributesReadingTest {
@@ -27,6 +28,7 @@ public class FileAttributesNio2ReadingTest extends FileAttributesReadingTest {
public static void setUpClass() throws Exception {
System.setProperty(FileSystemUtil.FORCE_USE_NIO2_KEY, "true");
FileSystemUtil.resetMediator();
assertEquals("NIO2", FileSystemUtil.getMediatorName());
}
@AfterClass
@@ -19,6 +19,7 @@ import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
@@ -30,6 +31,12 @@ public class FileAttributesReadingTest {
private final byte[] myTestData = new byte[]{'t', 'e', 's', 't'};
private File myTempDirectory;
@BeforeClass
public static void checkMediator() throws Exception {
final String expectedName = SystemInfo.isWindows ? "IdeaWin32" : "JnaUnix";
assertEquals(expectedName, FileSystemUtil.getMediatorName());
}
@Before
public void setUp() throws Exception {
myTempDirectory = FileUtil.createTempDirectory(getClass().getName(), ".tmp");
@@ -116,7 +123,7 @@ public class FileAttributesReadingTest {
@Test
public void linkToDirectory() throws Exception {
final File file = FileUtil.createTempDirectory(myTempDirectory, "test.", ".tmp");
assertTrue(file.setWritable(false, false));
if (SystemInfo.isUnix) assertTrue(file.setWritable(false, false));
assertTrue(file.setLastModified(file.lastModified() - 5000));
final File link = IoTestUtil.createTempLink(file.getPath(), new File(myTempDirectory, "link").getPath());
@@ -124,7 +131,7 @@ public class FileAttributesReadingTest {
assertEquals(FileAttributes.DIRECTORY | FileAttributes.SYM_LINK, attributes.type);
assertEquals(file.length(), attributes.length);
assertTimestampEquals(file.lastModified(), attributes.lastModified);
assertFalse(attributes.isWritable());
if (SystemInfo.isUnix) assertFalse(attributes.isWritable());
final String target = FileSystemUtil.resolveSymLink(link);
assertEquals(file.getPath(), target);