< prev index next >

test/jdk/java/nio/file/Files/Misc.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,11 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 4313887 6838333 8005566 8032220
+ * @bug 4313887 6838333 8005566 8032220 8202285
  * @summary Unit test for miscellenous methods in java.nio.file.Files
  * @library ..
  */
 
 import java.nio.file.*;

@@ -32,18 +32,22 @@
 import static java.nio.file.LinkOption.*;
 import java.nio.file.attribute.*;
 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();
         try {
             testCreateDirectories(dir);
             testIsHidden(dir);
             testIsSameFile(dir);
+            testIsSameContent(dir);
             testFileTypeMethods(dir);
             testAccessMethods(dir);
         } finally {
              TestUtil.removeAll(dir);
         }

@@ -186,10 +190,107 @@
             throw new RuntimeException("NullPointerException expected");
         } catch (NullPointerException ignore) { }
     }
 
     /**
+     * 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 {
         assertTrue(!isRegularFile(tmpdir));
         assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));
< prev index next >