KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > JavaBracketCompletionUnitTest


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.modules.editor.java;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Caret JavaDoc;
24 import org.netbeans.editor.TokenID;
25 import org.netbeans.editor.ext.ExtSyntaxSupport;
26 import org.netbeans.editor.ext.java.JavaTokenContext;
27 import org.netbeans.modules.editor.java.BracketCompletion;
28
29
30 /**
31  * Test java bracket completion.
32  *
33  * @autor Miloslav Metelka
34  */

35 public class JavaBracketCompletionUnitTest extends JavaBaseDocumentUnitTestCase {
36
37     public JavaBracketCompletionUnitTest(String JavaDoc testMethodName) {
38         super(testMethodName);
39     }
40     
41     // ------- Tests for completion of right parenthesis ')' -------------
42

43     public void testRightParenSimpleMethodCall() {
44         setLoadDocumentText("m()|)");
45         assertTrue(isSkipRightParen());
46     }
47     
48     public void testRightParenSwingInvokeLaterRunnable() {
49         setLoadDocumentText("SwingUtilities.invokeLater(new Runnable()|))");
50         assertTrue(isSkipRightParen());
51     }
52
53     public void testRightParenSwingInvokeLaterRunnableRun() {
54         setLoadDocumentText(
55             "SwingUtilities.invokeLater(new Runnable() {\n"
56           + " public void run()|)\n"
57           + "})"
58             
59         );
60         assertTrue(isSkipRightParen());
61     }
62     
63     public void testRightParenIfMethodCall() {
64         setLoadDocumentText(
65             " if (a()|) + 5 > 6) {\n"
66           + " }"
67         );
68         assertTrue(isSkipRightParen());
69     }
70
71     public void testRightParenNoSkipNonBracketChar() {
72         setLoadDocumentText("m()| ");
73         assertFalse(isSkipRightParen());
74     }
75
76     public void testRightParenNoSkipDocEnd() {
77         setLoadDocumentText("m()|");
78         assertFalse(isSkipRightParen());
79     }
80
81     
82     // ------- Tests for completion of right brace '}' -------------
83

84     public void testAddRightBraceIfLeftBrace() {
85         setLoadDocumentText("if (true) {|");
86         assertTrue(isAddRightBrace());
87     }
88
89     public void testAddRightBraceIfLeftBraceWhiteSpace() {
90         setLoadDocumentText("if (true) { \t|\n");
91         assertTrue(isAddRightBrace());
92     }
93     
94     public void testAddRightBraceIfLeftBraceLineComment() {
95         setLoadDocumentText("if (true) { // line-comment|\n");
96         assertTrue(isAddRightBrace());
97     }
98
99     public void testAddRightBraceIfLeftBraceBlockComment() {
100         setLoadDocumentText("if (true) { /* block-comment */|\n");
101         assertTrue(isAddRightBrace());
102     }
103
104     public void testAddRightBraceIfLeftBraceAlreadyPresent() {
105         setLoadDocumentText(
106             "if (true) {|\n"
107           + "}"
108         );
109         assertFalse(isAddRightBrace());
110     }
111
112     public void testAddRightBraceCaretInComment() {
113         setLoadDocumentText(
114             "if (true) { /* in-block-comment |\n"
115         );
116         assertFalse(isAddRightBrace());
117     }
118     
119     public void testSimpleAdditionOfOpeningParenthesisAfterWhile () throws Exception JavaDoc {
120         setLoadDocumentText (
121             "while |"
122         );
123         typeChar('(');
124         assertDocumentTextAndCaret ("Even a closing ')' should be added",
125             "while (|)"
126         );
127     }
128
129     
130     // ------- Tests for completion of quote (") -------------
131
public void testSimpleQuoteInEmptyDoc () throws Exception JavaDoc {
132         setLoadDocumentText (
133             "|"
134         );
135         typeQuoteChar('"');
136         assertDocumentTextAndCaret ("Simple Quote In Empty Doc",
137             "\"|\""
138         );
139     }
140
141     public void testSimpleQuoteAtBeginingOfDoc () throws Exception JavaDoc {
142         setLoadDocumentText (
143             "| "
144         );
145         typeQuoteChar('"');
146         assertDocumentTextAndCaret ("Simple Quote At Begining Of Doc",
147             "\"|\" "
148         );
149     }
150
151     public void testSimpleQuoteAtEndOfDoc () throws Exception JavaDoc {
152         setLoadDocumentText (
153             " |"
154         );
155         typeQuoteChar('"');
156         assertDocumentTextAndCaret ("Simple Quote At End Of Doc",
157             " \"|\""
158         );
159     }
160     
161     public void testSimpleQuoteInWhiteSpaceArea () throws Exception JavaDoc {
162         setLoadDocumentText (
163             " | "
164         );
165         typeQuoteChar('"');
166         assertDocumentTextAndCaret ("Simple Quote In White Space Area",
167             " \"|\" "
168         );
169     }
170     
171     public void testQuoteAtEOL () throws Exception JavaDoc {
172         setLoadDocumentText (
173             " |\n"
174         );
175         typeQuoteChar('"');
176         assertDocumentTextAndCaret ("Quote At EOL",
177             " \"|\"\n"
178         );
179     }
180     
181     public void testQuoteWithUnterminatedStringLiteral () throws Exception JavaDoc {
182         setLoadDocumentText (
183             " \"unterminated string| \n"
184         );
185         typeQuoteChar('"');
186         assertDocumentTextAndCaret ("Quote With Unterminated String Literal",
187             " \"unterminated string\"| \n"
188         );
189     }
190     
191     public void testQuoteAtEOLWithUnterminatedStringLiteral () throws Exception JavaDoc {
192         setLoadDocumentText (
193             " \"unterminated string |\n"
194         );
195         typeQuoteChar('"');
196         assertDocumentTextAndCaret ("Quote At EOL With Unterminated String Literal",
197             " \"unterminated string \"|\n"
198         );
199     }
200
201     public void testQuoteInsideStringLiteral () throws Exception JavaDoc {
202         setLoadDocumentText (
203             " \"stri|ng literal\" "
204         );
205         typeQuoteChar('"');
206         assertDocumentTextAndCaret ("Quote Inside String Literal",
207             " \"stri\"|ng literal\" "
208         );
209     }
210
211     public void testQuoteInsideEmptyParentheses () throws Exception JavaDoc {
212         setLoadDocumentText (
213             " System.out.println(|) "
214         );
215         typeQuoteChar('"');
216         assertDocumentTextAndCaret ("Quote Inside Empty Parentheses",
217             " System.out.println(\"|\") "
218         );
219     }
220
221     public void testQuoteInsideNonEmptyParentheses () throws Exception JavaDoc {
222         setLoadDocumentText (
223             " System.out.println(|some text) "
224         );
225         typeQuoteChar('"');
226         assertDocumentTextAndCaret ("Quote Inside Non Empty Parentheses",
227             " System.out.println(\"|some text) "
228         );
229     }
230     
231     public void testQuoteInsideNonEmptyParenthesesBeforeClosingParentheses () throws Exception JavaDoc {
232         setLoadDocumentText (
233             " System.out.println(i+|) "
234         );
235         typeQuoteChar('"');
236         assertDocumentTextAndCaret ("Quote Inside Non Empty Parentheses Before Closing Parentheses",
237             " System.out.println(i+\"|\") "
238         );
239     }
240     
241     public void testQuoteInsideNonEmptyParenthesesBeforeClosingParenthesesAndUnterminatedStringLiteral () throws Exception JavaDoc {
242         setLoadDocumentText (
243             " System.out.println(\"unterminated string literal |); "
244         );
245         typeQuoteChar('"');
246         assertDocumentTextAndCaret ("Quote Inside Non Empty Parentheses Before Closing Parentheses And Unterminated String Literal",
247             " System.out.println(\"unterminated string literal \"|); "
248         );
249     }
250
251     public void testQuoteBeforePlus () throws Exception JavaDoc {
252         setLoadDocumentText (
253             " System.out.println(|+\"string literal\"); "
254         );
255         typeQuoteChar('"');
256         assertDocumentTextAndCaret ("Quote Before Plus",
257             " System.out.println(\"|\"+\"string literal\"); "
258         );
259     }
260
261     public void testQuoteBeforeComma () throws Exception JavaDoc {
262         setLoadDocumentText (
263             "String s[] = new String[]{|,\"two\"};"
264         );
265         typeQuoteChar('"');
266         assertDocumentTextAndCaret ("Quote Before Comma",
267             "String s[] = new String[]{\"|\",\"two\"};"
268         );
269     }
270
271     public void testQuoteBeforeBrace () throws Exception JavaDoc {
272         setLoadDocumentText (
273             "String s[] = new String[]{\"one\",|};"
274         );
275         typeQuoteChar('"');
276         assertDocumentTextAndCaret ("Quote Before Brace",
277             "String s[] = new String[]{\"one\",\"|\"};"
278         );
279     }
280
281     public void testQuoteBeforeSemicolon() throws Exception JavaDoc {
282         setLoadDocumentText (
283             "String s = \"\" + |;"
284         );
285         typeQuoteChar('"');
286         assertDocumentTextAndCaret ("Quote Before Semicolon",
287             "String s = \"\" + \"|\";"
288         );
289     }
290
291     public void testQuoteBeforeSemicolonWithWhitespace() throws Exception JavaDoc {
292         setLoadDocumentText (
293             "String s = \"\" +| ;"
294         );
295         typeQuoteChar('"');
296         assertDocumentTextAndCaret ("Quote Before Semicolon With Whitespace",
297             "String s = \"\" +\"|\" ;"
298         );
299     }
300
301     public void testQuoteAfterEscapeSequence() throws Exception JavaDoc {
302         setLoadDocumentText (
303             "\\|"
304         );
305         typeQuoteChar('"');
306         assertDocumentTextAndCaret ("Quote Before Semicolon With Whitespace",
307             "\\\"|"
308         );
309     }
310     
311     /** issue #69524 */
312     public void testQuoteEaten() throws Exception JavaDoc {
313         setLoadDocumentText (
314             "|"
315         );
316         typeQuoteChar('"');
317         typeQuoteChar('"');
318         assertDocumentTextAndCaret ("Quote Eaten",
319             "\"\"|"
320         );
321     }
322
323     /** issue #69935 */
324     public void testQuoteInsideComments() throws Exception JavaDoc {
325         setLoadDocumentText (
326             "/** |\n */"
327         );
328         typeQuoteChar('"');
329         assertDocumentTextAndCaret ("Quote Inside Comments",
330             "/** \"|\n */"
331         );
332     }
333     
334     /** issue #71880 */
335     public void testQuoteAtTheEndOfLineCommentLine() throws Exception JavaDoc {
336         setLoadDocumentText (
337             "// test line comment |\n"
338         );
339         typeQuoteChar('"');
340         assertDocumentTextAndCaret ("Quote At The End Of Line Comment Line",
341             "// test line comment \"|\n"
342         );
343     }
344     
345     
346     // ------- Tests for completion of single quote (') -------------
347

348     public void testSingleQuoteInEmptyDoc () throws Exception JavaDoc {
349         setLoadDocumentText (
350             "|"
351         );
352         typeQuoteChar('\'');
353         assertDocumentTextAndCaret ("Single Quote In Empty Doc",
354             "'|'"
355         );
356     }
357
358     public void testSingleQuoteAtBeginingOfDoc () throws Exception JavaDoc {
359         setLoadDocumentText (
360             "| "
361         );
362         typeQuoteChar('\'');
363         assertDocumentTextAndCaret ("Single Quote At Begining Of Doc",
364             "'|' "
365         );
366     }
367
368     public void testSingleQuoteAtEndOfDoc () throws Exception JavaDoc {
369         setLoadDocumentText (
370             " |"
371         );
372         typeQuoteChar('\'');
373         assertDocumentTextAndCaret ("Single Quote At End Of Doc",
374             " '|'"
375         );
376     }
377     
378     public void testSingleQuoteInWhiteSpaceArea () throws Exception JavaDoc {
379         setLoadDocumentText (
380             " | "
381         );
382         typeQuoteChar('\'');
383         assertDocumentTextAndCaret ("Single Quote In White Space Area",
384             " '|' "
385         );
386     }
387     
388     public void testSingleQuoteAtEOL () throws Exception JavaDoc {
389         setLoadDocumentText (
390             " |\n"
391         );
392         typeQuoteChar('\'');
393         assertDocumentTextAndCaret ("Single Quote At EOL",
394             " '|'\n"
395         );
396     }
397     
398     public void testSingleQuoteWithUnterminatedCharLiteral () throws Exception JavaDoc {
399         setLoadDocumentText (
400             " '| \n"
401         );
402         typeQuoteChar('\'');
403         assertDocumentTextAndCaret ("Single Quote With Unterminated Char Literal",
404             " ''| \n"
405         );
406     }
407     
408     public void testSingleQuoteAtEOLWithUnterminatedCharLiteral () throws Exception JavaDoc {
409         setLoadDocumentText (
410             " ' |\n"
411         );
412         typeQuoteChar('\'');
413         assertDocumentTextAndCaret ("Single Quote At EOL With Unterminated Char Literal",
414             " ' '|\n"
415         );
416     }
417
418     public void testSingleQuoteInsideCharLiteral () throws Exception JavaDoc {
419         setLoadDocumentText (
420             " '| ' "
421         );
422         typeQuoteChar('\'');
423         assertDocumentTextAndCaret ("Single Quote Inside Char Literal",
424             " ''| ' "
425         );
426     }
427
428     public void testSingleQuoteInsideEmptyParentheses () throws Exception JavaDoc {
429         setLoadDocumentText (
430             " System.out.println(|) "
431         );
432         typeQuoteChar('\'');
433         assertDocumentTextAndCaret ("Single Quote Inside Empty Parentheses",
434             " System.out.println('|') "
435         );
436     }
437
438     public void testSingleQuoteInsideNonEmptyParentheses () throws Exception JavaDoc {
439         setLoadDocumentText (
440             " System.out.println(|some text) "
441         );
442         typeQuoteChar('\'');
443         assertDocumentTextAndCaret ("Single Quote Inside Non Empty Parentheses",
444             " System.out.println('|some text) "
445         );
446     }
447     
448     public void testSingleQuoteInsideNonEmptyParenthesesBeforeClosingParentheses () throws Exception JavaDoc {
449         setLoadDocumentText (
450             " System.out.println(i+|) "
451         );
452         typeQuoteChar('\'');
453         assertDocumentTextAndCaret ("Single Quote Inside Non Empty Parentheses Before Closing Parentheses",
454             " System.out.println(i+'|') "
455         );
456     }
457     
458     public void testSingleQuoteInsideNonEmptyParenthesesBeforeClosingParenthesesAndUnterminatedCharLiteral () throws Exception JavaDoc {
459         setLoadDocumentText (
460             " System.out.println(' |); "
461         );
462         typeQuoteChar('\'');
463         assertDocumentTextAndCaret ("Single Quote Inside Non Empty Parentheses Before Closing Parentheses And Unterminated Char Literal",
464             " System.out.println(' '|); "
465         );
466     }
467
468     public void testSingleQuoteBeforePlus () throws Exception JavaDoc {
469         setLoadDocumentText (
470             " System.out.println(|+\"string literal\"); "
471         );
472         typeQuoteChar('\'');
473         assertDocumentTextAndCaret ("Single Quote Before Plus",
474             " System.out.println('|'+\"string literal\"); "
475         );
476     }
477
478     public void testSingleQuoteBeforeComma () throws Exception JavaDoc {
479         setLoadDocumentText (
480             "String s[] = new String[]{|,\"two\"};"
481         );
482         typeQuoteChar('\'');
483         assertDocumentTextAndCaret ("Single Quote Before Comma",
484             "String s[] = new String[]{'|',\"two\"};"
485         );
486     }
487
488     public void testSingleQuoteBeforeBrace () throws Exception JavaDoc {
489         setLoadDocumentText (
490             "String s[] = new String[]{\"one\",|};"
491         );
492         typeQuoteChar('\'');
493         assertDocumentTextAndCaret ("Single Quote Before Brace",
494             "String s[] = new String[]{\"one\",'|'};"
495         );
496     }
497
498     public void testSingleQuoteBeforeSemicolon() throws Exception JavaDoc {
499         setLoadDocumentText (
500             "String s = \"\" + |;"
501         );
502         typeQuoteChar('\'');
503         assertDocumentTextAndCaret ("Single Quote Before Semicolon",
504             "String s = \"\" + '|';"
505         );
506     }
507
508     public void testsingleQuoteBeforeSemicolonWithWhitespace() throws Exception JavaDoc {
509         setLoadDocumentText (
510             "String s = \"\" +| ;"
511         );
512         typeQuoteChar('\'');
513         assertDocumentTextAndCaret ("Single Quote Before Semicolon With Whitespace",
514             "String s = \"\" +'|' ;"
515         );
516     }
517
518     public void testSingleQuoteAfterEscapeSequence() throws Exception JavaDoc {
519         setLoadDocumentText (
520             "\\|"
521         );
522         typeQuoteChar('\'');
523         assertDocumentTextAndCaret ("Single Quote Before Semicolon With Whitespace",
524             "\\'|"
525         );
526     }
527     
528     /** issue #69524 */
529     public void testSingleQuoteEaten() throws Exception JavaDoc {
530         setLoadDocumentText (
531             "|"
532         );
533         typeQuoteChar('\'');
534         typeQuoteChar('\'');
535         assertDocumentTextAndCaret ("Single Quote Eaten",
536             "''|"
537         );
538     }
539     
540     /** issue #69935 */
541     public void testSingleQuoteInsideComments() throws Exception JavaDoc {
542         setLoadDocumentText (
543             "/* |\n */"
544         );
545         typeQuoteChar('\'');
546         assertDocumentTextAndCaret ("Single Quote Inside Comments",
547             "/* \'|\n */"
548         );
549     }
550
551     /** issue #71880 */
552     public void testSingleQuoteAtTheEndOfLineCommentLine() throws Exception JavaDoc {
553         setLoadDocumentText (
554             "// test line comment |\n"
555         );
556         typeQuoteChar('\'');
557         assertDocumentTextAndCaret ("Single Quote At The End Of Line Comment Line",
558             "// test line comment \'|\n"
559         );
560     }
561     
562     
563     // ------- Private methods -------------
564

565     private void typeChar(char ch) throws Exception JavaDoc {
566         int pos = getCaretOffset();
567         getDocument ().insertString(pos, String.valueOf(ch), null);
568         BracketCompletion.charInserted(getDocument(), pos, getCaret(), ch);
569     }
570     
571     private void typeQuoteChar(char ch) throws Exception JavaDoc {
572         int pos = getCaretOffset();
573         Caret JavaDoc caret = getCaret();
574         boolean inserted = BracketCompletion.completeQuote(getDocument(), pos, caret, ch);
575         if (inserted){
576             caret.setDot(pos+1);
577         }else{
578             getDocument ().insertString(pos, String.valueOf(ch), null);
579         }
580     }
581     
582     private boolean isSkipRightParen() {
583         return isSkipRightBracketOrParen(true);
584     }
585     
586     private boolean isSkipRightBracket() {
587         return isSkipRightBracketOrParen(false);
588     }
589     
590     private boolean isSkipRightBracketOrParen(boolean parenthesis) {
591         TokenID bracketTokenId = parenthesis
592         ? JavaTokenContext.RPAREN
593         : JavaTokenContext.RBRACKET;
594         
595         try {
596             return BracketCompletion.isSkipClosingBracket(getDocument(),
597             getCaretOffset(), bracketTokenId);
598         } catch (BadLocationException JavaDoc e) {
599             e.printStackTrace(getLog());
600             fail();
601             return false; // should never be reached
602
}
603     }
604     
605     private boolean isAddRightBrace() {
606         try {
607             return BracketCompletion.isAddRightBrace(getDocument(),
608             getCaretOffset());
609         } catch (BadLocationException JavaDoc e) {
610             e.printStackTrace(getLog());
611             fail();
612             return false; // should never be reached
613
}
614     }
615
616 }
617
Popular Tags