001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.csmart.ui.console;
027:
028: import javax.swing.text.AttributeSet;
029: import javax.swing.text.BadLocationException;
030: import javax.swing.text.Highlighter;
031: import javax.swing.text.SimpleAttributeSet;
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestResult;
035: import junit.framework.TestSuite;
036:
037: public class ConsoleTextPaneTest extends TestCase {
038: ConsoleTextPane pane;
039: ConsoleStyledDocument doc;
040: AttributeSet attributeSet;
041:
042: public ConsoleTextPaneTest(String name) {
043: super (name);
044: }
045:
046: protected void setUp() {
047: doc = new ConsoleStyledDocument();
048: attributeSet = new SimpleAttributeSet();
049: pane = new ConsoleTextPane(doc, new NodeStatusButton(null));
050: doc.setBufferSize(20); // number of characters retained in the document
051: }
052:
053: /**
054: * Determine whether appending a string that matches the notify condition
055: * and is larger than the document buffer will be properly highlighted.
056: */
057:
058: public void testNotifyHighlight() {
059: doc.appendString("abcdefghijklmnopqrstuvw", attributeSet);
060: try {
061: assertTrue(
062: "Failed adding string that is longer than buffer",
063: doc.getText(0, doc.getLength()).equals(
064: "defghijklmnopqrstuvw"));
065: } catch (BadLocationException ble) {
066: System.out.println("Bad location exception: " + ble);
067: }
068: pane.setNotifyCondition("now is the time for all good men");
069: doc.appendString("now is the time for all good men",
070: attributeSet);
071: try {
072: assertTrue(
073: "Failed appending string that is longer than buffer",
074: doc.getText(0, doc.getLength()).equals(
075: "ime for all good men"));
076: } catch (BadLocationException ble) {
077: System.out.println("Bad location exception: " + ble);
078: }
079: Highlighter highlighter = pane.getHighlighter();
080: Highlighter.Highlight[] highlights = highlighter
081: .getHighlights();
082: // Next assertion fails. Since we trim before adding, the highlight
083: // never gets applied, so we now expect the value to be 0 here....
084: //assertTrue("Incorrect number of highlights",
085: // highlights.length == 1);
086: int start = highlights[0].getStartOffset();
087: int end = highlights[0].getEndOffset();
088: try {
089: assertTrue("Incorrect highlighting", doc
090: .getText(start, end).equals("ime for all good men"));
091: } catch (BadLocationException ble) {
092: System.out.println("Bad location exception: " + ble);
093: }
094: doc.appendString("0123456789", attributeSet);
095: try {
096: assertTrue(
097: "Highlighting incorrect after appending new text",
098: doc.getText(start, end).equals(
099: "l good men0123456789"));
100: } catch (BadLocationException ble) {
101: System.out.println("Bad location exception: " + ble);
102: }
103: javax.swing.JFrame frame = new javax.swing.JFrame();
104: frame.getContentPane().add(pane);
105: frame.pack();
106: frame.setSize(200, 200);
107: frame.setVisible(true);
108: }
109:
110: public static Test suite() {
111: return new TestSuite(ConsoleTextPaneTest.class);
112: }
113:
114: public static void main(String[] args) {
115: junit.textui.TestRunner.run(suite());
116: }
117: // Test test = new ConsoleTextPaneTest("Test ConsoleTextPane") {
118: // public void runTest() {
119: // testNotifyHighlight();
120: // }
121: // };
122: // Test test = new ConsoleTextPaneTest("testNotifyHighlight");
123: // test.run(new TestResult());
124: // }
125: }
|