KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > core > syntax > IndentationTest


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
21 package org.netbeans.test.web.core.syntax;
22
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import javax.swing.JEditorPane JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import junit.framework.Test;
31 import org.netbeans.jellytools.actions.SaveAllAction;
32 import org.netbeans.jemmy.JemmyProperties;
33 import org.netbeans.junit.AssertionFailedErrorException;
34 import org.netbeans.editor.BaseDocument;
35 import org.netbeans.jellytools.EditorOperator;
36 import org.netbeans.test.web.FileObjectFilter;
37 import org.netbeans.test.web.RecurrentSuiteFactory;
38 import org.netbeans.test.web.Waiter;
39 import org.openide.cookies.EditorCookie;
40 import org.openide.cookies.EditorCookie.Observable;
41 import org.openide.filesystems.FileObject;
42 import org.openide.loaders.DataObject;
43
44 /**
45  *
46  * @author jindra
47  */

48 public class IndentationTest extends CompletionTest{
49     private static final String JavaDoc startStep = "<!--CC";
50     private static final String JavaDoc endStep="-->";
51     
52     /** Creates a new instance of IndentationTest */
53     public IndentationTest(String JavaDoc name, FileObject testFileObj) {
54         super(name, testFileObj);
55     }
56     
57     public static Test suite() {
58         // find folder with test projects and define file objects filter
59
File JavaDoc datadir = new AutoCompletionTest(null, null).getDataDir();
60         File JavaDoc projectsDir = new File JavaDoc(datadir, "IndentationTestProjects");
61         FileObjectFilter filter = new FileObjectFilter() {
62             public boolean accept(FileObject fObject) {
63                 String JavaDoc ext = fObject.getExt();
64                 String JavaDoc name = fObject.getName();
65                 return (name.startsWith("test") || name.startsWith("Test"))
66                         && (xmlExts.contains(ext) || jspExts.contains(ext) || jspExts.equals("java"));
67             }
68         };
69         return RecurrentSuiteFactory.createSuite(IndentationTest.class, projectsDir, filter);
70     }
71     
72     
73     public void runTest() throws Exception JavaDoc {
74         try {
75             // get token chain
76
DataObject dataObj = DataObject.find(testFileObj);
77             final EditorCookie.Observable ed = (Observable) dataObj.getCookie(Observable.class);
78             
79             // prepare synchronization and register listener
80
final Waiter waiter = new Waiter();
81             final PropertyChangeListener JavaDoc pcl = new PropertyChangeListener JavaDoc() {
82                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
83                     if (evt.getPropertyName().equals(Observable.PROP_OPENED_PANES)) {
84                         waiter.notifyFinished();
85                     }
86                 }
87             };
88             ed.addPropertyChangeListener(pcl);
89             // open document
90
BaseDocument doc = (BaseDocument) ed.openDocument();
91             ed.open();
92             // wait for PROP_OPENED_PANES and remove listener
93
assertTrue("The editor pane was not opened in 10 secs.", waiter.waitFinished(10000));
94             ed.removePropertyChangeListener(pcl);
95             // wait 2s for editor initialization
96
Thread.currentThread().sleep(2000);
97             final LinkedList JavaDoc<JEditorPane JavaDoc> listPane = new LinkedList JavaDoc<JEditorPane JavaDoc>();
98             Runnable JavaDoc runnable = new Runnable JavaDoc(){
99                 public void run(){
100                     listPane.add(ed.getOpenedPanes()[0]);
101                 }
102             };
103             SwingUtilities.invokeAndWait(runnable);
104             JEditorPane JavaDoc editor = listPane.getFirst();
105             String JavaDoc text = doc.getText(0, doc.getLength());
106             int endPosition = 0;
107             int actualPosition = text.indexOf(startStep, 0);//position of first step
108
int currentDispatchingModel = JemmyProperties.getCurrentDispatchingModel();
109             JemmyProperties.setCurrentDispatchingModel(JemmyProperties.ROBOT_MODEL_MASK);
110             while (actualPosition != -1){//go through all cases
111
endPosition=text.indexOf(endStep, actualPosition);//get possition of comment end tag
112
// obsah = text.substring(actualPosition+startStep.length(), endPosition);//get offset number
113
// offset = Integer.valueOf(obsah.trim());
114
// if (offset >=0) editor.getCaret().setDot(endPosition+endStep.length()+offset);//set cursor possition
115
// else editor.getCaret().setDot(actualPosition+offset);
116
doc.remove(actualPosition, endPosition+endStep.length()-actualPosition);
117                 editor.getCaret().setDot(actualPosition);
118                 Thread.currentThread().sleep(500);
119                 //press Enter
120
// runInAWT(new Runnable() {
121
// public void run() {
122
EditorOperator eOperator = new EditorOperator(testFileObj.getNameExt());
123                 eOperator.pushKey(KeyEvent.VK_ENTER);
124                 // }
125
// });
126
Thread.currentThread().sleep(1500);
127                 doc.insertString(editor.getCaret().getDot(), "|", null);
128                 Thread.currentThread().sleep(1000);
129                 text = doc.getText(0, doc.getLength());
130                 actualPosition = text.indexOf(startStep, actualPosition);//new actualposition
131
}
132             JemmyProperties.setCurrentDispatchingModel(currentDispatchingModel);
133             ref(doc.getText(0, doc.getLength()));//the result into ref file
134
new SaveAllAction().performAPI();
135         } catch (Exception JavaDoc ex) {
136             throw new AssertionFailedErrorException(ex);
137         }
138         ending();
139     }
140     
141 }
142
Popular Tags