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.junit.diff;
043:
044: import java.io.*;
045: import java.util.StringTokenizer;
046:
047: /** Implementation of native OS diff.
048: */
049: public class NativeDiff implements Diff {
050:
051: String diffcmd;
052:
053: /** Creates new NativeDiff */
054: public NativeDiff() {
055: }
056:
057: public void setCmdLine(String cmdLine) {
058: diffcmd = cmdLine;
059: }
060:
061: public String getCmdLine() {
062: return diffcmd;
063: }
064:
065: /**
066: * @param first first file to compare
067: * @param second second file to compare
068: * @param diff difference file
069: * @return true iff files differ
070: */
071: public boolean diff(java.io.File first, java.io.File second,
072: java.io.File diff) throws java.io.IOException {
073: boolean result;
074: if (null != diff)
075: result = diff(first.getAbsolutePath(), second
076: .getAbsolutePath(), diff.getAbsolutePath());
077: else
078: result = diff(first.getAbsolutePath(), second
079: .getAbsolutePath(), null);
080:
081: return result;
082: }
083:
084: /**
085: * @param first first file to compare
086: * @param second second file to compare
087: * @param diff difference file
088: * @return true iff files differ
089: */
090: public boolean diff(final String first, final String second,
091: String diff) throws java.io.IOException {
092: Process prs = null;
093: File diffFile = null;
094:
095: if (null == diff)
096: diffFile = File.createTempFile("~diff", "tmp~");
097: else
098: diffFile = new File(diff);
099:
100: FileOutputStream fos = new FileOutputStream(diffFile);
101: prs = Runtime.getRuntime().exec(
102: prepareCommand(new File(first).getAbsolutePath(),
103: new File(second).getAbsolutePath()));
104: StreamGobbler outputGobbler = new StreamGobbler(prs
105: .getInputStream(), fos);
106: outputGobbler.start();
107:
108: try {
109: prs.waitFor();
110: outputGobbler.join();
111: } catch (java.lang.InterruptedException e) {
112: }
113:
114: try {
115: fos.flush();
116: fos.close();
117: } catch (IOException e) {
118: e.printStackTrace();
119: }
120:
121: if (0 == prs.exitValue() || null == diff) {
122: diffFile.delete();
123: }
124: return prs.exitValue() != 0;
125: }
126:
127: private String[] prepareCommand(String firstFile, String secondFile) {
128: StringTokenizer tok = new StringTokenizer(diffcmd);
129: int tokensCount = tok.countTokens();
130: String[] cmdarray = new String[tokensCount];
131: for (int i = 0; i < tokensCount; i++) {
132: String token = tok.nextToken();
133: if (token.equals("%TESTFILE%")) {
134: cmdarray[i] = firstFile;
135: } else if (token.equals("%PASSFILE%")) {
136: cmdarray[i] = secondFile;
137: } else {
138: cmdarray[i] = token;
139: }
140: }
141: return cmdarray;
142: }
143:
144: class StreamGobbler extends Thread {
145: InputStream is;
146: OutputStream os;
147:
148: StreamGobbler(InputStream is, OutputStream redirect) {
149: this .is = is;
150: this .os = redirect;
151: }
152:
153: public void run() {
154: try {
155: PrintWriter pw = null;
156: if (os != null)
157: pw = new PrintWriter(os);
158:
159: InputStreamReader isr = new InputStreamReader(is);
160: BufferedReader br = new BufferedReader(isr);
161: String line = null;
162: while ((line = br.readLine()) != null) {
163: if (pw != null)
164: pw.println(line);
165: }
166: if (pw != null) {
167: pw.flush();
168: pw.close();
169: }
170: } catch (IOException ioe) {
171: ioe.printStackTrace();
172: }
173: }
174: }
175: }
|