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.apisupport.refactoring;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.FileOutputStream;
048: import java.io.FileReader;
049: import java.io.IOException;
050: import java.io.PrintStream;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.junit.NbTestSuite;
053:
054: /**
055: *
056: * @author Petr Zajac
057: */
058: public class RenameTest extends NbTestCase {
059:
060: private static String[] resultFiles = {
061: "src/testRename/RenamedAction.java",
062: "src/testRename/layer.xml",
063: "src/testRename/RenamedLoader.java", "manifest.mf",
064: "src/META-INF/services/org.openide.loaders.UniFileLoader" };
065: private static String[] goldenFiles = { "RenamedAction.java.pass",
066: "layer.xml.pass", "RenamedLoader.java.pass",
067: "manifest.mf.pass",
068: "org.openide.loaders.UniFileLoader.pass" };
069:
070: private String PATH_PREFIX = "";
071:
072: // private static TypeClass typeProxy;
073: // private static JavaClass jc;
074:
075: private PrintStream refPs;
076:
077: /** Creates a new instance of Signature1Test */
078: public RenameTest(String name) {
079: super (name);
080: }
081:
082: public static NbTestSuite suite() {
083: NbTestSuite suite = new NbTestSuite();
084: // suite.addTestSuite(RenameTest.class);
085: return suite;
086: }
087:
088: // public void testRename() throws FileStateInvalidException, IOException {
089: // jc = (JavaClass) TestUtility.findClass("testRename.MyAction");
090: // typeProxy = ((JavaModelPackage) jc.refOutermostPackage()).getType();
091: //
092: // jc = (JavaClass) typeProxy.resolve("testRename.MyAction");
093: // RenameRefactoring refactoring = new RenameRefactoring(jc);
094: // refactoring.setNewName("RenamedAction");
095: // refactoring.checkParameters();
096: // RefactoringSession result = RefactoringSession.create("rename class");
097: // refactoring.prepare(result);
098: // result.doRefactoring(true);
099: //
100: // jc = (JavaClass) TestUtility.findClass("testRename.MyDataLoader");
101: // typeProxy = ((JavaModelPackage) jc.refOutermostPackage()).getType();
102: //
103: // jc = (JavaClass) typeProxy.resolve("testRename.MyDataLoader");
104: // refactoring = new RenameRefactoring(jc);
105: // refactoring.setNewName("RenamedLoader");
106: // refactoring.checkParameters();
107: // result = RefactoringSession.create("rename class");
108: // refactoring.prepare(result);
109: // result.doRefactoring(true);
110: //
111: // // check modified files
112: // for (int x = 0; x < resultFiles.length; x++) {
113: // String fileName = PATH_PREFIX + resultFiles[x] ;
114: // log("assertFile " + fileName);
115: // File resF = TestUtility.getFile(getDataDir(),"testRename", fileName);
116: // File goldenF = getGoldenFile(goldenFiles[x]);
117: // File f1 = writeToWorkDir(resF,resF.getName() + ".result");
118: // File f2 = writeToWorkDir(goldenF,goldenF.getName() + ".pass");
119: // assertFile(f1,f2);
120: // f1.delete();
121: // f2.delete();
122: // }
123: // }
124: // public void testWhereUsed() throws Exception {
125: //
126: // File f = new File(getWorkDir(),"whereUsed.ref" );
127: // refPs = new PrintStream(new FileOutputStream(f));
128: // jc = (JavaClass) TestUtility.findClass("testRename.WhereUsedDataLoader");
129: // ref("testrename.MyDataLoader");
130: // WhereUsedQuery wu= new WhereUsedQuery(jc);
131: // wu.setSearchInComments(true);
132: // findClass(wu);
133: // wu = new WhereUsedQuery(TestUtility.findClass("testRename.WhereUsedAction"));
134: // ref("testrename.WhereUsedAction");
135: // findClass(wu);
136: // refPs.close();
137: // assertFile(f, getGoldenFile("whereUsed.ref"));
138: //
139: // }
140: //
141: //
142: // public static String getAsString(String file) {
143: // String result;
144: // try {
145: // FileObject testFile = Repository.getDefault().findResource(file);
146: // DataObject dob = DataObject.find(testFile);
147: //
148: // EditorCookie ec = (EditorCookie) dob.getCookie(EditorCookie.class);
149: // StyledDocument doc = ec.openDocument();
150: // result = doc.getText(0, doc.getLength());
151: // }
152: // catch (Exception e) {
153: // throw new AssertionFailedErrorException(e);
154: // }
155: // return result;
156: // }
157:
158: // protected void findClass(WhereUsedQuery wu) {
159: // RefactoringSession result = RefactoringSession.create(null);
160: // refProblems(wu.prepare(result));
161: // refUsages(result);
162: // ref("");
163: // }
164:
165: /**
166: * Stores problems into ref file. Problems should be sorted.
167: * @return true if problem is not null and one of them is fatal
168: */
169: // public boolean refProblems(Problem problem) {
170: // Problem p=problem;
171: // boolean ret=false;
172: // if (p != null) {
173: // ArrayList list=new ArrayList();
174: // while (p != null) {
175: // if (p.isFatal()) {
176: // ret=true;
177: // list.add("Problem fatal: "+p.getMessage());
178: // } else {
179: // list.add("Problem: "+p.getMessage());
180: // }
181: // p=p.getNext();
182: // }
183: // Collections.sort(list);
184: // for (int i=0;i < list.size();i++) {
185: // ref(list.get(i));
186: // }
187: // }
188: // return ret;
189: // }
190: // protected void refUsages(RefactoringSession session) {
191: // Collection result = session.getRefactoringElements();
192: // ArrayList list=new ArrayList();
193: // HashMap map=new HashMap();
194: // for (Iterator it=result.iterator();it.hasNext();) {
195: // Object o=it.next();
196: // if (o instanceof RefactoringElement) {
197: // RefactoringElement wue=(RefactoringElement) o;
198: // Element el = wue.getJavaElement();
199: // if (el != null && el.getResource() != null) {
200: // String s;
201: // s=el.getResource().getName().replace(File.separatorChar,'/');
202: // list=(ArrayList)(map.get(s));
203: // if (list == null) {
204: // list=new ArrayList();
205: // map.put(s, list);
206: // }
207: // list.add(getDisplayText(wue));
208: // } else {
209: // log("refUsages without resource");
210: // log(getDisplayText(wue));
211: // map.put(getDisplayText(wue), "");
212: // }
213: // }
214: // }
215: // ref("Found "+String.valueOf(result.size())+" occurance(s).");
216: // Object[] keys=map.keySet().toArray();
217: // Arrays.sort(keys);
218: // for (int i=0;i < keys.length;i++) {
219: // ref("");
220: // if (map.get(keys[i]) instanceof ArrayList) {
221: // ref(keys[i]);
222: // list=(ArrayList)(map.get(keys[i]));
223: // Collections.sort(list);
224: // for (int j=0;j < list.size();j++) {
225: // ref(" "+list.get(j));
226: // }
227: // } else {
228: // ref(keys[i]);
229: // }
230: // }
231: // ref("");
232: // }
233: public void ref(String s) {
234: refPs.println(s);
235: }
236:
237: public void ref(Object o) {
238: ref(o.toString());
239: }
240:
241: public void ref(File file) throws Exception {
242: BufferedReader br = new BufferedReader(new FileReader(file));
243: String line;
244: while ((line = br.readLine()) != null) {
245: ref(line);
246: }
247: br.close();
248: }
249:
250: // protected String getDisplayText(RefactoringElement elm) {
251: // String app="";
252: // if (elm.getStatus() == RefactoringElement.WARNING) {
253: // app=" [ warning! ]";
254: // } else if (elm.getStatus() == RefactoringElement.GUARDED) {
255: // app=" [ error: code is in guarded block ]";
256: // }
257: // return elm.getDisplayText()+app;
258: // }
259:
260: private File writeToWorkDir(File resF, String name)
261: throws IOException {
262: byte buff[] = new byte[(int) resF.length()];
263: FileInputStream fis = new FileInputStream(resF);
264: File retF = new File(getWorkDir(), name);
265: FileOutputStream fos = new FileOutputStream(retF);
266: try {
267: fis.read(buff);
268: fos.write(buff);
269: } finally {
270: fis.close();
271: fos.close();
272: }
273: return retF;
274: }
275: }
|