KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > api > ui > ActionInvocationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.refactoring.api.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.JDialog JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
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 /**
44  *
45  * @author Jan Becicka
46  */

47 public class ActionInvocationTest extends LogTestCase {
48     
49
50     /** Creates a new instance of ActionInstantiationTest */
51     public ActionInvocationTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     protected void setUp() throws IOException JavaDoc {
56        super.setUp();
57        assertEquals(DD.class, Lookup.getDefault().lookup(DialogDisplayer.class).getClass());
58     }
59     
60     public void testRenameAction() throws InterruptedException JavaDoc, InvocationTargetException JavaDoc, IOException JavaDoc {
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 JavaDoc rename = RefactoringActionsFactory.renameAction().createContextAwareInstance(lookup);
69         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc, InvocationTargetException JavaDoc, DataObjectNotFoundException, IOException JavaDoc {
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 JavaDoc move = RefactoringActionsFactory.moveAction().createContextAwareInstance(lookup);
91         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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     /** Our own dialog displayer.
116      */

117     public static final class DD extends org.openide.DialogDisplayer {
118         public static Object JavaDoc[] options;
119         public static RefactoringUI rui;
120         private Object JavaDoc toReturn;
121         
122         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
123             JDialog JavaDoc dialog = new JDialog JavaDoc() {
124                 public void setVisible(boolean visible) {
125                 }
126                 
127                 public void show() {
128                 }
129             };
130             toReturn = descriptor.getMessage();
131             if (toReturn instanceof Component JavaDoc)
132                 dialog.getContentPane().add((Component JavaDoc) toReturn);
133             
134             if (toReturn instanceof ParametersPanel) {
135                 try {
136                     java.lang.reflect.Field JavaDoc f = toReturn.getClass().getDeclaredField("rui");
137                     f.setAccessible(true);
138                     rui = (RefactoringUI) f.get(toReturn);
139                 } catch (Exception JavaDoc e) {
140                     throw new RuntimeException JavaDoc(e);
141                 }
142             }
143             
144             return dialog;
145         }
146         
147         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
148             assertNull (options);
149             assertNotNull(toReturn);
150             options = descriptor.getOptions();
151             Object JavaDoc r = toReturn;
152             toReturn = null;
153             return r;
154         }
155         
156     } // end of DD
157
}
158
Popular Tags