KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > changesignature > ChangeSignatureTestCase


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.test.j2ee.refactoring.changesignature;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.CallableFeature;
26 import org.netbeans.jmi.javamodel.Parameter;
27 import org.netbeans.jmi.javamodel.Type;
28 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
29 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring;
30 import org.netbeans.modules.refactoring.api.RefactoringSession;
31 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring.ParameterInfo;
32 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase;
33 import org.netbeans.test.j2ee.refactoring.Utility;
34
35
36 /**
37  *
38  * @author eh103527
39  */

40 public class ChangeSignatureTestCase extends RefactoringTestCase {
41     
42     /** Creates a new instance of ChangeSignatureTestCase */
43     public ChangeSignatureTestCase(String JavaDoc name) {
44         super(name);
45     }
46     
47     protected void changeSignature(String JavaDoc className, String JavaDoc name, String JavaDoc[] arguments, String JavaDoc[][] newArguments, int access, boolean undo) {
48         setJavaClass(className);
49         Utility.prepareTest();
50         String JavaDoc args="";
51         ArrayList JavaDoc list=new ArrayList JavaDoc();
52         boolean finish=false;
53         HashMap JavaDoc files=null;
54         RefactoringSession result = RefactoringSession.create("Change method signature of " + name);
55         try {
56             if (arguments != null) {
57                 for (int i=0;i < arguments.length;i++) {
58                     list.add(Utility.findType(arguments[i], typeProxy, jc));
59                     args+=arguments[i];
60                     if (i < arguments.length-1) {
61                         args+=", ";
62                     }
63                 }
64             }
65             //get the method
66
CallableFeature method;
67             if (name == null) //constructor
68
method=jc.getConstructor(list, false);
69             else
70                 method=jc.getMethod(name, list, false);
71             
72             ref("Change signature of "+((name==null)?"constructor ":"method ")+
73                     getModifier(method.getModifiers())+" "+
74                     ((method.getName() == null)?"":method.getName())+"("+args+") of class "+className);
75             ref(((name==null)?"constructor ":"method ")+getModifier(access)+" "+
76                     ((method.getName() == null)?"":method.getName()));
77             ref("Arguments: ");
78             for (int i=0;i < newArguments.length;i++) {
79                 if (newArguments[i][1] == null) {
80                     ref(newArguments[i][0]);
81                 } else {
82                     ref(newArguments[i][1]+" "+newArguments[i][0]+" = "+newArguments[i][2]);
83                 }
84             }
85             //prepare arguments array
86
HashMap JavaDoc indicies=new HashMap JavaDoc();
87             List JavaDoc origArguments = method.getParameters();
88             for (int i=0;i < origArguments.size();i++) {
89                 Parameter par=(Parameter)(origArguments.get(i));
90                 for (int j=0;j < newArguments.length;j++) {
91                     if (newArguments[j][0].equals(par.getName())) {
92                         indicies.put(par.getName(), new Integer JavaDoc(i));
93                     }
94                 }
95             }
96             ParameterInfo[] argTable = new ParameterInfo[newArguments.length];
97             for (int i=0;i < argTable.length;i++) {
98                 Type type=null;
99                 if (newArguments[i][1] != null) {
100                     type=typeProxy.resolve(newArguments[i][1]);
101                     argTable[i]=new ParameterInfo(-1, newArguments[i][0], type, newArguments[i][2]);
102                 } else {
103                     argTable[i]=new ParameterInfo(((Integer JavaDoc)(indicies.get(newArguments[i][0]))).intValue());
104                 }
105             }
106             //make refactoring sequence
107
ChangeParametersRefactoring refactoring = new ChangeParametersRefactoring(method);
108             if (refProblems(refactoring.preCheck())) throw new Exception JavaDoc("Fatal problem");
109             refactoring.setParameterInfo(argTable);
110             refactoring.setModifiers(access);
111             if (refProblems(refactoring.checkParameters())) throw new Exception JavaDoc("Fatal problem");
112             if (refProblems(refactoring.prepare(result))) throw new Exception JavaDoc("Fatal problem");
113             files=getResources(result);
114             if (refProblems(result.doRefactoring(true))) throw new Exception JavaDoc("Fatal problem");
115             finish=true;
116         } catch (Throwable JavaDoc t) {
117             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
118                 t.printStackTrace(getLogStream());
119                 assertTrue(t.getMessage(), false);
120             }
121         } finally {
122             Utility.finishTest();
123         }
124         if (finish) {
125             try {
126                 if (undo) {
127                     JavaMetamodel.getUndoManager().undo();
128                 }
129                 compareResources(files, result, className, className);
130             } catch (Exception JavaDoc ex) {
131                 ex.printStackTrace(getLogStream());
132                 assertTrue(ex.getMessage(), false);
133             }
134         }
135     }
136 }
137
Popular Tags