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 editor_actions;
043:
044: import java.awt.datatransfer.Transferable;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.io.OutputStream;
049: import java.io.PrintStream;
050: import java.util.Hashtable;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.Document;
053: import lib.EditorTestCase;
054: import org.netbeans.jellytools.EditorOperator;
055: import org.netbeans.jemmy.operators.JEditorPaneOperator;
056:
057: /**
058: * Basic Editor Actions Test class.
059: * It contains basic editor actions functionality methods.
060: *
061: *
062: * @author Martin Roskanin
063: */
064: public class EditorActionsTest extends EditorTestCase {
065:
066: // private PrintStream wrapper for System.out
067: private PrintStream systemOutPSWrapper = new PrintStream(System.out);
068: private int index = 0;
069: public static final int WAIT_MAX_MILIS_FOR_UNDO_REDO = 2000;
070:
071: /** Creates a new instance of Main */
072: public EditorActionsTest(String testMethodName) {
073: super (testMethodName);
074: }
075:
076: private String getIndexAsString() {
077: String ret = String.valueOf(index);
078: if (ret.length() == 1)
079: ret = "0" + ret;
080: return ret;
081: }
082:
083: private String getRefFileName() {
084: return this .getName() + getIndexAsString() + ".ref"; //NOI18N
085: }
086:
087: private String getGoldenFileName() {
088: return this .getName() + getIndexAsString() + ".pass"; //NOI18N
089: }
090:
091: private String getDiffFileName() {
092: return this .getName() + getIndexAsString() + ".diff"; //NOI18N
093: }
094:
095: // hashtable holding all already used logs and correspondig printstreams
096: private Hashtable logStreamTable = null;
097:
098: private PrintStream getFileLog(String logName) throws IOException {
099: OutputStream outputStream;
100: FileOutputStream fileOutputStream;
101:
102: if ((logStreamTable == null) | (hasTestMethodChanged())) {
103: // we haven't used logging capability - create hashtables
104: logStreamTable = new Hashtable();
105: //System.out.println("Created new hashtable");
106: } else {
107: if (logStreamTable.containsKey(logName)) {
108: //System.out.println("Getting stream from cache:"+logName);
109: return (PrintStream) logStreamTable.get(logName);
110: }
111: }
112: // we didn't used this log, so let's create it
113: FileOutputStream fileLog = new FileOutputStream(new File(
114: getWorkDir(), logName));
115: PrintStream printStreamLog = new PrintStream(fileLog, true);
116: logStreamTable.put(logName, printStreamLog);
117: //System.out.println("Created new stream:"+logName);
118: return printStreamLog;
119: }
120:
121: private String lastTestMethod = null;
122:
123: private boolean hasTestMethodChanged() {
124: if (!this .getName().equals(lastTestMethod)) {
125: lastTestMethod = this .getName();
126: return true;
127: } else {
128: return false;
129: }
130: }
131:
132: public PrintStream getRef() {
133: String refFilename = getRefFileName();
134: try {
135: return getFileLog(refFilename);
136: } catch (IOException ioe) {
137: // canot get ref file - return system.out
138: //System.err.println("Test method "+this.getName()+" - cannot open ref file:"+refFilename
139: // +" - defaulting to System.out and failing test");
140: fail("Could not open reference file: " + refFilename);
141: return systemOutPSWrapper;
142: }
143: }
144:
145: protected void compareToGoldenFile(Document testDoc) {
146: //waitForMilis(150);
147: try {
148: ref(testDoc.getText(0, testDoc.getLength()));
149: compareReferenceFiles(getRefFileName(),
150: getGoldenFileName(), getDiffFileName());
151: index++;
152: } catch (BadLocationException e) {
153: e.printStackTrace(getLog());
154: fail();
155: }
156: }
157:
158: protected void waitForMilis(int maxMiliSeconds) {
159: int time = (int) maxMiliSeconds / 100;
160: while (time > 0) {
161: try {
162: Thread.currentThread().sleep(100);
163: } catch (InterruptedException ex) {
164: time = 0;
165: }
166: time--;
167:
168: }
169: }
170:
171: protected ValueResolver getFileLengthChangeResolver(
172: final JEditorPaneOperator txtOper, final int oldLength) {
173: log("");
174: log("oldLength:" + oldLength);
175: ValueResolver fileLengthValueResolver = new ValueResolver() {
176: public Object getValue() {
177: int newLength = txtOper.getDocument().getLength();
178: log("newLength:" + newLength);
179: return (newLength == oldLength) ? Boolean.TRUE
180: : Boolean.FALSE;
181: }
182: };
183:
184: return fileLengthValueResolver;
185: }
186:
187: protected void resetCounter() {
188: index = 0;
189: }
190:
191: }
|