This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 33162
Collapse All | Expand All

(-)FileObjectTestHid.java (+127 lines)
Lines 24-31 Link Here
24
import javax.xml.parsers.SAXParserFactory;
24
import javax.xml.parsers.SAXParserFactory;
25
import javax.xml.parsers.ParserConfigurationException;
25
import javax.xml.parsers.ParserConfigurationException;
26
import java.io.*;
26
import java.io.*;
27
import java.lang.ref.Reference;
28
import java.lang.ref.WeakReference;
27
import java.util.*;
29
import java.util.*;
28
import java.net.*;
30
import java.net.*;
31
import java.util.logging.Level;
32
import java.util.logging.Logger;
29
/**
33
/**
30
 *
34
 *
Lines 72-77 Link Here
72
        //Reference ref = new WeakReference (root);
76
        //Reference ref = new WeakReference (root);
73
        root = null;
77
        root = null;
74
        //assertGC("", ref);
78
        //assertGC("", ref);
79
    }
80
81
    private static enum EventType { DATA_CREATED, FOLDER_CREATED,DELETED, MODIFIED, RENAMED};
82
    public void testEventDelivery() throws Exception {
83
        checkSetUp();
84
        if (root.getFileSystem().isReadOnly()) {
85
            return;
86
        }
87
        final FileObject testRoot = root;
88
        FileObject testParentFolder = FileUtil.createFolder(testRoot,"/a");
89
        assertNotNull(testParentFolder);
90
        FileObject testfile = FileUtil.createData(testParentFolder,"/b/c/d/"+getName());
91
        assertTrue(testfile.isValid());
92
        final URI testfileURI = testfile.getURL().toURI();
93
        final URI testRootURI = testRoot.getURL().toURI();
94
        final URI testParentFolderURI = testParentFolder.getURL().toURI();
95
96
97
        testfile.delete();
98
        assertFalse(testfile.isValid());
99
        final String testPath = testfile.getPath();
100
        Reference ref = new WeakReference(testfile);
101
        testfile = null;
102
        assertGC("", ref);
103
104
        testParentFolder.delete();
105
        ref = new WeakReference(testParentFolder);
106
        testParentFolder = null;
107
        assertGC("", ref);
108
109
        FileChangeListenerImpl fml = new FileChangeListenerImpl(testfileURI);
110
        FileChangeListenerImpl fml2 = new FileChangeListenerImpl(testRootURI);
111
        FileChangeListenerImpl fml3 = new FileChangeListenerImpl(testParentFolderURI);
112
113
        assertFalse(fml.called);
114
        Repository.getDefault().addFileSystem(this.testedFS);
115
        assertTrue(FileChangeManager.addFileChangeListener(testfileURI, true, fml));
116
        assertTrue(FileChangeManager.addFileChangeListener(testRootURI, true, fml2));
117
        assertTrue(FileChangeManager.addFileChangeListener(testParentFolderURI, true, fml3));
118
        try {
119
            for (int i = 0; i < 10; i++) {
120
                assertFalse(fml.called);
121
                FileObject fo = FileUtil.createData(testRoot,testPath);
122
                assertEquals(fo.getPath(), testPath);
123
                assertTrue(fo.isValid());
124
                fml.asserts(testfileURI,EventType.DATA_CREATED);
125
                fml2.asserts(testfileURI,EventType.DATA_CREATED);
126
127
                OutputStream os = fo.getOutputStream();
128
                try {
129
                    os.write("string".getBytes());
130
                } finally {
131
                    os.close();
132
                }
133
                fml.asserts(testfileURI,EventType.MODIFIED);
134
                fml2.asserts(testfileURI,EventType.MODIFIED);
135
136
137
                fo.delete();
138
                fml.asserts(testfileURI,EventType.DELETED);
139
                fml2.asserts(testfileURI,EventType.DELETED);
140
            }
141
        } finally {
142
            assertTrue(FileChangeManager.removeFileChangeListener(testfileURI, fml));
143
            assertTrue(FileChangeManager.removeFileChangeListener(testRootURI, fml2));
144
            assertTrue(FileChangeManager.removeFileChangeListener(testParentFolderURI, fml3));
145
            Repository.getDefault().removeFileSystem(this.testedFS);
146
        }
147
    }
148
149
    private static class FileChangeListenerImpl implements FileChangeListener {
150
        boolean called;
151
        URI parent;
152
        URI uri;
153
        EventType type;
154
155
        FileChangeListenerImpl(URI parent) {
156
            this.parent = parent;
157
        }
158
159
        void asserts(URI uri, EventType type) {
160
            try {
161
                assertTrue(called);
162
                assertEquals(this.type, type);
163
                assertEquals(uri, this.uri);
164
            } finally {
165
                called = false;
166
            }
167
        }
168
169
        private void fileEvent(FileEvent fe, EventType type) {
170
            called = true;
171
            this.type = type;
172
            try {
173
                uri = URLMapper.findURL(fe.getFile(),URLMapper.INTERNAL).toURI();
174
            } catch (URISyntaxException ex) {
175
                Logger.getLogger("global").log(Level.SEVERE, ex.getMessage(), ex);
176
            }
177
        }
178
179
        public void fileFolderCreated(FileEvent fe) {
180
            fileEvent(fe, EventType.FOLDER_CREATED);
181
        }
182
183
        public void fileDataCreated(FileEvent fe) {
184
            fileEvent(fe, EventType.DATA_CREATED);
185
186
        }
187
188
        public void fileChanged(FileEvent fe) {
189
            fileEvent(fe, EventType.MODIFIED);
190
        }
191
192
        public void fileDeleted(FileEvent fe) {
193
            fileEvent(fe, EventType.DELETED);
194
        }
195
196
        public void fileRenamed(FileRenameEvent fe) {
197
            fileEvent(fe, EventType.RENAMED);
198
        }
199
200
        public void fileAttributeChanged(FileAttributeEvent fe) {
201
        }
75
    }
202
    }
76
    public void testEventsDelivery81746() throws Exception {
203
    public void testEventsDelivery81746() throws Exception {

Return to bug 33162