Issue #171092: use the same FileSystem.Status implementation for unit tests as in full platform. diff --git a/core.startup/src/org/netbeans/core/startup/layers/SystemFileSystem.java b/core.startup/src/org/netbeans/core/startup/layers/SystemFileSystem.java --- a/core.startup/src/org/netbeans/core/startup/layers/SystemFileSystem.java +++ b/core.startup/src/org/netbeans/core/startup/layers/SystemFileSystem.java @@ -41,20 +41,13 @@ package org.netbeans.core.startup.layers; -import java.awt.Image; -import java.awt.Toolkit; -import java.beans.BeanInfo; import java.beans.PropertyVetoException; import java.io.File; import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectStreamException; import java.io.Serializable; -import java.net.URL; -import java.util.Arrays; import java.util.HashSet; -import java.util.MissingResourceException; -import java.util.ResourceBundle; import java.util.Set; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -70,8 +63,8 @@ import org.openide.filesystems.FileUtil; import org.openide.filesystems.LocalFileSystem; import org.openide.filesystems.MultiFileSystem; +import org.openide.filesystems.Repository; import org.openide.util.Exceptions; -import org.openide.util.ImageUtilities; import org.openide.util.NbBundle; /** The system FileSystem - represents system files under $NETBEANS_HOME/system. @@ -79,7 +72,7 @@ * @author Jan Jancura, Ian Formanek, Petr Hamernik */ public final class SystemFileSystem extends MultiFileSystem -implements FileSystem.Status, FileChangeListener { +implements FileChangeListener { // Must be public for BeanInfo to work: #11186. /** generated Serialized Version UID */ @@ -88,14 +81,6 @@ /** system name of this filesystem */ private static final String SYSTEM_NAME = "SystemFileSystem"; // NOI18N - /** name of file attribute with localizing bundle */ - private static final String ATTR_BUNDLE = "SystemFileSystem.localizingBundle"; // NOI18N - - /** name of file attribute with URL to 16x16 color icon */ - private static final String ATTR_ICON_16 = "SystemFileSystem.icon"; // NOI18N - /** name of file attribute with URL to 32x32 color icon */ - private static final String ATTR_ICON_32 = "SystemFileSystem.icon32"; // NOI18N - private static final Logger LOG = Logger.getLogger(SystemFileSystem.class.getName()); /** user fs */ @@ -189,112 +174,9 @@ } public @Override FileSystem.Status getStatus() { - return this; + return Repository.createDefaultFileSystemStatus(); } - static final String annotateName(FileObject fo) { - - String bundleName = (String) fo.getAttribute(ATTR_BUNDLE); // NOI18N - if (bundleName != null) { - try { - bundleName = org.openide.util.Utilities.translate(bundleName); - ResourceBundle b = NbBundle.getBundle(bundleName); - try { - return b.getString(fo.getPath()); - } catch (MissingResourceException ex) { - // ignore--normal - } - } catch (MissingResourceException ex) { - Exceptions.attachMessage(ex, warningMessage(bundleName, fo)); - ModuleLayeredFileSystem.err.log(Level.WARNING, null, ex); - // ignore - } - } - return (String)fo.getAttribute("displayName"); // NOI18N - } - private static String warningMessage(String name, FileObject fo) { - Object by = fo.getAttribute("layers"); // NOI18N - if (by instanceof Object[]) { - by = Arrays.toString((Object[])by); - } - return "Cannot load " + name + " for " + fo + " defined by " + by; // NOI18N - } - - /** Annotate name - */ - public String annotateName(String s, Set files) { - - // Look for a localized file name. - // Note: all files in the set are checked. But please only place the attribute - // on the primary file, and use this primary file name as the bundle key. - for (FileObject fo : files) { - // annotate a name - String displayName = annotateName(fo); - if (displayName != null) { - return displayName; - } - } - - return s; - } - - static Image annotateIcon(FileObject fo, int type) { - String attr = null; - if (type == BeanInfo.ICON_COLOR_16x16) { - attr = ATTR_ICON_16; - } else if (type == BeanInfo.ICON_COLOR_32x32) { - attr = ATTR_ICON_32; - } - - if (attr != null) { - Object value = fo.getAttribute(attr); - if (value != null) { - if (value instanceof URL) { - return Toolkit.getDefaultToolkit().getImage((URL) value); - } else if (value instanceof Image) { - // #18832 - return (Image) value; - } else { - ModuleLayeredFileSystem.err.warning("Attribute " + attr + " on " + fo + " expected to be a URL or Image; was: " + value); - } - } - } - - String base = (String) fo.getAttribute("iconBase"); // NOI18N - if (base != null) { - if (type == BeanInfo.ICON_COLOR_16x16) { - return ImageUtilities.loadImage(base, true); - } else if (type == BeanInfo.ICON_COLOR_32x32) { - return ImageUtilities.loadImage(insertBeforeSuffix(base, "_32"), true); // NOI18N - } - } - return null; - } - - private static String insertBeforeSuffix(String path, String toInsert) { - String withoutSuffix = path; - String suffix = ""; // NOI18N - - if (path.lastIndexOf('.') >= 0) { - withoutSuffix = path.substring(0, path.lastIndexOf('.')); - suffix = path.substring(path.lastIndexOf('.'), path.length()); - } - - return withoutSuffix + toInsert + suffix; - } - - /** Annotate icon - */ - public Image annotateIcon(Image im, int type, Set files) { - for (FileObject fo : files) { - Image img = annotateIcon(fo, type); - if (img != null) { - return img; - } - } - return im; - } - /** Initializes and creates new repository. This repository's system fs is * based on the content of ${HOME_DIR}/system and ${USER_DIR}/system directories * diff --git a/core.startup/test/unit/src/org/netbeans/core/startup/layers/CacheManagerTestBaseHid.java b/core.startup/test/unit/src/org/netbeans/core/startup/layers/CacheManagerTestBaseHid.java --- a/core.startup/test/unit/src/org/netbeans/core/startup/layers/CacheManagerTestBaseHid.java +++ b/core.startup/test/unit/src/org/netbeans/core/startup/layers/CacheManagerTestBaseHid.java @@ -48,6 +48,7 @@ import java.io.InputStream; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import org.netbeans.junit.NbTestCase; @@ -55,6 +56,7 @@ import org.openide.filesystems.FileSystem; import org.openide.filesystems.FileUtil; import org.openide.filesystems.MultiFileSystem; +import org.openide.filesystems.Repository; /** Test layer cache managers generally. * @author Jesse Glick */ @@ -153,15 +155,16 @@ assertEquals("val/map2", attr(mfs, "foo/29356", "map2")); assertEquals("Ahoj", attr(mfs, "foo/29356", "mapDisplayName")); + FileSystem.Status s = Repository.createDefaultFileSystemStatus(); FileObject annot = f.findResource("foo/29356"); - String annotName = SystemFileSystem.annotateName(annot); + String annotName = s.annotateName(null, Collections.singleton(annot)); assertEquals("Ahoj", annotName); - Image img = SystemFileSystem.annotateIcon(annot, BeanInfo.ICON_COLOR_16x16); + Image img = s.annotateIcon(null, BeanInfo.ICON_COLOR_16x16, Collections.singleton(annot)); assertNotNull("Icon provided", img); assertEquals("height", 16, img.getHeight(this)); assertEquals("width", 16, img.getHeight(this)); - Image img32 = SystemFileSystem.annotateIcon(annot, BeanInfo.ICON_COLOR_32x32); + Image img32 = s.annotateIcon(null, BeanInfo.ICON_COLOR_32x32, Collections.singleton(annot)); assertNotNull("Icon 32 provided", img32); assertEquals("height", 32, img32.getHeight(this)); assertEquals("width", 32, img32.getHeight(this)); diff --git a/openide.filesystems/src/org/openide/filesystems/Repository.java b/openide.filesystems/src/org/openide/filesystems/Repository.java --- a/openide.filesystems/src/org/openide/filesystems/Repository.java +++ b/openide.filesystems/src/org/openide/filesystems/Repository.java @@ -41,6 +41,9 @@ package org.openide.filesystems; +import java.awt.Image; +import java.awt.Toolkit; +import java.beans.BeanInfo; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyVetoException; @@ -54,19 +57,27 @@ import java.io.Serializable; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.List; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.Set; import java.util.Vector; import java.util.concurrent.atomic.AtomicReference; import java.util.jar.Manifest; import java.util.logging.Level; import java.util.logging.Logger; +import org.openide.filesystems.FileSystem.Status; +import org.openide.util.Exceptions; +import org.openide.util.ImageUtilities; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; +import org.openide.util.NbBundle; import org.openide.util.NbCollections; import org.openide.util.io.NbMarshalledObject; @@ -198,6 +209,11 @@ public void resultChanged(LookupEvent ev) { setDelegates(computeDelegates()); } + + public @Override Status getStatus() { + return createDefaultFileSystemStatus(); + } + } // end of MainFS static final long serialVersionUID = -6344768369160069704L; @@ -820,4 +836,127 @@ return getDefault(); } } + + /** + * Factory for a standard implementation of name/icon annotation for the system filesystem. + * Intended for use from core, not modules. + * @return a status object suitable for {@link FileSystem#getStatus()} + * @since XXX + */ + public static FileSystem.Status createDefaultFileSystemStatus() { + return new SFSStatus(); + } + private static final class SFSStatus implements FileSystem.Status { + /** name of file attribute with localizing bundle */ + private static final String ATTR_BUNDLE = "SystemFileSystem.localizingBundle"; // NOI18N + /** name of file attribute with URL to 16x16 color icon */ + private static final String ATTR_ICON_16 = "SystemFileSystem.icon"; // NOI18N + /** name of file attribute with URL to 32x32 color icon */ + private static final String ATTR_ICON_32 = "SystemFileSystem.icon32"; // NOI18N + + static final String annotateName(FileObject fo) { + + String bundleName = (String) fo.getAttribute(ATTR_BUNDLE); // NOI18N + if (bundleName != null) { + try { + bundleName = org.openide.util.Utilities.translate(bundleName); + ResourceBundle b = NbBundle.getBundle(bundleName); + try { + return b.getString(fo.getPath()); + } catch (MissingResourceException ex) { + // ignore--normal + } + } catch (MissingResourceException ex) { + Exceptions.attachMessage(ex, warningMessage(bundleName, fo)); + LOG.log(Level.WARNING, null, ex); + // ignore + } + } + return (String) fo.getAttribute("displayName"); // NOI18N + } + + private static String warningMessage(String name, FileObject fo) { + Object by = fo.getAttribute("layers"); // NOI18N + if (by instanceof Object[]) { + by = Arrays.toString((Object[]) by); + } + return "Cannot load " + name + " for " + fo + " defined by " + by; // NOI18N + } + + /** Annotate name + */ + public String annotateName(String s, Set files) { + + // Look for a localized file name. + // Note: all files in the set are checked. But please only place the attribute + // on the primary file, and use this primary file name as the bundle key. + for (FileObject fo : files) { + // annotate a name + String displayName = annotateName(fo); + if (displayName != null) { + return displayName; + } + } + + return s; + } + + static Image annotateIcon(FileObject fo, int type) { + String attr = null; + if (type == BeanInfo.ICON_COLOR_16x16) { + attr = ATTR_ICON_16; + } else if (type == BeanInfo.ICON_COLOR_32x32) { + attr = ATTR_ICON_32; + } + + if (attr != null) { + Object value = fo.getAttribute(attr); + if (value != null) { + if (value instanceof URL) { + return Toolkit.getDefaultToolkit().getImage((URL) value); + } else if (value instanceof Image) { + // #18832 + return (Image) value; + } else { + LOG.warning("Attribute " + attr + " on " + fo + " expected to be a URL or Image; was: " + value); + } + } + } + + String base = (String) fo.getAttribute("iconBase"); // NOI18N + if (base != null) { + if (type == BeanInfo.ICON_COLOR_16x16) { + return ImageUtilities.loadImage(base, true); + } else if (type == BeanInfo.ICON_COLOR_32x32) { + return ImageUtilities.loadImage(insertBeforeSuffix(base, "_32"), true); // NOI18N + } + } + return null; + } + + private static String insertBeforeSuffix(String path, String toInsert) { + String withoutSuffix = path; + String suffix = ""; // NOI18N + + if (path.lastIndexOf('.') >= 0) { + withoutSuffix = path.substring(0, path.lastIndexOf('.')); + suffix = path.substring(path.lastIndexOf('.'), path.length()); + } + + return withoutSuffix + toInsert + suffix; + } + + /** Annotate icon + */ + public Image annotateIcon(Image im, int type, Set files) { + for (FileObject fo : files) { + Image img = annotateIcon(fo, type); + if (img != null) { + return img; + } + } + return im; + } + } + } diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/RepositoryTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/RepositoryTest.java --- a/openide.filesystems/test/unit/src/org/openide/filesystems/RepositoryTest.java +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/RepositoryTest.java @@ -51,6 +51,7 @@ import java.net.URLConnection; import java.net.URLStreamHandler; import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -193,4 +194,13 @@ os.close(); } + public void testStatus() throws Exception { + FileObject r = FileUtil.getConfigRoot(); + FileSystem.Status s = r.getFileSystem().getStatus(); + FileObject f = r.createData("f"); + f.setAttribute("displayName", "F!"); + assertEquals("F!", s.annotateName("f", Collections.singleton(f))); + // XXX test SystemFileSystem.localizingBundle, iconBase, SystemFileSystem.icon + } + } diff --git a/projectuiapi/test/unit/src/org/netbeans/spi/project/ui/support/ProjectCustomizerTest.java b/projectuiapi/test/unit/src/org/netbeans/spi/project/ui/support/ProjectCustomizerTest.java --- a/projectuiapi/test/unit/src/org/netbeans/spi/project/ui/support/ProjectCustomizerTest.java +++ b/projectuiapi/test/unit/src/org/netbeans/spi/project/ui/support/ProjectCustomizerTest.java @@ -125,9 +125,7 @@ assertEquals("One", categories[0].getDisplayName()); assertEquals("one", dcp.create(categories[0]).getName()); assertEquals("Category1", categories[1].getName()); - /* XXX does not work yet because ExternalUtil.MainFS does not provide a FS.Status: assertEquals("Category #1", categories[1].getDisplayName()); - */ assertEquals("two", dcp.create(categories[1]).getName()); Category[] subcategories = categories[1].getSubcategories(); assertEquals(1, subcategories.length); @@ -135,9 +133,7 @@ assertEquals("Three", subcategories[0].getDisplayName()); assertEquals("three", dcp.create(subcategories[0]).getName()); assertEquals("Category2", categories[2].getName()); - /* assertEquals("Category #2", categories[2].getDisplayName()); - */ assertEquals(null, dcp.create(categories[2]).getName()); subcategories = categories[2].getSubcategories(); assertEquals(1, subcategories.length);