001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.tools.ant.module.run;
043:
044: import java.awt.EventQueue;
045: import java.io.PrintStream;
046: import javax.swing.JEditorPane;
047: import org.netbeans.junit.NbTestCase;
048: import org.openide.cookies.SaveCookie;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.loaders.DataObject;
052: import org.openide.text.CloneableEditor;
053: import org.openide.windows.OutputListener;
054: import org.openide.windows.TopComponent;
055:
056: /**
057: * Check that hyperlinks go to the right place.
058: * @author Jesse Glick
059: */
060: public class HyperlinkTest extends NbTestCase {
061:
062: public HyperlinkTest(String n) {
063: super (n);
064: }
065:
066: private FileObject f1;
067: private OutputListener h11;
068: private OutputListener h12;
069:
070: @Override
071: protected void setUp() throws Exception {
072: super .setUp();
073: clearWorkDir();
074: FileObject root = FileUtil.toFileObject(getWorkDir());
075: f1 = root.createData("f1");
076: PrintStream ps = new PrintStream(f1.getOutputStream());
077: ps.println("#comment 1");
078: ps.println("bug #1");
079: ps.println("#COMMENT 2");
080: ps.println("bug #2");
081: ps.println("#CoMmEnT 3");
082: ps.flush();
083: ps.close();
084: h11 = new Hyperlink(f1.getURL(), "f1 #1", 2, 5, -1, -1);
085: h12 = new Hyperlink(f1.getURL(), "f1 #2", 4, 5, -1, -1);
086: System.setProperty(
087: "org.openide.windows.DummyWindowManager.VISIBLE",
088: "false");
089: }
090:
091: @Override
092: protected void tearDown() throws Exception {
093: super .tearDown();
094: Hyperlink.hyperlinks.clear();
095: }
096:
097: public void testMovingHyperlinkWithoutSave() throws Exception {
098: doTestMovingHyperlink(false);
099: }
100:
101: public void testMovingHyperlinkWithSave() throws Exception { // #62623
102: doTestMovingHyperlink(true);
103: }
104:
105: private void doTestMovingHyperlink(boolean save) throws Exception {
106: click(h11);
107: JEditorPane ep1 = ((CloneableEditor) TopComponent.getRegistry()
108: .getActivated()).getEditorPane();
109: assertEquals("#1\n", ep1.getDocument().getText(
110: ep1.getCaretPosition(), 3));
111: ep1.getDocument().insertString(ep1.getCaretPosition() + 3,
112: "fixstuff\n", null);
113: if (save) {
114: DataObject.find(f1).getCookie(SaveCookie.class).save();
115: }
116: click(h11);
117: assertEquals("#1\n", ep1.getDocument().getText(
118: ep1.getCaretPosition(), 3));
119: ep1.getDocument().insertString(ep1.getCaretPosition() + 3,
120: "fixstuff\n", null);
121: click(h12);
122: assertEquals("#2\n", ep1.getDocument().getText(
123: ep1.getCaretPosition(), 3));
124: click(h11);
125: assertEquals("#1\n", ep1.getDocument().getText(
126: ep1.getCaretPosition(), 3));
127: }
128:
129: private void click(OutputListener hyperlink) throws Exception {
130: hyperlink.outputLineAction(null);
131: // Need to wait for CloneableEditorSupport.openAt.Selector.run to finish:
132: EventQueue.invokeAndWait(new Runnable() {
133: public void run() {
134: }
135: });
136: }
137:
138: }
|