jdk/test/java/nio/file/DirectoryStream/DriveLetter.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8158 77d9c0f1c19f
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:
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8158
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     4
 *
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     8
 *
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    13
 * accompanied this code).
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    14
 *
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2071
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2071
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2071
diff changeset
    21
 * questions.
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    22
 */
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    23
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    24
/* @test
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    25
 * @bug 6808647
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    26
 * @summary Checks that a DirectoryStream's iterator returns the expected
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    27
 *    path when opening a directory by specifying only the drive letter.
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    28
 * @library ..
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    29
 */
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    30
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    31
import java.nio.file.*;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    32
import java.io.File;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    33
import java.io.IOException;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    34
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    35
public class DriveLetter {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    36
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    37
    public static void main(String[] args) throws IOException {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    38
        String os = System.getProperty("os.name");
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    39
        if (!os.startsWith("Windows")) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    40
            System.out.println("This is Windows specific test");
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    41
            return;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    42
        }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    43
        String here = System.getProperty("user.dir");
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    44
        if (here.length() < 2 || here.charAt(1) != ':')
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    45
            throw new RuntimeException("Unable to determine drive letter");
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    46
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    47
        // create temporary file in current directory
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    48
        File tempFile = File.createTempFile("foo", "tmp", new File(here));
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    49
        try {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    50
            // we should expect C:foo.tmp to be returned by iterator
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    51
            String drive = here.substring(0, 2);
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    52
            Path expected = Paths.get(drive).resolve(tempFile.getName());
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    53
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    54
            boolean found = false;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    55
            Path dir = Paths.get(drive);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    56
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    57
                for (Path file : stream) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    58
                    if (file.equals(expected)) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    59
                        found = true;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    60
                        break;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    61
                    }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    62
                }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    63
            }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    64
            if (!found)
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    65
                throw new RuntimeException("Temporary file not found???");
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    66
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    67
        } finally {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    68
            tempFile.delete();
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    69
        }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    70
    }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents:
diff changeset
    71
}