Basic.java revision 893:f06f30b29f36
1/*
2 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4313887
26 * @summary Unit test for java.nio.file.attribute.UserDefinedFileAttributeView
27 * @library ../..
28 */
29
30import 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;
39
40public class Basic {
41
42    private static Random rand = new Random();
43
44    private static final String ATTR_NAME = "mime_type";
45    private static final String ATTR_VALUE = "text/plain";
46    private static final String ATTR_VALUE2 = "text/html";
47
48    static interface Task {
49        void run() throws Exception;
50    }
51
52    static void tryCatch(Class<? extends Throwable> ex, Task task) {
53        boolean caught = false;
54        try {
55            task.run();
56        } catch (Throwable x) {
57            if (ex.isAssignableFrom(x.getClass())) {
58                caught = true;
59            } else {
60                throw new RuntimeException(x);
61            }
62        }
63        if (!caught)
64            throw new RuntimeException(ex.getName() + " expected");
65    }
66
67    static void expectNullPointerException(Task task) {
68        tryCatch(NullPointerException.class, task);
69    }
70
71    static boolean hasAttribute(UserDefinedFileAttributeView view, String attr)
72        throws IOException
73    {
74        for (String name: view.list()) {
75            if (name.equals(ATTR_NAME))
76                return true;
77        }
78        return false;
79    }
80
81    static void test(Path file, LinkOption... options) throws IOException {
82        final UserDefinedFileAttributeView view = file
83            .getFileAttributeView(UserDefinedFileAttributeView.class, options);
84        ByteBuffer buf = rand.nextBoolean() ?
85            ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100);
86
87        // Test: write
88        buf.put(ATTR_VALUE.getBytes()).flip();
89        int size = buf.remaining();
90        int nwrote = view.write(ATTR_NAME, buf);
91        if (nwrote != size)
92            throw new RuntimeException("Unexpected number of bytes written");
93
94        // Test: size
95        if (view.size(ATTR_NAME) != size)
96            throw new RuntimeException("Unexpected size");
97
98        // Test: read
99        buf.clear();
100        int nread = view.read(ATTR_NAME, buf);
101        if (nread != size)
102            throw new RuntimeException("Unexpected number of bytes read");
103        buf.flip();
104        String value = Charset.defaultCharset().decode(buf).toString();
105        if (!value.equals(ATTR_VALUE))
106            throw new RuntimeException("Unexpected attribute value");
107
108        // Test: read with insufficient space
109        tryCatch(IOException.class, new Task() {
110            public void run() throws IOException {
111                view.read(ATTR_NAME, ByteBuffer.allocateDirect(1));
112            }});
113
114        // Test: replace value
115        buf.clear();
116        buf.put(ATTR_VALUE2.getBytes()).flip();
117        size = buf.remaining();
118        view.write(ATTR_NAME, buf);
119        if (view.size(ATTR_NAME) != size)
120            throw new RuntimeException("Unexpected size");
121
122        // Test: list
123        if (!hasAttribute(view, ATTR_NAME))
124            throw new RuntimeException("Attribute name not in list");
125
126        // Test: delete
127        view.delete(ATTR_NAME);
128        if (hasAttribute(view, ATTR_NAME))
129            throw new RuntimeException("Attribute name in list");
130
131        // Test: dynamic access
132        byte[] valueAsBytes = ATTR_VALUE.getBytes();
133        view.setAttribute(ATTR_NAME, valueAsBytes);
134        byte[] actualAsBytes = (byte[])view.getAttribute(ATTR_NAME);
135        if (!Arrays.equals(valueAsBytes, actualAsBytes))
136            throw new RuntimeException("Unexpected attribute value");
137        Map<String,?> map = view.readAttributes(ATTR_NAME);
138        if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME)))
139            throw new RuntimeException("Unexpected attribute value");
140        map = view.readAttributes(ATTR_NAME, "*");
141        if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME)))
142            throw new RuntimeException("Unexpected attribute value");
143        map = view.readAttributes("DoesNotExist");
144        if (!map.isEmpty())
145            throw new RuntimeException("Map expected to be empty");
146    }
147
148    static void miscTests(Path file) throws IOException {
149        final UserDefinedFileAttributeView view = file
150            .getFileAttributeView(UserDefinedFileAttributeView.class);
151        view.write(ATTR_NAME, ByteBuffer.wrap(ATTR_VALUE.getBytes()));
152
153        // NullPointerException
154        final ByteBuffer buf = ByteBuffer.allocate(100);
155
156        expectNullPointerException(new Task() {
157            public void run() throws IOException {
158                view.read(null, buf);
159            }});
160        expectNullPointerException(new Task() {
161            public void run() throws IOException {
162                view.read(ATTR_NAME, null);
163            }});
164        expectNullPointerException(new Task() {
165            public void run() throws IOException {
166                view.write(null, buf);
167            }});
168        expectNullPointerException(new Task() {
169            public void run() throws IOException {
170               view.write(ATTR_NAME, null);
171            }});
172        expectNullPointerException(new Task() {
173            public void run() throws IOException {
174                view.size(null);
175            }});
176        expectNullPointerException(new Task() {
177            public void run() throws IOException {
178                view.delete(null);
179            }});
180        expectNullPointerException(new Task() {
181            public void run() throws IOException {
182                view.getAttribute(null);
183            }});
184        expectNullPointerException(new Task() {
185            public void run() throws IOException {
186                view.setAttribute(ATTR_NAME, null);
187            }});
188        expectNullPointerException(new Task() {
189            public void run() throws IOException {
190                view.setAttribute(null, new byte[0]);
191            }});
192         expectNullPointerException(new Task() {
193            public void run() throws IOException {
194               view.readAttributes(null);
195            }});
196        expectNullPointerException(new Task() {
197            public void run() throws IOException {
198                view.readAttributes("*", (String[])null);
199            }});
200        expectNullPointerException(new Task() {
201            public void run() throws IOException {
202                view.readAttributes("*", ATTR_NAME, null);
203            }});
204
205        // Read-only buffer
206        tryCatch(IllegalArgumentException.class, new Task() {
207            public void run() throws IOException {
208                ByteBuffer buf = ByteBuffer.wrap(ATTR_VALUE.getBytes()).asReadOnlyBuffer();
209                view.write(ATTR_NAME, buf);
210                buf.flip();
211                view.read(ATTR_NAME, buf);
212            }});
213
214        // Zero bytes remaining
215        tryCatch(IOException.class, new Task() {
216            public void run() throws IOException {
217                ByteBuffer buf = buf = ByteBuffer.allocateDirect(100);
218                buf.position(buf.capacity());
219                view.read(ATTR_NAME, buf);
220            }});
221    }
222
223    public static void main(String[] args) throws IOException {
224        // create temporary directory to run tests
225        Path dir = TestUtil.createTemporaryDirectory();
226        try {
227            if (!dir.getFileStore().supportsFileAttributeView("xattr")) {
228                System.out.println("UserDefinedFileAttributeView not supported - skip test");
229                return;
230            }
231
232            // test access to user defined attributes of regular file
233            Path file = dir.resolve("foo.html").createFile();
234            try {
235                test(file);
236            } finally {
237                file.delete();
238            }
239
240            // test access to user define attributes of directory
241            file = dir.resolve("foo").createDirectory();
242            try {
243                test(file);
244            } finally {
245                file.delete();
246            }
247
248            // test access to user defined attributes of sym link
249            if (TestUtil.supportsLinks(dir)) {
250                Path target = dir.resolve("doesnotexist");
251                Path link = dir.resolve("link").createSymbolicLink(target);
252                try {
253                    test(link, NOFOLLOW_LINKS);
254                } catch (IOException x) {
255                    // access to attributes of sym link may not be supported
256                } finally {
257                    link.delete();
258                }
259            }
260
261            // misc. tests
262            try {
263                file = dir.resolve("foo.txt").createFile();
264                miscTests(dir);
265            } finally {
266                file.delete();
267            }
268
269        } finally {
270            TestUtil.removeAll(dir);
271        }
272    }
273 }
274