1818 */
1919package org .netbeans .modules .java .editor .imports ;
2020
21+ import java .awt .Toolkit ;
2122import java .beans .PropertyChangeEvent ;
2223import java .beans .PropertyChangeListener ;
2324import java .io .IOException ;
25+ import java .util .Map ;
2426import java .util .concurrent .Exchanger ;
2527import java .util .concurrent .TimeUnit ;
2628import java .util .regex .Pattern ;
3436import org .netbeans .api .lexer .Language ;
3537import org .netbeans .core .startup .Main ;
3638import org .netbeans .junit .NbTestCase ;
39+ import org .netbeans .modules .java .editor .imports .ClipboardHandler .ImportsWrapper ;
3740import org .netbeans .modules .java .source .BootClassPathUtil ;
3841import org .netbeans .modules .java .source .parsing .JavacParser ;
3942import org .openide .cookies .EditorCookie ;
@@ -119,6 +122,111 @@ public void testStaticImportsOn11_JIRA3019() throws Exception {
119122 "11" );
120123 }
121124
125+ public void testSwitchSimpleEnumNames1 () throws Exception {
126+ doCopyAndPaste ("""
127+ package test;
128+ import static test.E.A;
129+ public class Test {
130+ private void test(E e) {
131+ |switch (e) {
132+ case A:
133+ case B:
134+ case C:
135+ System.err.println(A);
136+ break;
137+ }|
138+ }
139+ }
140+ enum E {
141+ A, B, C;
142+ }
143+ """ ,
144+ "21" );
145+ ImportsWrapper wrapper = (ImportsWrapper ) Toolkit .getDefaultToolkit ().getSystemClipboard ().getData (ClipboardHandler .IMPORT_FLAVOR );
146+ Map <String , String > simple2ImportFQN = wrapper .getSimple2ImportFQN ();
147+ assertEquals (Map .of ("A" , "test.E.A" , "System" , "java.lang.System" ),
148+ simple2ImportFQN );
149+ }
150+
151+ public void testSwitchSimpleEnumNames2 () throws Exception {
152+ doCopyAndPaste ("""
153+ package test;
154+ import static test.E.A;
155+ public class Test {
156+ private int test(E e) {
157+ return |switch (e) {
158+ case A, B, C -> {
159+ System.err.println(A);
160+ yield 0;
161+ }
162+ }|
163+ }
164+ }
165+ enum E {
166+ A, B, C;
167+ }
168+ """ ,
169+ "21" );
170+ ImportsWrapper wrapper = (ImportsWrapper ) Toolkit .getDefaultToolkit ().getSystemClipboard ().getData (ClipboardHandler .IMPORT_FLAVOR );
171+ Map <String , String > simple2ImportFQN = wrapper .getSimple2ImportFQN ();
172+ assertEquals (Map .of ("A" , "test.E.A" , "System" , "java.lang.System" ),
173+ simple2ImportFQN );
174+ }
175+
176+ public void testSwitchSimpleEnumNames3 () throws Exception {
177+ doCopyAndPaste ("""
178+ package test;
179+ import static test.E.*;
180+ public class Test {
181+ private int test(Object o) {
182+ return |switch (o) {
183+ case A:
184+ case B:
185+ case C:
186+ System.err.println(A);
187+ yield 0;
188+ default: yield 0;
189+ }|
190+ }
191+ }
192+ enum E {
193+ A, B, C;
194+ }
195+ """ ,
196+ "21" );
197+ ImportsWrapper wrapper = (ImportsWrapper ) Toolkit .getDefaultToolkit ().getSystemClipboard ().getData (ClipboardHandler .IMPORT_FLAVOR );
198+ Map <String , String > simple2ImportFQN = wrapper .getSimple2ImportFQN ();
199+ assertEquals (Map .of ("A" , "test.E.A" , "B" , "test.E.B" , "C" , "test.E.C" , "System" , "java.lang.System" ),
200+ simple2ImportFQN );
201+ }
202+
203+ public void testSwitchSimpleEnumNames4 () throws Exception {
204+ doCopyAndPaste ("""
205+ package test;
206+ import static test.E.A;
207+ public class Test {
208+ private int test(Undef o) {
209+ return |switch (o) {
210+ case A:
211+ case B:
212+ case C:
213+ System.err.println(A);
214+ yield 0;
215+ default: yield 0;
216+ }|
217+ }
218+ }
219+ enum E {
220+ A, B, C;
221+ }
222+ """ ,
223+ "21" );
224+ ImportsWrapper wrapper = (ImportsWrapper ) Toolkit .getDefaultToolkit ().getSystemClipboard ().getData (ClipboardHandler .IMPORT_FLAVOR );
225+ Map <String , String > simple2ImportFQN = wrapper .getSimple2ImportFQN ();
226+ assertEquals (Map .of ("A" , "test.E.A" , "System" , "java.lang.System" ),
227+ simple2ImportFQN );
228+ }
229+
122230 private void copyAndPaste (String from , final String to , String golden ) throws Exception {
123231 copyAndPaste (from , to , golden , null );
124232 }
@@ -128,6 +236,36 @@ private void copyAndPaste(String from, final String to, String golden, String so
128236
129237 assertTrue (pastePos >= 0 );
130238
239+ FileObject src = doCopyAndPaste (from , sourceLevel );
240+ final JEditorPane [] target = new JEditorPane [1 ];
241+ final Exception [] fromAWT = new Exception [1 ];
242+
243+ target [0 ] = paneFor (src , "test/Target.java" , to .replaceAll (Pattern .quote ("^" ), "" ), sourceLevel );
244+ SwingUtilities .invokeAndWait (new Runnable () {
245+ @ Override public void run () {
246+ try {
247+ target [0 ].setCaretPosition (pastePos );
248+
249+ target [0 ].paste ();
250+ } catch (Exception ex ) {
251+ fromAWT [0 ] = ex ;
252+ }
253+ }
254+ });
255+
256+ if (fromAWT [0 ] != null ) throw fromAWT [0 ];
257+
258+ final String [] actual = new String [1 ];
259+
260+ SwingUtilities .invokeAndWait (new Runnable () {
261+ @ Override public void run () {
262+ actual [0 ] = target [0 ].getText ();
263+ }
264+ });
265+ assertEquals (golden , actual [0 ]);
266+ }
267+
268+ private FileObject doCopyAndPaste (String from , String sourceLevel ) throws Exception {
131269 String [] split = from .split (Pattern .quote ("|" ));
132270
133271 assertEquals (3 , split .length );
@@ -145,22 +283,16 @@ private void copyAndPaste(String from, final String to, String golden, String so
145283 SourceUtilsTestUtil .prepareTest (src , build , cache );
146284 SourceUtilsTestUtil .compileRecursively (src );
147285
148- final JEditorPane [] target = new JEditorPane [1 ];
149286 final Exception [] fromAWT = new Exception [1 ];
150287
151288 final JEditorPane source = paneFor (src , "test/Test.java" , cleanFrom , sourceLevel );
152- target [0 ] = paneFor (src , "test/Target.java" , to .replaceAll (Pattern .quote ("^" ), "" ), sourceLevel );
153289 SwingUtilities .invokeAndWait (new Runnable () {
154290 @ Override public void run () {
155291 try {
156292 source .setSelectionStart (start );
157293 source .setSelectionEnd (end );
158294
159295 source .copy ();
160-
161- target [0 ].setCaretPosition (pastePos );
162-
163- target [0 ].paste ();
164296 } catch (Exception ex ) {
165297 fromAWT [0 ] = ex ;
166298 }
@@ -169,14 +301,7 @@ private void copyAndPaste(String from, final String to, String golden, String so
169301
170302 if (fromAWT [0 ] != null ) throw fromAWT [0 ];
171303
172- final String [] actual = new String [1 ];
173-
174- SwingUtilities .invokeAndWait (new Runnable () {
175- @ Override public void run () {
176- actual [0 ] = target [0 ].getText ();
177- }
178- });
179- assertEquals (golden , actual [0 ]);
304+ return src ;
180305 }
181306
182307 private JEditorPane paneFor (FileObject src , String fileName , String code , String sourceLevel ) throws Exception , DataObjectNotFoundException , IOException {
0 commit comments