8264400: (fs) WindowsFileStore equality depends on how the FileStore was constructed

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2021-04-09 15:41:52 +00:00
parent 6de0bb204a
commit cc54de76ca
2 changed files with 38 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,17 @@
package sun.nio.fs;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.FileStore;
import java.nio.file.FileSystemException;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileStoreAttributeView;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.io.IOException;
import java.util.Locale;
import static sun.nio.fs.WindowsConstants.*;
import static sun.nio.fs.WindowsNativeDispatcher.*;
@ -44,6 +52,8 @@ class WindowsFileStore
private final int volType;
private final String displayName; // returned by toString
private int hashCode;
private WindowsFileStore(String root) throws WindowsException {
assert root.charAt(root.length()-1) == '\\';
this.root = root;
@ -229,15 +239,22 @@ class WindowsFileStore
public boolean equals(Object ob) {
if (ob == this)
return true;
if (!(ob instanceof WindowsFileStore))
return false;
WindowsFileStore other = (WindowsFileStore)ob;
return root.equals(other.root);
if (ob instanceof WindowsFileStore other) {
if (root.equals(other.root))
return true;
if (volType == DRIVE_FIXED && other.volumeType() == DRIVE_FIXED)
return root.equalsIgnoreCase(other.root);
}
return false;
}
@Override
public int hashCode() {
return root.hashCode();
if (hashCode == 0) { // Don't care about race
hashCode = (volType == DRIVE_FIXED) ?
root.toLowerCase(Locale.ROOT).hashCode() : root.hashCode();
}
return hashCode;
}
@Override
@ -251,4 +268,4 @@ class WindowsFileStore
sb.append(")");
return sb.toString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,7 +22,7 @@
*/
/* @test
* @bug 4313887 6873621 6979526 7006126 7020517
* @bug 4313887 6873621 6979526 7006126 7020517 8264400
* @summary Unit test for java.nio.file.FileStore
* @key intermittent
* @library .. /test/lib
@ -36,9 +36,9 @@ import java.nio.file.attribute.*;
import java.io.File;
import java.io.IOException;
import jdk.test.lib.Platform;
import jdk.test.lib.util.FileUtils;
public class Basic {
static final long G = 1024L * 1024L * 1024L;
@ -80,6 +80,15 @@ public class Basic {
assertTrue(store2.equals(store1));
assertTrue(store1.hashCode() == store2.hashCode());
if (Platform.isWindows()) {
/**
* Test: FileStore.equals() should not be case sensitive
*/
FileStore upper = Files.getFileStore(Path.of("C:\\"));
FileStore lower = Files.getFileStore(Path.of("c:\\"));
assertTrue(lower.equals(upper));
}
/**
* Test: File and FileStore attributes
*/