--- old/test/jdk/java/nio/file/Files/Misc.java 2018-05-09 18:55:06.715773719 -0700 +++ new/test/jdk/java/nio/file/Files/Misc.java 2018-05-09 18:55:06.366741159 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2018, 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 6838333 8005566 8032220 + * @bug 4313887 6838333 8005566 8032220 8202285 * @summary Unit test for miscellenous methods in java.nio.file.Files * @library .. */ @@ -34,7 +34,10 @@ import java.io.IOException; import java.util.*; + public class Misc { + // used for random byte content + private static Random RAND = new Random(); public static void main(String[] args) throws IOException { Path dir = TestUtil.createTemporaryDirectory(); @@ -42,6 +45,7 @@ testCreateDirectories(dir); testIsHidden(dir); testIsSameFile(dir); + testIsSameContent(dir); testFileTypeMethods(dir); testAccessMethods(dir); } finally { @@ -188,6 +192,103 @@ } /** + * Tests isSameContent. The isSameContent is an extension to isSameFile, the + * test therefore is a copy of the testIsSameFile until the last section that + * compares the content. + */ + static void testIsSameContent(Path tmpdir) throws IOException { + Path thisFile = tmpdir.resolve("thisFile1"); + Path thatFile = tmpdir.resolve("thatFile1"); + + /** + * Test: isSameContent for self + */ + assertTrue(isSameContent(thisFile, thisFile)); + + /** + * Test: Neither files exist + */ + try { + isSameContent(thisFile, thatFile); + throw new RuntimeException("IOException not thrown"); + } catch (IOException x) { + } + try { + isSameContent(thatFile, thisFile); + throw new RuntimeException("IOException not thrown"); + } catch (IOException x) { + } + + createFile(thisFile); + try { + /** + * Test: One file exists + */ + try { + isSameContent(thisFile, thatFile); + throw new RuntimeException("IOException not thrown"); + } catch (IOException x) { + } + try { + isSameContent(thatFile, thisFile); + throw new RuntimeException("IOException not thrown"); + } catch (IOException x) { + } + + /** + * Test: Both file exists + */ + createFile(thatFile); + try { + assertTrue(!isSameContent(thisFile, thatFile)); + assertTrue(!isSameContent(thatFile, thisFile)); + } finally { + delete(thatFile); + } + + /** + * Test: Symbolic links + */ + if (TestUtil.supportsLinks(tmpdir)) { + createSymbolicLink(thatFile, thisFile); + try { + assertTrue(isSameContent(thisFile, thatFile)); + assertTrue(isSameContent(thatFile, thisFile)); + } finally { + TestUtil.deleteUnchecked(thatFile); + } + } + } finally { + delete(thisFile); + } + + // nulls + try { + isSameContent(thisFile, null); + throw new RuntimeException("NullPointerException expected"); + } catch (NullPointerException ignore) { } + try { + isSameContent(null, thatFile); + throw new RuntimeException("NullPointerException expected"); + } catch (NullPointerException ignore) { } + + // compares the contents + byte[] bytes = genBytes(512); + Files.write(thisFile, bytes, StandardOpenOption.CREATE); + Files.write(thatFile, bytes, StandardOpenOption.CREATE); + assertTrue(isSameContent(thisFile, thatFile)); + } + + /** + * Returns a byte[] of the given size with random content + */ + static private byte[] genBytes(int size) { + byte[] arr = new byte[size]; + RAND.nextBytes(arr); + return arr; + } + + /** * Exercise isRegularFile, isDirectory, isSymbolicLink */ static void testFileTypeMethods(Path tmpdir) throws IOException {