test/java/nio/file/attribute/UserDefinedFileAttributeView/Basic.java

Print this page

        

*** 77,88 **** } return false; } static void test(Path file, LinkOption... options) throws IOException { ! final UserDefinedFileAttributeView view = file ! .getFileAttributeView(UserDefinedFileAttributeView.class, options); ByteBuffer buf = rand.nextBoolean() ? ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100); // Test: write buf.put(ATTR_VALUE.getBytes()).flip(); --- 77,88 ---- } return false; } static void test(Path file, LinkOption... options) throws IOException { ! final UserDefinedFileAttributeView view = ! Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options); ByteBuffer buf = rand.nextBoolean() ? ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100); // Test: write buf.put(ATTR_VALUE.getBytes()).flip();
*** 129,156 **** throw new RuntimeException("Attribute name in list"); // Test: dynamic access String name = "user:" + ATTR_NAME; byte[] valueAsBytes = ATTR_VALUE.getBytes(); ! file.setAttribute(name, valueAsBytes); ! byte[] actualAsBytes = (byte[])file.getAttribute(name); if (!Arrays.equals(valueAsBytes, actualAsBytes)) throw new RuntimeException("Unexpected attribute value"); ! Map<String,?> map = file.readAttributes(name); if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) throw new RuntimeException("Unexpected attribute value"); ! map = file.readAttributes("user:*"); if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) throw new RuntimeException("Unexpected attribute value"); ! map = file.readAttributes("user:DoesNotExist"); if (!map.isEmpty()) throw new RuntimeException("Map expected to be empty"); } static void miscTests(final Path file) throws IOException { ! final UserDefinedFileAttributeView view = file ! .getFileAttributeView(UserDefinedFileAttributeView.class); view.write(ATTR_NAME, ByteBuffer.wrap(ATTR_VALUE.getBytes())); // NullPointerException final ByteBuffer buf = ByteBuffer.allocate(100); --- 129,156 ---- throw new RuntimeException("Attribute name in list"); // Test: dynamic access String name = "user:" + ATTR_NAME; byte[] valueAsBytes = ATTR_VALUE.getBytes(); ! Files.setAttribute(file, name, valueAsBytes); ! byte[] actualAsBytes = (byte[])Files.getAttribute(file, name); if (!Arrays.equals(valueAsBytes, actualAsBytes)) throw new RuntimeException("Unexpected attribute value"); ! Map<String,?> map = Files.readAttributes(file, name); if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) throw new RuntimeException("Unexpected attribute value"); ! map = Files.readAttributes(file, "user:*"); if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) throw new RuntimeException("Unexpected attribute value"); ! map = Files.readAttributes(file, "user:DoesNotExist"); if (!map.isEmpty()) throw new RuntimeException("Map expected to be empty"); } static void miscTests(final Path file) throws IOException { ! final UserDefinedFileAttributeView view = ! Files.getFileAttributeView(file, UserDefinedFileAttributeView.class); view.write(ATTR_NAME, ByteBuffer.wrap(ATTR_VALUE.getBytes())); // NullPointerException final ByteBuffer buf = ByteBuffer.allocate(100);
*** 178,212 **** public void run() throws IOException { view.delete(null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.getAttribute(null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.getAttribute("user:" + ATTR_NAME, (LinkOption[])null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.setAttribute("user:" + ATTR_NAME, null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.setAttribute(null, new byte[0]); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.setAttribute("user: " + ATTR_NAME, new byte[0], (LinkOption[])null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.readAttributes((String)null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! file.readAttributes("*", (LinkOption[])null); }}); // Read-only buffer tryCatch(IllegalArgumentException.class, new Task() { public void run() throws IOException { --- 178,212 ---- public void run() throws IOException { view.delete(null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.getAttribute(file, null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.getAttribute(file, "user:" + ATTR_NAME, (LinkOption[])null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.setAttribute(file, "user:" + ATTR_NAME, null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.setAttribute(file, null, new byte[0]); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.setAttribute(file, "user: " + ATTR_NAME, new byte[0], (LinkOption[])null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.readAttributes(file, (String)null); }}); expectNullPointerException(new Task() { public void run() throws IOException { ! Files.readAttributes(file, "*", (LinkOption[])null); }}); // Read-only buffer tryCatch(IllegalArgumentException.class, new Task() { public void run() throws IOException {
*** 227,276 **** public static void main(String[] args) throws IOException { // create temporary directory to run tests Path dir = TestUtil.createTemporaryDirectory(); try { ! if (!dir.getFileStore().supportsFileAttributeView("user")) { System.out.println("UserDefinedFileAttributeView not supported - skip test"); return; } // test access to user defined attributes of regular file ! Path file = dir.resolve("foo.html").createFile(); try { test(file); } finally { ! file.delete(); } ! // test access to user define attributes of directory ! file = dir.resolve("foo").createDirectory(); try { ! test(file); } finally { ! file.delete(); } // test access to user defined attributes of sym link if (TestUtil.supportsLinks(dir)) { Path target = dir.resolve("doesnotexist"); ! Path link = dir.resolve("link").createSymbolicLink(target); try { test(link, NOFOLLOW_LINKS); } catch (IOException x) { // access to attributes of sym link may not be supported } finally { ! link.delete(); } } // misc. tests try { ! file = dir.resolve("foo.txt").createFile(); miscTests(dir); } finally { ! file.delete(); } } finally { TestUtil.removeAll(dir); } --- 227,280 ---- public static void main(String[] args) throws IOException { // create temporary directory to run tests Path dir = TestUtil.createTemporaryDirectory(); try { ! if (!Files.getFileStore(dir).supportsFileAttributeView("user")) { System.out.println("UserDefinedFileAttributeView not supported - skip test"); return; } // test access to user defined attributes of regular file ! Path file = dir.resolve("foo.html"); ! Files.createFile(file); try { test(file); } finally { ! Files.delete(file); } ! // test access to user defined attributes of directory ! Path subdir = dir.resolve("foo"); ! Files.createDirectory(subdir); try { ! test(subdir); } finally { ! Files.delete(subdir); } // test access to user defined attributes of sym link if (TestUtil.supportsLinks(dir)) { Path target = dir.resolve("doesnotexist"); ! Path link = dir.resolve("link"); ! Files.createSymbolicLink(link, target); try { test(link, NOFOLLOW_LINKS); } catch (IOException x) { // access to attributes of sym link may not be supported } finally { ! Files.delete(link); } } // misc. tests try { ! file = dir.resolve("foo.txt"); ! Files.createFile(file); miscTests(dir); } finally { ! Files.delete(file); } } finally { TestUtil.removeAll(dir); }