001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.refactoring.api.ui;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import java.lang.reflect.InvocationTargetException;
047: import javax.swing.Action;
048: import javax.swing.JDialog;
049: import javax.swing.SwingUtilities;
050: import junit.framework.TestCase;
051: import org.netbeans.api.java.source.SourceUtils;
052: import org.netbeans.modules.refactoring.api.MoveRefactoring;
053: import org.netbeans.modules.refactoring.api.RenameRefactoring;
054: import org.netbeans.modules.refactoring.java.LogTestCase;
055: import org.netbeans.modules.refactoring.spi.impl.ParametersPanel;
056: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
057: import org.openide.DialogDisplayer;
058: import org.openide.filesystems.FileObject;
059: import org.openide.loaders.DataObject;
060: import org.openide.loaders.DataObjectNotFoundException;
061: import org.openide.nodes.Node;
062: import org.openide.util.Exceptions;
063: import org.openide.util.Lookup;
064: import org.openide.util.lookup.AbstractLookup;
065: import org.openide.util.lookup.InstanceContent;
066:
067: /**
068: *
069: * @author Jan Becicka
070: */
071: public class ActionInvocationTest extends LogTestCase {
072:
073: /** Creates a new instance of ActionInstantiationTest */
074: public ActionInvocationTest(String name) {
075: super (name);
076: }
077:
078: protected void setUp() throws IOException {
079: try {
080: super .setUp();
081: SourceUtils.waitScanFinished();
082: assertEquals(DD.class, Lookup.getDefault().lookup(
083: DialogDisplayer.class).getClass());
084: } catch (InterruptedException ex) {
085: Exceptions.printStackTrace(ex);
086: }
087: }
088:
089: public void testRenameAction() throws InterruptedException,
090: InvocationTargetException, IOException {
091: final FileObject test = getFileInProject("default",
092: "src/defaultpkg/Main.java");
093: DataObject testdo = DataObject.find(test);
094: final Node node = testdo.getNodeDelegate();
095:
096: InstanceContent ic = new InstanceContent();
097: Lookup lookup = new AbstractLookup(ic);
098: ic.add(node);
099: final Action rename = RefactoringActionsFactory.renameAction()
100: .createContextAwareInstance(lookup);
101: SwingUtilities.invokeAndWait(new Runnable() {
102: public void run() {
103: if (rename.isEnabled()) {
104: rename
105: .actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
106: if (!((RenameRefactoring) DD.rui.getRefactoring())
107: .getRefactoringSource().lookup(
108: FileObject.class).equals(test))
109: fail("Rename dialog was opened with wrong data");
110: } else {
111: fail("Action is not enabled.");
112: }
113: }
114: });
115: }
116:
117: public void testMoveAction() throws InterruptedException,
118: InvocationTargetException, DataObjectNotFoundException,
119: IOException {
120: final FileObject test = getFileInProject("default",
121: "src/defaultpkg/Main.java");
122: DataObject testdo = DataObject.find(test);
123: final Node node = testdo.getNodeDelegate();
124:
125: InstanceContent ic = new InstanceContent();
126: Lookup lookup = new AbstractLookup(ic);
127: ic.add(node);
128: final Action move = RefactoringActionsFactory.moveAction()
129: .createContextAwareInstance(lookup);
130: SwingUtilities.invokeAndWait(new Runnable() {
131: public void run() {
132: if (move.isEnabled()) {
133: move
134: .actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
135: if (!((MoveRefactoring) DD.rui.getRefactoring())
136: .getRefactoringSource().lookup(
137: FileObject.class).equals(test))
138: fail("MoveClass was opened with wrong data");
139: } else {
140: fail("Action is not enabled.");
141: }
142: }
143: });
144: }
145:
146: public static final class Lkp extends
147: org.openide.util.lookup.AbstractLookup {
148: public Lkp() {
149: this (new org.openide.util.lookup.InstanceContent());
150: }
151:
152: private Lkp(org.openide.util.lookup.InstanceContent ic) {
153: super (ic);
154: ic.add(new DD());
155: }
156: }
157:
158: /** Our own dialog displayer.
159: */
160: public static final class DD extends org.openide.DialogDisplayer {
161: public static Object[] options;
162: public static RefactoringUI rui;
163: private Object toReturn;
164:
165: public java.awt.Dialog createDialog(
166: org.openide.DialogDescriptor descriptor) {
167: JDialog dialog = new JDialog() {
168: public void setVisible(boolean visible) {
169: }
170:
171: public void show() {
172: }
173: };
174: toReturn = descriptor.getMessage();
175: if (toReturn instanceof Component)
176: dialog.getContentPane().add((Component) toReturn);
177:
178: if (toReturn instanceof ParametersPanel) {
179: try {
180: java.lang.reflect.Field f = toReturn.getClass()
181: .getDeclaredField("rui");
182: f.setAccessible(true);
183: rui = (RefactoringUI) f.get(toReturn);
184: } catch (Exception e) {
185: throw new RuntimeException(e);
186: }
187: }
188:
189: return dialog;
190: }
191:
192: public Object notify(org.openide.NotifyDescriptor descriptor) {
193: assertNull(options);
194: assertNotNull(toReturn);
195: options = descriptor.getOptions();
196: Object r = toReturn;
197: toReturn = null;
198: return r;
199: }
200:
201: } // end of DD
202: }
|