KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > qa > form > OpenForm


1 /*
2  * newSimpleNbJUnitTest.java
3  * NetBeans JUnit based test
4  *
5  * Created on June 22, 2005, 11:28 AM
6  */

7
8 package org.netbeans.qa.form;
9 import java.io.BufferedReader JavaDoc;
10 import java.io.BufferedWriter JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.nio.MappedByteBuffer JavaDoc;
19 import java.nio.channels.FileChannel JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.jellytools.JellyTestCase;
25 import org.netbeans.jellytools.actions.ActionNoBlock;
26 import org.netbeans.jellytools.modules.form.FormDesignerOperator;
27 import org.netbeans.jemmy.EventTool;
28 import org.netbeans.jemmy.operators.FrameOperator;
29 import org.netbeans.jemmy.operators.JFileChooserOperator;
30 import org.netbeans.junit.*;
31
32 /**
33  *
34  * @author Marek G.
35  */

36 public class OpenForm extends JellyTestCase {
37     
38     public OpenForm(java.lang.String JavaDoc testName) {
39         super(testName);
40     }
41     
42     public static void main(java.lang.String JavaDoc[] args) {
43         junit.textui.TestRunner.run(suite());
44     }
45     
46     public static Test suite() {
47         TestSuite suite = new NbTestSuite(OpenForm.class);
48         return suite;
49     }
50     
51     // Add test methods here, they have to start with 'test' name.
52
// for example:
53
// public void testHello() {}
54

55     private List JavaDoc getJavaFormList() {
56         String JavaDoc dataDir = getDataDir().getAbsolutePath();
57         String JavaDoc fileSeparator = System.getProperty("file.separator");
58         
59         FileReader JavaDoc fileReader = null;
60         BufferedReader JavaDoc bufferedReader = null;
61         
62         List JavaDoc list = new ArrayList JavaDoc();
63         
64         try {
65             //System.out.println("PATH : " + dataDir + fileSeparator + "FormList.dat");
66
fileReader = new FileReader JavaDoc(dataDir + fileSeparator + "FormList.dat");
67             bufferedReader = new BufferedReader JavaDoc(fileReader);
68             String JavaDoc line = null;
69             while ((line = bufferedReader.readLine()) != null){
70                 list.add(line);
71             }
72         } catch (FileNotFoundException JavaDoc ex) {
73             ex.printStackTrace();
74         } catch (IOException JavaDoc ex){
75             ex.printStackTrace();
76         } finally {
77             try {
78                 if (bufferedReader!= null) {
79                     bufferedReader.close();
80                 }
81             } catch (IOException JavaDoc ex) {
82                 ex.printStackTrace();
83             }
84         }
85         return list;
86     }
87     
88     public void testOpenForm() {
89         
90         System.out.println("XXXXXXXXXXXXX");
91         System.out.println("xtest.data = " + System.getProperty("xtest.data"));
92         System.out.println("getDataDir = " + getDataDir());
93         System.out.println("getWorkDirPath = " + getWorkDirPath());
94         System.out.println("xtest.module = " + System.getProperty("xtest.module"));
95         System.out.println("xtest.home = " + System.getProperty("xtest.home"));
96         System.out.println("xtest.workdir = " + System.getProperty("xtest.workdir"));
97         System.out.println("xtest.attribs = " + System.getProperty("xtest.attribs"));
98         
99         System.out.println("XXXXXXXXXXXXX");
100         String JavaDoc dataDir = getDataDir().getAbsolutePath();
101         String JavaDoc workdirpath = getWorkDirPath();
102         String JavaDoc formPath = null;
103         String JavaDoc fileSeparator = System.getProperty("file.separator");
104         String JavaDoc lineSeparator = System.getProperty("line.separator");
105         String JavaDoc xtest_sketchpad = System.getProperty("xtest.sketchpad");
106         
107         List JavaDoc list = getJavaFormList();
108         // String prePath = "/space/cvs-netbeans/form/src";
109
String JavaDoc prePath = workdirpath.substring(0,workdirpath.indexOf(fileSeparator + "testOpenForm"));
110         
111         FileReader JavaDoc fileReader = null;
112         FileWriter JavaDoc fileWriter = null;
113         BufferedReader JavaDoc bufferedReader = null;
114         BufferedWriter JavaDoc bufferedWriter = null;
115         
116         try {
117             fileReader = new FileReader JavaDoc(dataDir + fileSeparator + "OpenForm.template");
118             bufferedReader = new BufferedReader JavaDoc(fileReader);
119             fileWriter = new FileWriter JavaDoc(workdirpath + fileSeparator + "OpenForm.html");
120             bufferedWriter = new BufferedWriter JavaDoc(fileWriter);
121                         
122             String JavaDoc line = null;
123             while (!(line = bufferedReader.readLine()).equals("<PUT CONTENT HERE>")) {
124                 //System.out.println("LINE : " + line + " : ");
125
bufferedWriter.write(line + lineSeparator);
126             }
127             
128             int lastindex = 0;
129             String JavaDoc module = null;
130             String JavaDoc directory = null;
131             String JavaDoc filename = null;
132             String JavaDoc filenamenoext = null;
133             String JavaDoc fullPath = null;
134             ActionNoBlock actionNoBlock;
135             JFileChooserOperator jFileChooserOperator;
136             FormDesignerOperator formDesignerOperator = null;
137             FrameOperator frameOperator = null;
138             for (ListIterator JavaDoc listIterator = list.listIterator(); listIterator.hasNext(); ) {
139                 /** read items from list formPath: EventCustomEditor.form, FormCustomEditor.form,
140                  * FormCustomEditorAdvanced.form, MethodPicker.form, ParametersPicker.form, PropertyPicker.form, CustomCodeEditor.form, StringArrayCustomEditor.form, ConnectionPanel1.form, ConnectionPanel2.form, ConnectionPanel3.form]
141                  **/

142                 formPath = (String JavaDoc) listIterator.next();
143                 
144                 fullPath = new StringBuffer JavaDoc(prePath).append(fileSeparator).append(formPath).toString();
145                 System.out.println("fullpath: " + fullPath);
146                 lastindex = fullPath.lastIndexOf(fileSeparator);
147      
148                 // module = formPath.substring(0, formPath.indexOf(fileSeparator));
149
directory = dataDir + fileSeparator + "OpenForm"; //fullPath.substring(0,lastindex);
150
filename = fullPath.substring(lastindex + 1);
151                 filenamenoext = filename.substring(0, filename.lastIndexOf("."));
152                 actionNoBlock = new ActionNoBlock("File|Open File...", null);
153                 actionNoBlock.perform();
154                 jFileChooserOperator = new JFileChooserOperator();
155                 jFileChooserOperator.setCurrentDirectory(new File JavaDoc(directory));
156                 jFileChooserOperator.selectFile(filename);
157                 jFileChooserOperator.approve();
158                 formDesignerOperator = new FormDesignerOperator(filenamenoext);
159                 
160                 
161                 formDesignerOperator.btPreviewForm().push();
162                 frameOperator = new FrameOperator("Design Preview [" + filenamenoext +"]");
163                 
164                 
165                 // org.netbeans.jemmy.util.PNGEncoder.captureScreen(formDesignerOperator.fakePane().getSource(), workdirpath + fileSeparator + filenamenoext + ".png");
166
new EventTool().waitNoEvent(1000);
167                 System.out.println("xtest_sketchpad: " + xtest_sketchpad);
168                 copy(new File JavaDoc(dataDir + fileSeparator + "OpenForm"+ fileSeparator + filenamenoext + ".png"), new File JavaDoc(xtest_sketchpad + fileSeparator + filenamenoext + ".png"));
169                 
170                 org.netbeans.jemmy.util.PNGEncoder.captureScreen(frameOperator.getSource(), workdirpath + fileSeparator + filenamenoext + ".png");
171                 
172                 frameOperator.close();
173                 formDesignerOperator.close();
174                 
175                 bufferedWriter.write("<TABLE width=\"98%\" cellspacing=\"2\" cellpadding=\"5\" border=\"0\">");
176                 bufferedWriter.write("<TR bgcolor=\"#A6CAF0\" align=\"center\">");
177                 bufferedWriter.write("<TD ALIGN=\"LEFT\" colspan=\"2\"> " + "<B>" + filenamenoext + "</B></TD>");
178                 bufferedWriter.write("</TR>");
179                 bufferedWriter.write("<TR bgcolor=\"#A6CAF0\" align=\"center\">");
180                 bufferedWriter.write("<TD ALIGN=\"LEFT\" >" + "<B>" + " Current testing " + "</B></TD>");
181                 bufferedWriter.write("<TD ALIGN=\"LEFT\" >" + "<B>" + " <FONT color=\"#EEEE0E\">Golden file</FONT> " + "</B></TD>");
182                 bufferedWriter.write("</TR>");
183                 bufferedWriter.write("<TR>");
184                 bufferedWriter.write("<TD ALIGN=\"CENTER\"><IMG BORDER=\"2\" TITLE=\"" + filenamenoext + "\" SRC=\"" + filenamenoext + ".png" + "\"></IMG></TD>");
185                 bufferedWriter.write("<TD bgcolor=\"#EEEE0E\" ALIGN=\"CENTER\"><IMG BORDER=\"2\" TITLE=\"" + filenamenoext + "\" SRC=\"" + "../../sketch-pad" + fileSeparator + filenamenoext + ".png" + "\"></IMG></TD>");
186                 bufferedWriter.write("</TR>");
187                 bufferedWriter.write("</TABLE>");
188                 bufferedWriter.write("<HR>");
189                 
190             }
191             
192             while ((line = bufferedReader.readLine()) != null){
193                 // System.out.println("XLINE : " + line + " : ");
194
bufferedWriter.write(line + lineSeparator);
195             }
196             
197         } catch (FileNotFoundException JavaDoc ex) {
198             ex.printStackTrace();
199         } catch (IOException JavaDoc ex){
200             ex.printStackTrace();
201         } finally {
202             try {
203                 if (bufferedReader!= null) {
204                     bufferedReader.close();
205                 }
206                 if (bufferedWriter!= null) {
207                     bufferedWriter.close();
208                 }
209             } catch (IOException JavaDoc ex) {
210                 ex.printStackTrace();
211             }
212         }
213         
214         assertTrue("Look at the work directory for particular form's screenshots", true);
215     }
216     
217     private void copy(File JavaDoc source, File JavaDoc destination) throws IOException JavaDoc {
218         FileChannel JavaDoc input = null, output = null;
219         try {
220             input = new FileInputStream JavaDoc(source).getChannel();
221             output = new FileOutputStream JavaDoc(destination).getChannel();
222             
223             long size = input.size();
224             MappedByteBuffer JavaDoc buffer = input.map(FileChannel.MapMode.READ_ONLY, 0, size);
225             
226             output.write(buffer);
227             
228         } finally {
229             if (input != null)
230                 input.close();
231             if (output != null)
232                 output.close();
233         }
234     }
235     
236 }
237
Popular Tags