|
| 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 | +} |
0 commit comments