diff -r 7fe7a1b1a4ef java.j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/CountingSecurityManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/CountingSecurityManager.java Tue May 06 04:17:13 2008 +0200 @@ -0,0 +1,117 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.java.j2seproject; + +import java.io.FileDescriptor; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.security.Permission; +import junit.framework.Assert; + +/** + * + * @author Jaroslav Tulach + */ +final class CountingSecurityManager extends SecurityManager { + private static int cnt; + private static StringWriter msgs; + private static PrintWriter pw; + private static String prefix; + + public static void initialize(String prefix) { + if (System.getSecurityManager() instanceof CountingSecurityManager) { + // ok + } else { + System.setSecurityManager(new CountingSecurityManager()); + } + cnt = 0; + msgs = new StringWriter(); + pw = new PrintWriter(msgs); + CountingSecurityManager.prefix = prefix; + } + + public static void assertCounts(String msg, int expectedCnt) { + if (expectedCnt < cnt) { + Assert.fail(msg + "\n" + msgs + " expected: " + expectedCnt + " real: " + cnt); + } + cnt = 0; + msgs = new StringWriter(); + pw = new PrintWriter(msgs); + } + + @Override + public void checkRead(String file) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkRead: " + file); + // new Exception().printStackTrace(pw); + } + } + + @Override + public void checkRead(String file, Object context) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkRead2: " + file); + } + } + + @Override + public void checkWrite(FileDescriptor fd) { + cnt++; + pw.println("Fd: " + fd); + } + + @Override + public void checkWrite(String file) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkWrite: " + file); + } + } + + @Override + public void checkPermission(Permission perm) { + } + + @Override + public void checkPermission(Permission perm, Object context) { + } +} diff -r 7fe7a1b1a4ef java.j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/PackageViewTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/PackageViewTest.java Tue May 06 04:17:13 2008 +0200 @@ -0,0 +1,131 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.java.j2seproject; + +import java.io.File; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.java.j2seproject.ui.SourceNodeFactory; +import org.netbeans.spi.project.support.ant.AntProjectHelper; +import org.netbeans.spi.project.ui.LogicalViewProvider; +import org.netbeans.spi.project.ui.support.NodeList; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.modules.SpecificationVersion; +import org.openide.nodes.Node; + +/** + * Generates project, tries to open a package and counts number of accesses + * to it. + * + * @author Jaroslav Tulach + */ +public class PackageViewTest extends NbTestCase { + + public PackageViewTest(String testName) { + super(testName); + } + + private static final String[] createFiles = { + "build.xml", + "nbproject/build-impl.xml", + "nbproject/project.xml", + "nbproject/project.properties", + "src/", + "src/net/sf/dvbcentral/Test.java", + "src/net/sf/dvbcentral/Test1.java", + "src/net/sf/dvbcentral/Test2.java", + "src/net/sf/dvbcentral/Test3.java", + "src/net/sf/dvbcentral/Test4.java", + "test/", + }; + + + public void testCreateProject() throws Exception { + File proj = getWorkDir(); + clearWorkDir(); + J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N + AntProjectHelper aph = J2SEProjectGenerator.createProject(proj, "test-project", null, "manifest.mf", null); + J2SEProjectGenerator.setDefaultSourceLevel(null); + assertNotNull(aph); + FileObject fo = aph.getProjectDirectory(); + for (int i=0; i < createFiles.length; i++) { + if (createFiles[i].endsWith("/")) { + FileUtil.createFolder(fo, createFiles[i]); + } else { + FileUtil.createData(fo, createFiles[i]); + } + assertNotNull(createFiles[i]+" file/folder cannot be found", fo.getFileObject(createFiles[i])); + } + + Project prj = FileOwnerQuery.getOwner(fo); + + + CountingSecurityManager.initialize(FileUtil.toFile(fo).getAbsolutePath()); + + SourceNodeFactory factory = new SourceNodeFactory(); + NodeList arr = factory.createNodes(prj); + assertNotNull("Not null root node created", arr); + assertEquals("Arr is now 2", 2, arr.keys().size()); + + Node src = arr.node(arr.keys().get(0)); + Node pkg = assertNode(src, "net.sf.dvbcentral"); + assertNotNull("Package found"); + + CountingSecurityManager.assertCounts("Just few checks", 1); + } + + + private static Node assertNode(Node where, String what) { + StringBuffer sb = new StringBuffer(); + for (Node n : where.getChildren().getNodes(true)) { + if (n.getName().equals(what)) { + return n; + } + sb.append(" n: " + n.getName() + "\n"); + } + fail("Cannot find node " + what + " only " + where.getChildren().getNodesCount() + " is available:\n" + sb); + return null; + } +} diff -r 7fe7a1b1a4ef java.project/src/org/netbeans/spi/java/project/support/ui/PackageView.java --- a/java.project/src/org/netbeans/spi/java/project/support/ui/PackageView.java Sat Apr 26 22:19:38 2008 +0200 +++ b/java.project/src/org/netbeans/spi/java/project/support/ui/PackageView.java Tue May 06 04:17:13 2008 +0200 @@ -72,6 +72,7 @@ import org.openide.nodes.AbstractNode; import org.openide.nodes.AbstractNode; import org.openide.nodes.FilterNode; import org.openide.nodes.Node; +import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.WeakListeners; @@ -152,6 +153,15 @@ public class PackageView { * @param showProgress whether to show a progress handle or not */ static void findNonExcludedPackages(PackageViewChildren children, Collection target, FileObject fo, SourceGroup group, boolean showProgress) { + PackagesProvider.Request request = new PackagesProvider.Request(fo, group); + PackagesProvider.Response response = new PackagesProvider.Response(children, target); + for (PackagesProvider p : Lookup.getDefault().lookupAll(PackagesProvider.class)) { + p.compute(request, response); + if (response.done) { + return; + } + } + if (showProgress) { ProgressHandle progress = ProgressHandleFactory.createHandle(NbBundle.getMessage(PackageView.class, "PackageView.find_packages_progress", FileUtil.getFileDisplayName(fo))); progress.start(1000); diff -r 7fe7a1b1a4ef java.project/src/org/netbeans/spi/java/project/support/ui/PackagesProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.project/src/org/netbeans/spi/java/project/support/ui/PackagesProvider.java Tue May 06 04:17:14 2008 +0200 @@ -0,0 +1,96 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.java.project.support.ui; + +import java.util.Collection; +import org.netbeans.api.project.SourceGroup; +import org.netbeans.spi.java.project.support.ui.PackageView.PackageItem; +import org.openide.filesystems.FileObject; + +/** An interface that can be registered into {@link org.openide.util.Lookup.getDefault()} + * to provide pre-cached results for list of packages for given folder and source group. + * + * @author Jaroslav Tulach + */ +public interface PackagesProvider { + public void compute(Request request, Response response); + + public static final class Request { + final FileObject fo; + final SourceGroup sourceGroup; + + Request(FileObject fo, SourceGroup sourceGroup) { + this.fo = fo; + this.sourceGroup = sourceGroup; + } + + public FileObject getFolder() { + return fo; + } + + public SourceGroup getSourceGroup() { + return sourceGroup; + } + } + + public static final class Response { + private final Collection res; + private final PackageViewChildren children; + boolean done; + + Response(PackageViewChildren children, Collection res) { + this.res = res; + this.children = children; + } + + public void addPackage(SourceGroup group, FileObject packageFolder, boolean empty) { + if (res != null) { + res.add(new PackageItem(group, packageFolder, empty)); + } else { + children.add(packageFolder, empty, false); + } + done = true; + } + + public void finish() { + done = true; + } + } +} diff -r 7fe7a1b1a4ef java.project/test/unit/src/org/netbeans/spi/java/project/support/ui/CountingSecurityManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.project/test/unit/src/org/netbeans/spi/java/project/support/ui/CountingSecurityManager.java Tue May 06 04:17:14 2008 +0200 @@ -0,0 +1,117 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.java.project.support.ui; + +import java.io.FileDescriptor; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.security.Permission; +import junit.framework.Assert; + +/** + * + * @author Jaroslav Tulach + */ +final class CountingSecurityManager extends SecurityManager { + private static int cnt; + private static StringWriter msgs; + private static PrintWriter pw; + private static String prefix; + + public static void initialize(String prefix) { + if (System.getSecurityManager() instanceof CountingSecurityManager) { + // ok + } else { + System.setSecurityManager(new CountingSecurityManager()); + } + cnt = 0; + msgs = new StringWriter(); + pw = new PrintWriter(msgs); + CountingSecurityManager.prefix = prefix; + } + + public static void assertCounts(String msg, int expectedCnt) { + if (expectedCnt < cnt) { + Assert.fail(msg + "\n" + msgs + " expected: " + expectedCnt + " real: " + cnt); + } + cnt = 0; + msgs = new StringWriter(); + pw = new PrintWriter(msgs); + } + + @Override + public void checkRead(String file) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkRead: " + file); + // new Exception().printStackTrace(pw); + } + } + + @Override + public void checkRead(String file, Object context) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkRead2: " + file); + } + } + + @Override + public void checkWrite(FileDescriptor fd) { + cnt++; + pw.println("Fd: " + fd); + } + + @Override + public void checkWrite(String file) { + if (file.startsWith(prefix)) { + cnt++; + pw.println("checkWrite: " + file); + } + } + + @Override + public void checkPermission(Permission perm) { + } + + @Override + public void checkPermission(Permission perm, Object context) { + } +} diff -r 7fe7a1b1a4ef java.project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java --- a/java.project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java Sat Apr 26 22:19:38 2008 +0200 +++ b/java.project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java Tue May 06 04:17:14 2008 +0200 @@ -60,10 +60,12 @@ import javax.swing.Icon; import javax.swing.Icon; import javax.swing.SwingUtilities; import javax.swing.event.ChangeListener; +import junit.framework.Test; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.TestUtil; import org.netbeans.api.queries.VisibilityQuery; import org.netbeans.junit.NbTestCase; +import org.netbeans.junit.NbTestSuite; import org.netbeans.spi.queries.VisibilityQueryImplementation; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; @@ -85,6 +87,11 @@ public class PackageViewTest extends NbT super( name ); } + public static Test suite() { + //return new NbTestSuite(PackageViewTest.class); + return new PackageViewTest("testDiskAccessFolders"); + } + private FileObject root; protected void setUp() throws Exception { @@ -94,7 +101,10 @@ public class PackageViewTest extends NbT // Get "Wrong # or names of nodes expected:<[c.d, c.f, p.me.toolsx]> but was:<[c.d, c.f, p.me.tools, p.me.toolsx]>" // from testRename after call to n.setName("p.me.toolsx") // This however seems to work all the time: - TestUtil.setLookup( Lookups.fixed( new Object[] { new VQImpl(), PackageViewTest.class.getClassLoader() } ) ); + TestUtil.setLookup( Lookups.fixed( new Object[] { + new VQImpl(), PackageViewTest.class.getClassLoader(), + new MyFolderProvider() + } ) ); root = TestUtil.makeScratchDir(this); } @@ -966,6 +976,51 @@ public class PackageViewTest extends NbT } + public void testDiskAccessFolders() throws Exception { + assertNull( "source folder should not exist yet", root.getFileObject( "src" ) ); + + FileObject src = FileUtil.createFolder(root, "src"); + // Create children + SourceGroup group = new SimpleSourceGroup(src); + + Set folders = new HashSet(); + for (int i = 0; i < 10000; i++) { + String folder = "" + (i / 1000) + "/" + ((i / 100) % 10); + + FileObject fo = FileUtil.createData( + src, + folder + "/File" + (i % 10) + ".java" + ); + folders.add(folder); + } + MyFolderProvider.folders = folders; + + CountingSecurityManager.initialize(getWorkDirPath()); + Children ch = PackageView.createPackageView( group ).getChildren(); + Node[] compute = ch.getNodes(true); + assertEquals("packages is 100", 100, compute.length); + CountingSecurityManager.assertCounts("almost no touches", 250); + } + + public static final class MyFolderProvider implements PackagesProvider { + static Set folders; + + public void compute(Request request, Response response) { + if (folders == null) { + return; + } + for (String s : folders) { + response.addPackage( + request.getSourceGroup(), + request.getFolder().getFileObject(s), + false + ); + } + folders = null; + } + } + + private static class VQImpl implements VisibilityQueryImplementation { public static String IGNORED = "KRTEK";