mbien commented on code in PR #8662:
URL: https://github.com/apache/netbeans/pull/8662#discussion_r2296884183


##########
java/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/queries/ModuleInfoAccessibilityQueryImplTest.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.java.api.common.queries;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.netbeans.api.java.queries.AccessibilityQuery.Accessibility;
+import org.netbeans.api.project.Project;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.api.common.TestProject;
+import org.netbeans.modules.java.api.common.ant.UpdateHelper;
+import org.netbeans.spi.java.queries.AccessibilityQueryImplementation2.Result;
+import org.netbeans.spi.project.support.ant.AntProjectHelper;
+import org.netbeans.spi.project.support.ant.EditableProperties;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.test.MockLookup;
+
+/**
+ *
+ * @author peterhull
+ */
+public class ModuleInfoAccessibilityQueryImplTest extends NbTestCase {
+
+    private static final String MODULE_INFO_JAVA = "module-info.java";  
//NOI18N
+
+    // Module definition which exports mymodule but not myinternal
+    private static final String MODULE_INFO = """
+                                                  module mymodule {
+                                                  exports mypackage;
+                                                  }
+                                                  """;
+
+    private FileObject src;
+    private FileObject test;
+    private FileObject workDir;
+    private TestProject testProject;
+    private FileObject mypackage;
+    private FileObject myinternal;
+
+    public ModuleInfoAccessibilityQueryImplTest(String name) {
+        super(name);
+    }
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        clearWorkDir();
+        MockLookup.setInstances(TestProject.createProjectType());
+        workDir = FileUtil.toFileObject(FileUtil.normalizeFile(getWorkDir()));
+        src = workDir.createFolder("src"); //NOI18N
+        test = workDir.createFolder("test"); //NOI18N
+        mypackage = src.createFolder("mypackage"); //NOI18N
+        myinternal = src.createFolder("myinternal"); //NOI18N
+        try (OutputStream os = src.createAndOpen(MODULE_INFO_JAVA)) {
+            os.write(MODULE_INFO.getBytes(StandardCharsets.UTF_8));
+        }
+        Project prj = TestProject.createProject(workDir, src, test);
+        testProject = prj.getLookup().lookup(TestProject.class);
+        // Using modules, so project has to be source level 9 or later.
+        UpdateHelper helper = testProject.getUpdateHelper();
+        EditableProperties properties = 
helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
+        properties.putAll(Map.of("javac.source", "9", "javac.target", "9"));
+        helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, 
properties);
+        /* Modular project has the structure
+        ROOT
+        +-src
+        | +-mypackage
+        | | `-*.java
+        | +-myinternal
+        | + `-*.java
+        | `-module-info.java
+        `-test
+          `-mypackage
+            `-MyClassTest.java
+         */
+    }
+
+    /**
+     * Test of isPubliclyAccessible method, of class
+     * ModuleInfoAccessibilityQueryImpl. Tests if an exported package
+     * 'mypackage' is accessible.
+     *
+     * @throws java.io.IOException if test file creation failed.
+     */
+    @Test
+    public void testIsPubliclyAccessible1() throws IOException {
+        ModuleInfoAccessibilityQueryImpl instance = new 
ModuleInfoAccessibilityQueryImpl(null, testProject.getSourceRoots(), null, 
testProject.getTestRoots());
+        Result result = instance.isPubliclyAccessible(mypackage);
+        // Return value can be null, that's a fail
+        if (result == null) {
+            fail("AccessibilityQuery returned null"); //NOI18N
+        } else {
+            Accessibility accessibility = result.getAccessibility();
+            Accessibility expAccessibility = Accessibility.EXPORTED;
+            assertEquals(expAccessibility, accessibility);
+        }
+    }
+
+    /**
+     * Test of isPubliclyAccessible method, of class
+     * ModuleInfoAccessibilityQueryImpl. Tests if a not-exported package
+     * 'myinternal' is not accessible.
+     *
+     * @throws java.io.IOException if test file creation failed.
+     */
+    @Test
+    public void testIsPubliclyAccessible2() throws IOException {
+        ModuleInfoAccessibilityQueryImpl instance = new 
ModuleInfoAccessibilityQueryImpl(null, testProject.getSourceRoots(), null, 
testProject.getTestRoots());
+        Result result = instance.isPubliclyAccessible(myinternal);
+        // Return value can be null, that's a fail
+        if (result == null) {
+            fail("AccessibilityQuery returned null"); //NOI18N
+        } else {
+            Accessibility accessibility = result.getAccessibility();
+            Accessibility expAccessibility = Accessibility.PRIVATE;
+            assertEquals(expAccessibility, accessibility);
+        }
+    }
+
+    /**
+     * Test of propertyChange method, of class 
ModuleInfoAccessibilityQueryImpl.
+     */
+    @Test
+    public void testPropertyChange() {
+        // This doesn't need testing
+    }

Review Comment:
   please remove the empty test methods



##########
java/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/queries/ModuleInfoAccessibilityQueryImplTest.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.java.api.common.queries;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.netbeans.api.java.queries.AccessibilityQuery.Accessibility;
+import org.netbeans.api.project.Project;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.api.common.TestProject;
+import org.netbeans.modules.java.api.common.ant.UpdateHelper;
+import org.netbeans.spi.java.queries.AccessibilityQueryImplementation2.Result;
+import org.netbeans.spi.project.support.ant.AntProjectHelper;
+import org.netbeans.spi.project.support.ant.EditableProperties;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.test.MockLookup;
+
+/**
+ *
+ * @author peterhull
+ */
+public class ModuleInfoAccessibilityQueryImplTest extends NbTestCase {
+
+    private static final String MODULE_INFO_JAVA = "module-info.java";  
//NOI18N
+
+    // Module definition which exports mymodule but not myinternal
+    private static final String MODULE_INFO = """
+                                                  module mymodule {
+                                                  exports mypackage;
+                                                  }
+                                                  """;

Review Comment:
   nitpick: align `"""`



##########
java/java.api.common/src/org/netbeans/modules/java/api/common/queries/ModuleInfoAccessibilityQueryImpl.java:
##########
@@ -232,48 +236,49 @@ private Collection<FileObject[]> collectModuleRoots(
         final MultiModule model = MultiModule.getOrCreate(mods, src);
         for (String modName : model.getModuleNames()) {
             final ClassPath cp = model.getModuleSources(modName);
-            res.add(cp.getRoots());
+            if (cp != null) {
+                res.add(cp.getRoots());
+            }
         }
         return res;
     }
 
     @NonNull
-    private static Optional<Pair<Set<FileObject>,Set<FileObject>>> readExports(
-            @NonNull final FileObject[] roots,
-            @NonNull final Set<? super FileObject> rootsCollector) {
-        Collections.addAll(rootsCollector, roots);
-        final Optional<FileObject> moduleInfo = Arrays.stream(roots)
-                .map((root) -> root.getFileObject(MODULE_INFO_JAVA))
-                .filter((mi) -> mi != null)
+    private static Optional<Pair<Set<FileObject>, Set<FileObject>>> 
extractExports(
+            @NonNull final FileObject[] roots) {
+        return Arrays.stream(roots)
+                .map(root -> root.getFileObject(MODULE_INFO_JAVA))
+                .filter(Objects::nonNull)
+                .map(mi -> {
+                    Set<FileObject> rootsSet = new HashSet<>();
+                    Collections.addAll(rootsSet, roots);
+                    Set<FileObject> exportsSet = readExports(mi, rootsSet);
+                    return Pair.of(rootsSet, exportsSet);
+                })
                 .findFirst();

Review Comment:
   this is fine but a bit dangerous in case someone changes something in future 
(e.g adds sort or something like that).
   
   moving the `map()` stage after `findFirst()` would be safer.
   
   nitpick:
   copy constructor candidate:
   ```java
                       Set<FileObject> rootsSet = new HashSet<>(roots);
   ```



##########
java/java.api.common/src/org/netbeans/modules/java/api/common/queries/ModuleInfoAccessibilityQueryImpl.java:
##########
@@ -46,6 +49,7 @@
 import org.netbeans.api.java.source.JavaSource;
 import org.netbeans.modules.java.api.common.SourceRoots;
 import org.netbeans.modules.java.api.common.impl.MultiModule;
+import org.netbeans.spi.java.queries.AccessibilityQueryImplementation;

Review Comment:
   import is probably not needed since `AccessibilityQueryImplementation2` is 
in use looks like



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to