Path: blob/master/test/jdk/java/nio/file/attribute/UserDefinedFileAttributeView/Basic.java
41155 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 683833325* @summary Unit test for java.nio.file.attribute.UserDefinedFileAttributeView26* @library ../..27* @key randomness28*/2930import java.nio.ByteBuffer;31import java.nio.charset.Charset;32import java.nio.file.*;33import static java.nio.file.LinkOption.*;34import java.nio.file.attribute.*;35import java.util.Arrays;36import java.util.Map;37import java.util.Random;38import java.io.IOException;3940public class Basic {4142private static Random rand = new Random();4344private static final String ATTR_NAME = "mime_type";45private static final String ATTR_VALUE = "text/plain";46private static final String ATTR_VALUE2 = "text/html";4748static interface Task {49void run() throws Exception;50}5152static void tryCatch(Class<? extends Throwable> ex, Task task) {53boolean caught = false;54try {55task.run();56} catch (Throwable x) {57if (ex.isAssignableFrom(x.getClass())) {58caught = true;59} else {60throw new RuntimeException(x);61}62}63if (!caught)64throw new RuntimeException(ex.getName() + " expected");65}6667static void expectNullPointerException(Task task) {68tryCatch(NullPointerException.class, task);69}7071static boolean hasAttribute(UserDefinedFileAttributeView view, String attr)72throws IOException73{74for (String name: view.list()) {75if (name.equals(ATTR_NAME))76return true;77}78return false;79}8081static void test(Path file, LinkOption... options) throws IOException {82final UserDefinedFileAttributeView view =83Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options);84ByteBuffer buf = rand.nextBoolean() ?85ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100);8687// Test: write88buf.put(ATTR_VALUE.getBytes()).flip();89int size = buf.remaining();90int nwrote = view.write(ATTR_NAME, buf);91if (nwrote != size)92throw new RuntimeException("Unexpected number of bytes written");9394// Test: size95if (view.size(ATTR_NAME) != size)96throw new RuntimeException("Unexpected size");9798// Test: read99buf.clear();100int nread = view.read(ATTR_NAME, buf);101if (nread != size)102throw new RuntimeException("Unexpected number of bytes read");103buf.flip();104String value = Charset.defaultCharset().decode(buf).toString();105if (!value.equals(ATTR_VALUE))106throw new RuntimeException("Unexpected attribute value");107108// Test: read with insufficient space109tryCatch(IOException.class, new Task() {110public void run() throws IOException {111view.read(ATTR_NAME, ByteBuffer.allocateDirect(1));112}});113114// Test: replace value115buf.clear();116buf.put(ATTR_VALUE2.getBytes()).flip();117size = buf.remaining();118view.write(ATTR_NAME, buf);119if (view.size(ATTR_NAME) != size)120throw new RuntimeException("Unexpected size");121122// Test: list123if (!hasAttribute(view, ATTR_NAME))124throw new RuntimeException("Attribute name not in list");125126// Test: delete127view.delete(ATTR_NAME);128if (hasAttribute(view, ATTR_NAME))129throw new RuntimeException("Attribute name in list");130131// Test: dynamic access132String name = "user:" + ATTR_NAME;133byte[] valueAsBytes = ATTR_VALUE.getBytes();134Files.setAttribute(file, name, valueAsBytes);135byte[] actualAsBytes = (byte[])Files.getAttribute(file, name);136if (!Arrays.equals(valueAsBytes, actualAsBytes))137throw new RuntimeException("Unexpected attribute value");138Map<String,?> map = Files.readAttributes(file, name);139if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME)))140throw new RuntimeException("Unexpected attribute value");141map = Files.readAttributes(file, "user:*");142if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME)))143throw new RuntimeException("Unexpected attribute value");144}145146static void miscTests(final Path file) throws IOException {147final UserDefinedFileAttributeView view =148Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);149view.write(ATTR_NAME, ByteBuffer.wrap(ATTR_VALUE.getBytes()));150151// NullPointerException152final ByteBuffer buf = ByteBuffer.allocate(100);153154expectNullPointerException(new Task() {155public void run() throws IOException {156view.read(null, buf);157}});158expectNullPointerException(new Task() {159public void run() throws IOException {160view.read(ATTR_NAME, null);161}});162expectNullPointerException(new Task() {163public void run() throws IOException {164view.write(null, buf);165}});166expectNullPointerException(new Task() {167public void run() throws IOException {168view.write(ATTR_NAME, null);169}});170expectNullPointerException(new Task() {171public void run() throws IOException {172view.size(null);173}});174expectNullPointerException(new Task() {175public void run() throws IOException {176view.delete(null);177}});178expectNullPointerException(new Task() {179public void run() throws IOException {180Files.getAttribute(file, null);181}});182expectNullPointerException(new Task() {183public void run() throws IOException {184Files.getAttribute(file, "user:" + ATTR_NAME, (LinkOption[])null);185}});186expectNullPointerException(new Task() {187public void run() throws IOException {188Files.setAttribute(file, "user:" + ATTR_NAME, null);189}});190expectNullPointerException(new Task() {191public void run() throws IOException {192Files.setAttribute(file, null, new byte[0]);193}});194expectNullPointerException(new Task() {195public void run() throws IOException {196Files.setAttribute(file, "user: " + ATTR_NAME, new byte[0], (LinkOption[])null);197}});198expectNullPointerException(new Task() {199public void run() throws IOException {200Files.readAttributes(file, (String)null);201}});202expectNullPointerException(new Task() {203public void run() throws IOException {204Files.readAttributes(file, "*", (LinkOption[])null);205}});206207// Read-only buffer208tryCatch(IllegalArgumentException.class, new Task() {209public void run() throws IOException {210ByteBuffer buf = ByteBuffer.wrap(ATTR_VALUE.getBytes()).asReadOnlyBuffer();211view.write(ATTR_NAME, buf);212buf.flip();213view.read(ATTR_NAME, buf);214}});215216// Zero bytes remaining217tryCatch(IOException.class, new Task() {218public void run() throws IOException {219ByteBuffer buf = buf = ByteBuffer.allocateDirect(100);220buf.position(buf.capacity());221view.read(ATTR_NAME, buf);222}});223}224225public static void main(String[] args) throws IOException {226// create temporary directory to run tests227Path dir = TestUtil.createTemporaryDirectory();228try {229if (!Files.getFileStore(dir).supportsFileAttributeView("user")) {230System.out.println("UserDefinedFileAttributeView not supported - skip test");231return;232}233234// test access to user defined attributes of regular file235Path file = dir.resolve("foo.html");236Files.createFile(file);237try {238test(file);239} finally {240Files.delete(file);241}242243// test access to user defined attributes of directory244Path subdir = dir.resolve("foo");245Files.createDirectory(subdir);246try {247test(subdir);248} finally {249Files.delete(subdir);250}251252// test access to user defined attributes of sym link253if (TestUtil.supportsLinks(dir)) {254Path target = dir.resolve("doesnotexist");255Path link = dir.resolve("link");256Files.createSymbolicLink(link, target);257try {258test(link, NOFOLLOW_LINKS);259} catch (IOException x) {260// access to attributes of sym link may not be supported261} finally {262Files.delete(link);263}264}265266// misc. tests267try {268file = dir.resolve("foo.txt");269Files.createFile(file);270miscTests(dir);271} finally {272Files.delete(file);273}274275} finally {276TestUtil.removeAll(dir);277}278}279}280281282