You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2019/01/11 11:22:09 UTC

[GitHub] asfgit closed pull request #1070: LineCookie.getLineSet() can enable disabled features

asfgit closed pull request #1070: LineCookie.getLineSet() can enable disabled features
URL: https://github.com/apache/incubator-netbeans/pull/1070
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectFactory.java b/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectFactory.java
index f3eaeecf3c..56c7ce29f7 100644
--- a/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectFactory.java
+++ b/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectFactory.java
@@ -28,6 +28,7 @@
 import javax.swing.event.ChangeListener;
 import org.netbeans.modules.ide.ergonomics.Utilities;
 import org.openide.cookies.EditCookie;
+import org.openide.cookies.LineCookie;
 import org.openide.cookies.OpenCookie;
 import org.openide.filesystems.FileObject;
 import org.openide.loaders.DataLoader;
@@ -40,6 +41,7 @@
 import org.openide.loaders.MultiFileLoader;
 import org.openide.nodes.Children;
 import org.openide.nodes.Node;
+import org.openide.text.Line;
 import org.openide.util.Exceptions;
 import org.openide.util.NbBundle;
 import org.openide.util.WeakListeners;
@@ -96,10 +98,10 @@ public DataObject findDataObject(FileObject fo, Set<? super FileObject> recogniz
     }
 
     private final class Cookies extends MultiDataObject
-    implements OpenCookie, EditCookie, ChangeListener {
+    implements OpenCookie, EditCookie, LineCookie, ChangeListener {
         private final FileObject fo;
         private final ChangeListener weakL;
-        private boolean open;
+        private Boolean open;
         
         private Cookies(FileObject fo, MultiFileLoader loader) throws DataObjectExistsException {
             super(fo, loader);
@@ -130,7 +132,23 @@ public void edit() {
             delegate(false);
         }
 
-        private void delegate(boolean open) {
+        @Override
+        public Line.Set getLineSet() {
+            delegate(null);
+            DataObject obj;
+            try {
+                obj = DataObject.find(fo);
+            } catch (DataObjectNotFoundException ex) {
+                return null;
+            }
+            if (this == obj) {
+                return null;
+            }
+            LineCookie lc = obj.getLookup().lookup(LineCookie.class);
+            return lc.getLineSet();
+        }
+        
+        private void delegate(Boolean open) {
             FeatureInfo info = FoDLayersProvider.getInstance().whichProvides(definition);
             String msg = NbBundle.getMessage(FodDataObjectFactory.class, "MSG_Opening_File", fo.getNameExt());
 
@@ -148,7 +166,15 @@ private void finishOpen() {
             try {
                 DataObject obj = DataObject.find(fo);
                 FoDLayersProvider.LOG.log(Level.FINER, "finishOpen {0}", obj);
-                Class<?> what = open ? OpenCookie.class : EditCookie.class;
+                if (obj == this) {
+                    obj.setValid(false);
+                    obj = DataObject.find(fo);
+                }
+                Boolean doOpen = open;
+                if (doOpen == null) {
+                    return;
+                }
+                Class<?> what = doOpen ? OpenCookie.class : EditCookie.class;
                 Object oc = obj.getLookup().lookup(what);
                 if (oc == this) {
                     obj.setValid(false);
diff --git a/ergonomics/ide.ergonomics/test/unit/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectLineSetTest.java b/ergonomics/ide.ergonomics/test/unit/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectLineSetTest.java
new file mode 100644
index 0000000000..735a8dff8b
--- /dev/null
+++ b/ergonomics/ide.ergonomics/test/unit/src/org/netbeans/modules/ide/ergonomics/fod/FodDataObjectLineSetTest.java
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.netbeans.modules.ide.ergonomics.fod;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.logging.Level;
+import junit.framework.Test;
+import org.netbeans.junit.Log;
+import org.netbeans.junit.NbModuleSuite;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.junit.RandomlyFails;
+import org.openide.cookies.LineCookie;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataObject;
+import org.openide.modules.ModuleInfo;
+import org.openide.text.Line;
+import org.openide.util.Lookup;
+import org.openide.util.lookup.AbstractLookup;
+import org.openide.util.lookup.InstanceContent;
+
+/**
+ *
+ * @author Jaroslav Tulach <jt...@netbeans.org>
+ */
+public class FodDataObjectLineSetTest extends NbTestCase {
+    private static final InstanceContent ic = new InstanceContent();
+
+    static {
+        FeatureManager.assignFeatureTypesLookup(new AbstractLookup(ic));
+    }
+    private ModuleInfo au;
+    private ModuleInfo fav;
+
+    public FodDataObjectLineSetTest(String n) {
+        super(n);
+    }
+
+    public static Test suite() {
+        System.setProperty("org.netbeans.core.startup.level", "OFF");
+
+        return NbModuleSuite.create(NbModuleSuite.emptyConfiguration().
+            addTest(FodDataObjectLineSetTest.class).
+            gui(false)
+        );
+    }
+
+    @Override
+    protected Level logLevel() {
+        return Level.FINE;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        clearWorkDir();
+
+        ic.set(Collections.emptyList(), null);
+
+        URI uri = ModuleInfo.class.getProtectionDomain().getCodeSource().getLocation().toURI();
+        File jar = new File(uri);
+        System.setProperty("netbeans.home", jar.getParentFile().getParent());
+        System.setProperty("netbeans.user", getWorkDirPath());
+        StringBuffer sb = new StringBuffer();
+        int found = 0;
+        Exception ex2 = null;
+        for (ModuleInfo info : Lookup.getDefault().lookupAll(ModuleInfo.class)) {
+            boolean disable = false;
+            if (
+                info.getCodeNameBase().equals("org.netbeans.modules.subversion")
+            ) {
+                disable = true;
+                au = info;
+            }
+            if (
+                info.getCodeNameBase().equals("org.netbeans.modules.favorites")
+            ) {
+                disable = true;
+                fav = info;
+            }
+            if (disable) {
+             Method m = null;
+                Class<?> c = info.getClass();
+                for (;;) {
+                    if (c == null) {
+                        throw ex2;
+                    }
+                    try {
+                        m = c.getDeclaredMethod("setEnabled", Boolean.TYPE);
+                    } catch (Exception ex) {
+                        ex2 = ex;
+                    }
+                    if (m != null) {
+                        break;
+                    }
+                    c = c.getSuperclass();
+                }
+                m.setAccessible(true);
+                m.invoke(info, false);
+                assertFalse("Module is disabled", info.isEnabled());
+                found++;
+            }
+            sb.append(info.getCodeNameBase()).append('\n');
+        }
+        if (found != 2) {
+            fail("Two shall be found, was " + found + ":\n" + sb);
+        }
+        FeatureInfo info = FeatureInfo.create(
+            "TestFactory",
+            ParseXMLContentTest.class.getResource("FeatureInfoTest.xml"),
+            ParseXMLContentTest.class.getResource("TestBundle.properties")
+        );
+        ic.add(info);
+        FoDLayersProvider.getInstance().refreshForce();
+    }
+
+
+
+    @RandomlyFails // ergonomics #3485
+    public void testCreateRecognize() throws Exception {
+        assertFalse("Autoupdate is disabled", au.isEnabled());
+        FileUtil.setMIMEType("huh", "text/x-huh");
+        FileObject fo = FileUtil.createData(FileUtil.getConfigRoot(), "test/my.huh");
+        DataObject obj = DataObject.find(fo);
+        FileObject fo2 = FileUtil.createData(FileUtil.getConfigRoot(), "test/subdir/my.huh");
+        DataObject obj2 = DataObject.find(fo2);
+        CharSequence log = Log.enable("org.openide.loaders", Level.WARNING);
+        LineCookie lineCookie = obj.getLookup().lookup(LineCookie.class);
+        assertNotNull("Line cookie found", lineCookie);
+        assertEquals("Cookie is OK too", lineCookie, obj.getCookie(LineCookie.class));
+        assertEquals("Node is OK too", lineCookie, obj.getNodeDelegate().getCookie(LineCookie.class));
+        assertEquals("Node lookup is OK too", lineCookie, obj.getNodeDelegate().getLookup().lookup(LineCookie.class));
+        assertTrue("It is our cookie: " + lineCookie, lineCookie.getClass().getName().contains("ergonomics"));
+        assertEquals("No warnings: " + log, 0, log.length());
+
+        Line.Set lineSet = lineCookie.getLineSet();
+        assertTrue("Autoupdate is enabled", au.isEnabled());
+        assertNotNull("Line set found", lineSet);
+        for (int i = 0; ; i++) {
+            DataObject newObj = DataObject.find(fo);
+            if (obj == newObj) {
+                if (i < 50) {
+                    Thread.sleep(1000);
+                    continue;
+                }
+                fail("New object shall be created: " + newObj);
+            }
+            break;
+        }
+        assertFalse("Old is no longer valid", obj.isValid());
+
+        DataObject newObj2 = DataObject.find(fo2);
+        if (obj2 == newObj2) {
+            fail("New object shall be created for all objects: " + newObj2);
+        }
+
+        DataObject folder = FodDataObjectFactory.create(fo).findDataObject(fo.getParent(), new HashSet<FileObject>());
+        assertNull("Folders are not recognized", folder);
+    }
+
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists