6
6
import java .io .File ;
7
7
import java .io .IOException ;
8
8
import java .io .UnsupportedEncodingException ;
9
+ import java .nio .file .Files ;
10
+
9
11
import org .eclipse .cdt .lsp .LspPlugin ;
10
12
import org .eclipse .cdt .lsp .editor .ui .test .TestUtils ;
11
13
import org .eclipse .cdt .lsp .server .ICLanguageServerProvider ;
15
17
import org .junit .jupiter .api .BeforeEach ;
16
18
import org .junit .jupiter .api .Test ;
17
19
import org .junit .jupiter .api .TestInfo ;
18
- import org .junit .jupiter .api .io .TempDir ;
19
20
20
21
21
22
public class LspEditorPreferencesTesterTest {
22
23
private static final String FILE_CONTENT = "// sample file content" ;
23
24
private static final String MAIN_CPP = "main.cpp" ;
24
- private static final String EXTERNAL_HEADER_HPP = "ExternalHeader.hpp" ;
25
+ private static final String HEADER_HPP = "header.hpp" ;
26
+ private static final String MAIN_C = "main.c" ;
27
+ private static final String HEADER_H = "header.h" ;
28
+ //private static final String EXTERNAL_HEADER_HPP = "ExternalHeader.hpp";
25
29
private IProject project ;
26
30
27
- @ TempDir
28
- private File tempDir ;
31
+ // @TempDir -> does not work with org.junit.jupiter.api. Needs junit-jupiter-api and junit-jupiter-params.
32
+ // These packages are not accessible on the CI build server because we build with Eclipse 2022-06
33
+ // Path tempDir = Files.createTempFile("ExternalHeader", ".hpp", null);
29
34
35
+ private File createTempHppHeaderfile () throws IOException {
36
+ return Files .createTempFile ("ExternalHeader" , ".hpp" ).toFile ();
37
+ }
30
38
31
39
@ BeforeEach
32
40
public void setUp (TestInfo testInfo ) throws CoreException {
@@ -81,11 +89,11 @@ public void testLsEnableByUriTest_WITHOUT_LsEditorPreferred() throws CoreExcepti
81
89
}
82
90
83
91
/**
84
- * Tests whether the C/C++ Editor is used for a resource to open whose project has "Prefer C/C++ Editor (LSP)" disabled.
92
+ * Tests whether the C/C++ Editor is used for a C++ source file to open whose project has "Prefer C/C++ Editor (LSP)" disabled.
85
93
* @throws UnsupportedEncodingException
86
94
*/
87
95
@ Test
88
- public void testEditorUsedToOpenFile_WITHOUT_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
96
+ public void testEditorUsedToOpenCppFile_WITHOUT_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
89
97
//GIVEN is a project with DISABLED "Prefer C/C++ Editor (LSP)" in the preferences:
90
98
TestUtils .setLspPreferred (project , false );
91
99
//AND a file exits in the given project:
@@ -94,14 +102,15 @@ public void testEditorUsedToOpenFile_WITHOUT_LsEditorPreferred() throws CoreExce
94
102
var editorPart = TestUtils .openInEditor (file );
95
103
//THEN it will be opened in the C/C++ Editor:
96
104
assertEquals (LspPlugin .C_EDITOR_ID , editorPart .getEditorSite ().getId ());
105
+ TestUtils .closeEditor (editorPart , false );
97
106
}
98
107
99
108
/**
100
- * Tests whether the C/C++ Editor (LSP) is used for a resource to open whose project has "Prefer C/C++ Editor (LSP)" enabled.
109
+ * Tests whether the C/C++ Editor (LSP) is used for a C++ source file to open whose project has "Prefer C/C++ Editor (LSP)" enabled.
101
110
* @throws UnsupportedEncodingException
102
111
*/
103
112
@ Test
104
- public void testEditorUsedToOpenFile_WITH_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
113
+ public void testEditorUsedToOpenCppFile_WITH_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
105
114
//GIVEN is a project with ENABLED "Prefer C/C++ Editor (LSP)" in the preferences:
106
115
TestUtils .setLspPreferred (project , true );
107
116
//AND a file exits in the given project:
@@ -110,6 +119,109 @@ public void testEditorUsedToOpenFile_WITH_LsEditorPreferred() throws CoreExcepti
110
119
var editorPart = TestUtils .openInEditor (file );
111
120
//THEN it will be opened in the C/C++ Editor (LSP):
112
121
assertEquals (LspPlugin .LSP_C_EDITOR_ID , editorPart .getEditorSite ().getId ());
122
+ TestUtils .closeEditor (editorPart , false );
123
+ }
124
+
125
+ /**
126
+ * Tests whether the C/C++ Editor is used for a C++ header file to open whose project has "Prefer C/C++ Editor (LSP)" disabled.
127
+ * @throws UnsupportedEncodingException
128
+ */
129
+ @ Test
130
+ public void testEditorUsedToOpenCppHeaderFile_WITHOUT_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
131
+ //GIVEN is a project with DISABLED "Prefer C/C++ Editor (LSP)" in the preferences:
132
+ TestUtils .setLspPreferred (project , false );
133
+ //AND a file exits in the given project:
134
+ var file = TestUtils .createFile (project , HEADER_HPP , FILE_CONTENT );
135
+ //WHEN this file will be opened:
136
+ var editorPart = TestUtils .openInEditor (file );
137
+ //THEN it will be opened in the C/C++ Editor:
138
+ assertEquals (LspPlugin .C_EDITOR_ID , editorPart .getEditorSite ().getId ());
139
+ TestUtils .closeEditor (editorPart , false );
140
+ }
141
+
142
+ /**
143
+ * Tests whether the C/C++ Editor (LSP) is used for a C++ header file to open whose project has "Prefer C/C++ Editor (LSP)" enabled.
144
+ * @throws UnsupportedEncodingException
145
+ */
146
+ @ Test
147
+ public void testEditorUsedToOpenCppHeaderFile_WITH_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
148
+ //GIVEN is a project with ENABLED "Prefer C/C++ Editor (LSP)" in the preferences:
149
+ TestUtils .setLspPreferred (project , true );
150
+ //AND a file exits in the given project:
151
+ var file = TestUtils .createFile (project , HEADER_HPP , FILE_CONTENT );
152
+ //WHEN this file will be opened:
153
+ var editorPart = TestUtils .openInEditor (file );
154
+ //THEN it will be opened in the C/C++ Editor (LSP):
155
+ assertEquals (LspPlugin .LSP_C_EDITOR_ID , editorPart .getEditorSite ().getId ());
156
+ TestUtils .closeEditor (editorPart , false );
157
+ }
158
+
159
+ /**
160
+ * Tests whether the C/C++ Editor is used for a C source file to open whose project has "Prefer C/C++ Editor (LSP)" disabled.
161
+ * @throws UnsupportedEncodingException
162
+ */
163
+ @ Test
164
+ public void testEditorUsedToOpenCFile_WITHOUT_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
165
+ //GIVEN is a project with DISABLED "Prefer C/C++ Editor (LSP)" in the preferences:
166
+ TestUtils .setLspPreferred (project , false );
167
+ //AND a file exits in the given project:
168
+ var file = TestUtils .createFile (project , MAIN_C , FILE_CONTENT );
169
+ //WHEN this file will be opened:
170
+ var editorPart = TestUtils .openInEditor (file );
171
+ //THEN it will be opened in the C/C++ Editor:
172
+ assertEquals (LspPlugin .C_EDITOR_ID , editorPart .getEditorSite ().getId ());
173
+ TestUtils .closeEditor (editorPart , false );
174
+ }
175
+
176
+ /**
177
+ * Tests whether the C/C++ Editor (LSP) is used for a C source file to open whose project has "Prefer C/C++ Editor (LSP)" enabled.
178
+ * @throws UnsupportedEncodingException
179
+ */
180
+ @ Test
181
+ public void testEditorUsedToOpenCFile_WITH_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
182
+ //GIVEN is a project with ENABLED "Prefer C/C++ Editor (LSP)" in the preferences:
183
+ TestUtils .setLspPreferred (project , true );
184
+ //AND a file exits in the given project:
185
+ var file = TestUtils .createFile (project , MAIN_C , FILE_CONTENT );
186
+ //WHEN this file will be opened:
187
+ var editorPart = TestUtils .openInEditor (file );
188
+ //THEN it will be opened in the C/C++ Editor (LSP):
189
+ assertEquals (LspPlugin .LSP_C_EDITOR_ID , editorPart .getEditorSite ().getId ());
190
+ TestUtils .closeEditor (editorPart , false );
191
+ }
192
+
193
+ /**
194
+ * Tests whether the C/C++ Editor is used for a C header file to open whose project has "Prefer C/C++ Editor (LSP)" disabled.
195
+ * @throws UnsupportedEncodingException
196
+ */
197
+ @ Test
198
+ public void testEditorUsedToOpenCHeaderFile_WITHOUT_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
199
+ //GIVEN is a project with DISABLED "Prefer C/C++ Editor (LSP)" in the preferences:
200
+ TestUtils .setLspPreferred (project , false );
201
+ //AND a file exits in the given project:
202
+ var file = TestUtils .createFile (project , HEADER_HPP , FILE_CONTENT );
203
+ //WHEN this file will be opened:
204
+ var editorPart = TestUtils .openInEditor (file );
205
+ //THEN it will be opened in the C/C++ Editor:
206
+ assertEquals (LspPlugin .C_EDITOR_ID , editorPart .getEditorSite ().getId ());
207
+ TestUtils .closeEditor (editorPart , false );
208
+ }
209
+
210
+ /**
211
+ * Tests whether the C/C++ Editor (LSP) is used for a C header file to open whose project has "Prefer C/C++ Editor (LSP)" enabled.
212
+ * @throws UnsupportedEncodingException
213
+ */
214
+ @ Test
215
+ public void testEditorUsedToOpenCHeaderFile_WITH_LsEditorPreferred () throws CoreException , UnsupportedEncodingException {
216
+ //GIVEN is a project with ENABLED "Prefer C/C++ Editor (LSP)" in the preferences:
217
+ TestUtils .setLspPreferred (project , true );
218
+ //AND a file exits in the given project:
219
+ var file = TestUtils .createFile (project , HEADER_H , FILE_CONTENT );
220
+ //WHEN this file will be opened:
221
+ var editorPart = TestUtils .openInEditor (file );
222
+ //THEN it will be opened in the C/C++ Editor (LSP):
223
+ assertEquals (LspPlugin .LSP_C_EDITOR_ID , editorPart .getEditorSite ().getId ());
224
+ TestUtils .closeEditor (editorPart , false );
113
225
}
114
226
115
227
/**
@@ -119,12 +231,14 @@ public void testEditorUsedToOpenFile_WITH_LsEditorPreferred() throws CoreExcepti
119
231
@ Test
120
232
public void testLsEnableByExternalUriTest_NoEditorOpen () throws CoreException , IOException {
121
233
//GIVEN is an external file which does not exists in the given project and is not opened:
122
- File externalFile = new File ( tempDir , EXTERNAL_HEADER_HPP );
234
+ File externalFile = createTempHppHeaderfile ( );
123
235
//AND a ICLanguageServerProvider which uses LspEditorPreferencesTester as enabledWhen tester:
124
236
ICLanguageServerProvider cLanguageServerProvider = LspPlugin .getDefault ().getCLanguageServerProvider ();
125
237
//WHEN the LspEditorPreferencesTester gets called by the property tester in the enabledWhen element of the serverProvider extension point,
126
238
//THEN the LspEditorPreferencesTester.test returns FALSE for the given file URI:
127
239
assertTrue (!cLanguageServerProvider .isEnabledFor (externalFile .toURI ()));
240
+ //ensure clean up
241
+ externalFile .delete ();
128
242
}
129
243
130
244
/**
@@ -133,7 +247,7 @@ public void testLsEnableByExternalUriTest_NoEditorOpen() throws CoreException, I
133
247
@ Test
134
248
public void testLsEnableByExternalUriTest_OpenedInLspCEditor () throws CoreException , IOException {
135
249
//GIVEN is an existing external file:
136
- File externalFile = new File ( tempDir , EXTERNAL_HEADER_HPP );
250
+ File externalFile = createTempHppHeaderfile ( );
137
251
externalFile .createNewFile ();
138
252
//AND it's opened in the LSP based C/C++ Editor:
139
253
var editor = TestUtils .openInEditor (externalFile .toURI (), LspPlugin .LSP_C_EDITOR_ID );
@@ -143,6 +257,8 @@ public void testLsEnableByExternalUriTest_OpenedInLspCEditor() throws CoreExcept
143
257
//THEN the LspEditorPreferencesTester.test returns TRUE for the given file URI:
144
258
assertTrue (cLanguageServerProvider .isEnabledFor (externalFile .toURI ()));
145
259
TestUtils .closeEditor (editor , false );
260
+ //ensure clean up
261
+ externalFile .delete ();
146
262
}
147
263
148
264
/**
@@ -151,7 +267,7 @@ public void testLsEnableByExternalUriTest_OpenedInLspCEditor() throws CoreExcept
151
267
@ Test
152
268
public void testLsEnableByExternalUriTest_OpenedInCEditor () throws CoreException , IOException {
153
269
//GIVEN is an existing external file:
154
- File externalFile = new File ( tempDir , EXTERNAL_HEADER_HPP );
270
+ File externalFile = createTempHppHeaderfile ( );
155
271
externalFile .createNewFile ();
156
272
//AND it's opened in the C/C++ Editor:
157
273
var editor = TestUtils .openInEditor (externalFile .toURI (), LspPlugin .C_EDITOR_ID );
@@ -161,6 +277,8 @@ public void testLsEnableByExternalUriTest_OpenedInCEditor() throws CoreException
161
277
//THEN the LspEditorPreferencesTester.test returns FALSE for the given file URI:
162
278
assertTrue (!cLanguageServerProvider .isEnabledFor (externalFile .toURI ()));
163
279
TestUtils .closeEditor (editor , false );
280
+ //ensure clean up
281
+ externalFile .delete ();
164
282
}
165
283
166
284
}
0 commit comments