jdk/test/java/nio/file/DirectoryStream/Filters.java
changeset 2057 3acf8e5e2ca0
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     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.DirectoryStreamFilters
       
    27  * @library ..
       
    28  */
       
    29 
       
    30 import java.nio.file.*;
       
    31 import static java.nio.file.DirectoryStreamFilters.*;
       
    32 import java.nio.file.attribute.Attributes;
       
    33 import java.io.*;
       
    34 import java.util.*;
       
    35 
       
    36 public class Filters {
       
    37     static final Random rand = new Random();
       
    38 
       
    39     // returns a filter that only accepts files that are larger than a given size
       
    40     static DirectoryStream.Filter<FileRef> newMinimumSizeFilter(final long min) {
       
    41         return new DirectoryStream.Filter<FileRef>() {
       
    42             public boolean accept(FileRef file) {
       
    43                 try {
       
    44                     long size = Attributes.readBasicFileAttributes(file).size();
       
    45                     return size >= min;
       
    46                 } catch (IOException e) {
       
    47                     throw new IOError(e);
       
    48                 }
       
    49             }
       
    50         };
       
    51     }
       
    52 
       
    53     // returns a filter that only accepts files that are matched by a given glob
       
    54     static DirectoryStream.Filter<Path> newGlobFilter(final String glob) {
       
    55         return new DirectoryStream.Filter<Path>() {
       
    56             PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:"+ glob);
       
    57             public boolean accept(Path file) {
       
    58                 return matcher.matches(file.getName());
       
    59             }
       
    60         };
       
    61     }
       
    62 
       
    63     static final int BIG_FILE_THRESHOLD = 8192;
       
    64 
       
    65     static int totalCount;
       
    66     static int htmlCount;
       
    67     static int bigAndHtmlCount;
       
    68     static int bigOrHtmlCount;
       
    69 
       
    70     // generates random files in the test directory and initializes the counts
       
    71     static void setup(Path dir) throws IOException {
       
    72         // create 10-26 files.
       
    73         totalCount = 10 + rand.nextInt(17);
       
    74         char firstChar = 'A';
       
    75         for (int i=0; i<totalCount; i++) {
       
    76             boolean isHtml = rand.nextBoolean();
       
    77             boolean isBig = rand.nextBoolean();
       
    78             if (isHtml) {
       
    79                 htmlCount++;
       
    80                 if (isBig) bigAndHtmlCount++;
       
    81             }
       
    82             if (isHtml || isBig)
       
    83                 bigOrHtmlCount++;
       
    84             String name;
       
    85             if (isHtml) {
       
    86                 name = firstChar + ".html";
       
    87             } else {
       
    88                 name = firstChar + ".tmp";
       
    89             }
       
    90             firstChar++;
       
    91             int size = rand.nextInt(BIG_FILE_THRESHOLD);
       
    92             if (isBig)
       
    93                 size += BIG_FILE_THRESHOLD;
       
    94             Path file = dir.resolve(name);
       
    95             OutputStream out = file.newOutputStream();
       
    96             try {
       
    97                 if (size > 0)
       
    98                     out.write(new byte[size]);
       
    99             } finally {
       
   100                 out.close();
       
   101             }
       
   102             System.out.format("Created %s, size %d byte(s)\n", name, size);
       
   103         }
       
   104     }
       
   105 
       
   106     static boolean isHtml(Path file) {
       
   107         return file.toString().endsWith(".html");
       
   108     }
       
   109 
       
   110     static boolean isBig(Path file) throws IOException {
       
   111         long size = Attributes.readBasicFileAttributes(file).size();
       
   112         return size >= BIG_FILE_THRESHOLD;
       
   113     }
       
   114 
       
   115     static void checkCount(int expected, int actual) {
       
   116         if (actual != expected)
       
   117             throw new RuntimeException("'" + expected +
       
   118                 "' entries expected, actual: " + actual);
       
   119     }
       
   120 
       
   121     static void doTests(Path dir) throws IOException {
       
   122         final List<DirectoryStream.Filter<Path>> emptyList = Collections.emptyList();
       
   123 
       
   124         // list containing two filters
       
   125         List<DirectoryStream.Filter<? super Path>> filters =
       
   126             new ArrayList<DirectoryStream.Filter<? super Path>>();
       
   127         filters.add(newMinimumSizeFilter(BIG_FILE_THRESHOLD));
       
   128         filters.add(newGlobFilter("*.html"));
       
   129 
       
   130         int accepted;
       
   131         DirectoryStream<Path> stream;
       
   132 
       
   133         System.out.println("Test: newContentTypeFilter");
       
   134         accepted = 0;
       
   135         stream = dir.newDirectoryStream(newContentTypeFilter("text/html"));
       
   136         try {
       
   137             for (Path entry: stream) {
       
   138                 if (!isHtml(entry))
       
   139                     throw new RuntimeException("html file expected");
       
   140                 accepted++;
       
   141             }
       
   142         } finally {
       
   143             stream.close();
       
   144         }
       
   145         checkCount(htmlCount, accepted);
       
   146 
       
   147         System.out.println("Test: allOf with list of filters");
       
   148         accepted = 0;
       
   149         stream = dir.newDirectoryStream(allOf(filters));
       
   150         try {
       
   151             for (Path entry: stream) {
       
   152                 if (!isHtml(entry))
       
   153                     throw new RuntimeException("html file expected");
       
   154                 if (!isBig(entry))
       
   155                     throw new RuntimeException("big file expected");
       
   156                 accepted++;
       
   157             }
       
   158         } finally {
       
   159             stream.close();
       
   160         }
       
   161         checkCount(bigAndHtmlCount, accepted);
       
   162 
       
   163         System.out.println("Test: allOf with empty list");
       
   164         accepted = 0;
       
   165         stream = dir.newDirectoryStream(allOf(emptyList));
       
   166         try {
       
   167             for (Path entry: stream) {
       
   168                 accepted++;
       
   169             }
       
   170         } finally {
       
   171             stream.close();
       
   172         }
       
   173         checkCount(totalCount, accepted);
       
   174 
       
   175         System.out.println("Test: anyOf with list of filters");
       
   176         accepted = 0;
       
   177         stream = dir.newDirectoryStream(anyOf(filters));
       
   178         try {
       
   179             for (Path entry: stream) {
       
   180                 if (!isHtml(entry) && !isBig(entry))
       
   181                     throw new RuntimeException("html or big file expected");
       
   182                 accepted++;
       
   183             }
       
   184         } finally {
       
   185             stream.close();
       
   186         }
       
   187         checkCount(bigOrHtmlCount, accepted);
       
   188 
       
   189         System.out.println("Test: anyOf with empty list");
       
   190         accepted = 0;
       
   191         stream = dir.newDirectoryStream(anyOf(emptyList));
       
   192         try {
       
   193             for (Path entry: stream) {
       
   194                 accepted++;
       
   195             }
       
   196         } finally {
       
   197             stream.close();
       
   198         }
       
   199         checkCount(0, accepted);
       
   200 
       
   201         System.out.println("Test: complementOf");
       
   202         accepted = 0;
       
   203         stream = dir.newDirectoryStream(complementOf(newGlobFilter("*.html")));
       
   204         try {
       
   205             for (Path entry: stream) {
       
   206                 accepted++;
       
   207             }
       
   208         } finally {
       
   209             stream.close();
       
   210         }
       
   211         checkCount(totalCount-htmlCount, accepted);
       
   212 
       
   213         System.out.println("Test: nulls");
       
   214         try {
       
   215             newContentTypeFilter(null);
       
   216             throw new RuntimeException("NullPointerException expected");
       
   217         } catch (NullPointerException npe) { }
       
   218         try {
       
   219             allOf(null);
       
   220             throw new RuntimeException("NullPointerException expected");
       
   221         } catch (NullPointerException npe) { }
       
   222         try {
       
   223             anyOf(null);
       
   224             throw new RuntimeException("NullPointerException expected");
       
   225         } catch (NullPointerException npe) { }
       
   226         try {
       
   227             complementOf(null);
       
   228             throw new RuntimeException("NullPointerException expected");
       
   229         } catch (NullPointerException npe) { }
       
   230     }
       
   231 
       
   232     public static void main(String[] args) throws IOException {
       
   233         Path dir = TestUtil.createTemporaryDirectory();
       
   234         try {
       
   235             setup(dir);
       
   236             doTests(dir);
       
   237         } finally {
       
   238             TestUtil.removeAll(dir);
       
   239         }
       
   240     }
       
   241 }