jdk/test/java/nio/file/DirectoryStream/Basic.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8580 d1585a92e376
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8580
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4058
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4058
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4058
diff changeset
    21
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    22
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    23
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
/* @test
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    25
 * @bug 4313887 6838333
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
 * @summary Unit test for java.nio.file.DirectoryStream
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
 * @library ..
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
import java.nio.file.*;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    31
import static java.nio.file.Files.*;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
import java.util.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.io.IOException;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
public class Basic {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
    static boolean found;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
    static void doTest(final Path dir) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
        DirectoryStream<Path> stream;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
        // test that directory is empty
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    42
        try (DirectoryStream<Path> ds = newDirectoryStream(dir)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    43
            if (ds.iterator().hasNext())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
                throw new RuntimeException("directory not empty");
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    45
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    46
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
        // create file in directory
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
        final Path foo = Paths.get("foo");
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    49
        createFile(dir.resolve(foo));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    51
        // iterate over directory and check there is one entry
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    52
        stream = newDirectoryStream(dir);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    53
        found = false;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    54
        try {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    55
            for (Path entry: stream) {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    56
                if (entry.getFileName().equals(foo)) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    57
                    if (found)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    58
                        throw new RuntimeException("entry already found");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
                    found = true;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
                } else {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    61
                    throw new RuntimeException("entry " + entry.getFileName() +
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    62
                        " not expected");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
            }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    65
        } finally {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    66
            stream.close();
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    67
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    68
        if (!found)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    69
            throw new RuntimeException("entry not found");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    70
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    71
        // check filtering: f* should match foo
8580
d1585a92e376 7025468: Tests using diamond with anonymous inner classes needs to be reverted
alanb
parents: 8158
diff changeset
    72
        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    73
            private PathMatcher matcher =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
                dir.getFileSystem().getPathMatcher("glob:f*");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
            public boolean accept(Path file) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
                return matcher.matches(file);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    77
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
        };
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    79
        try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    80
            for (Path entry: ds) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    81
                if (!entry.getFileName().equals(foo))
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    82
                    throw new RuntimeException("entry not expected");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    83
            }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    84
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    85
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    86
        // check filtering: z* should not match any files
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    87
        filter = new DirectoryStream.Filter<Path>() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    88
            private PathMatcher matcher =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    89
                dir.getFileSystem().getPathMatcher("glob:z*");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
            public boolean accept(Path file) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
                return matcher.matches(file);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    92
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    93
        };
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    94
        try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    95
            if (ds.iterator().hasNext())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    96
                throw new RuntimeException("no matching entries expected");
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    97
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    98
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
    99
        // check that an IOException thrown by a filter is propagated
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   100
        filter = new DirectoryStream.Filter<Path>() {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   101
            public boolean accept(Path file) throws IOException {
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   102
                throw new java.util.zip.ZipException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   103
            }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   104
        };
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   105
        stream = newDirectoryStream(dir, filter);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   106
        try {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   107
            stream.iterator().hasNext();
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   108
            throw new RuntimeException("DirectoryIteratorException expected");
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   109
        } catch (DirectoryIteratorException x) {
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   110
            IOException cause = x.getCause();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   111
            if (!(cause instanceof java.util.zip.ZipException))
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   112
                throw new RuntimeException("Expected IOException not propagated");
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   113
        } finally {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   114
            stream.close();
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   115
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   116
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   117
        // check that exception or error thrown by filter is not thrown
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   118
        // by newDirectoryStream or iterator method.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   119
        stream = newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   120
            public boolean accept(Path file) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   121
                throw new RuntimeException("Should not be visible");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   122
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   123
        });
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   124
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   125
            stream.iterator();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   126
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   127
            stream.close();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   128
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   129
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   130
        // test NotDirectoryException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   131
        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   132
            newDirectoryStream(dir.resolve(foo));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   133
            throw new RuntimeException("NotDirectoryException not thrown");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   134
        } catch (NotDirectoryException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   135
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   136
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   137
        // test UnsupportedOperationException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   138
        stream = newDirectoryStream(dir);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   139
        Iterator<Path> i = stream.iterator();
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   140
        i.next();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   141
        try {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   142
            i.remove();
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   143
            throw new RuntimeException("UnsupportedOperationException expected");
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   144
        } catch (UnsupportedOperationException uoe) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   145
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   146
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   147
        // test IllegalStateException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   148
        stream = newDirectoryStream(dir);
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   149
        stream.iterator();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   150
        try {
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   151
            // attempt to obtain second iterator
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   152
            stream.iterator();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   153
            throw new RuntimeException("IllegalStateException not thrown as expected");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   154
        } catch (IllegalStateException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   155
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   156
        stream.close();
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   157
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   158
        stream = newDirectoryStream(dir);
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   159
        stream.close();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
        try {
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   161
            // attempt to obtain iterator after stream is closed
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   162
            stream.iterator();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   163
            throw new RuntimeException("IllegalStateException not thrown as expected");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   164
        } catch (IllegalStateException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   165
        }
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   166
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   167
        // test that iterator reads to end of stream when closed
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   168
        stream = newDirectoryStream(dir);
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   169
        i = stream.iterator();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   170
        stream.close();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   171
        while (i.hasNext())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   172
            i.next();
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   173
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   174
        stream = newDirectoryStream(dir);
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   175
        i = stream.iterator();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   176
        stream.close();
4058
21c6bd5381ca 6879463: (file) DirectoryStream#iterator's remove method throws wrong exception when stream is closed
alanb
parents: 3065
diff changeset
   177
        try {
6537
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   178
            for (;;) i.next();
7aa4e7bb5dae 6881498: (file) Re-examine DirectoryStream exception handling
alanb
parents: 5506
diff changeset
   179
        } catch (NoSuchElementException expected) { }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   180
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   181
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   182
    public static void main(String[] args) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   183
        Path dir = TestUtil.createTemporaryDirectory();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   184
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   185
            doTest(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   186
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   187
            TestUtil.removeAll(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   188
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   189
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   190
}