|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may 'ou may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.netbeans.modules.nbcode.java.notebook; |
| 18 | + |
| 19 | +import com.google.gson.JsonObject; |
| 20 | +import com.google.gson.JsonArray; |
| 21 | +import com.google.gson.JsonPrimitive; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import org.eclipse.lsp4j.ConfigurationItem; |
| 27 | +import org.eclipse.lsp4j.ConfigurationParams; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
| 33 | +/* TODO |
| 34 | + test multiple configuration scenarios |
| 35 | + 1.Client sends nulls/empty |
| 36 | + 2.Missing keys |
| 37 | +*/ |
| 38 | + |
| 39 | +/* |
| 40 | +Version 1 21/08/25 |
| 41 | +*/ |
| 42 | + |
| 43 | +/** |
| 44 | + * Mock LSP Client sending sample configurations |
| 45 | + * Verifies that the NotebookConfigs class |
| 46 | + * parses and handles configurations appropriately |
| 47 | + * |
| 48 | + * @author shimadan |
| 49 | + */ |
| 50 | +public class NotebookConfigsTest { |
| 51 | + |
| 52 | + private NotebookConfigs instance; |
| 53 | + private CompletableFuture<Void> initialized; |
| 54 | + private JsonObject configsObj = new JsonObject(); |
| 55 | + private static final String CLASSPATH_KEY = "jdk.notebook.classpath"; |
| 56 | + private static final String IMPLICIT_IMPORTS_KEY = "jdk.notebook.implicitImports"; |
| 57 | + private static final String ADD_MODULES_KEY = "jdk.notebook.addmodules"; |
| 58 | + private static final String ENABLE_PREVIEW_KEY = "jdk.notebook.enablePreview"; |
| 59 | + private static final String MODULEPATH_KEY = "jdk.notebook.modulepath"; |
| 60 | + |
| 61 | + public NotebookConfigsTest() { |
| 62 | + } |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setUp() { |
| 66 | + setConfigObject(); |
| 67 | + LanguageClientInstance.getInstance(). |
| 68 | + setClient(new MockNbClientConfigs()); |
| 69 | + instance = NotebookConfigs.getInstance(); |
| 70 | + instance.initConfigs(); |
| 71 | + initialized = instance.getInitialized(); |
| 72 | + } |
| 73 | + |
| 74 | + @After |
| 75 | + public void tearDown() { |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test of getInitialized method, of class NotebookConfigs. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + public void testGetInitialized() { |
| 83 | + System.out.println("getInitialized"); |
| 84 | + CompletableFuture<Void> result = instance.getInitialized(); |
| 85 | + try { |
| 86 | + result.get(5, TimeUnit.SECONDS); |
| 87 | + } catch (Exception ex) { |
| 88 | + fail("Configuration initialization failed"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test of getClassPath method, of class NotebookConfigs. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void testGetClassPath() { |
| 97 | + System.out.println("getClassPath"); |
| 98 | + try { |
| 99 | + initialized.get(5, TimeUnit.SECONDS); |
| 100 | + String expResult = configsObj.get(CLASSPATH_KEY).getAsString(); |
| 101 | + String result = instance.getClassPath(); |
| 102 | + assertEquals(expResult, result); |
| 103 | + } catch (Exception ex) { |
| 104 | + fail("Configuration initialization failed"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test of getModulePath method, of class NotebookConfigs. |
| 110 | + */ |
| 111 | + @Test |
| 112 | + public void testGetModulePath() { |
| 113 | + System.out.println("getModulePath"); |
| 114 | + |
| 115 | + try { |
| 116 | + initialized.get(5, TimeUnit.SECONDS); |
| 117 | + String expResult = configsObj.get(MODULEPATH_KEY).getAsString(); |
| 118 | + String result = instance.getModulePath(); |
| 119 | + assertEquals(expResult, result); |
| 120 | + } catch (Exception ex) { |
| 121 | + fail("Configuration initialization failed"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test of getAddModules method, of class NotebookConfigs. |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void testGetAddModules() { |
| 130 | + System.out.println("getAddModules"); |
| 131 | + try { |
| 132 | + initialized.get(5, TimeUnit.SECONDS); |
| 133 | + String expResult = configsObj.get(ADD_MODULES_KEY).getAsString(); |
| 134 | + String result = instance.getAddModules(); |
| 135 | + assertEquals(expResult, result); |
| 136 | + } catch (Exception ex) { |
| 137 | + fail("Configuration initialization failed"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test of isEnablePreview method, of class NotebookConfigs. |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void testIsEnablePreview() { |
| 146 | + System.out.println("getIsEnablePreview"); |
| 147 | + try { |
| 148 | + initialized.get(5, TimeUnit.SECONDS); |
| 149 | + boolean expResult = configsObj.get(ENABLE_PREVIEW_KEY).getAsBoolean(); |
| 150 | + boolean result = instance.isEnablePreview(); |
| 151 | + assertEquals(expResult, result); |
| 152 | + } catch (Exception ex) { |
| 153 | + fail("Configuration initialization failed"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Test of getImplicitImports method, of class NotebookConfigs. |
| 159 | + */ |
| 160 | + @Test |
| 161 | + public void testGetImplicitImports() { |
| 162 | + System.out.println("getImplicitImports"); |
| 163 | + try { |
| 164 | + initialized.get(5, TimeUnit.SECONDS); |
| 165 | + List<String> expResult = configsObj.get(IMPLICIT_IMPORTS_KEY). |
| 166 | + getAsJsonArray().asList().stream(). |
| 167 | + map((elem) -> elem.getAsString()).toList(); |
| 168 | + List<String> result = instance.getImplicitImports(); |
| 169 | + assertEquals(expResult, result); |
| 170 | + } catch (Exception ex) { |
| 171 | + fail("Configuration initialization failed"); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private void setConfigObject() { |
| 176 | + JsonArray imports = new JsonArray(); |
| 177 | + imports.add(new JsonPrimitive("java.math.*")); |
| 178 | + imports.add(new JsonPrimitive("javafx.scene.control.*")); |
| 179 | + configsObj.add(IMPLICIT_IMPORTS_KEY, imports); |
| 180 | + configsObj.add(CLASSPATH_KEY, new JsonPrimitive("path/to/javafx-sdk-24.0.1/lib/javafx.base.jar")); |
| 181 | + configsObj.add(MODULEPATH_KEY, new JsonPrimitive("/path/to/javafx-sdk/lib")); |
| 182 | + configsObj.add(ENABLE_PREVIEW_KEY, new JsonPrimitive(false)); |
| 183 | + configsObj.add(ADD_MODULES_KEY, new JsonPrimitive("javafx.controls,javafx.graphics")); |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + private class MockNbClientConfigs extends MockNbClient { |
| 188 | + |
| 189 | + @Override |
| 190 | + public CompletableFuture<List<Object>> configuration(ConfigurationParams configurationParams) { |
| 191 | + List<ConfigurationItem> items = configurationParams.getItems(); |
| 192 | + List<Object> configs = new ArrayList<>(); |
| 193 | + for (ConfigurationItem item : items) { |
| 194 | + configs.add(configsObj.get(item.getSection())); |
| 195 | + } |
| 196 | + return CompletableFuture.completedFuture(configs); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments