KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > HintsControllerImplTest


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 package org.netbeans.modules.editor.hints;
20
21 import java.util.Arrays JavaDoc;
22 import javax.swing.text.DefaultStyledDocument JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import org.netbeans.editor.BaseDocument;
25 import org.netbeans.editor.BaseKit;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.modules.editor.NbEditorDocument;
28
29 /**
30  *
31  * @author Jan Lahoda
32  */

33 public class HintsControllerImplTest extends NbTestCase {
34     
35     public HintsControllerImplTest(String JavaDoc name) {
36         super(name);
37     }
38     
39     public void testComputeLineSpan() throws Exception JavaDoc {
40         doTestComputeLineSpan(new DocumentCreator() {
41             public Document JavaDoc createDocument() {
42                 return new NbEditorDocument(BaseKit.class);
43             }
44         });
45         doTestComputeLineSpan(new DocumentCreator() {
46             public Document JavaDoc createDocument() {
47                 return new DefaultStyledDocument JavaDoc();
48             }
49         });
50     }
51     
52     private void doTestComputeLineSpan(DocumentCreator creator) throws Exception JavaDoc {
53         Document JavaDoc bdoc = creator.createDocument();
54         
55         bdoc.insertString(0, " 1234 \n 567\n567 \n456", null);
56         
57         assertSpan(bdoc, 1, 2, 6);
58         assertSpan(bdoc, 2, 10, 13);
59         assertSpan(bdoc, 3, 14, 17);
60         assertSpan(bdoc, 4, 19, 22);
61         
62         bdoc = creator.createDocument();
63         
64         bdoc.insertString(0, "456", null);
65         
66         assertSpan(bdoc, 1, 0, 3);
67         
68         bdoc = creator.createDocument();
69         
70         bdoc.insertString(0, " ", null);
71         
72         assertSpan(bdoc, 1, 0, 0);
73     }
74     
75     private static interface DocumentCreator {
76         public Document JavaDoc createDocument();
77     }
78     
79     private void assertSpan(Document JavaDoc doc, int lineNumber, int... expectedSpan) throws Exception JavaDoc {
80         int[] returnedSpan = HintsControllerImpl.computeLineSpan(doc, lineNumber);
81         
82         assertTrue(Arrays.toString(returnedSpan), Arrays.equals(expectedSpan, returnedSpan));
83     }
84 }
85
Popular Tags