1 19 20 package org.netbeans.modules.refactoring.api.ui; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import javax.swing.Action ; 26 import javax.swing.JDialog ; 27 import javax.swing.SwingUtilities ; 28 import junit.framework.TestCase; 29 import org.netbeans.modules.refactoring.api.MoveRefactoring; 30 import org.netbeans.modules.refactoring.api.RenameRefactoring; 31 import org.netbeans.modules.refactoring.java.LogTestCase; 32 import org.netbeans.modules.refactoring.spi.impl.ParametersPanel; 33 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI; 34 import org.openide.DialogDisplayer; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 import org.openide.nodes.Node; 39 import org.openide.util.Lookup; 40 import org.openide.util.lookup.AbstractLookup; 41 import org.openide.util.lookup.InstanceContent; 42 43 47 public class ActionInvocationTest extends LogTestCase { 48 49 50 51 public ActionInvocationTest(String name) { 52 super(name); 53 } 54 55 protected void setUp() throws IOException { 56 super.setUp(); 57 assertEquals(DD.class, Lookup.getDefault().lookup(DialogDisplayer.class).getClass()); 58 } 59 60 public void testRenameAction() throws InterruptedException , InvocationTargetException , IOException { 61 final FileObject test = getFileInProject("default","src/defaultpkg/Main.java" ); 62 DataObject testdo = DataObject.find(test); 63 final Node node = testdo.getNodeDelegate(); 64 65 InstanceContent ic = new InstanceContent(); 66 Lookup lookup = new AbstractLookup(ic); 67 ic.add(node); 68 final Action rename = RefactoringActionsFactory.renameAction().createContextAwareInstance(lookup); 69 SwingUtilities.invokeAndWait(new Runnable () { 70 public void run() { 71 if (rename.isEnabled()) { 72 rename.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT); 73 if (!((RenameRefactoring) DD.rui.getRefactoring()).getRefactoringSource().lookup(FileObject.class).equals(test)) 74 fail("Rename dialog was opened with wrong data"); 75 } else { 76 fail("Action is not enabled."); 77 } 78 } 79 }); 80 } 81 82 public void testMoveAction() throws InterruptedException , InvocationTargetException , DataObjectNotFoundException, IOException { 83 final FileObject test = getFileInProject("default","src/defaultpkg/Main.java" ); 84 DataObject testdo = DataObject.find(test); 85 final Node node = testdo.getNodeDelegate(); 86 87 InstanceContent ic = new InstanceContent(); 88 Lookup lookup = new AbstractLookup(ic); 89 ic.add(node); 90 final Action move = RefactoringActionsFactory.moveAction().createContextAwareInstance(lookup); 91 SwingUtilities.invokeAndWait(new Runnable () { 92 public void run() { 93 if (move.isEnabled()) { 94 move.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT); 95 if (!((MoveRefactoring) DD.rui.getRefactoring()).getRefactoringSource().lookup(FileObject.class).equals(test)) 96 fail("MoveClass was opened with wrong data"); 97 } else { 98 fail("Action is not enabled."); 99 } 100 } 101 }); 102 } 103 104 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 105 public Lkp () { 106 this (new org.openide.util.lookup.InstanceContent ()); 107 } 108 109 private Lkp (org.openide.util.lookup.InstanceContent ic) { 110 super (ic); 111 ic.add (new DD ()); 112 } 113 } 114 115 117 public static final class DD extends org.openide.DialogDisplayer { 118 public static Object [] options; 119 public static RefactoringUI rui; 120 private Object toReturn; 121 122 public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) { 123 JDialog dialog = new JDialog () { 124 public void setVisible(boolean visible) { 125 } 126 127 public void show() { 128 } 129 }; 130 toReturn = descriptor.getMessage(); 131 if (toReturn instanceof Component ) 132 dialog.getContentPane().add((Component ) toReturn); 133 134 if (toReturn instanceof ParametersPanel) { 135 try { 136 java.lang.reflect.Field f = toReturn.getClass().getDeclaredField("rui"); 137 f.setAccessible(true); 138 rui = (RefactoringUI) f.get(toReturn); 139 } catch (Exception e) { 140 throw new RuntimeException (e); 141 } 142 } 143 144 return dialog; 145 } 146 147 public Object notify(org.openide.NotifyDescriptor descriptor) { 148 assertNull (options); 149 assertNotNull(toReturn); 150 options = descriptor.getOptions(); 151 Object r = toReturn; 152 toReturn = null; 153 return r; 154 } 155 156 } } 158 | Popular Tags |