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.DirectoryStream 27 * @library .. 28 */ 29 30import java.nio.file.*; 31import java.util.*; 32import java.io.IOException; 33 34public class Basic { 35 static boolean found; 36 37 static void doTest(final Path dir) throws IOException { 38 DirectoryStream<Path> stream; 39 40 // test that directory is empty 41 Files.withDirectory(dir, new FileAction<FileRef>() { 42 public void invoke(FileRef entry) { 43 throw new RuntimeException("directory not empty"); 44 } 45 }); 46 47 // create file in directory 48 final Path foo = Paths.get("foo"); 49 dir.resolve(foo).createFile(); 50 51 // iterate over directory and check there is one entry 52 found = false; 53 Files.withDirectory(dir, new FileAction<Path>() { 54 public void invoke(Path entry) { 55 if (entry.getName().equals(foo)) { 56 if (found) 57 throw new RuntimeException("entry already found"); 58 found = true; 59 } else { 60 throw new RuntimeException("entry " + entry.getName() + 61 " not expected"); 62 } 63 } 64 }); 65 if (!found) 66 throw new RuntimeException("entry not found"); 67 68 // check filtering: f* should match foo 69 DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { 70 private PathMatcher matcher = 71 dir.getFileSystem().getPathMatcher("glob:f*"); 72 public boolean accept(Path file) { 73 return matcher.matches(file); 74 } 75 }; 76 Files.withDirectory(dir, filter, new FileAction<Path>() { 77 public void invoke(Path entry) { 78 if (!entry.getName().equals(foo)) 79 throw new RuntimeException("entry not expected"); 80 } 81 }); 82 83 // check filtering: z* should not match any files 84 filter = new DirectoryStream.Filter<Path>() { 85 private PathMatcher matcher = 86 dir.getFileSystem().getPathMatcher("glob:z*"); 87 public boolean accept(Path file) { 88 return matcher.matches(file); 89 } 90 }; 91 Files.withDirectory(dir, filter, new FileAction<FileRef>() { 92 public void invoke(FileRef entry) { 93 throw new RuntimeException("no matching entries expected"); 94 } 95 }); 96 97 // check that exception or error thrown by filter is not thrown 98 // by newDirectoryStream or iterator method. 99 stream = dir.newDirectoryStream(new DirectoryStream.Filter<Path>() { 100 public boolean accept(Path file) { 101 throw new RuntimeException("Should not be visible"); 102 } 103 }); 104 try { 105 stream.iterator(); 106 } finally { 107 stream.close(); 108 } 109 110 // test NotDirectoryException 111 try { 112 dir.resolve(foo).newDirectoryStream(); 113 throw new RuntimeException("NotDirectoryException not thrown"); 114 } catch (NotDirectoryException x) { 115 } 116 117 // test iterator remove method 118 stream = dir.newDirectoryStream(); 119 Iterator<Path> i = stream.iterator(); 120 while (i.hasNext()) { 121 Path entry = i.next(); 122 if (!entry.getName().equals(foo)) 123 throw new RuntimeException("entry not expected"); 124 i.remove(); 125 } 126 stream.close(); 127 128 // test IllegalStateException 129 stream = dir.newDirectoryStream(); 130 i = stream.iterator(); 131 try { 132 stream.iterator(); 133 throw new RuntimeException("IllegalStateException not thrown as expected"); 134 } catch (IllegalStateException x) { 135 } 136 stream.close(); 137 try { 138 stream.iterator(); 139 throw new RuntimeException("IllegalStateException not thrown as expected"); 140 } catch (IllegalStateException x) { 141 } 142 try { 143 i.hasNext(); 144 throw new RuntimeException("ConcurrentModificationException not thrown as expected"); 145 } catch (ConcurrentModificationException x) { 146 Throwable t = x.getCause(); 147 if (!(t instanceof IllegalStateException)) 148 throw new RuntimeException("Cause is not IllegalStateException as expected"); 149 } 150 try { 151 i.next(); 152 throw new RuntimeException("IllegalStateException not thrown as expected"); 153 } catch (ConcurrentModificationException x) { 154 Throwable t = x.getCause(); 155 if (!(t instanceof IllegalStateException)) 156 throw new RuntimeException("Cause is not IllegalStateException as expected"); 157 } 158 } 159 160 public static void main(String[] args) throws IOException { 161 Path dir = TestUtil.createTemporaryDirectory(); 162 try { 163 doTest(dir); 164 } finally { 165 TestUtil.removeAll(dir); 166 } 167 } 168} 169