KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > editor_actions > EditorActionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package editor_actions;
21
22 import java.awt.datatransfer.Transferable JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.Document JavaDoc;
31 import lib.EditorTestCase;
32 import org.netbeans.jellytools.EditorOperator;
33 import org.netbeans.jemmy.operators.JEditorPaneOperator;
34
35 /**
36  * Basic Editor Actions Test class.
37  * It contains basic editor actions functionality methods.
38  *
39  *
40  * @author Martin Roskanin
41  */

42   public class EditorActionsTest extends EditorTestCase {
43
44     // private PrintStream wrapper for System.out
45
private PrintStream JavaDoc systemOutPSWrapper = new PrintStream JavaDoc(System.out);
46     private int index = 0;
47     public static final int WAIT_MAX_MILIS_FOR_UNDO_REDO = 2000;
48       
49     /** Creates a new instance of Main */
50     public EditorActionsTest(String JavaDoc testMethodName) {
51         super(testMethodName);
52     }
53
54     private String JavaDoc getIndexAsString(){
55         String JavaDoc ret = String.valueOf(index);
56         if (ret.length() == 1) ret = "0" + ret;
57         return ret;
58     }
59     
60     private String JavaDoc getRefFileName(){
61         return this.getName()+getIndexAsString()+".ref"; //NOI18N
62
}
63     
64     private String JavaDoc getGoldenFileName(){
65         return this.getName()+getIndexAsString()+".pass"; //NOI18N
66
}
67     
68     private String JavaDoc getDiffFileName(){
69         return this.getName()+getIndexAsString()+".diff"; //NOI18N
70
}
71     
72     // hashtable holding all already used logs and correspondig printstreams
73
private Hashtable JavaDoc logStreamTable = null;
74     
75     private PrintStream JavaDoc getFileLog(String JavaDoc logName) throws IOException JavaDoc {
76         OutputStream JavaDoc outputStream;
77         FileOutputStream JavaDoc fileOutputStream;
78         
79         if ((logStreamTable == null)|(hasTestMethodChanged())) {
80             // we haven't used logging capability - create hashtables
81
logStreamTable = new Hashtable JavaDoc();
82             //System.out.println("Created new hashtable");
83
} else {
84             if (logStreamTable.containsKey(logName)) {
85                 //System.out.println("Getting stream from cache:"+logName);
86
return (PrintStream JavaDoc)logStreamTable.get(logName);
87             }
88         }
89         // we didn't used this log, so let's create it
90
FileOutputStream JavaDoc fileLog = new FileOutputStream JavaDoc(new File JavaDoc(getWorkDir(),logName));
91         PrintStream JavaDoc printStreamLog = new PrintStream JavaDoc(fileLog,true);
92         logStreamTable.put(logName,printStreamLog);
93         //System.out.println("Created new stream:"+logName);
94
return printStreamLog;
95     }
96     
97     private String JavaDoc lastTestMethod=null;
98     
99     private boolean hasTestMethodChanged() {
100         if (!this.getName().equals(lastTestMethod)) {
101             lastTestMethod=this.getName();
102             return true;
103         } else {
104             return false;
105         }
106     }
107     
108     public PrintStream JavaDoc getRef() {
109         String JavaDoc refFilename = getRefFileName();
110         try {
111             return getFileLog(refFilename);
112         } catch (IOException JavaDoc ioe) {
113             // canot get ref file - return system.out
114
//System.err.println("Test method "+this.getName()+" - cannot open ref file:"+refFilename
115
// +" - defaulting to System.out and failing test");
116
fail("Could not open reference file: "+refFilename);
117             return systemOutPSWrapper;
118         }
119     }
120
121     protected void compareToGoldenFile(Document JavaDoc testDoc){
122         //waitForMilis(150);
123
try {
124         ref(testDoc.getText(0, testDoc.getLength()));
125         compareReferenceFiles(getRefFileName(), getGoldenFileName(), getDiffFileName());
126         index++;
127         } catch (BadLocationException JavaDoc e) {
128             e.printStackTrace(getLog());
129             fail();
130         }
131     }
132     
133     
134     protected void waitForMilis(int maxMiliSeconds){
135         int time = (int) maxMiliSeconds / 100;
136         while (time > 0) {
137             try {
138                 Thread.currentThread().sleep(100);
139             } catch (InterruptedException JavaDoc ex) {
140                 time=0;
141             }
142             time--;
143             
144         }
145     }
146
147     protected ValueResolver getFileLengthChangeResolver(final JEditorPaneOperator txtOper, final int oldLength){
148         log("");
149         log("oldLength:"+oldLength);
150         ValueResolver fileLengthValueResolver = new ValueResolver(){
151             public Object JavaDoc getValue(){
152                 int newLength = txtOper.getDocument().getLength();
153                 log("newLength:"+newLength);
154                 return (newLength == oldLength) ? Boolean.TRUE : Boolean.FALSE;
155             }
156         };
157         
158         return fileLengthValueResolver;
159     }
160     
161     
162 }
163
Popular Tags