FileTreeModifier.java revision 1319:5208d0c90d73
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 6838333 26 * @summary Sanity test for Sun-specific FILE_TREE watch event modifier 27 * @library .. 28 */ 29 30import java.nio.file.*; 31import static java.nio.file.StandardWatchEventKind.*; 32import java.io.IOException; 33import java.io.OutputStream; 34import java.util.*; 35import java.util.concurrent.*; 36import static com.sun.nio.file.ExtendedWatchEventModifier.*; 37 38public class FileTreeModifier { 39 40 static void checkExpectedEvent(WatchService watcher, 41 WatchEvent.Kind<?> expectedType, 42 Object expectedContext) 43 { 44 WatchKey key; 45 try { 46 key = watcher.take(); 47 } catch (InterruptedException x) { 48 // should not happen 49 throw new RuntimeException(x); 50 } 51 WatchEvent<?> event = key.pollEvents().iterator().next(); 52 System.out.format("Event: type=%s, count=%d, context=%s\n", 53 event.kind(), event.count(), event.context()); 54 if (event.kind() != expectedType) 55 throw new RuntimeException("unexpected event"); 56 if (!expectedContext.equals(event.context())) 57 throw new RuntimeException("unexpected context"); 58 } 59 60 static void doTest(Path top) throws IOException { 61 FileSystem fs = top.getFileSystem(); 62 WatchService watcher = fs.newWatchService(); 63 64 // create directories 65 Path subdir = top 66 .resolve("a").createDirectory() 67 .resolve("b").createDirectory() 68 .resolve("c").createDirectory(); 69 70 // Test ENTRY_CREATE with FILE_TREE modifier. 71 72 WatchKey key = top.register(watcher, 73 new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE); 74 75 // create file in a/b/c and check we get create event 76 Path file = subdir.resolve("foo").createFile(); 77 checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); 78 key.reset(); 79 80 // Test ENTRY_DELETE with FILE_TREE modifier. 81 82 WatchKey k = top.register(watcher, 83 new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE); 84 if (k != key) 85 throw new RuntimeException("Existing key not returned"); 86 87 // delete a/b/c/foo and check we get delete event 88 file.delete(); 89 checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file)); 90 key.reset(); 91 92 // Test changing registration to ENTRY_CREATE without modifier 93 94 k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }); 95 if (k != key) 96 throw new RuntimeException("Existing key not returned"); 97 98 // create a/b/c/foo 99 file.createFile(); 100 101 // check that key is not queued 102 try { 103 k = watcher.poll(3, TimeUnit.SECONDS); 104 } catch (InterruptedException e) { 105 throw new RuntimeException(); 106 } 107 if (k != null) 108 throw new RuntimeException("WatchKey not expected to be polled"); 109 110 // create bar and check we get create event 111 file = top.resolve("bar").createFile(); 112 checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); 113 key.reset(); 114 115 // Test changing registration to <all> with FILE_TREE modifier 116 117 k = top.register(watcher, 118 new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY }, 119 FILE_TREE); 120 if (k != key) 121 throw new RuntimeException("Existing key not returned"); 122 123 // modify bar and check we get modify event 124 OutputStream out = file.newOutputStream(); 125 try { 126 out.write("Double shot expresso please".getBytes("UTF-8")); 127 } finally { 128 out.close(); 129 } 130 checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file)); 131 key.reset(); 132 } 133 134 135 public static void main(String[] args) throws IOException { 136 if (!System.getProperty("os.name").startsWith("Windows")) { 137 System.out.println("This is Windows-only test at this time!"); 138 return; 139 } 140 141 Path dir = TestUtil.createTemporaryDirectory(); 142 try { 143 doTest(dir); 144 } finally { 145 TestUtil.removeAll(dir); 146 } 147 } 148} 149