KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > run > HyperlinkTest


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.apache.tools.ant.module.run;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.cookies.SaveCookie;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.loaders.DataObject;
30 import org.openide.text.CloneableEditor;
31 import org.openide.windows.OutputListener;
32 import org.openide.windows.TopComponent;
33
34 /**
35  * Check that hyperlinks go to the right place.
36  * @author Jesse Glick
37  */

38 public class HyperlinkTest extends NbTestCase {
39
40     public HyperlinkTest(String JavaDoc n) {
41         super(n);
42     }
43
44     private FileObject f1;
45     private OutputListener h11;
46     private OutputListener h12;
47
48     @Override JavaDoc
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         clearWorkDir();
52         FileObject root = FileUtil.toFileObject(getWorkDir());
53         f1 = root.createData("f1");
54         PrintStream JavaDoc ps = new PrintStream JavaDoc(f1.getOutputStream());
55         ps.println("#comment 1");
56         ps.println("bug #1");
57         ps.println("#COMMENT 2");
58         ps.println("bug #2");
59         ps.println("#CoMmEnT 3");
60         ps.flush();
61         ps.close();
62         h11 = new Hyperlink(f1.getURL(), "f1 #1", 2, 5, -1, -1);
63         h12 = new Hyperlink(f1.getURL(), "f1 #2", 4, 5, -1, -1);
64         System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false");
65     }
66
67     @Override JavaDoc
68     protected void tearDown() throws Exception JavaDoc {
69         super.tearDown();
70         Hyperlink.detachAllAnnotations();
71     }
72
73     public void testMovingHyperlinkWithoutSave() throws Exception JavaDoc {
74         doTestMovingHyperlink(false);
75     }
76
77     public void testMovingHyperlinkWithSave() throws Exception JavaDoc { // #62623
78
doTestMovingHyperlink(true);
79     }
80
81     private void doTestMovingHyperlink(boolean save) throws Exception JavaDoc {
82         click(h11);
83         JEditorPane JavaDoc ep1 = ((CloneableEditor) TopComponent.getRegistry().getActivated()).getEditorPane();
84         assertEquals("#1\n", ep1.getDocument().getText(ep1.getCaretPosition(), 3));
85         ep1.getDocument().insertString(ep1.getCaretPosition() + 3, "fixstuff\n", null);
86         if (save) {
87             DataObject.find(f1).getCookie(SaveCookie.class).save();
88         }
89         click(h11);
90         assertEquals("#1\n", ep1.getDocument().getText(ep1.getCaretPosition(), 3));
91         ep1.getDocument().insertString(ep1.getCaretPosition() + 3, "fixstuff\n", null);
92         click(h12);
93         assertEquals("#2\n", ep1.getDocument().getText(ep1.getCaretPosition(), 3));
94         click(h11);
95         assertEquals("#1\n", ep1.getDocument().getText(ep1.getCaretPosition(), 3));
96     }
97
98     private void click(OutputListener hyperlink) throws Exception JavaDoc {
99         hyperlink.outputLineAction(null);
100         // Need to wait for CloneableEditorSupport.openAt.Selector.run to finish:
101
EventQueue.invokeAndWait(new Runnable JavaDoc() {public void run() {}});
102     }
103
104 }
105
Popular Tags