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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.refactoring.ast;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileReader;
033: import java.io.FileWriter;
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import org.netbeans.api.project.Project;
037: import org.netbeans.api.project.ProjectManager;
038: import org.netbeans.api.project.ui.OpenProjects;
039: import org.netbeans.junit.MockServices;
040: import org.netbeans.junit.NbTestCase;
041: import org.netbeans.modules.java.source.usages.RepositoryUpdater;
042: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
043: import org.netbeans.modules.refactoring.api.Problem;
044: import org.netbeans.modules.refactoring.api.RefactoringSession;
045: import org.netbeans.modules.refactoring.api.RenameRefactoring;
046: import org.netbeans.modules.refactoring.java.ParameterSetter;
047: import org.openide.filesystems.FileObject;
048: import org.openide.filesystems.FileUtil;
049: import org.openide.util.lookup.Lookups;
050:
051: /**
052: *
053: * @author Jiri Prox
054: */
055: public class ASTGenTest extends NbTestCase {
056:
057: protected PrintWriter ref = null;
058:
059: protected PrintWriter log = null;
060:
061: public static File sourceDir = null;
062:
063: public static File resultDir = null;
064:
065: public static FileObject openedProject = null;
066:
067: public ASTGenTest(String name) {
068: super (name);
069: }
070:
071: @Override
072: @SuppressWarnings("deprecation")
073: protected void setUp() throws Exception {
074: super .setUp();
075: clearWorkDir();
076: MockServices.setServices();
077: FileUtil.setMIMEType("java", "text/x-java");
078: File cacheFolder = new File(getWorkDir(), "var/cache/index");
079: cacheFolder.mkdirs();
080: System.setProperty("netbeans.user", cacheFolder.getPath());
081: if (System.getProperty("source.dir") != null) {
082: String sourceDirPath = System.getProperty("source.dir");
083: File directory = new File(sourceDirPath);
084: if (directory.isAbsolute())
085: sourceDir = directory;
086: else
087: sourceDir = new File(getDataDir(), sourceDirPath);
088: } else {
089: sourceDir = getDataDir();
090: }
091:
092: if (System.getProperty("result.dir") != null) {
093: String resultDirPath = System.getProperty("result.dir");
094: File directory = new File(resultDirPath);
095: if (directory.isAbsolute())
096: resultDir = directory;
097: else
098: resultDir = new File(getDataDir(), resultDirPath);
099: } else {
100: resultDir = getWorkDir();
101: }
102: System.out.println("#### " + getName() + " ####");
103: ref = new PrintWriter(new File(resultDir, getName() + ".ref"));
104: log = new PrintWriter(new File(resultDir, getName() + ".log"));
105: ref.println("#### " + getName() + " ####");
106: log.println("#### " + getName() + " ####");
107: }
108:
109: @Override
110: protected void tearDown() throws Exception {
111: log.close();
112: ref.close();
113: super .tearDown();
114: }
115:
116: public void testRename() throws IOException, InterruptedException {
117: File[] projects = sourceDir.listFiles();
118: for (int i = 0; i < projects.length; i++) {
119: File project = projects[i];
120: if (project.isFile())
121: continue; //only directories
122: FileObject srcRoot = FileUtil.toFileObject(new File(
123: project, "src"));
124: RepositoryUpdater.getDefault().scheduleCompilationAndWait(
125: srcRoot, srcRoot).await();
126: openedProject = openProject(project);
127: String testClass = "A";
128: if (System.getProperty("test.class") != null) {
129: testClass = System.getProperty("test.class");
130: }
131: final String newName;
132: if (System.getProperty("new.name") != null) {
133: newName = System.getProperty("new.name");
134: } else {
135: newName = "NewName";
136: }
137: FileObject test = openedProject.getFileObject("src/"
138: + testClass.replace('.', '/') + ".java");
139: final RenameRefactoring renameRefactoring = new RenameRefactoring(
140: Lookups.singleton(test));
141: performASTGenRerfactoring(renameRefactoring,
142: new ParameterSetter() {
143: public void setParameters() {
144: renameRefactoring.setNewName(newName);
145: }
146: }, project);
147: }
148: }
149:
150: public FileObject openProject(File project) throws IOException {
151: FileObject projectFO = FileUtil.toFileObject(project);
152: Project p = ProjectManager.getDefault().findProject(projectFO);
153: OpenProjects.getDefault().open(new Project[] { p }, false);
154: assertNotNull("Project is not opened", p);
155: log.println("Project " + project.getName() + " opened");
156: return projectFO;
157: }
158:
159: public boolean performASTGenRerfactoring(
160: AbstractRefactoring absRefactoring,
161: ParameterSetter parameterSetter, File project) {
162: Problem problem = absRefactoring.preCheck();
163: boolean fatal = false;
164: while (problem != null) {
165: ref.print(problem.getMessage());
166: fatal = fatal || problem.isFatal();
167: problem = problem.getNext();
168: }
169: if (fatal) {
170: return false;
171: }
172: parameterSetter.setParameters();
173: problem = absRefactoring.fastCheckParameters();
174: while (problem != null) {
175: ref.print(problem.getMessage());
176: fatal = fatal || problem.isFatal();
177: problem = problem.getNext();
178: }
179: if (fatal) {
180: return false;
181: }
182: problem = absRefactoring.checkParameters();
183: while (problem != null) {
184: ref.print(problem.getMessage());
185: fatal = fatal || problem.isFatal();
186: problem = problem.getNext();
187: }
188: if (fatal) {
189: return false;
190: }
191: RefactoringSession rs = RefactoringSession.create("Session");
192: absRefactoring.prepare(rs);
193: rs.doRefactoring(true);
194: writeResult(project);
195: return true;
196: }
197:
198: private void writeResult(File project) {
199: File srcRoot = new File(sourceDir, project.getName() + "/src");
200: copyDir(srcRoot, new File(resultDir, project.getName()), true);
201: }
202:
203: public void copyDir(File srcDir, File destDir, boolean recursive) {
204: if (!destDir.exists())
205: destDir.mkdirs();
206: File[] files = srcDir.listFiles();
207: for (int i = 0; i < files.length; i++) {
208: File file = files[i];
209: if (file.getName().startsWith("."))
210: continue; //skip hidden
211: if (file.isDirectory()) {
212: if (recursive)
213: copyDir(file, new File(destDir, file.getName()),
214: true);
215: } else {
216: copyFile(file, new File(destDir, file.getName()));
217: }
218: }
219: }
220:
221: public void copyFile(File src, File dst) {
222: BufferedReader br = null;
223: FileWriter fw = null;
224: try {
225: br = new BufferedReader(new FileReader(src));
226: fw = new FileWriter(dst);
227: String buff;
228: while ((buff = br.readLine()) != null)
229: fw.write(buff + "\n");
230: fw.close();
231: br.close();
232: } catch (IOException ioexception) {
233: ioexception.printStackTrace(log);
234: fail("Error while storing results");
235: } finally {
236: try {
237: if (fw != null)
238: fw.close();
239: if (br != null)
240: br.close();
241: } catch (IOException ioexception) {
242: ioexception.printStackTrace(log);
243: fail("Error while storing results");
244: }
245: }
246: }
247:
248: }
|