Skip to content

Commit

Permalink
Fixing possible NPE. Tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Oct 3, 2023
1 parent 5b5e083 commit 56ff0a3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1386,10 +1386,10 @@ protected List<File> getParents() {
while(true) {
try {
MavenProject parent = loadParentOf(getEmbedder(), project);
if (NbMavenProject.isErrorPlaceholder(parent)) {
if (parent == null || NbMavenProject.isErrorPlaceholder(parent)) {
break;
}
File parentFile = parent != null ? parent.getFile() : null;
File parentFile = parent.getFile();
if(parentFile != null) {
ret.add(parentFile);
project = parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ public static ExecutorTask executeMaven(final RunConfig config, InputOutput io,
* Hooks for tests to mock the Maven execution.
*/
public static class ExecuteMaven {
// tests only
MavenExecutor createCommandLineExecutor(RunConfig config, InputOutput io, TabContext tc) {
return new MavenCommandLineExecutor(config, io, tc);
}

public ExecutorTask execute(RunConfig config, InputOutput io, TabContext tc) {
LifecycleManager.getDefault().saveAll();
MavenExecutor exec = new MavenCommandLineExecutor(config, io, tc);
MavenExecutor exec = createCommandLineExecutor(config, io, tc);
ExecutorTask task = ExecutionEngine.getDefault().execute(config.getTaskDisplayName(), exec, new ProxyNonSelectableInputOutput(exec.getInputOutput()));
exec.setTask(task);
task.addTaskListener(new TaskListener() {
Expand Down Expand Up @@ -628,7 +633,8 @@ private static String quote2apos(String s) {
return sb.toString();
}

private ProcessBuilder constructBuilder(final RunConfig clonedConfig, InputOutput ioput) {
// tests only
ProcessBuilder constructBuilder(final RunConfig clonedConfig, InputOutput ioput) {
File javaHome = null;
Map<String, String> envMap = new LinkedHashMap<String, String>();
for (Map.Entry<? extends String,? extends String> entry : clonedConfig.getProperties().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import org.netbeans.modules.maven.api.NbMavenProject;
import org.netbeans.modules.maven.debug.MavenJPDAStart;
import org.netbeans.modules.maven.embedder.EmbedderFactory;
import org.netbeans.modules.maven.execute.MavenExecMonitor;
import org.netbeans.modules.maven.execute.MockMavenExec;
import org.netbeans.modules.maven.modelcache.MavenProjectCache;
import org.netbeans.modules.projectapi.nb.TimedWeakReference;
import org.netbeans.spi.project.ActionProgress;
Expand All @@ -57,6 +59,7 @@
import org.openide.modules.DummyInstalledFileLocator;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.test.MockLookup;
import org.openide.windows.IOProvider;

public class NbMavenProjectImplTest2 extends NbTestCase {
Expand Down Expand Up @@ -145,6 +148,11 @@ public void finished(boolean success) {
primeLatch.await(300, TimeUnit.SECONDS);
}

MavenExecMonitor mme;

/**
* Checks that subproject reload after the subproject primes.
*/
public void testSubprojectsReloadAfterPriming() throws Exception {
cleanMavenRepository();
clearWorkDir();
Expand All @@ -161,7 +169,6 @@ public void testSubprojectsReloadAfterPriming() throws Exception {
assertNotNull(sub);

// check the project's validity:
NbMavenProject parentMaven = p.getLookup().lookup(NbMavenProject.class);
NbMavenProject subMaven = sub.getLookup().lookup(NbMavenProject.class);
assertTrue("Fallback parent project is expected on unpopulated repository", NbMavenProject.isErrorPlaceholder(subMaven.getMavenProject()));
assertTrue("Fallback subproject project is expected on unpopulated repository", NbMavenProject.isErrorPlaceholder(subMaven.getMavenProject()));
Expand All @@ -170,6 +177,48 @@ public void testSubprojectsReloadAfterPriming() throws Exception {
assertFalse("Subproject must recover after priming itself", NbMavenProject.isIncomplete(subMaven.getMavenProject()));
}

/**
* Checks that Priming action on a subproject actually runs on a reactor with --auto-make to build the subproject.
* @throws Exception
*/
public void testSubprojectPrimeRunsReactor() throws Exception {
cleanMavenRepository();
clearWorkDir();

mme = new MavenExecMonitor();
MockLookup.setLayersAndInstances(mme);

FileUtil.toFileObject(getWorkDir()).refresh();

FileObject testApp = dataFO.getFileObject("projects/multiproject/democa");
FileObject prjCopy = FileUtil.copyFile(testApp, FileUtil.toFileObject(getWorkDir()), "simpleProject");

Project p = ProjectManager.getDefault().findProject(prjCopy);
assertNotNull(p);

Project sub = ProjectManager.getDefault().findProject(prjCopy.getFileObject("lib"));
assertNotNull(sub);

// check the project's validity:
NbMavenProject subMaven = sub.getLookup().lookup(NbMavenProject.class);
assertTrue("Fallback parent project is expected on unpopulated repository", NbMavenProject.isErrorPlaceholder(subMaven.getMavenProject()));
assertTrue("Fallback subproject project is expected on unpopulated repository", NbMavenProject.isErrorPlaceholder(subMaven.getMavenProject()));

primeProject(sub);

assertEquals("Just single maven executed:", 1, mme.builders.size());

ProcessBuilder b = mme.builders.getFirst();
assertEquals("Runs in root project's dir", FileUtil.toFile(prjCopy), b.directory());
assertTrue("Specifies also-make", b.command().indexOf("--also-make") > 0);
int idx = b.command().indexOf("--projects");
assertTrue("Specifies projects", idx > 0);
assertEquals("Runs up to the lib subprojectsd", "lib", b.command().get(idx + 1));
}

/**
* Checks that subproject reload after its parent project primes.
*/
public void testSubprojectsReloadAfterParentPriming() throws Exception {
cleanMavenRepository();
clearWorkDir();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.maven.execute;

import java.util.concurrent.ConcurrentLinkedDeque;
import org.netbeans.modules.maven.api.execute.RunConfig;
import org.openide.windows.InputOutput;

/**
*
* @author sdedic
*/
public class MavenExecMonitor extends MavenCommandLineExecutor.ExecuteMaven {
public ConcurrentLinkedDeque<ProcessBuilder> builders = new ConcurrentLinkedDeque<>();

@Override
MavenExecutor createCommandLineExecutor(RunConfig config, InputOutput io, AbstractMavenExecutor.TabContext tc) {
return new CmdlineExecutor(config, io, tc);
}

class CmdlineExecutor extends MavenCommandLineExecutor {

public CmdlineExecutor(RunConfig conf, InputOutput io, TabContext tc) {
super(conf, io, tc);
}

@Override
ProcessBuilder constructBuilder(RunConfig clonedConfig, InputOutput ioput) {
ProcessBuilder b = super.constructBuilder(clonedConfig, ioput);
builders.add(b);
return b;
}
}
}

0 comments on commit 56ff0a3

Please sign in to comment.