KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > errorannotations > ErrorAnnotations


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 org.netbeans.test.java.gui.errorannotations;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.jellytools.EditorOperator;
28 import org.netbeans.jellytools.EditorWindowOperator;
29 import org.netbeans.jellytools.JellyTestCase;
30 import org.netbeans.jellytools.ProjectsTabOperator;
31 import org.netbeans.jellytools.actions.ActionNoBlock;
32 import org.netbeans.jellytools.actions.OpenAction;
33 import org.netbeans.jellytools.actions.SaveAction;
34 import org.netbeans.jellytools.nodes.Node;
35 import org.netbeans.jemmy.JemmyProperties;
36 import org.netbeans.jemmy.TestOut;
37 import org.netbeans.junit.NbTestSuite;
38 import org.netbeans.test.java.Utilities;
39
40
41
42 /**
43  * Tests Error annotations.
44  * @author Roman Strobl
45  */

46 public class ErrorAnnotations extends JellyTestCase {
47     
48     // default timeout for actions in miliseconds
49
private static final int ACTION_TIMEOUT = 6000;
50     
51     // name of sample project
52
private static final String JavaDoc TEST_PROJECT_NAME = "default";
53     
54     // path to sample files
55
private static final String JavaDoc TEST_PACKAGE_PATH =
56             "org.netbeans.test.java.gui.errorannotations";
57     
58     // name of sample package
59
private static final String JavaDoc TEST_PACKAGE_NAME = TEST_PACKAGE_PATH+".test";
60     
61     // name of sample class
62
private static final String JavaDoc TEST_CLASS_NAME = "TestClass";
63     
64     /**
65      * error log
66      */

67     protected static PrintStream JavaDoc err;
68     
69     /**
70      * standard log
71      */

72     protected static PrintStream JavaDoc log;
73     
74     // workdir, default /tmp, changed to NBJUnit workdir during test
75
private String JavaDoc workDir = "/tmp";
76     
77     // actual directory with project
78
private static String JavaDoc projectDir;
79     
80     /**
81      * Needs to be defined because of JUnit
82      * @param name test name
83      */

84     public ErrorAnnotations(String JavaDoc name) {
85         super(name);
86     }
87     
88     /**
89      * Adds tests into the test suite.
90      * @return suite
91      */

92     public static NbTestSuite suite() {
93         NbTestSuite suite = new NbTestSuite();
94         // prepare testing project and package - not a core test but needed
95
suite.addTest(new ErrorAnnotations("testAnnotationsSimple"));
96         suite.addTest(new ErrorAnnotations("testUndo"));
97         suite.addTest(new ErrorAnnotations("testAnnotationsSimple2"));
98         suite.addTest(new ErrorAnnotations("testAnnotationsSimple3"));
99         suite.addTest(new ErrorAnnotations("testChangeCloseDiscart"));
100         
101         return suite;
102     }
103     
104     /**
105      * Main method for standalone execution.
106      * @param args the command line arguments
107      */

108     public static void main(java.lang.String JavaDoc[] args) {
109         TestRunner.run(suite());
110     }
111     
112     /**
113      * Sets up logging facilities.
114      */

115     public void setUp() {
116         System.out.println("######## "+getName()+" #######");
117         err = getLog();
118         log = getRef();
119         JemmyProperties.getProperties().setOutput(new TestOut(null,
120                 new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, false), null));
121         try {
122             File JavaDoc wd = getWorkDir();
123             workDir = wd.toString();
124         } catch (IOException JavaDoc e) { }
125     }
126     
127     /**
128      * Simple annotations tests - tries a simple error.
129      */

130     public void testAnnotationsSimple() {
131         Node pn = new ProjectsTabOperator().getProjectRootNode(
132                 TEST_PROJECT_NAME);
133         pn.select();
134         
135         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
136                 "org.netbeans.modules.java.j2seproject.Bundle",
137                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
138                 +TEST_CLASS_NAME);
139         
140         n.select();
141         new OpenAction().perform();
142         
143         // test a simple error - a space in public keyword
144
EditorWindowOperator ewo = new EditorWindowOperator();
145         EditorOperator editor = ewo.getEditor(TEST_CLASS_NAME);
146         editor.insert(" ", 11, 3);
147         
148         Utilities.takeANap(ACTION_TIMEOUT);
149         
150         log(editor.getText());
151         Object JavaDoc[] annots = editor.getAnnotations();
152         assertNotNull(annots);
153         assertEquals(1, annots.length);
154         assertEquals("org-netbeans-spi-editor-hints-parser_annotation_err",
155                 EditorOperator.getAnnotationType(annots[0]));
156         assertEquals("class or interface expected",
157                 EditorOperator.getAnnotationShortDescription(annots[0]));
158     }
159     
160     /**
161      * Tests undo after simple annotations test.
162      */

163     public void testUndo() {
164         // undo
165
new ActionNoBlock("Edit|Undo", null).perform();
166         
167         Utilities.takeANap(ACTION_TIMEOUT);
168         
169         EditorWindowOperator ewo = new EditorWindowOperator();
170         EditorOperator editor = ewo.getEditor(TEST_CLASS_NAME);
171         log(editor.getText());
172         Object JavaDoc[] annots = editor.getAnnotations();
173         
174         // there should be no annotations
175
assertEquals(annots.length, 0);
176     }
177     
178     /**
179      * Simple annotations tests - tries a simple error.
180      */

181     public void testAnnotationsSimple2() {
182         Node pn = new ProjectsTabOperator().getProjectRootNode(
183                 TEST_PROJECT_NAME);
184         pn.select();
185         
186         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
187                 "org.netbeans.modules.java.j2seproject.Bundle",
188                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
189                 +TEST_CLASS_NAME);
190         
191         n.select();
192         new OpenAction().perform();
193         
194         // change class to klasa
195
EditorWindowOperator ewo = new EditorWindowOperator();
196         EditorOperator editor = ewo.getEditor(TEST_CLASS_NAME);
197         editor.replace("class", "klasa");
198         
199         Utilities.takeANap(ACTION_TIMEOUT);
200         log(editor.getText());
201         // check error annotations
202
Object JavaDoc[] annots = editor.getAnnotations();
203         assertNotNull("There are not any annotations.", annots);
204         assertEquals("There are not one annotation", 2, annots.length);
205         assertEquals("Wrong annotation type ", "org-netbeans-spi-editor-hints-parser_annotation_err",
206                 EditorOperator.getAnnotationType(annots[0]));
207         assertEquals("Wrong annotation short description","class or interface expected",
208                 EditorOperator.getAnnotationShortDescription(annots[0]));
209         assertEquals("Wrong annotation type ", "org-netbeans-spi-editor-hints-parser_annotation_err",
210                 EditorOperator.getAnnotationType(annots[1]));
211         assertEquals("Wrong annotation short description","class or interface expected",
212                 EditorOperator.getAnnotationShortDescription(annots[1]));
213         new ActionNoBlock("Edit|Undo", null).perform();
214         Utilities.takeANap(ACTION_TIMEOUT);
215         ewo.closeDiscard();
216     }
217     
218     /**
219      * Simple annotations tests - tries a simple error.
220      */

221     public void testAnnotationsSimple3() {
222         Node pn = new ProjectsTabOperator().getProjectRootNode(
223                 TEST_PROJECT_NAME);
224         pn.select();
225         
226         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
227                 "org.netbeans.modules.java.j2seproject.Bundle",
228                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
229                 +TEST_CLASS_NAME);
230         
231         n.select();
232         new OpenAction().perform();
233         
234         // add xxx string to ctor
235
EditorWindowOperator ewo = new EditorWindowOperator();
236         EditorOperator editor = ewo.getEditor(TEST_CLASS_NAME);
237         editor.replace(TEST_CLASS_NAME, TEST_CLASS_NAME+"xxx", 3);
238         
239         Utilities.takeANap(ACTION_TIMEOUT);
240         log(editor.getText());
241         // check error annotations
242
Object JavaDoc[] annots = editor.getAnnotations();
243         assertNotNull("There are not any annotations.", annots);
244         assertEquals("There are more than one annotation: "+String.valueOf(annots.length), 1, annots.length);
245         assertEquals("Wrong annotation type: "+EditorOperator.getAnnotationType(annots[0]),
246                 "org-netbeans-spi-editor-hints-parser_annotation_err",
247                 EditorOperator.getAnnotationType(annots[0]));
248         assertEquals("Wrong annotation short description: "+EditorOperator.getAnnotationShortDescription(annots[0]),
249                 "invalid method declaration; return type required",
250                 EditorOperator.getAnnotationShortDescription(annots[0]));
251         System.out.println(EditorOperator.getAnnotationType(annots[0]));
252         System.out.println(EditorOperator.getAnnotationShortDescription(annots[0]));
253         new ActionNoBlock("Edit|Undo", null).perform();
254         Utilities.takeANap(ACTION_TIMEOUT);
255         ewo.closeDiscard();
256     }
257     
258     public void testChangeCloseDiscart() {
259         Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
260         pn.select();
261         
262         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
263                 "org.netbeans.modules.java.j2seproject.Bundle",
264                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
265                 +TEST_CLASS_NAME);
266         
267         n.select();
268         new OpenAction().perform();
269         
270         
271         EditorWindowOperator ewo = new EditorWindowOperator();
272         EditorOperator editor = ewo.getEditor(TEST_CLASS_NAME);
273         String JavaDoc context = "" +
274                 "/*\n" +
275                 " * TestClass.java \n" +
276                 " *\n" +
277                 " */\n" +
278                 "\n" +
279                 "package org.netbeans.test.java.gui.errorannotations.test;\n" +
280                 "\n" +
281                 "/**\n" +
282                 " *\n" +
283                 " */\n" +
284                 "public class TestClass {\n" +
285                 " \n" +
286                 " /** Creates a new instance of TestClass */\n" +
287                 " public TestClass() {\n" +
288                 " }\n" +
289                 " \n" +
290                 "}\n";
291                 
292         editor.delete(0,editor.getText().length());
293         editor.insert(context); //setting right content of file to avoid dependency between tests
294
new SaveAction().perform();
295                 
296         editor.insert(" ", 11,3);
297         Utilities.takeANap(ACTION_TIMEOUT);
298         log(editor.getText());
299         Object JavaDoc[] annots = editor.getAnnotations();
300         assertNotNull("There are not any annotations.", annots);
301         assertEquals("There are annotations: "+String.valueOf(annots.length), 1, annots.length);
302         ewo.closeDiscard();
303         n.select();
304         new OpenAction().perform();
305         editor = ewo.getEditor(TEST_CLASS_NAME);
306         annots = editor.getAnnotations();
307         assertEquals(0,annots.length); //there should be no annotations
308
ewo.closeDiscard();
309     }
310     
311     
312     
313 }
314
Popular Tags