| 1 | |
package org.webslinger.commons.vfs.tests; |
| 2 | |
|
| 3 | |
import org.webslinger.httpunit.WebslingerTestCase; |
| 4 | |
|
| 5 | |
import java.io.File; |
| 6 | |
import java.io.FileOutputStream; |
| 7 | |
import java.io.OutputStream; |
| 8 | |
import java.lang.reflect.Method; |
| 9 | |
import java.util.ArrayList; |
| 10 | |
|
| 11 | |
import org.apache.commons.vfs.FileChangeEvent; |
| 12 | |
import org.apache.commons.vfs.FileListener; |
| 13 | |
import org.apache.commons.vfs.FileObject; |
| 14 | |
import org.apache.commons.vfs.FileSystemException; |
| 15 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 16 | |
import org.apache.commons.vfs.Selectors; |
| 17 | |
import org.apache.commons.vfs.provider.AbstractFileSystem; |
| 18 | |
import org.apache.commons.vfs.provider.local.LocalFile; |
| 19 | |
import org.apache.commons.vfs.impl.DefaultFileMonitor; |
| 20 | |
import org.apache.commons.vfs.impl.StandardFileSystemManager; |
| 21 | |
|
| 22 | |
import org.webslinger.containers.TestContainer; |
| 23 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 24 | |
import org.webslinger.commons.vfs.tests.FileObjectRoot; |
| 25 | |
import org.webslinger.commons.vfs.tests.LocalFileObjectRoot; |
| 26 | |
|
| 27 | |
import org.webslinger.io.Monitor; |
| 28 | |
import org.webslinger.io.IOUtil; |
| 29 | |
import org.webslinger.io.tests.FileRoot; |
| 30 | |
import org.webslinger.io.tests.Root; |
| 31 | |
|
| 32 | |
public class TestListener extends TestContainer.TestContainerTestCase { |
| 33 | |
protected StandardFileSystemManager sfsm; |
| 34 | |
protected FileObject root; |
| 35 | |
|
| 36 | |
public TestListener(String name) { |
| 37 | 1 | super(name); |
| 38 | 1 | } |
| 39 | |
|
| 40 | |
protected void setUp() throws Exception { |
| 41 | 1 | super.setUp(); |
| 42 | 1 | sfsm = VFSUtil.createStandardFileSystemManager(); |
| 43 | 1 | root = sfsm.resolveFile(getBase().toURL().toString(), new FileSystemOptions()); |
| 44 | 1 | root.delete(Selectors.SELECT_ALL); |
| 45 | 1 | root.createFolder(); |
| 46 | 1 | } |
| 47 | |
|
| 48 | |
protected void tearDown() throws Exception { |
| 49 | |
try { |
| 50 | 1 | root.delete(Selectors.SELECT_ALL); |
| 51 | 0 | } catch (Exception e) { |
| 52 | 1 | } |
| 53 | 1 | super.tearDown(); |
| 54 | 1 | } |
| 55 | |
|
| 56 | |
protected void doTest(Root under, Root over) throws Exception { |
| 57 | 0 | IOUtil.writeString(under.create("to-modify").openOut(), "this file will be changed"); |
| 58 | 0 | IOUtil.writeString(under.create("to-delete").openOut(), "file that will be deleted"); |
| 59 | 0 | Root.Listener toModifyListener = over.create("to-modify").createListener(); |
| 60 | 0 | Root.Listener toDeleteListener = over.create("to-delete").createListener(); |
| 61 | |
|
| 62 | 0 | Root.Listener toCreateListener = over.create("to-create").createListener(); |
| 63 | 0 | System.err.println("starting listeners"); |
| 64 | 0 | toModifyListener.start(); |
| 65 | 0 | toDeleteListener.start(); |
| 66 | |
|
| 67 | 0 | toCreateListener.start(); |
| 68 | |
|
| 69 | 0 | System.err.println("doing changes"); |
| 70 | 0 | under.create("to-modify").touch(5000); |
| 71 | 0 | under.create("to-delete").delete(); |
| 72 | 0 | IOUtil.writeString(under.create("to-create").openOut(), "this file was just created"); |
| 73 | 0 | Thread.sleep(250); |
| 74 | 0 | System.err.println("stopping listeners"); |
| 75 | 0 | toModifyListener.stop(); |
| 76 | 0 | toDeleteListener.stop(); |
| 77 | |
|
| 78 | 0 | toCreateListener.stop(); |
| 79 | 0 | System.err.println("modify: " + toModifyListener.events); |
| 80 | 0 | System.err.println("delete: " + toDeleteListener.events); |
| 81 | |
|
| 82 | 0 | System.err.println("create: " + toCreateListener.events); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
public void testRoot() throws Exception { |
| 86 | 1 | System.err.println(root.resolveFile("to-create").exists()); |
| 87 | |
|
| 88 | 1 | System.err.println(root.resolveFile("to-create").exists()); |
| 89 | 1 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
} |