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.java;
043:
044: import java.io.*;
045: import java.util.ArrayList;
046: import org.netbeans.api.project.Project;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.junit.MockServices;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.junit.diff.LineDiff;
051: import org.netbeans.modules.java.source.usages.RepositoryUpdater;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileUtil;
054: import org.netbeans.api.project.ui.OpenProjects;
055:
056: /** LogTestCase
057: * @author Jan Becicka
058: */
059: public class LogTestCase extends NbTestCase {
060:
061: /**
062: * state - true - testing
063: * - false - generating goldenfiles
064: */
065: public static boolean CREATE_GOLDENFILES = false;
066:
067: protected FileObject projectDirFo;
068:
069: private static boolean initProjects = true;
070:
071: static {
072: if (System.getProperty("create.goldenfiles") != null
073: && System.getProperty("create.goldenfiles").equals(
074: "true")) {
075: CREATE_GOLDENFILES = true;
076: }
077: }
078:
079: /** directory, where the golden and .diff files resides
080: */
081: protected File classPathWorkDir;
082: /** test will generate this file
083: */
084: protected File refFile;
085:
086: protected PrintWriter log = null;
087: protected PrintWriter ref = null;
088: protected PrintWriter golden = null;
089:
090: public LogTestCase(java.lang.String testName) {
091: super (testName);
092: }
093:
094: /** sets the PrintWriters
095: */
096: protected void setUp() throws IOException {
097: clearWorkDir();
098: MockServices.setServices();
099: FileUtil.setMIMEType("java", "text/x-java");
100: File cacheFolder = new File(getWorkDir(), "var/cache/index");
101: cacheFolder.mkdirs();
102: System.setProperty("netbeans.user", cacheFolder.getPath());
103:
104: prepareProject();
105:
106: FileObject fo = FileUtil.toFileObject(classPathWorkDir);
107: if (initProjects) {
108: RepositoryUpdater.getDefault().scheduleCompilationAndWait(
109: fo, fo);
110: initProjects = false;
111: }
112:
113: try {
114: //logs and refs
115: refFile = new File(getWorkDir(), getName() + ".ref");
116: File logFile = new File(getWorkDir(), getName() + ".log");
117: ref = new PrintWriter(new BufferedWriter(new FileWriter(
118: refFile)));
119: log = new PrintWriter(new BufferedWriter(new FileWriter(
120: logFile)));
121: if (CREATE_GOLDENFILES) { //generates golden files
122: File f;
123: //generate goldenfile name
124: f = getDataDir().getParentFile();
125: ArrayList names = new ArrayList();
126: names.add("goldenfiles"); //!reverse order
127: names.add("data"); //!reverse order
128: names.add("unit"); //!reverse order
129: while (!f.getName().equals("test")) {
130: if (!f.getName().equals("sys")
131: && !f.getName().equals("work")
132: && !f.getName().equals("tests")) {
133: names.add(f.getName());
134: }
135: f = f.getParentFile(); //cr. goldenf. inside test/unit/data/
136: }
137: for (int i = names.size() - 1; i > -1; i--) {
138: f = new File(f, (String) (names.get(i)));
139: }
140: f = new File(f, getClass().getName().replace('.',
141: File.separatorChar));
142: f = new File(f, getName() + ".pass");
143: if (!f.getParentFile().exists()) {
144: f.getParentFile().mkdirs();
145: }
146: golden = new PrintWriter(new BufferedWriter(
147: new FileWriter(f)));
148: log("Passive mode: generate golden file into "
149: + f.getAbsolutePath());
150: }
151: } catch (Exception e) {
152: e.printStackTrace();
153: assertTrue(e.toString(), false);
154: }
155: }
156:
157: public void prepareProject() {//default - override for another projects
158: classPathWorkDir = new File(getDataDir(),
159: "projects.default.src".replace('.', File.separatorChar));
160: }
161:
162: public void log(String s) {
163: log.println(s);
164: }
165:
166: public void log(Object o) {
167: log.println(o);
168: }
169:
170: public void log(File file) {
171: try {
172: BufferedReader br = new BufferedReader(new FileReader(file));
173: String line;
174: while ((line = br.readLine()) != null) {
175: log(line);
176: }
177: br.close();
178: } catch (Exception ex) {
179: ex.printStackTrace();
180: }
181: }
182:
183: public void ref(String s) {
184: ref.println(s);
185: if (CREATE_GOLDENFILES) {
186: golden.println(s);
187: }
188: }
189:
190: public void ref(Object o) {
191: ref.println(o.toString());
192: if (CREATE_GOLDENFILES) {
193: golden.println(o.toString());
194: }
195: }
196:
197: public void ref(File file) {
198: try {
199: BufferedReader br = new BufferedReader(new FileReader(file));
200: String line;
201: while ((line = br.readLine()) != null) {
202: ref(line);
203: }
204: br.close();
205: } catch (Exception ex) {
206: ex.printStackTrace();
207: }
208: }
209:
210: /** sets the PrintWriters
211: */
212: protected void tearDown() {
213: ref.close();
214: log.close();
215: if (CREATE_GOLDENFILES) {
216: golden.flush();
217: golden.close();
218: assertTrue("Passive mode", false);
219: } else {
220: try {
221: assertFile("Golden file differs ", refFile,
222: getGoldenFile(), getWorkDir(), new LineDiff());
223: } catch (Exception ex) {
224: ex.printStackTrace();
225: assertTrue(ex.toString(), false);
226: }
227: }
228: }
229:
230: public FileObject openProject(String projectName)
231: throws IOException {
232: File projectsDir = FileUtil.normalizeFile(new File(
233: getDataDir(), "projects"));
234: FileObject projectsDirFO = FileUtil.toFileObject(projectsDir);
235: FileObject projdir = projectsDirFO.getFileObject(projectName);
236: Project p = ProjectManager.getDefault().findProject(projdir);
237: OpenProjects.getDefault().open(new Project[] { p }, false);
238: assertNotNull("Project is not opened", p);
239: return projdir;
240: }
241:
242: public void copyFile(File src, File dst) {
243: BufferedReader br = null;
244: FileWriter fw = null;
245: try {
246: br = new BufferedReader(new FileReader(src));
247: fw = new FileWriter(dst);
248: String buff;
249: while ((buff = br.readLine()) != null)
250: fw.write(buff + "\n");
251: fw.close();
252: br.close();
253: } catch (IOException ioexception) {
254: ioexception.printStackTrace(log);
255: fail("Error while creating backupfile");
256: } finally {
257: try {
258: if (fw != null)
259: fw.close();
260: if (br != null)
261: br.close();
262: } catch (IOException ioexception) {
263: ioexception.printStackTrace(log);
264: fail("Error while closing backupfile");
265: }
266: }
267: }
268:
269: protected String getRelativeFileName(FileObject fo) {
270: String relPath = FileUtil.getRelativePath(projectDirFo, fo);
271: String res = relPath.replace('/', '.');
272: if (res.startsWith("src."))
273: res = res.substring(4);
274: return res;
275: }
276:
277: protected FileObject getFileInProject(String project, String file)
278: throws IOException {
279: projectDirFo = openProject(project);
280: FileObject test = projectDirFo.getFileObject(file);
281: return test;
282: }
283:
284: }
|