Skip to content

Commit 3e15eae

Browse files
authored
Merge pull request apache#7011 from jtulach/jtulach/MultiLineTextsInMx
`NbLaunchDelegate` fix and test multi line texts in suite.py
2 parents 4c43851 + c27fa6f commit 3e15eae

File tree

5 files changed

+211
-4
lines changed

5 files changed

+211
-4
lines changed

java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public void finished(boolean success) {
550550
ActionProvider provider = null;
551551
String command = null;
552552
Collection<ActionProvider> actionProviders = findActionProviders(prj);
553-
Lookup testLookup = preferProjActions && prj != null ? Lookups.singleton(prj) : (singleMethod != null) ? Lookups.fixed(toRun, singleMethod) : Lookups.singleton(toRun);
553+
Lookup testLookup = createTargetLookup(preferProjActions ? prj : null, singleMethod, toRun);
554554
String[] actions;
555555
if (!mainSource && singleMethod != null) {
556556
actions = debug ? new String[] {SingleMethod.COMMAND_DEBUG_SINGLE_METHOD}
@@ -564,7 +564,7 @@ public void finished(boolean success) {
564564
if (debug && !mainSource) {
565565
// We are calling COMMAND_DEBUG_TEST_SINGLE instead of a missing COMMAND_DEBUG_TEST
566566
// This is why we need to add the file to the lookup
567-
testLookup = (singleMethod != null) ? Lookups.fixed(toRun, singleMethod) : Lookups.singleton(toRun);
567+
testLookup = createTargetLookup(null, singleMethod, toRun);
568568
}
569569
} else {
570570
actions = debug ? mainSource ? new String[] {ActionProvider.COMMAND_DEBUG_SINGLE}
@@ -625,6 +625,18 @@ public void invokeAction(String command, Lookup context) throws IllegalArgumentE
625625
return Pair.of(provider, command);
626626
}
627627

628+
static Lookup createTargetLookup(Project prj, SingleMethod singleMethod, FileObject toRun) {
629+
if (prj != null) {
630+
return prj.getLookup();
631+
}
632+
if (singleMethod != null) {
633+
Lookup methodLookup = Lookups.singleton(singleMethod);
634+
return new ProxyLookup(toRun.getLookup(), methodLookup);
635+
} else {
636+
return toRun.getLookup();
637+
}
638+
}
639+
628640
private static Collection<ActionProvider> findActionProviders(Project prj) {
629641
Collection<ActionProvider> actionProviders = new ArrayList<>();
630642
if (prj != null) {

java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.concurrent.CompletableFuture;
33-
import java.util.concurrent.atomic.AtomicReference;
3433
import java.util.function.Consumer;
3534
import java.util.regex.Matcher;
3635
import java.util.regex.Pattern;
@@ -61,7 +60,6 @@
6160
import org.openide.filesystems.FileUtil;
6261
import org.openide.util.Lookup;
6362
import org.openide.util.Utilities;
64-
import org.openide.util.lookup.Lookups;
6563

6664
/**
6765
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.netbeans.modules.java.lsp.server.debugging.launch;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.Map;
24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.function.Consumer;
26+
import org.junit.After;
27+
import org.junit.AfterClass;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import static org.junit.Assert.*;
32+
import org.netbeans.api.project.Project;
33+
import org.netbeans.api.project.ProjectManager;
34+
import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext;
35+
import org.netbeans.spi.project.ActionProvider;
36+
import org.netbeans.spi.project.ProjectFactory;
37+
import org.netbeans.spi.project.ProjectState;
38+
import org.netbeans.spi.project.SingleMethod;
39+
import org.openide.filesystems.FileObject;
40+
import org.openide.filesystems.FileUtil;
41+
import org.openide.loaders.DataObject;
42+
import org.openide.util.Lookup;
43+
import org.openide.util.Pair;
44+
import org.openide.util.lookup.Lookups;
45+
import org.openide.util.lookup.ServiceProvider;
46+
47+
public class NbLaunchDelegateTest {
48+
49+
public NbLaunchDelegateTest() {
50+
}
51+
52+
@Test
53+
public void testFileObjectsLookup() throws Exception {
54+
FileObject fo = FileUtil.createMemoryFileSystem().getRoot().createData("test.txt");
55+
Lookup lkp = NbLaunchDelegate.createTargetLookup(null, null, fo);
56+
assertEquals(fo, lkp.lookup(FileObject.class));
57+
58+
DataObject obj = lkp.lookup(DataObject.class);
59+
assertNotNull("DataObject also found", obj);
60+
61+
assertEquals("It's FileObject's data object", obj, DataObject.find(fo));
62+
assertNull("No single method", lkp.lookup(SingleMethod.class));
63+
}
64+
65+
@Test
66+
public void testFileObjectsLookupWithSingleMethod() throws Exception {
67+
FileObject fo = FileUtil.createMemoryFileSystem().getRoot().createData("test-with-method.txt");
68+
SingleMethod m = new SingleMethod(fo, "main");
69+
Lookup lkp = NbLaunchDelegate.createTargetLookup(null, m, fo);
70+
assertEquals(fo, lkp.lookup(FileObject.class));
71+
72+
DataObject obj = lkp.lookup(DataObject.class);
73+
assertNotNull("DataObject also found", obj);
74+
75+
assertEquals("It's FileObject's data object", obj, DataObject.find(fo));
76+
77+
assertEquals("Found single method", m, lkp.lookup(SingleMethod.class));
78+
}
79+
80+
@Test
81+
public void testFindsMavenProject() throws Exception {
82+
FileObject dir = FileUtil.createMemoryFileSystem().getRoot().createFolder("testprj");
83+
FileObject xml = dir.createData("build.xml");
84+
Project prj = ProjectManager.getDefault().findProject(dir);
85+
assertNotNull("Project dir recognized", prj);
86+
87+
SingleMethod m = new SingleMethod(xml, "main");
88+
Lookup lkp = NbLaunchDelegate.createTargetLookup(prj, m, xml);
89+
assertNull("No file object", lkp.lookup(FileObject.class));
90+
DataObject obj = lkp.lookup(DataObject.class);
91+
assertNull("No DataObject ", obj);
92+
assertNull("No single method", lkp.lookup(SingleMethod.class));
93+
assertEquals(prj, lkp.lookup(Project.class));
94+
}
95+
96+
@ServiceProvider(service = ProjectFactory.class)
97+
public static final class MockProjectFactory implements ProjectFactory {
98+
99+
@Override
100+
public boolean isProject(FileObject projectDirectory) {
101+
return projectDirectory.getNameExt().equals("testprj");
102+
}
103+
104+
@Override
105+
public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException {
106+
return new MockProject(projectDirectory);
107+
}
108+
109+
@Override
110+
public void saveProject(Project project) throws IOException, ClassCastException {
111+
}
112+
113+
private static final class MockProject implements Project {
114+
private final FileObject dir;
115+
116+
MockProject(FileObject dir) {
117+
this.dir = dir;
118+
}
119+
120+
@Override
121+
public FileObject getProjectDirectory() {
122+
return dir;
123+
}
124+
125+
@Override
126+
public Lookup getLookup() {
127+
return Lookups.fixed(this);
128+
}
129+
130+
}
131+
}
132+
}

java/java.mx.project/test/unit/src/org/netbeans/modules/java/mx/project/ParseSuitesTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.io.InputStream;
2324
import java.io.PrintWriter;
2425
import java.io.StringWriter;
26+
import java.net.URL;
2527
import java.nio.file.FileVisitResult;
2628
import java.nio.file.FileVisitor;
2729
import java.nio.file.Files;
2830
import java.nio.file.Path;
2931
import java.nio.file.attribute.BasicFileAttributes;
3032
import org.netbeans.junit.NbTestCase;
33+
import org.netbeans.modules.java.mx.project.suitepy.MxDistribution;
3134
import org.netbeans.modules.java.mx.project.suitepy.MxSuite;
3235

3336
public final class ParseSuitesTest extends NbTestCase {
@@ -39,6 +42,15 @@ public ParseSuitesTest(String name) {
3942
public void testParseThemAll() throws IOException {
4043
assertSuitePys(getDataDir(), 15);
4144
}
45+
46+
public void testParseMultiLineSuite() throws IOException {
47+
URL url = getClass().getResource("multilinetexts.py");
48+
MxSuite suite = MxSuite.parse(url);
49+
assertNotNull("suite parsed", suite);
50+
MxDistribution tool = suite.distributions().get("TOOLCHAIN");
51+
assertNotNull("toolchain found", tool);
52+
assertEquals("No deps", 0, tool.dependencies().size());
53+
}
4254

4355
public static void main(String... args) throws IOException {
4456
if (args.length == 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
suite = {
20+
"mxversion": "7.5.0",
21+
"name" : "multilinetexts",
22+
"version" : "1.0.0",
23+
"repositories" : {
24+
},
25+
"libraries" : {
26+
},
27+
"licenses" : {
28+
},
29+
"distributions" : {
30+
"TOOLCHAIN": {
31+
"native": True,
32+
"platformDependent": True,
33+
"os": {
34+
"linux": {
35+
"layout": {
36+
"toolchain.multi": {
37+
"source_type": "string",
38+
"value": '''
39+
line1
40+
line2
41+
line3
42+
line5
43+
'''
44+
},
45+
},
46+
"dependencies": [
47+
],
48+
},
49+
},
50+
"maven" : False,
51+
},
52+
},
53+
}

0 commit comments

Comments
 (0)