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

(-)a/java.source/nbproject/project.properties (-1 / +1 lines)
Lines 46-52 Link Here
46
javadoc.title=Java Source
46
javadoc.title=Java Source
47
javadoc.arch=${basedir}/arch.xml
47
javadoc.arch=${basedir}/arch.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
49
spec.version.base=0.113
49
spec.version.base=0.114
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
(-)a/java.source/src/org/netbeans/api/java/source/SourceUtils.java (-98 / +27 lines)
Lines 91-102 Link Here
91
import org.netbeans.api.editor.mimelookup.MimePath;
91
import org.netbeans.api.editor.mimelookup.MimePath;
92
import org.netbeans.api.java.classpath.ClassPath;
92
import org.netbeans.api.java.classpath.ClassPath;
93
import org.netbeans.api.java.lexer.JavaTokenId;
93
import org.netbeans.api.java.lexer.JavaTokenId;
94
import org.netbeans.api.java.classpath.GlobalPathRegistry;
95
import org.netbeans.api.java.queries.JavadocForBinaryQuery;
94
import org.netbeans.api.java.queries.JavadocForBinaryQuery;
96
import org.netbeans.api.java.queries.SourceForBinaryQuery;
95
import org.netbeans.api.java.queries.SourceForBinaryQuery;
97
import org.netbeans.api.java.source.ClasspathInfo.PathKind;
96
import org.netbeans.api.java.source.ClasspathInfo.PathKind;
98
import org.netbeans.api.java.source.JavaSource.Phase;
97
import org.netbeans.api.java.source.JavaSource.Phase;
99
import static org.netbeans.api.java.source.SourceUtils.getDependentRootsImpl;
100
import org.netbeans.api.java.source.matching.Matcher;
98
import org.netbeans.api.java.source.matching.Matcher;
101
import org.netbeans.api.java.source.matching.Occurrence;
99
import org.netbeans.api.java.source.matching.Occurrence;
102
import org.netbeans.api.java.source.matching.Pattern;
100
import org.netbeans.api.java.source.matching.Pattern;
Lines 120-126 Link Here
120
import org.netbeans.modules.parsing.api.ResultIterator;
118
import org.netbeans.modules.parsing.api.ResultIterator;
121
import org.netbeans.modules.parsing.api.UserTask;
119
import org.netbeans.modules.parsing.api.UserTask;
122
import org.netbeans.modules.parsing.api.indexing.IndexingManager;
120
import org.netbeans.modules.parsing.api.indexing.IndexingManager;
123
import org.netbeans.modules.parsing.impl.indexing.friendapi.IndexingController;
121
import org.netbeans.modules.parsing.spi.indexing.support.QuerySupport;
124
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
122
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
125
123
126
import org.openide.filesystems.FileObject;
124
import org.openide.filesystems.FileObject;
Lines 672-682 Link Here
672
     * incoming root, never returns null.
670
     * incoming root, never returns null.
673
     * @since 0.10
671
     * @since 0.10
674
     */
672
     */
675
    @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")    //NOI18N
673
    @NonNull
676
    public static Set<URL> getDependentRoots (final URL root) {
674
    @org.netbeans.api.annotations.common.SuppressWarnings(value = {"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")
677
        final Map<URL, List<URL>> sourceDeps = IndexingController.getDefault().getRootDependencies();
675
    public static Set<URL> getDependentRoots (@NonNull final URL root) {
678
        final Map<URL, List<URL>> binaryDeps = IndexingController.getDefault().getBinaryRootDependencies();
676
        return getDependentRoots(root, true);
679
        return getDependentRootsImpl (root, sourceDeps, binaryDeps, true);
680
    }
677
    }
681
    
678
    
682
    /**
679
    /**
Lines 691-787 Link Here
691
     * root, never returns null.
688
     * root, never returns null.
692
     * @since 0.110
689
     * @since 0.110
693
     */
690
     */
694
    @org.netbeans.api.annotations.common.SuppressWarnings(value = {"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")    //NOI18N
691
    @NonNull
695
    public static Set<URL> getDependentRoots(final URL root, boolean filterNonOpenedProjects) {
692
    @org.netbeans.api.annotations.common.SuppressWarnings(value = {"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")
696
        final Map<URL, List<URL>> sourceDeps = IndexingController.getDefault().getRootDependencies();
693
    public static Set<URL> getDependentRoots(
697
        final Map<URL, List<URL>> binaryDeps = IndexingController.getDefault().getBinaryRootDependencies();
694
        @NonNull final URL root,
698
        return getDependentRootsImpl(root, sourceDeps, binaryDeps, filterNonOpenedProjects);
695
        final boolean filterNonOpenedProjects) {
696
        final FileObject rootFO = URLMapper.findFileObject(root);
697
        if (rootFO != null) {
698
            return mapToURLs(QuerySupport.findDependentRoots(rootFO,filterNonOpenedProjects));
699
        } else {
700
            return Collections.<URL>singleton(root);
701
        }
699
    }
702
    }
700
703
        
701
    @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")    //NOI18N
702
    static Set<URL> getDependentRootsImpl (final URL root, final Map<URL, List<URL>> sourceDeps, Map<URL, List<URL>> binaryDeps, boolean filterNonOpenedProjects) {
703
        Set<URL> urls;
704
705
        if (sourceDeps.containsKey(root)) {
706
            urls = findReverseSourceRoots(root, sourceDeps);
707
        } else {
708
            FileObject rootFO = URLMapper.findFileObject(root);
709
710
            if (rootFO != null) {
711
                urls = new HashSet<URL>();
712
713
                for (URL binary : findBinaryRootsForSourceRoot(rootFO, binaryDeps)) {
714
                    List<URL> deps = binaryDeps.get(binary);
715
716
                    if (deps != null) {
717
                        urls.addAll(deps);
718
                    }
719
                }
720
            } else {
721
                urls = new HashSet<URL>();
722
            }
723
        }
724
725
        if(filterNonOpenedProjects) {
726
            Set<ClassPath> cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE);
727
            Set<URL> toRetain = new HashSet<URL>();
728
            for (ClassPath cp : cps) {
729
                for (ClassPath.Entry e : cp.entries()) {
730
                    toRetain.add(e.getURL());
731
                }
732
            }
733
            urls.retainAll(toRetain);
734
        }
735
        return urls;
736
    }    
737
    
738
    private static Set<URL> findReverseSourceRoots(final URL thisSourceRoot, Map<URL, List<URL>> deps) {
739
        //Create inverse dependencies
740
        final Map<URL, List<URL>> inverseDeps = new HashMap<URL, List<URL>> ();
741
        for (Map.Entry<URL,List<URL>> entry : deps.entrySet()) {
742
            final URL u1 = entry.getKey();
743
            final List<URL> l1 = entry.getValue();
744
            for (URL u2 : l1) {
745
                List<URL> l2 = inverseDeps.get(u2);
746
                if (l2 == null) {
747
                    l2 = new ArrayList<URL>();
748
                    inverseDeps.put (u2,l2);
749
                }
750
                l2.add (u1);
751
            }
752
        }
753
        //Collect dependencies
754
        final Set<URL> result = new HashSet<URL>();
755
        final LinkedList<URL> todo = new LinkedList<URL> ();
756
        todo.add (thisSourceRoot);
757
        while (!todo.isEmpty()) {
758
            final URL u = todo.removeFirst();
759
            if (!result.contains(u)) {
760
                result.add (u);
761
                final List<URL> ideps = inverseDeps.get(u);
762
                if (ideps != null) {
763
                    todo.addAll (ideps);
764
                }
765
            }
766
        }
767
768
        return result;
769
    }
770
771
    private static Set<URL> findBinaryRootsForSourceRoot(FileObject sourceRoot, Map<URL, List<URL>> binaryDeps) {
772
        Set<URL> result = new HashSet<URL>();
773
774
        for (URL bin : binaryDeps.keySet()) {
775
            for (FileObject s : SourceForBinaryQuery.findSourceRoots(bin).getRoots()) {
776
                if (s == sourceRoot) {
777
                    result.add(bin);
778
                }
779
            }
780
        }
781
782
        return result;
783
    }
784
785
    //Helper methods
704
    //Helper methods
786
    
705
    
787
    /**
706
    /**
Lines 1267-1270 Link Here
1267
            return s.toLowerCase();
1186
            return s.toLowerCase();
1268
        }
1187
        }
1269
    }
1188
    }
1189
1190
    @NonNull
1191
    private static Set<URL> mapToURLs(
1192
        @NonNull final Collection<? extends FileObject> fos) {
1193
        final Set<URL> res = new HashSet<URL>(fos.size());
1194
        for (FileObject fo : fos) {
1195
            res.add(fo.toURL());
1196
        }
1197
        return res;
1198
    }
1270
}
1199
}
(-)a/parsing.api/apichanges.xml (+16 lines)
Lines 110-115 Link Here
110
<!-- ACTUAL CHANGES BEGIN HERE: -->
110
<!-- ACTUAL CHANGES BEGIN HERE: -->
111
111
112
  <changes>
112
  <changes>
113
  <change id="QuerySupport.findDependentRoots">
114
      <api name="ParsingAPI"/>
115
      <summary>Added <code>QuerySupport.findDependentRoots</code> to find out source roots depending on given source root.</summary>
116
      <version major="1" minor="64"/>
117
      <date day="18" month="3" year="2013"/>
118
      <author login="tzezula"/>
119
      <compatibility source="compatible" binary="compatible" semantic="compatible" deletion="no" addition="yes" modification="no"/>
120
      <description>
121
          <p>
122
              Added a <code>QuerySupport.findDependentRoots</code> method returning the dependent source roots for given source root.
123
              It returns all the source roots which have either direct or transitive dependency on the given source root.
124
          </p>
125
      </description>
126
      <class package="org.netbeans.modules.parsing.spi.indexing.support" name="QuerySupport"/>
127
      <issue number="227363"/>
128
  </change>
113
  <change id="EmbeddingProvider.Registration">
129
  <change id="EmbeddingProvider.Registration">
114
      <api name="ParsingAPI"/>
130
      <api name="ParsingAPI"/>
115
      <summary>Added <code>EmbeddingProvider.Registration</code> annotation to register <code>EmbeddingProvider</code>s.</summary>
131
      <summary>Added <code>EmbeddingProvider.Registration</code> annotation to register <code>EmbeddingProvider</code>s.</summary>
(-)a/parsing.api/nbproject/project.properties (-1 / +1 lines)
Lines 3-9 Link Here
3
javac.source=1.6
3
javac.source=1.6
4
javadoc.apichanges=${basedir}/apichanges.xml
4
javadoc.apichanges=${basedir}/apichanges.xml
5
javadoc.arch=${basedir}/arch.xml
5
javadoc.arch=${basedir}/arch.xml
6
spec.version.base=1.63.0
6
spec.version.base=1.64.0
7
7
8
test.config.stableBTD.includes=**/*Test.class
8
test.config.stableBTD.includes=**/*Test.class
9
test.config.stableBTD.excludes=\
9
test.config.stableBTD.excludes=\
(-)a/parsing.api/src/org/netbeans/modules/parsing/spi/indexing/support/QuerySupport.java (-1 / +174 lines)
Lines 60-66 Link Here
60
import java.util.concurrent.Callable;
60
import java.util.concurrent.Callable;
61
import java.util.logging.Level;
61
import java.util.logging.Level;
62
import java.util.logging.Logger;
62
import java.util.logging.Logger;
63
import org.netbeans.api.annotations.common.CheckForNull;
64
import org.netbeans.api.annotations.common.NonNull;
63
import org.netbeans.api.java.classpath.ClassPath;
65
import org.netbeans.api.java.classpath.ClassPath;
66
import org.netbeans.api.java.classpath.GlobalPathRegistry;
67
import org.netbeans.api.java.queries.SourceForBinaryQuery;
64
import org.netbeans.api.project.FileOwnerQuery;
68
import org.netbeans.api.project.FileOwnerQuery;
65
import org.netbeans.api.project.Project;
69
import org.netbeans.api.project.Project;
66
import org.netbeans.modules.parsing.impl.Installer;
70
import org.netbeans.modules.parsing.impl.Installer;
Lines 76-81 Link Here
76
import org.netbeans.modules.parsing.impl.indexing.TransientUpdateSupport;
80
import org.netbeans.modules.parsing.impl.indexing.TransientUpdateSupport;
77
import org.netbeans.modules.parsing.impl.indexing.URLCache;
81
import org.netbeans.modules.parsing.impl.indexing.URLCache;
78
import org.netbeans.modules.parsing.impl.indexing.Util;
82
import org.netbeans.modules.parsing.impl.indexing.Util;
83
import org.netbeans.modules.parsing.impl.indexing.friendapi.IndexingController;
79
import org.netbeans.modules.parsing.impl.indexing.lucene.LayeredDocumentIndex;
84
import org.netbeans.modules.parsing.impl.indexing.lucene.LayeredDocumentIndex;
80
import org.netbeans.modules.parsing.impl.indexing.lucene.LuceneIndexFactory;
85
import org.netbeans.modules.parsing.impl.indexing.lucene.LuceneIndexFactory;
81
import org.netbeans.modules.parsing.lucene.support.DocumentIndex;
86
import org.netbeans.modules.parsing.lucene.support.DocumentIndex;
Lines 83-88 Link Here
83
import org.netbeans.modules.parsing.lucene.support.Queries;
88
import org.netbeans.modules.parsing.lucene.support.Queries;
84
import org.openide.filesystems.FileObject;
89
import org.openide.filesystems.FileObject;
85
import org.openide.filesystems.FileStateInvalidException;
90
import org.openide.filesystems.FileStateInvalidException;
91
import org.openide.filesystems.URLMapper;
86
import org.openide.util.Parameters;
92
import org.openide.util.Parameters;
87
93
88
/**
94
/**
Lines 155-160 Link Here
155
    }
161
    }
156
162
157
    /**
163
    /**
164
     * Returns the dependent source roots for given source root. It returns
165
     * all the source roots which have either direct or transitive dependency on
166
     * the given source root.
167
     *
168
     * @param root to find the dependent roots for
169
     * @param filterNonOpenedProjects if true the results contains only roots of
170
     * opened projects
171
     * @return {@link Collection} of {@link FileObject}s containing at least the incoming
172
     * root, never returns null.
173
     * @since 1.64
174
     */
175
    @NonNull
176
    @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")
177
    public static Collection<FileObject> findDependentRoots(
178
            @NonNull final FileObject file,
179
            final boolean filterNonOpenedProjects) {
180
        Parameters.notNull("file", file);   //NOI18N
181
182
        final URL root = findOwnerRoot(file);
183
        if (root == null) {
184
            return Collections.<FileObject>emptySet();
185
        }
186
        final IndexingController ic = IndexingController.getDefault();
187
        final Map<URL, List<URL>> binaryDeps = ic.getBinaryRootDependencies();
188
        final Map<URL, List<URL>> sourceDeps = ic.getRootDependencies();
189
        final Map<URL, List<URL>> peerDeps = ic.getRootPeers();
190
        Set<URL> urls;
191
192
        if (sourceDeps.containsKey(root)) {
193
            urls = findReverseSourceRoots(root, sourceDeps, peerDeps);
194
        } else {
195
            urls = new HashSet<URL>();
196
            final FileObject rootFo = URLMapper.findFileObject(root);
197
            if (rootFo != null) {
198
                for (URL binary : findBinaryRootsForSourceRoot(rootFo, binaryDeps)) {
199
                    List<URL> deps = binaryDeps.get(binary);
200
                    if (deps != null) {
201
                        urls.addAll(deps);
202
                    }
203
                }
204
            }
205
        }
206
207
        if(filterNonOpenedProjects) {
208
            final GlobalPathRegistry gpr = GlobalPathRegistry.getDefault();
209
            final Set<ClassPath> cps = new HashSet<ClassPath>();
210
            for (String id : PathRecognizerRegistry.getDefault().getSourceIds()) {
211
                cps.addAll(gpr.getPaths(id));
212
            }
213
            Set<URL> toRetain = new HashSet<URL>();
214
            for (ClassPath cp : cps) {
215
                for (ClassPath.Entry e : cp.entries()) {
216
                    toRetain.add(e.getURL());
217
                }
218
            }
219
            urls.retainAll(toRetain);
220
        }
221
        return mapToFileObjects(urls);
222
    }
223
224
    /**
158
     * Gets classpath roots relevant for a project. This method tries to find
225
     * Gets classpath roots relevant for a project. This method tries to find
159
     * classpaths with <code>sourcePathIds</code>, <code>libraryPathIds</code> and
226
     * classpaths with <code>sourcePathIds</code>, <code>libraryPathIds</code> and
160
     * <code>binaryPathIds</code> supplied by the <code>project</code>.
227
     * <code>binaryPathIds</code> supplied by the <code>project</code>.
Lines 470-476 Link Here
470
            case CASE_INSENSITIVE_CAMEL_CASE: return Queries.QueryKind.CASE_INSENSITIVE_CAMEL_CASE;
537
            case CASE_INSENSITIVE_CAMEL_CASE: return Queries.QueryKind.CASE_INSENSITIVE_CAMEL_CASE;
471
            default: throw new UnsupportedOperationException (kind.toString());
538
            default: throw new UnsupportedOperationException (kind.toString());
472
        }
539
        }
473
    } 
540
    }
541
542
    @CheckForNull
543
    private static URL findOwnerRoot(@NonNull final FileObject file) {
544
        final PathRecognizerRegistry regs = PathRecognizerRegistry.getDefault();
545
        URL res = findOwnerRoot(file, regs.getSourceIds());
546
        if (res != null) {
547
            return res;
548
        }
549
        res = findOwnerRoot(file, regs.getBinaryLibraryIds());
550
        if (res != null) {
551
            return res;
552
        }
553
        res = findOwnerRoot(file, regs.getLibraryIds());
554
        if (res != null) {
555
            return res;
556
        }
557
        //Fallback for roots with wrong classpath
558
        return file.isFolder() ? file.toURL() : null;
559
    }
560
561
    @CheckForNull
562
    private static URL findOwnerRoot(
563
        @NonNull final FileObject file,
564
        @NonNull final Collection<? extends String> ids) {
565
        for (String id : ids) {
566
            final ClassPath cp = ClassPath.getClassPath(file, id);
567
            if (cp != null) {
568
                final FileObject owner = cp.findOwnerRoot(file);
569
                if (owner != null) {
570
                    return owner.toURL();
571
                }
572
            }
573
        }
574
        return null;
575
    }
576
577
    @NonNull
578
    @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")
579
    private static Set<URL> findReverseSourceRoots(
580
            @NonNull final URL thisSourceRoot,
581
            @NonNull final Map<URL, List<URL>> deps,
582
            @NonNull final Map<URL, List<URL>> peers) {
583
        //Create inverse dependencies
584
        final Map<URL, List<URL>> inverseDeps = new HashMap<URL, List<URL>> ();
585
        for (Map.Entry<URL,List<URL>> entry : deps.entrySet()) {
586
            final URL u1 = entry.getKey();
587
            final List<URL> l1 = entry.getValue();
588
            for (URL u2 : l1) {
589
                List<URL> l2 = inverseDeps.get(u2);
590
                if (l2 == null) {
591
                    l2 = new ArrayList<URL>();
592
                    inverseDeps.put (u2,l2);
593
                }
594
                l2.add (u1);
595
            }
596
        }
597
        //Collect dependencies
598
        final Set<URL> result = new HashSet<URL>();
599
        final LinkedList<URL> todo = new LinkedList<URL> ();
600
        todo.add (thisSourceRoot);
601
        while (!todo.isEmpty()) {
602
            final URL u = todo.removeFirst();
603
            if (!result.contains(u)) {
604
                result.add (u);
605
                List<URL> ideps = inverseDeps.get(u);
606
                if (ideps != null) {
607
                    todo.addAll (ideps);
608
                }                
609
                ideps = peers.get(u);
610
                if (ideps != null) {
611
                    todo.addAll (ideps);
612
                }
613
            }
614
        }
615
616
        return result;
617
    }
618
619
    @NonNull
620
    @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}, justification="URLs have never host part")
621
    private static Set<URL> findBinaryRootsForSourceRoot(
622
            @NonNull final FileObject sourceRoot,
623
            @NonNull final Map<URL, List<URL>> binaryDeps) {
624
        Set<URL> result = new HashSet<URL>();
625
        for (URL bin : binaryDeps.keySet()) {
626
            for (FileObject fo : SourceForBinaryQuery.findSourceRoots(bin).getRoots()) {
627
                if (sourceRoot.equals(fo)) {
628
                    result.add(bin);
629
                }
630
            }
631
        }
632
        return result;
633
    }
634
635
    @NonNull
636
    private static Collection<FileObject> mapToFileObjects(
637
        @NonNull final Collection<? extends URL> urls) {
638
        final Collection<FileObject> result = new ArrayList<FileObject>(urls.size());
639
        for (URL u : urls) {
640
            final FileObject fo = URLMapper.findFileObject(u);
641
            if (fo != null) {
642
                result.add(fo);
643
            }
644
        }
645
        return result;
646
    }
474
647
475
    /* test */ static final class IndexerQuery {
648
    /* test */ static final class IndexerQuery {
476
649
(-)a/parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/RepositoryUpdaterTest.java (-3 / +3 lines)
Lines 161-168 Link Here
161
public class RepositoryUpdaterTest extends NbTestCase {
161
public class RepositoryUpdaterTest extends NbTestCase {
162
162
163
163
164
    static final int TIME = Integer.getInteger("RepositoryUpdaterTest.timeout", 5000);                 //NOI18N
164
    public static final int TIME = Integer.getInteger("RepositoryUpdaterTest.timeout", 5000);                 //NOI18N
165
    static final int NEGATIVE_TIME = Integer.getInteger("RepositoryUpdaterTest.negative-timeout", 5000); //NOI18N
165
    public static final int NEGATIVE_TIME = Integer.getInteger("RepositoryUpdaterTest.negative-timeout", 5000); //NOI18N
166
    private static final String SOURCES = "FOO_SOURCES";
166
    private static final String SOURCES = "FOO_SOURCES";
167
    private static final String PLATFORM = "FOO_PLATFORM";
167
    private static final String PLATFORM = "FOO_PLATFORM";
168
    private static final String LIBS = "FOO_LIBS";
168
    private static final String LIBS = "FOO_LIBS";
Lines 335-341 Link Here
335
        }
335
        }
336
    }
336
    }
337
337
338
    /* package */ static void waitForRepositoryUpdaterInit() throws Exception {
338
    public static void waitForRepositoryUpdaterInit() throws Exception {
339
        RepositoryUpdater.getDefault().ignoreIndexerCacheEvents(true);
339
        RepositoryUpdater.getDefault().ignoreIndexerCacheEvents(true);
340
        RepositoryUpdater.getDefault().start(true);
340
        RepositoryUpdater.getDefault().start(true);
341
        RepositoryUpdater.State state;
341
        RepositoryUpdater.State state;
(-)a/parsing.api/test/unit/src/org/netbeans/modules/parsing/spi/indexing/support/QuerySupportTest.java (+616 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.parsing.spi.indexing.support;
43
44
import java.io.File;
45
import java.io.IOException;
46
import java.net.URL;
47
import java.util.Arrays;
48
import java.util.Collection;
49
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.HashSet;
52
import java.util.IdentityHashMap;
53
import java.util.List;
54
import java.util.Map;
55
import java.util.Set;
56
import javax.swing.event.ChangeListener;
57
import org.netbeans.api.java.classpath.ClassPath;
58
import org.netbeans.api.java.classpath.GlobalPathRegistry;
59
import org.netbeans.api.java.queries.SourceForBinaryQuery;
60
import org.netbeans.junit.MockServices;
61
import org.netbeans.junit.NbTestCase;
62
import org.netbeans.modules.parsing.impl.indexing.Pair;
63
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater;
64
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest;
65
import org.netbeans.modules.parsing.impl.indexing.friendapi.IndexingController;
66
import org.netbeans.modules.parsing.lucene.support.LowMemoryWatcher;
67
import org.netbeans.modules.parsing.spi.indexing.PathRecognizer;
68
import org.netbeans.spi.java.classpath.ClassPathProvider;
69
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
70
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2;
71
import org.openide.filesystems.FileObject;
72
import org.openide.filesystems.FileUtil;
73
74
/**
75
 *
76
 * @author Tomas Zezula
77
 */
78
public class QuerySupportTest extends NbTestCase {
79
80
    private final Map<String, Set<ClassPath>> registeredClasspaths = new HashMap<String, Set<ClassPath>>();
81
82
    public QuerySupportTest(String name) {
83
        super(name);
84
    }
85
86
    @Override
87
    protected void setUp() throws Exception {
88
        super.setUp();
89
        clearWorkDir();
90
        MockServices.setServices(
91
                JavaLikePathRecognizer.class,
92
                ScriptingLikePathRecognizer.class,
93
                ClassPathProviderImpl.class,
94
                SourceForBinaryQueryImpl.class);
95
        RepositoryUpdaterTest.waitForRepositoryUpdaterInit();
96
    }
97
98
    @Override
99
    protected void tearDown() throws Exception {        
100
        for(String id : registeredClasspaths.keySet()) {
101
            final Set<ClassPath> classpaths = registeredClasspaths.get(id);
102
            GlobalPathRegistry.getDefault().unregister(id, classpaths.toArray(new ClassPath[classpaths.size()]));
103
        }
104
        registeredClasspaths.clear();
105
        ClassPathProviderImpl.register(Collections.<FileObject,Map<String,ClassPath>>emptyMap());
106
        SourceForBinaryQueryImpl.register(Collections.<URL,Pair<Boolean,List<FileObject>>>emptyMap());
107
        LowMemoryWatcher.getInstance().free();
108
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
109
        super.tearDown();
110
    }
111
112
113
114
    public void testGetDependentRootsJavaLikeDependenciesAllProjectsOpened() throws IOException, InterruptedException {
115
        final FileObject wd = FileUtil.toFileObject(getWorkDir());
116
        final FileObject src1 = FileUtil.createFolder(wd,"src1");   //NOI18N
117
        final URL build1 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build1"));//NOI18N
118
        final FileObject src2 = FileUtil.createFolder(wd,"src2");   //NOI18N
119
        final URL build2 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build2"));    //NOI18N
120
        final FileObject src3 = FileUtil.createFolder(wd,"src3");   //NOI18N
121
        final URL build3 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build3"));    //NOI18N
122
        final FileObject src4 = FileUtil.createFolder(wd,"src4");   //NOI18N
123
        final URL build4 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build4"));   //NOI18N
124
125
        final ClassPath src1Source = ClassPathSupport.createClassPath(src1);
126
        final ClassPath src2Source = ClassPathSupport.createClassPath(src2);
127
        final ClassPath src2Compile = ClassPathSupport.createClassPath(build1);
128
        final ClassPath src3Source = ClassPathSupport.createClassPath(src3);
129
        final ClassPath src3Compile = ClassPathSupport.createClassPath(build1);
130
        final ClassPath src4Source = ClassPathSupport.createClassPath(src4);
131
        final ClassPath src4Compile = ClassPathSupport.createClassPath(build2);
132
133
        final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>();
134
        sfbq.put(build1,Pair.of(true,Collections.singletonList(src1)));
135
        sfbq.put(build2,Pair.of(true,Collections.singletonList(src2)));
136
        sfbq.put(build3,Pair.of(true,Collections.singletonList(src3)));
137
        sfbq.put(build4,Pair.of(true,Collections.singletonList(src4)));
138
        SourceForBinaryQueryImpl.register(sfbq);
139
140
141
        final ClassPath boot = ClassPath.EMPTY;
142
        final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>();
143
        src1cps.put(JavaLikePathRecognizer.BOOT, boot);
144
        src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY);
145
        src1cps.put(JavaLikePathRecognizer.SRC, src1Source);
146
        final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>();
147
        src2cps.put(JavaLikePathRecognizer.BOOT, boot);
148
        src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile);
149
        src2cps.put(JavaLikePathRecognizer.SRC, src2Source);
150
        final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>();
151
        src3cps.put(JavaLikePathRecognizer.BOOT, boot);
152
        src3cps.put(JavaLikePathRecognizer.COMPILE, src3Compile);
153
        src3cps.put(JavaLikePathRecognizer.SRC, src3Source);
154
        final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>();
155
        src4cps.put(JavaLikePathRecognizer.BOOT, boot);
156
        src4cps.put(JavaLikePathRecognizer.COMPILE, src4Compile);
157
        src4cps.put(JavaLikePathRecognizer.SRC, src4Source);
158
        final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>();
159
        cps.put(src1,src1cps);
160
        cps.put(src2,src2cps);
161
        cps.put(src3,src3cps);
162
        cps.put(src4,src4cps);
163
        ClassPathProviderImpl.register(cps);
164
165
        globalPathRegistry_register(
166
                JavaLikePathRecognizer.BOOT,
167
                new ClassPath[]{
168
                    boot
169
                });
170
        globalPathRegistry_register(
171
                JavaLikePathRecognizer.COMPILE,
172
                new ClassPath[]{
173
                    src2Compile,
174
                    src3Compile,
175
                    src4Compile
176
                });
177
        globalPathRegistry_register(
178
                JavaLikePathRecognizer.SRC,
179
                new ClassPath[]{
180
                    src1Source,
181
                    src2Source,
182
                    src3Source,
183
                    src4Source
184
                });
185
186
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
187
        assertEquals(
188
                IndexingController.getDefault().getRootDependencies().keySet().toString(),
189
                4,
190
                IndexingController.getDefault().getRootDependencies().size());
191
192
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true));
193
        assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, true));
194
        assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, true));
195
        assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, true));
196
197
198
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false));
199
        assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, false));
200
        assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false));
201
        assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, false));
202
    }
203
204
205
    public void testGetDependentRootsJavaLikeDependenciesProjectsClosed() throws IOException, InterruptedException {
206
        final FileObject wd = FileUtil.toFileObject(getWorkDir());
207
        final FileObject src1 = FileUtil.createFolder(wd,"src1");   //NOI18N
208
        final URL build1 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build1"));//NOI18N
209
        final FileObject src2 = FileUtil.createFolder(wd,"src2");   //NOI18N
210
        final URL build2 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build2"));    //NOI18N
211
        final FileObject src3 = FileUtil.createFolder(wd,"src3");   //NOI18N
212
        final URL build3 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build3"));    //NOI18N
213
        final FileObject src4 = FileUtil.createFolder(wd,"src4");   //NOI18N
214
        final URL build4 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build4"));   //NOI18N
215
216
        final ClassPath src1Source = ClassPathSupport.createClassPath(src1);
217
        final ClassPath src2Source = ClassPathSupport.createClassPath(src2);
218
        final ClassPath src2Compile = ClassPathSupport.createClassPath(build1);
219
        final ClassPath src3Source = ClassPathSupport.createClassPath(src3);
220
        final ClassPath src3Compile = ClassPathSupport.createClassPath(build1);
221
        final ClassPath src4Source = ClassPathSupport.createClassPath(src4);
222
        final ClassPath src4Compile = ClassPathSupport.createClassPath(build2);
223
224
        final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>();
225
        sfbq.put(build1,Pair.of(true,Collections.singletonList(src1)));
226
        sfbq.put(build2,Pair.of(true,Collections.singletonList(src2)));
227
        sfbq.put(build3,Pair.of(true,Collections.singletonList(src3)));
228
        sfbq.put(build4,Pair.of(true,Collections.singletonList(src4)));
229
        SourceForBinaryQueryImpl.register(sfbq);
230
231
232
        final ClassPath boot = ClassPath.EMPTY;
233
        final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>();
234
        src1cps.put(JavaLikePathRecognizer.BOOT, boot);
235
        src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY);
236
        src1cps.put(JavaLikePathRecognizer.SRC, src1Source);
237
        final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>();
238
        src2cps.put(JavaLikePathRecognizer.BOOT, boot);
239
        src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile);
240
        src2cps.put(JavaLikePathRecognizer.SRC, src2Source);
241
        final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>();
242
        src3cps.put(JavaLikePathRecognizer.BOOT, boot);
243
        src3cps.put(JavaLikePathRecognizer.COMPILE, src3Compile);
244
        src3cps.put(JavaLikePathRecognizer.SRC, src3Source);
245
        final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>();
246
        src4cps.put(JavaLikePathRecognizer.BOOT, boot);
247
        src4cps.put(JavaLikePathRecognizer.COMPILE, src4Compile);
248
        src4cps.put(JavaLikePathRecognizer.SRC, src4Source);
249
        final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>();
250
        cps.put(src1,src1cps);
251
        cps.put(src2,src2cps);
252
        cps.put(src3,src3cps);
253
        cps.put(src4,src4cps);
254
        ClassPathProviderImpl.register(cps);
255
256
        globalPathRegistry_register(
257
                JavaLikePathRecognizer.BOOT,
258
                new ClassPath[]{
259
                    boot
260
                });
261
        globalPathRegistry_register(
262
                JavaLikePathRecognizer.COMPILE,
263
                new ClassPath[]{
264
                    src4Compile
265
                });
266
        globalPathRegistry_register(
267
                JavaLikePathRecognizer.SRC,
268
                new ClassPath[]{
269
                    src4Source
270
                });
271
272
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
273
        assertEquals(
274
                IndexingController.getDefault().getRootDependencies().keySet().toString(),
275
                3,
276
                IndexingController.getDefault().getRootDependencies().size());
277
278
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true));       
279
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src2, true));
280
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src1, true));
281
282
283
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false));
284
        assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false));
285
        assertEquals(new FileObject[] {src1, src2, src4}, QuerySupport.findDependentRoots(src1, false));
286
    }
287
288
289
    public void testGetDependentRootsJavaLikeBinaryDependencies() throws IOException, InterruptedException {
290
291
        IndexingController.getDefault().getRootDependencies().keySet();
292
293
        final FileObject wd = FileUtil.toFileObject(getWorkDir());
294
        final FileObject bin1 = FileUtil.createFolder(wd, "bin1");  //NOI18N
295
        final FileObject bin1Sources = FileUtil.createFolder(wd, "bin1src");    //NOI18N
296
        final FileObject src1 = FileUtil.createFolder(wd,"src1");   //NOI18N
297
        final FileObject src2 = FileUtil.createFolder(wd,"src2");   //NOI18N
298
        final FileObject src3 = FileUtil.createFolder(wd,"src3");   //NOI18N
299
300
        final ClassPath src1Source = ClassPathSupport.createClassPath(src1);
301
        final ClassPath src1Compile = ClassPathSupport.createClassPath(bin1);
302
        final ClassPath src2Source = ClassPathSupport.createClassPath(src2);
303
        final ClassPath src2Compile = ClassPathSupport.createClassPath(bin1);
304
        final ClassPath src3Source = ClassPathSupport.createClassPath(src3);        
305
306
        final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>();
307
        sfbq.put(bin1.toURL(),Pair.of(false,Collections.singletonList(bin1Sources)));
308
        SourceForBinaryQueryImpl.register(sfbq);
309
310
311
        final ClassPath boot = ClassPath.EMPTY;
312
        final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>();
313
        src1cps.put(JavaLikePathRecognizer.BOOT, boot);
314
        src1cps.put(JavaLikePathRecognizer.COMPILE, src1Compile);
315
        src1cps.put(JavaLikePathRecognizer.SRC, src1Source);
316
        final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>();
317
        src2cps.put(JavaLikePathRecognizer.BOOT, boot);
318
        src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile);
319
        src2cps.put(JavaLikePathRecognizer.SRC, src2Source);
320
        final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>();
321
        src3cps.put(JavaLikePathRecognizer.BOOT, boot);
322
        src3cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY);
323
        src3cps.put(JavaLikePathRecognizer.SRC, src3Source);        
324
        final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>();
325
        cps.put(src1,src1cps);
326
        cps.put(src2,src2cps);
327
        cps.put(src3,src3cps);
328
        ClassPathProviderImpl.register(cps);
329
330
        globalPathRegistry_register(
331
                JavaLikePathRecognizer.BOOT,
332
                new ClassPath[]{
333
                    boot
334
                });
335
        globalPathRegistry_register(
336
                JavaLikePathRecognizer.COMPILE,
337
                new ClassPath[]{
338
                    src1Compile,
339
                    src2Compile                    
340
                });
341
        globalPathRegistry_register(
342
                JavaLikePathRecognizer.SRC,
343
                new ClassPath[]{
344
                    src1Source,
345
                    src2Source,
346
                    src3Source
347
                });
348
349
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
350
        assertEquals(
351
                IndexingController.getDefault().getRootDependencies().keySet().toString(),
352
                3,
353
                IndexingController.getDefault().getRootDependencies().size());
354
355
        assertEquals(new FileObject[] {src1, src2}, QuerySupport.findDependentRoots(bin1Sources, true));
356
    }
357
358
    public void testGetDependentRootsJavaLikeDependenciesWithPeerRoots() throws IOException, InterruptedException {
359
        final FileObject wd = FileUtil.toFileObject(getWorkDir());
360
        final FileObject src1a = FileUtil.createFolder(wd,"src1a");   //NOI18N
361
        final FileObject src1b = FileUtil.createFolder(wd,"src1b");   //NOI18N
362
363
        final ClassPath src1Source = ClassPathSupport.createClassPath(src1a, src1b);
364
365
366
        final ClassPath boot = ClassPath.EMPTY;
367
        final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>();
368
        src1cps.put(JavaLikePathRecognizer.BOOT, boot);
369
        src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY);
370
        src1cps.put(JavaLikePathRecognizer.SRC, src1Source);        
371
        final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>();
372
        cps.put(src1a,src1cps);
373
        cps.put(src1b,src1cps);
374
        ClassPathProviderImpl.register(cps);
375
376
        globalPathRegistry_register(
377
                JavaLikePathRecognizer.BOOT,
378
                new ClassPath[]{
379
                    boot
380
                });        
381
        globalPathRegistry_register(
382
                JavaLikePathRecognizer.SRC,
383
                new ClassPath[]{
384
                    src1Source
385
                });
386
387
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
388
        assertEquals(
389
                IndexingController.getDefault().getRootDependencies().keySet().toString(),
390
                2,
391
                IndexingController.getDefault().getRootDependencies().size());
392
393
        assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1a, true));
394
        assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1b, true));
395
396
        assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1a, false));
397
        assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1b, false));
398
    }
399
400
    public void testGetDependentRootsScriptingLikeDependenciesAllProjectsOpened() throws IOException, InterruptedException {
401
        final FileObject wd = FileUtil.toFileObject(getWorkDir());
402
        final FileObject libSrc = FileUtil.createFolder(wd,"libSrc");   //NOI18N
403
        final FileObject src1 = FileUtil.createFolder(wd,"src1");   //NOI18N
404
        final FileObject src2 = FileUtil.createFolder(wd,"src2");   //NOI18N
405
        final FileObject src3 = FileUtil.createFolder(wd,"src3");   //NOI18N
406
        final FileObject src4 = FileUtil.createFolder(wd,"src4");   //NOI18N
407
408
        final ClassPath src1Source = ClassPathSupport.createClassPath(src1);
409
        final ClassPath src1Compile = ClassPathSupport.createClassPath(libSrc);
410
        final ClassPath src2Source = ClassPathSupport.createClassPath(src2);
411
        final ClassPath src2Compile = ClassPathSupport.createClassPath(src1);
412
        final ClassPath src3Source = ClassPathSupport.createClassPath(src3);
413
        final ClassPath src3Compile = ClassPathSupport.createClassPath(src1);
414
        final ClassPath src4Source = ClassPathSupport.createClassPath(src4);
415
        final ClassPath src4Compile = ClassPathSupport.createClassPath(src2);
416
417
418
        final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>();
419
        src1cps.put(ScriptingLikePathRecognizer.COMPILE, src1Compile);
420
        src1cps.put(ScriptingLikePathRecognizer.SRC, src1Source);
421
        final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>();
422
        src2cps.put(ScriptingLikePathRecognizer.COMPILE, src2Compile);
423
        src2cps.put(ScriptingLikePathRecognizer.SRC, src2Source);
424
        final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>();
425
        src3cps.put(ScriptingLikePathRecognizer.COMPILE, src3Compile);
426
        src3cps.put(ScriptingLikePathRecognizer.SRC, src3Source);
427
        final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>();
428
        src4cps.put(ScriptingLikePathRecognizer.COMPILE, src4Compile);
429
        src4cps.put(ScriptingLikePathRecognizer.SRC, src4Source);
430
        final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>();
431
        cps.put(src1,src1cps);
432
        cps.put(src2,src2cps);
433
        cps.put(src3,src3cps);
434
        cps.put(src4,src4cps);
435
        ClassPathProviderImpl.register(cps);
436
437
        globalPathRegistry_register(
438
                ScriptingLikePathRecognizer.COMPILE,
439
                new ClassPath[]{
440
                    src2Compile,
441
                    src3Compile,
442
                    src4Compile
443
                });
444
        globalPathRegistry_register(
445
                ScriptingLikePathRecognizer.SRC,
446
                new ClassPath[]{
447
                    src1Source,
448
                    src2Source,
449
                    src3Source,
450
                    src4Source
451
                });
452
453
        RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME);
454
        assertEquals(
455
                IndexingController.getDefault().getRootDependencies().keySet().toString(),
456
                5,
457
                IndexingController.getDefault().getRootDependencies().size());
458
459
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true));
460
        assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, true));
461
        assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, true));
462
        assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, true));
463
        assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(libSrc, true));
464
465
        assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false));
466
        assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, false));
467
        assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false));
468
        assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, false));
469
        assertEquals(new FileObject[] {libSrc, src1, src2, src3, src4}, QuerySupport.findDependentRoots(libSrc, false));
470
    }
471
472
    private void assertEquals(FileObject[] expected, Collection<FileObject> res) {
473
        final Set<FileObject> set = new HashSet<FileObject>();
474
        Collections.addAll(set, expected);
475
        for (FileObject fo : res) {
476
            assertTrue(
477
                String.format("expected: %s res: %s", Arrays.toString(expected), res),  //NOI18N
478
                set.remove(fo));
479
        }
480
        assertTrue(
481
            String.format("expected: %s res: %s", Arrays.toString(expected), res),      //NOI8N
482
            set.isEmpty());
483
    }
484
485
    private void globalPathRegistry_register(String id, ClassPath [] classpaths) {
486
        Set<ClassPath> set = registeredClasspaths.get(id);
487
        if (set == null) {
488
            set = Collections.newSetFromMap(new IdentityHashMap<ClassPath, Boolean>());
489
            registeredClasspaths.put(id, set);
490
        }
491
        for (ClassPath cp :  classpaths) {
492
            set.add(cp);
493
        }
494
        GlobalPathRegistry.getDefault().register(id, classpaths);
495
    }    
496
497
    public static final class JavaLikePathRecognizer extends PathRecognizer {
498
499
        public static final String SRC = "source";          //NOI18N
500
        public static final String COMPILE = "compile";     //NOI18N
501
        public static final String BOOT = "boot";           //NOI18N
502
        public static final String MIME_JAVA = "text/x-java";   //NOI18N
503
504
        @Override
505
        public Set<String> getSourcePathIds() {
506
            return Collections.singleton(SRC);
507
        }
508
509
        @Override
510
        public Set<String> getLibraryPathIds() {
511
            return Collections.<String>emptySet();
512
        }
513
514
        @Override
515
        public Set<String> getBinaryLibraryPathIds() {
516
            return new HashSet<String>(Arrays.asList(BOOT, COMPILE));
517
        }
518
519
        @Override
520
        public Set<String> getMimeTypes() {
521
            return Collections.singleton(MIME_JAVA);
522
        }
523
524
    }
525
526
    public static final class ScriptingLikePathRecognizer extends PathRecognizer {
527
528
        public static final String SRC = "scripts";          //NOI18N
529
        public static final String COMPILE = "included";     //NOI18N
530
        public static final String MIME_JAVA = "text/x-script";   //NOI18N
531
532
        @Override
533
        public Set<String> getSourcePathIds() {
534
            return Collections.singleton(SRC);
535
        }
536
537
        @Override
538
        public Set<String> getLibraryPathIds() {
539
            return Collections.singleton(COMPILE);
540
        }
541
542
        @Override
543
        public Set<String> getBinaryLibraryPathIds() {
544
            return Collections.<String>emptySet();
545
        }
546
547
        @Override
548
        public Set<String> getMimeTypes() {
549
            return Collections.singleton(MIME_JAVA);
550
        }
551
552
    }
553
554
    public static final class ClassPathProviderImpl implements ClassPathProvider {
555
556
        private static volatile Map<FileObject,Map<String,ClassPath>> cps = Collections.emptyMap();
557
558
        @Override
559
        public ClassPath findClassPath(FileObject file, String type) {
560
            for (Map.Entry<FileObject,Map<String,ClassPath>> e : cps.entrySet()) {
561
                if (e.getKey().equals(file) || FileUtil.isParentOf(e.getKey(), file)) {
562
                    return e.getValue().get(type);
563
                }
564
            }
565
            return null;
566
        }
567
568
        static void register(Map<FileObject,Map<String,ClassPath>> newClassPaths) {
569
            cps = newClassPaths;
570
        }
571
572
    }
573
574
    public static final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation2 {
575
576
        private static volatile Map<URL, Pair<Boolean,List<FileObject>>> mapping = Collections.emptyMap();
577
578
        @Override
579
        public Result findSourceRoots2(final URL binaryRoot) {
580
            final Pair<Boolean,List<FileObject>> sources = mapping.get(binaryRoot);
581
            if (sources == null) {
582
                return null;
583
            }
584
            return new Result() {
585
                @Override
586
                public boolean preferSources() {
587
                    return sources.first;
588
                }
589
590
                @Override
591
                public FileObject[] getRoots() {
592
                    return sources.second.toArray(new FileObject[sources.second.size()]);
593
                }
594
595
                @Override
596
                public void addChangeListener(ChangeListener l) {
597
                }
598
599
                @Override
600
                public void removeChangeListener(ChangeListener l) {
601
                }
602
            };
603
        }
604
605
        @Override
606
        public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
607
            return findSourceRoots2(binaryRoot);
608
        }
609
610
        static void register(Map<URL,Pair<Boolean,List<FileObject>>> newMapping) {
611
            mapping = newMapping;
612
        }
613
614
    }    
615
616
}

Return to bug 227363