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 168237
Collapse All | Expand All

(-)test/unit/src/org/netbeans/modules/parsing/impl/indexing/RepositoryUpdaterExternalChangesTest.java (+332 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.modules.parsing.impl.indexing;
41
42
import java.io.File;
43
import java.io.FileOutputStream;
44
import java.io.IOException;
45
import java.io.OutputStream;
46
import java.lang.ref.Reference;
47
import java.lang.ref.WeakReference;
48
import java.net.URL;
49
import java.util.Arrays;
50
import java.util.HashMap;
51
import java.util.HashSet;
52
import java.util.Map;
53
import java.util.Set;
54
import java.util.logging.Level;
55
import java.util.logging.Logger;
56
import org.netbeans.api.editor.mimelookup.MimePath;
57
import org.netbeans.api.editor.mimelookup.test.MockMimeLookup;
58
import org.netbeans.api.java.classpath.ClassPath;
59
import org.netbeans.api.java.classpath.GlobalPathRegistry;
60
import org.netbeans.junit.MockServices;
61
import org.netbeans.junit.NbTestCase;
62
import org.netbeans.modules.parsing.impl.indexing.PathRegistryTest.FooPathRecognizer;
63
import org.netbeans.modules.parsing.impl.indexing.PathRegistryTest.OpenProject;
64
import org.netbeans.modules.parsing.impl.indexing.PathRegistryTest.SFBQImpl;
65
import org.netbeans.spi.java.classpath.ClassPathFactory;
66
import org.openide.filesystems.FileObject;
67
import org.openide.filesystems.FileUtil;
68
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater.*;
69
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.BinIndexerFactory;
70
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.EmbIndexerFactory;
71
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.EmbIndexerFactory;
72
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.EmbParserFactory;
73
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.EmbPathRecognizer;
74
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.FooIndexerFactory;
75
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.MutableClassPathImplementation;
76
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest.TestHandler;
77
78
/**
79
 * TODO:
80
 * - test that modifying .zip/.jar triggeres rescan of this binary
81
 *
82
 * @author Tomas Zezula
83
 */
84
public class RepositoryUpdaterExternalChangesTest extends NbTestCase {
85
86
    private static final int TIME = 5000;
87
    private static final String SOURCES = "FOO_SOURCES";
88
    private static final String PLATFORM = "FOO_PLATFORM";
89
    private static final String LIBS = "FOO_LIBS";
90
    private static final String MIME = "text/foo";
91
    private static final String EMIME = "text/emb";
92
    private static final String JARMIME = "application/java-archive";
93
94
    private FileObject srcRoot1;
95
    private FileObject srcRoot2;
96
    private FileObject srcRoot3;
97
    private FileObject compRoot1;
98
    private FileObject compRoot2;
99
    private FileObject bootRoot1;
100
    private FileObject bootRoot2;
101
    private FileObject bootRoot3;
102
    private FileObject compSrc1;
103
    private FileObject compSrc2;
104
    private FileObject bootSrc1;
105
    private FileObject unknown1;
106
    private FileObject unknown2;
107
    private FileObject unknownSrc2;
108
    private FileObject srcRootWithFiles1;
109
110
    FileObject f3;
111
112
    private URL[] customFiles;
113
    private URL[] embeddedFiles;
114
115
    private final BinIndexerFactory binIndexerFactory = new BinIndexerFactory();
116
// Binary indexer have to be registered for MimePath.EMPTY, no mime-type specific binary indexers
117
//    private final BinIndexerFactory jarIndexerFactory = new BinIndexerFactory();
118
    private final FooIndexerFactory indexerFactory = new FooIndexerFactory();
119
    private final EmbIndexerFactory eindexerFactory = new EmbIndexerFactory();
120
121
    private final Map<String, Set<ClassPath>> registeredClasspaths = new HashMap<String, Set<ClassPath>>();
122
123
    public RepositoryUpdaterExternalChangesTest (String name) {
124
        super (name);
125
    }
126
127
    @Override
128
    protected void setUp() throws Exception {
129
//        TopLogging.initializeQuietly();
130
        super.setUp();
131
        this.clearWorkDir();
132
        final File _wd = this.getWorkDir();
133
        final FileObject wd = FileUtil.toFileObject(_wd);
134
        final FileObject cache = wd.createFolder("cache");
135
        CacheFolder.setCacheFolder(cache);
136
137
        MockServices.setServices(FooPathRecognizer.class, EmbPathRecognizer.class, SFBQImpl.class, OpenProject.class);
138
        MockMimeLookup.setInstances(MimePath.EMPTY, binIndexerFactory);
139
//        MockMimeLookup.setInstances(MimePath.get(JARMIME), jarIndexerFactory);
140
        MockMimeLookup.setInstances(MimePath.get(MIME), indexerFactory);
141
        MockMimeLookup.setInstances(MimePath.get(EMIME), eindexerFactory, new EmbParserFactory());
142
        Set<String> mt = new HashSet<String>();
143
        mt.add(EMIME);
144
        mt.add(MIME);
145
        Util.allMimeTypes = mt;
146
147
        assertNotNull("No masterfs",wd);
148
        srcRoot1 = wd.createFolder("src1");
149
        assertNotNull(srcRoot1);
150
        srcRoot2 = wd.createFolder("src2");
151
        assertNotNull(srcRoot2);
152
        srcRoot3 = wd.createFolder("src3");
153
        assertNotNull (srcRoot3);
154
        compRoot1 = wd.createFolder("comp1");
155
        assertNotNull (compRoot1);
156
        compRoot2 = wd.createFolder("comp2");
157
        assertNotNull (compRoot2);
158
        bootRoot1 = wd.createFolder("boot1");
159
        assertNotNull (bootRoot1);
160
        bootRoot2 = wd.createFolder("boot2");
161
        assertNotNull (bootRoot2);
162
        FileUtil.setMIMEType("jar", JARMIME);
163
        FileObject jarFile = FileUtil.toFileObject(getDataDir()).getFileObject("JavaApplication1.jar");
164
        assertNotNull(jarFile);
165
        assertTrue(FileUtil.isArchiveFile(jarFile));
166
        bootRoot3 = FileUtil.getArchiveRoot(jarFile);
167
        assertNotNull (bootRoot3);
168
        compSrc1 = wd.createFolder("cs1");
169
        assertNotNull (compSrc1);
170
        compSrc2 = wd.createFolder("cs2");
171
        assertNotNull (compSrc2);
172
        bootSrc1 = wd.createFolder("bs1");
173
        assertNotNull (bootSrc1);
174
        unknown1 = wd.createFolder("uknw1");
175
        assertNotNull (unknown1);
176
        unknown2 = wd.createFolder("uknw2");
177
        assertNotNull (unknown2);
178
        unknownSrc2 = wd.createFolder("uknwSrc2");
179
        assertNotNull(unknownSrc2);
180
        SFBQImpl.register (bootRoot1,bootSrc1);
181
        SFBQImpl.register (compRoot1,compSrc1);
182
        SFBQImpl.register (compRoot2,compSrc2);
183
        SFBQImpl.register (unknown2,unknownSrc2);
184
185
        srcRootWithFiles1 = wd.createFolder("srcwf1");
186
        assertNotNull(srcRootWithFiles1);
187
        FileUtil.setMIMEType("foo", MIME);
188
        FileObject f1 = FileUtil.createData(srcRootWithFiles1,"folder/a.foo");
189
        assertNotNull(f1);
190
        assertEquals(MIME, f1.getMIMEType());
191
        FileObject f2 = FileUtil.createData(srcRootWithFiles1,"folder/b.foo");
192
        assertNotNull(f2);
193
        assertEquals(MIME, f2.getMIMEType());
194
        customFiles = new URL[] {f1.getURL(), f2.getURL()};
195
196
        FileUtil.setMIMEType("emb", EMIME);
197
        f3 = FileUtil.createData(srcRootWithFiles1,"folder/a.emb");
198
        assertNotNull(f3);
199
        assertEquals(EMIME, f3.getMIMEType());
200
        FileObject f4 = FileUtil.createData(srcRootWithFiles1,"folder/b.emb");
201
        assertNotNull(f4);
202
        assertEquals(EMIME, f4.getMIMEType());
203
        embeddedFiles = new URL[] {f3.getURL(), f4.getURL()};
204
205
        waitForRepositoryUpdaterInit();
206
    }
207
208
    @Override
209
    protected void tearDown() throws Exception {
210
        for(String id : registeredClasspaths.keySet()) {
211
            Set<ClassPath> classpaths = registeredClasspaths.get(id);
212
            GlobalPathRegistry.getDefault().unregister(id, classpaths.toArray(new ClassPath[classpaths.size()]));
213
        }
214
215
        super.tearDown();
216
    }
217
218
    protected final void globalPathRegistry_register(String id, ClassPath [] classpaths) {
219
        Set<ClassPath> set = registeredClasspaths.get(id);
220
        if (set == null) {
221
            set = new HashSet<ClassPath>();
222
            registeredClasspaths.put(id, set);
223
        }
224
        set.addAll(Arrays.asList(classpaths));
225
        GlobalPathRegistry.getDefault().register(id, classpaths);
226
    }
227
228
    protected final void globalPathRegistry_unregister(String id, ClassPath [] classpaths) {
229
        GlobalPathRegistry.getDefault().unregister(id, classpaths);
230
        Set<ClassPath> set = registeredClasspaths.get(id);
231
        if (set != null) {
232
            set.removeAll(Arrays.asList(classpaths));
233
        }
234
    }
235
236
    /* package */ static void waitForRepositoryUpdaterInit() throws Exception {
237
        RepositoryUpdaterTest.waitForRepositoryUpdaterInit();;
238
    }
239
240
    public void testFileChanges() throws Exception {
241
        final TestHandler handler = new TestHandler();
242
        final Logger logger = Logger.getLogger(RepositoryUpdater.class.getName()+".tests");
243
        logger.setLevel (Level.FINEST);
244
        logger.addHandler(handler);
245
        indexerFactory.indexer.setExpectedFile(customFiles, new URL[0], new URL[0]);
246
        eindexerFactory.indexer.setExpectedFile(embeddedFiles, new URL[0], new URL[0]);
247
        MutableClassPathImplementation mcpi1 = new MutableClassPathImplementation ();
248
        mcpi1.addResource(this.srcRootWithFiles1);
249
        ClassPath cp1 = ClassPathFactory.createClassPath(mcpi1);
250
        globalPathRegistry_register(SOURCES,new ClassPath[]{cp1});
251
        assertTrue (handler.await());
252
        assertEquals(0, handler.getBinaries().size());
253
        assertEquals(1, handler.getSources().size());
254
        assertEquals(this.srcRootWithFiles1.getURL(), handler.getSources().get(0));
255
        assertTrue(indexerFactory.indexer.awaitIndex());
256
        assertTrue(eindexerFactory.indexer.awaitIndex());
257
258
        //Test modifications
259
        indexerFactory.indexer.setExpectedFile(new URL[0], new URL[0], new URL[0]);
260
        eindexerFactory.indexer.setExpectedFile(new URL[]{f3.getURL()}, new URL[0], new URL[0]);
261
262
        File f = FileUtil.toFile(f3);
263
        Reference<Object> ref = new WeakReference<Object>(f3);
264
        f3 = null;
265
        assertGC("File object can disappear", ref);
266
267
        final OutputStream out = new FileOutputStream(f);
268
        try {
269
            out.write(0);
270
        } finally {
271
            out.close();
272
        }
273
274
        simulateUserRefreshGesture();
275
276
        assertTrue(indexerFactory.indexer.awaitIndex());
277
        assertTrue(eindexerFactory.indexer.awaitIndex());
278
        assertEquals(1, eindexerFactory.indexer.indexCounter);
279
280
        //Test file creation
281
        final File container = f.getParentFile();
282
        File newFile = new File (container,"c.emb");
283
        indexerFactory.indexer.setExpectedFile(new URL[0], new URL[0], new URL[0]);
284
        eindexerFactory.indexer.setExpectedFile(new URL[]{newFile.toURI().toURL()}, new URL[0], new URL[0]);
285
        newFile.createNewFile();
286
        simulateUserRefreshGesture();
287
288
        assertTrue(indexerFactory.indexer.awaitIndex());
289
        assertTrue(eindexerFactory.indexer.awaitIndex());
290
        assertEquals(1, eindexerFactory.indexer.indexCounter);
291
292
        //Test folder creation
293
        File newFolder = new File (container,"subfolder");
294
        newFile = new File (newFolder,"d.emb");
295
        File newFile2 = new File (newFolder,"e.emb");
296
        indexerFactory.indexer.setExpectedFile(new URL[0], new URL[0], new URL[0]);
297
        eindexerFactory.indexer.setExpectedFile(new URL[]{newFile.toURI().toURL(), newFile2.toURI().toURL()}, new URL[0], new URL[0]);
298
        newFolder.mkdirs();
299
        touchFile (newFile);
300
        touchFile (newFile2);
301
        assertEquals(2,newFolder.list().length);
302
        simulateUserRefreshGesture();
303
        assertTrue(indexerFactory.indexer.awaitIndex());
304
        assertTrue(eindexerFactory.indexer.awaitIndex());
305
        assertEquals(2, eindexerFactory.indexer.indexCounter);
306
307
        //Test file deleted
308
        handler.reset(TestHandler.Type.DELETE);
309
        indexerFactory.indexer.setExpectedFile(new URL[0], new URL[0], new URL[0]);
310
        eindexerFactory.indexer.setExpectedFile(new URL[0], new URL[]{f3.getURL()}, new URL[0]);
311
        f.delete();
312
        simulateUserRefreshGesture();
313
        assertTrue (handler.await());
314
        assertTrue(indexerFactory.indexer.awaitDeleted());
315
        assertTrue(eindexerFactory.indexer.awaitDeleted());
316
        assertEquals(0, eindexerFactory.indexer.indexCounter);
317
        assertEquals(0,eindexerFactory.indexer.expectedDeleted.size());
318
        assertEquals(0, eindexerFactory.indexer.expectedDirty.size());
319
    }
320
321
322
    private void touchFile (final File file) throws IOException {
323
        OutputStream out = new FileOutputStream (file);
324
        out.close();
325
    }
326
327
    private void simulateUserRefreshGesture() {
328
        FileUtil.refreshAll();
329
    }
330
331
332
}
(-)test/unit/src/org/netbeans/modules/parsing/impl/indexing/RepositoryUpdaterTest.java (-11 / +11 lines)
Lines 1069-1075 Link Here
1069
1069
1070
    }
1070
    }
1071
1071
1072
    private static class BinIndexerFactory extends BinaryIndexerFactory {
1072
    static class BinIndexerFactory extends BinaryIndexerFactory {
1073
1073
1074
        private final BinIndexer indexer = new BinIndexer();
1074
        private final BinIndexer indexer = new BinIndexer();
1075
1075
Lines 1120-1128 Link Here
1120
        }
1120
        }
1121
    }
1121
    }
1122
1122
1123
    private static class FooIndexerFactory extends CustomIndexerFactory {
1123
    static class FooIndexerFactory extends CustomIndexerFactory {
1124
1124
1125
        private final FooIndexer indexer = new FooIndexer();
1125
        final FooIndexer indexer = new FooIndexer();
1126
1126
1127
        @Override
1127
        @Override
1128
        public CustomIndexer createIndexer() {
1128
        public CustomIndexer createIndexer() {
Lines 1167-1173 Link Here
1167
        }
1167
        }
1168
    }
1168
    }
1169
1169
1170
    private static class FooIndexer extends CustomIndexer {
1170
    static class FooIndexer extends CustomIndexer {
1171
1171
1172
        private Set<URL> expectedIndex = new HashSet<URL>();
1172
        private Set<URL> expectedIndex = new HashSet<URL>();
1173
        private CountDownLatch indexFilesLatch;
1173
        private CountDownLatch indexFilesLatch;
Lines 1230-1238 Link Here
1230
        }
1230
        }
1231
    }
1231
    }
1232
1232
1233
    private static class EmbIndexerFactory extends EmbeddingIndexerFactory {
1233
    static class EmbIndexerFactory extends EmbeddingIndexerFactory {
1234
1234
1235
        private EmbIndexer indexer = new EmbIndexer ();
1235
        EmbIndexer indexer = new EmbIndexer ();
1236
1236
1237
        @Override
1237
        @Override
1238
        public EmbeddingIndexer createIndexer(final Indexable indexable, final Snapshot snapshot) {
1238
        public EmbeddingIndexer createIndexer(final Indexable indexable, final Snapshot snapshot) {
Lines 1272-1288 Link Here
1272
        }
1272
        }
1273
    }
1273
    }
1274
1274
1275
    private static class EmbIndexer extends EmbeddingIndexer {
1275
    static class EmbIndexer extends EmbeddingIndexer {
1276
1276
1277
        private Set<URL> expectedIndex = new HashSet<URL>();
1277
        private Set<URL> expectedIndex = new HashSet<URL>();
1278
        private CountDownLatch indexFilesLatch;
1278
        private CountDownLatch indexFilesLatch;
1279
        private CountDownLatch deletedFilesLatch;
1279
        private CountDownLatch deletedFilesLatch;
1280
        private CountDownLatch dirtyFilesLatch;
1280
        private CountDownLatch dirtyFilesLatch;
1281
        private volatile int indexCounter;
1281
        volatile int indexCounter;
1282
        private volatile int deletedCounter;
1282
        private volatile int deletedCounter;
1283
        private volatile int dirtyCounter;
1283
        private volatile int dirtyCounter;
1284
        private Set<URL> expectedDeleted = new HashSet<URL>();
1284
        Set<URL> expectedDeleted = new HashSet<URL>();
1285
        private Set<URL> expectedDirty = new HashSet<URL>();
1285
        Set<URL> expectedDirty = new HashSet<URL>();
1286
1286
1287
        public void setExpectedFile (URL[] files, URL[] deleted, URL[] dirty) {
1287
        public void setExpectedFile (URL[] files, URL[] deleted, URL[] dirty) {
1288
            expectedIndex.clear();
1288
            expectedIndex.clear();
Lines 1339-1345 Link Here
1339
1339
1340
    }
1340
    }
1341
1341
1342
    private static class EmbParserFactory extends ParserFactory {
1342
    static class EmbParserFactory extends ParserFactory {
1343
1343
1344
        @Override
1344
        @Override
1345
        public Parser createParser(Collection<Snapshot> snapshots) {
1345
        public Parser createParser(Collection<Snapshot> snapshots) {

Return to bug 168237